-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_stats_darwin.go
79 lines (68 loc) · 1.93 KB
/
proc_stats_darwin.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//go:build darwin && cgo
// +build darwin,cgo
package procstats
// @see https://github.com/apple/darwin-xnu/blob/master/bsd/sys/proc_info.h
// also @see http://vinceyuan.github.io/wrong-info-from-procpidinfo/
// #include <libproc.h>
//
// int get_mem_info(int pid, uint64_t *rss)
// {
// struct proc_taskallinfo ti;
// int nb = 0;
// nb = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &ti, sizeof(ti));
// if (nb <= 0 || nb < sizeof(ti)) {
// return -1;
// }
// *rss = ti.ptinfo.pti_resident_size;
// return 0;
// }
//
// int get_cpu_info(int pid, uint64_t *total_user, uint64_t *total_system)
// {
// struct proc_taskallinfo ti;
// int nb = 0;
// nb = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &ti, sizeof(ti));
// if (nb <= 0 || nb < sizeof(ti)) {
// return -1;
// }
// *total_user = ti.ptinfo.pti_total_user;
// *total_system = ti.ptinfo.pti_total_system;
// return 0;
// }
import "C"
import (
"fmt"
"time"
)
func readProcessRSS(pid int) (int64, error) {
var rss C.ulonglong
success := C.int(0)
ret := C.get_mem_info(C.int(pid), &rss)
if ret != success {
return 0, fmt.Errorf("failed to get mem stats for pid: non-zero return")
}
return int64(rss), nil
}
func readProcessCPUTime(pid int) (CPUTime, error) {
var totalUser C.ulonglong
var totalSystem C.ulonglong
success := C.int(0)
ret := C.get_cpu_info(C.int(pid), &totalUser, &totalSystem)
if ret != success {
return CPUTime{},
fmt.Errorf("failed to get cpu stats for pid: non-zero return")
}
clockTick := time.Duration(sysClockTick())
cpuTime := CPUTime{}
cpuTime.Utime = time.Duration(uint64(totalUser)) * time.Second / clockTick
cpuTime.Stime = time.Duration(uint64(totalSystem)) * time.Second / clockTick
return cpuTime, nil
}
func readMaxRSS(pid int) (int64, error) {
// darwin doesn't appear to expose Max RSS independently
return readProcessRSS(pid)
}
func resetMaxRSS(pid int) error {
// noop
return nil
}