-
Notifications
You must be signed in to change notification settings - Fork 70
/
cpu.go
61 lines (51 loc) · 1.48 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
package calculator
import (
"github.com/elastic/beats/libbeat/common"
"strconv"
)
type CPUCalculator interface {
PerCpuUsage() common.MapStr
TotalUsage() float64
UsageInKernelmode() float64
UsageInUsermode() float64
}
type CPUCalculatorImpl struct {
Old CPUData
New CPUData
}
type CPUData struct {
PerCpuUsage []uint64
TotalUsage uint64
UsageInKernelmode uint64
UsageInUsermode uint64
}
func (c CPUCalculatorImpl) PerCpuUsage() common.MapStr {
var output common.MapStr
if cap(c.New.PerCpuUsage) == cap(c.Old.PerCpuUsage) {
output = common.MapStr{}
for index := range c.New.PerCpuUsage {
output["cpu"+strconv.Itoa(index)] = c.calculateLoad(c.New.PerCpuUsage[index], c.Old.PerCpuUsage[index])
}
}
return output
}
func (c CPUCalculatorImpl) TotalUsage() float64 {
return c.calculateLoad(c.New.TotalUsage, c.Old.TotalUsage)
}
func (c CPUCalculatorImpl) UsageInKernelmode() float64 {
return c.calculateLoad(c.New.UsageInKernelmode, c.Old.UsageInKernelmode)
}
func (c CPUCalculatorImpl) UsageInUsermode() float64 {
return c.calculateLoad(c.New.UsageInUsermode, c.Old.UsageInUsermode)
}
func (c CPUCalculatorImpl) calculateLoad(oldValue uint64, newValue uint64) float64 {
// value is the count of CPU nanosecond in 1sec
// TODO save the old stat timestamp and reuse here in case of docker read time changes...
// 1s = 1000000000 ns
// value / 1000000000
value := int64(oldValue - newValue)
if value < 0 {
value = 0
}
return float64(value) / float64(1000000000)
}