-
Notifications
You must be signed in to change notification settings - Fork 0
/
procinfo.go
51 lines (45 loc) · 1.1 KB
/
procinfo.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
package happening
import (
"os"
"runtime"
"syscall"
"time"
)
type procinfo struct {
CpuUsage float64
MemoryUsage float64
}
func maxRSSMultiplier() float64 {
switch runtime.GOOS {
case "linux":
return 1 << 10
default:
return 1
}
}
func computeCPUTime(rusage *syscall.Rusage) float64 {
userCpuTime := time.Duration(rusage.Utime.Sec)*time.Second +
time.Duration(rusage.Utime.Usec)*time.Microsecond
sysCpuTime := time.Duration(rusage.Stime.Sec)*time.Second +
time.Duration(rusage.Stime.Usec)*time.Microsecond
cpuTime := userCpuTime + sysCpuTime
return cpuTime.Seconds()
}
func getProcinfo(processState *os.ProcessState) *procinfo {
rusage := processState.SysUsage().(*syscall.Rusage)
return &procinfo{
CpuUsage: computeCPUTime(rusage),
MemoryUsage: float64(rusage.Maxrss) * maxRSSMultiplier(),
}
}
func getProcinfoSelf() *procinfo {
rusage := new(syscall.Rusage)
err := syscall.Getrusage(syscall.RUSAGE_SELF, rusage)
if err == nil {
return &procinfo{
CpuUsage: computeCPUTime(rusage),
MemoryUsage: float64(rusage.Maxrss) * maxRSSMultiplier(),
}
}
return &procinfo{}
}