-
Notifications
You must be signed in to change notification settings - Fork 194
/
stats.go
executable file
·69 lines (57 loc) · 1.56 KB
/
stats.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
package system
import (
"time"
"github.com/neuvector/neuvector/share"
)
func calculateCPU(prevCPU, prevCPUSystem uint64, cpu, cpuSystem uint64) float64 {
var cDelta float64 = float64(cpu - prevCPU)
var sDelta float64 = float64(cpuSystem - prevCPUSystem)
if sDelta > 0.0 {
if cDelta > sDelta {
return 1
} else {
return cDelta / sDelta
}
}
return 0
}
func UpdateStats(cs *share.ContainerStats, memory, cpu, cpuSystem uint64) {
ratio := calculateCPU(cs.PrevCPU, cs.PrevCPUSystem, cpu, cpuSystem)
cs.PrevCPU = cpu
cs.PrevCPUSystem = cpuSystem
cs.ReadAt = time.Now().UTC()
cs.Cpu[cs.CurSlot] = ratio
cs.Memory[cs.CurSlot] = memory
cs.CurSlot++
if cs.CurSlot == share.ContainerStatsSlots {
cs.CurSlot = 0
}
}
func PopulateSystemStats(data *share.CLUSStats, stats *share.ContainerStats) {
var s uint
var cpu float64
var memory uint64
s = (stats.CurSlot + share.ContainerStatsSlots - 1) % share.ContainerStatsSlots
data.Span1.CPU = stats.Cpu[s]
data.Span1.Memory = stats.Memory[s]
cpu = 0
memory = 0
s = (stats.CurSlot + share.ContainerStatsSlots - 12) % share.ContainerStatsSlots
for n := 0; n < 12; n++ {
cpu += stats.Cpu[s]
memory += stats.Memory[s]
s = (s + 1) % share.ContainerStatsSlots
}
data.Span12.CPU = cpu / 12
data.Span12.Memory = memory / 12
cpu = 0
memory = 0
s = (stats.CurSlot + share.ContainerStatsSlots - 59) % share.ContainerStatsSlots
for n := 0; n < 59; n++ {
cpu += stats.Cpu[s]
memory += stats.Memory[s]
s = (s + 1) % share.ContainerStatsSlots
}
data.Span60.CPU = cpu / 59
data.Span60.Memory = memory / 59
}