-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate-stats.go
76 lines (61 loc) · 1.79 KB
/
calculate-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
70
71
72
73
74
75
76
package main
import (
"github.com/docker/docker/api/types"
)
// based on docker client code:
// https://github.com/docker/cli/blob/4586609f71ac10dc43c846751c76e51a9ab81d56/cli/command/container/stats_helpers.go
func CalculateCPUPercentage(os string, v types.Stats) float64 {
var cpuPercent = 0.0
switch os {
case "windows":
var (
possIntervals = uint64(v.Read.Sub(v.PreRead).Nanoseconds() / 100)
intervalsUsed = v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
)
possIntervals *= uint64(v.NumProcs)
if possIntervals > 0 {
cpuPercent = float64(intervalsUsed) / float64(possIntervals) * 100.0
}
case "linux":
var (
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage)
systemDelta = float64(v.CPUStats.SystemUsage - v.PreCPUStats.SystemUsage)
onlineCPUs = float64(v.CPUStats.OnlineCPUs)
)
if onlineCPUs == 0.0 {
onlineCPUs = float64(len(v.CPUStats.CPUUsage.PercpuUsage))
}
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0
}
}
return cpuPercent
}
func CalculateMemoryUsage(os string, v types.Stats) float64 {
var memoryUsage = 0.0
switch os {
case "windows":
memoryUsage = float64(v.MemoryStats.PrivateWorkingSet)
case "linux":
memoryUsage = float64(v.MemoryStats.Usage - v.MemoryStats.Stats["cache"])
}
return memoryUsage
}
func CalculateMemoryLimit(os string, v types.Stats) float64 {
var memoryLimit = 0.0
switch os {
case "linux":
memoryLimit = float64(v.MemoryStats.Limit)
}
return memoryLimit
}
func CalculateMemoryPercentage(os string, v types.Stats) float64 {
var (
memoryUsage = CalculateMemoryUsage(os, v)
memoryLimit = CalculateMemoryLimit(os, v)
)
if memoryLimit != 0 {
return memoryUsage / memoryLimit * 100.0
}
return 0.0
}