-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
process.go
149 lines (118 loc) · 2.69 KB
/
process.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package psutil
import (
"context"
"sync"
"time"
psprocess "github.com/shirou/gopsutil/v3/process"
)
type Process interface {
CPUPercent() (*CPUInfoStat, error)
VirtualMemory() (uint64, error)
Stop()
}
type process struct {
pid int32
hasCgroup bool
cpuLimit uint64
ncpu float64
proc *psprocess.Process
stopTicker context.CancelFunc
lock sync.RWMutex
statCurrent cpuTimesStat
statCurrentTime time.Time
statPrevious cpuTimesStat
statPreviousTime time.Time
}
func (u *util) Process(pid int32) (Process, error) {
p := &process{
pid: pid,
hasCgroup: u.hasCgroup,
cpuLimit: u.cpuLimit,
ncpu: u.ncpu,
}
proc, err := psprocess.NewProcess(pid)
if err != nil {
return nil, err
}
p.proc = proc
ctx, cancel := context.WithCancel(context.Background())
p.stopTicker = cancel
go p.tick(ctx)
return p, nil
}
func NewProcess(pid int32) (Process, error) {
return DefaultUtil.Process(pid)
}
func (p *process) tick(ctx context.Context) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case t := <-ticker.C:
stat := p.collect()
p.lock.Lock()
p.statPrevious, p.statCurrent = p.statCurrent, stat
p.statPreviousTime, p.statCurrentTime = p.statCurrentTime, t
p.lock.Unlock()
}
}
}
func (p *process) collect() cpuTimesStat {
stat, err := p.cpuTimes()
if err != nil {
return cpuTimesStat{
total: float64(time.Now().Unix()),
idle: float64(time.Now().Unix()),
}
}
return *stat
}
func (p *process) Stop() {
p.stopTicker()
}
func (p *process) cpuTimes() (*cpuTimesStat, error) {
times, err := p.proc.Times()
if err != nil {
return nil, err
}
s := &cpuTimesStat{
total: cpuTotal(times),
system: times.System,
user: times.User,
}
s.other = s.total - s.system - s.user
return s, nil
}
func (p *process) CPUPercent() (*CPUInfoStat, error) {
var diff float64
p.lock.RLock()
defer p.lock.RUnlock()
if p.hasCgroup && p.cpuLimit > 0 {
diff = float64(p.cpuLimit) * (p.statCurrentTime.Sub(p.statPreviousTime)).Seconds() / 1e9
} else {
diff = p.statCurrentTime.Sub(p.statPreviousTime).Seconds() * p.ncpu
}
s := &CPUInfoStat{
System: 0,
User: 0,
Idle: 0,
Other: 0,
}
if diff <= 0 {
return s, nil
}
s.System = 100 * (p.statCurrent.system - p.statPrevious.system) / diff
s.User = 100 * (p.statCurrent.user - p.statPrevious.user) / diff
s.Idle = 100 * (p.statCurrent.idle - p.statPrevious.idle) / diff
s.Other = 100 * (p.statCurrent.other - p.statPrevious.other) / diff
return s, nil
}
func (p *process) VirtualMemory() (uint64, error) {
info, err := p.proc.MemoryInfo()
if err != nil {
return 0, err
}
return info.RSS, nil
}