-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_stats_bsd.go
54 lines (45 loc) · 1.08 KB
/
proc_stats_bsd.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
//go:build freebsd && cgo
// +build freebsd,cgo
package procstats
// #include <sys/types.h>
// #include <sys/sysctl.h>
// #include <sys/user.h>
// #include <stdlib.h>
// int64_t ExtractRSSKinfoProc(void *stat_bytes) {
// struct kinfo_proc *kp = (struct kinfo_proc*)stat_bytes;
// #ifdef DARWIN
// int64_t rss = kp->kp_eproc.e_vm.vm_rssize;
// #else
// int64_t rss = kp->ki_rssize * 4096;
// #endif
// free(stat_bytes);
// return rss;
// }
import "C"
import (
"fmt"
"golang.org/x/sys/unix"
)
func readProcessStats(pid int) ([]byte, error) {
statsEnc, err := unix.SysctlRaw("kern.proc.pid", pid)
if err != nil {
return nil, err
}
return statsEnc, nil
}
func readProcessRSS(pid int) (int64, error) {
pstats, err := readProcessStats(pid)
if err != nil {
return 0, fmt.Errorf("failed to get stats for pid: %s", err)
}
cpstats := C.CBytes(pstats)
return int64(C.ExtractRSSKinfoProc(cpstats)), nil
}
func readMaxRSS(pid int) (int64, error) {
// bsd doesn't appear to expose Max RSS independently
return readProcessRSS(pid)
}
func resetMaxRSS(pid int) error {
// noop
return nil
}