-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathprocess_stats.go
58 lines (48 loc) · 1.17 KB
/
process_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import "fmt"
// ProcessStats contains processes count stats
type ProcessStats struct {
// The total number of processes
Total int
// The number of running processes
Running int
// The number of sleeping processes
Sleeping int
// The number of stopped processes
Stopped int
// The number of zombie processes
Zombie int
}
// ProcessStats get prceosses related stats
// note that 1st call to 100ms may return NaN as values
// Go equivalent to sg_cpu_percents
func (s *Stat) ProcessStats() *ProcessStats {
s.Lock()
defer s.Unlock()
// Throw away the first reading as thats averaged over the machines uptime
pstat := C.sg_get_process_count_of(C.sg_entire_process_count)
p := &ProcessStats{
Total: int(pstat.total),
Running: int(pstat.running),
Sleeping: int(pstat.sleeping),
Stopped: int(pstat.stopped),
Zombie: int(pstat.zombie),
}
return p
}
func (p *ProcessStats) String() string {
return fmt.Sprintf(
"Total:\t\t%d\n"+
"Running:\t%d\n"+
"Sleeping:\t%d\n"+
"Stopped:\t%d\n"+
"Zombie:\t\t%d\n",
p.Total,
p.Running,
p.Sleeping,
p.Stopped,
p.Zombie)
}