-
Notifications
You must be signed in to change notification settings - Fork 6
/
cpu.go
196 lines (170 loc) · 4.41 KB
/
cpu.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cpuhist
import (
"runtime"
"sync"
"sync/atomic"
"time"
"get.pme.sh/pmesh/concurrent"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/process"
)
const cpuUsageInterval = 5 * time.Second
var NumCPU = runtime.NumCPU()
func init() {
n, _ := cpu.Counts(false)
if n != 0 {
NumCPU = n
}
}
type times struct {
used float64
idle float64
timestamp time.Time
isprocess bool
}
func (from times) getAverageUseBetween(to times) (float64, bool) {
if from.timestamp.IsZero() || to.timestamp.IsZero() {
return 0, false
}
used := to.used - from.used
if used < 0 {
return 0, false
}
if !to.isprocess {
idle := to.idle - from.idle
if idle < 0 {
return 0, false
}
elapsed := used + idle
p := used / elapsed
p = min(float64(1), max(p, 0.0))
return p, true
} else {
elapsed := to.timestamp.Sub(from.timestamp).Seconds()
p := used / elapsed
p = min(float64(NumCPU), max(p, 0.0))
return p, true
}
}
type processLike interface {
times() times
alive() bool
creation() time.Time
pid() int32
}
type realProcess struct {
proc *process.Process
}
func (p realProcess) pid() int32 { return p.proc.Pid }
func (p realProcess) alive() (running bool) { running, _ = p.proc.IsRunning(); return }
func (p realProcess) creation() time.Time { ts, _ := p.proc.CreateTime(); return time.UnixMilli(ts) }
func (p realProcess) times() (r times) {
u, err := p.proc.Times()
if err != nil {
return
}
r.used = u.User + u.System + u.Iowait + u.Irq + u.Nice + u.Softirq + u.Steal + u.Guest + u.GuestNice
r.idle = 0
r.timestamp = time.Now()
r.isprocess = true
return
}
type systemProcess struct{}
var bootTime, _ = host.BootTime()
func (p systemProcess) pid() int32 { return -1 }
func (p systemProcess) times() (r times) {
res, err := cpu.Times(false)
if err != nil {
return
}
for _, v := range res {
r.used += v.User + v.System + v.Iowait + v.Irq + v.Nice + v.Softirq + v.Steal + v.Guest + v.GuestNice
r.idle += v.Idle
}
r.timestamp = time.Now()
r.isprocess = false
return
}
func (p systemProcess) alive() bool { return true }
func (p systemProcess) creation() time.Time { return time.Unix(int64(bootTime), 0) }
type cpuUsageHistory struct {
lastUse atomic.Int64
mu sync.Mutex
proc processLike
r0, r1 times
lastobsv float64
}
type uniqueProcessID struct {
createtime int64
pid int32
}
func (h *cpuUsageHistory) abandoned() bool {
if !h.proc.alive() {
return true
}
if h.lastUse.Load() < time.Now().Add(-time.Minute).UnixNano() {
return true
}
return false
}
func (h *cpuUsageHistory) getAverageUse() float64 {
h.mu.Lock()
defer h.mu.Unlock()
record := h.proc.times()
if record.timestamp.IsZero() {
return 0
}
h.lastUse.Store(record.timestamp.UnixNano())
if h.r1.timestamp.Before(record.timestamp.Add(-cpuUsageInterval)) {
if h.r1.timestamp.IsZero() {
h.r0.timestamp = h.proc.creation()
} else {
h.r0 = h.r1
}
h.r1 = record
}
res, ok := h.r0.getAverageUseBetween(record)
if !ok {
return h.lastobsv
}
h.lastobsv = res
return res
}
var cpuUsageHistories = concurrent.Map[uniqueProcessID, *cpuUsageHistory]{}
var cpuUsageHistoryCleanup = sync.Once{}
func getUsePercentage(proc processLike) float64 {
cpuUsageHistoryCleanup.Do(func() {
go func() {
for range time.Tick(time.Minute) {
cpuUsageHistories.Range(func(k uniqueProcessID, v *cpuUsageHistory) bool {
if v.abandoned() {
cpuUsageHistories.Delete(k)
}
return true
})
}
}()
})
if !proc.alive() {
return 0
}
uid := uniqueProcessID{pid: proc.pid(), createtime: proc.creation().UnixNano()}
history, ok := cpuUsageHistories.Load(uid)
if !ok {
history, _ = cpuUsageHistories.LoadOrStore(uid, &cpuUsageHistory{proc: proc})
}
return history.getAverageUse()
}
// GetUsePercentage returns the average CPU usage percentage of the process.
// Percentage is expressed as [0, #CPU] where 0 means no usage and #CPU means full usage of all cores.
// The average is calculated over a period of time.
func GetUsePercentage(proc *process.Process) float64 {
return getUsePercentage(realProcess{proc})
}
// GetSystemUsePercentage returns the average CPU usage percentage of the system.
// Percentage is expressed as [0, 1] where 0 means no usage and 1 means full usage of all cores.
// The average is calculated over a period of time.
func GetSystemUsePercentage() float64 {
return getUsePercentage(systemProcess{})
}