forked from dhamith93/systats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
126 lines (105 loc) · 2.99 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
package systats
import (
"regexp"
"strconv"
"strings"
"time"
"github.com/dhamith93/systats/internal/fileops"
"github.com/dhamith93/systats/internal/strops"
)
// CPU holds information on CPU and CPU usage
type CPU struct {
LoadAvg int
CoreAvg []int
Model string
NoOfCores int
Freq string
Cache string
Time int64
}
func getCPU(systats *SyStats, milliseconds int) (CPU, error) {
output := CPU{}
statStr1, err := fileops.ReadFileWithError(systats.StatFilePath)
if err != nil {
return output, err
}
// to calculate the cpu usage the /proc/stat has to be read some time apart
time.Sleep(time.Duration(milliseconds) * time.Millisecond)
statStr2, err := fileops.ReadFileWithError(systats.StatFilePath)
if err != nil {
return output, err
}
processStatFileContents(&output, &statStr1, &statStr2)
cpuinfoStr, err := fileops.ReadFileWithError(systats.CPUinfoFilePath)
if err != nil {
return output, err
}
processCPUInfoFileContent(&output, &cpuinfoStr)
return output, nil
}
func processCPUInfoFileContent(output *CPU, content *string) {
split := strings.Split(*content, "\n")
for _, line := range split {
lineArr := strings.Fields(line)
if len(lineArr) == 0 {
continue
}
if len(lineArr) > 3 && (lineArr[0]+lineArr[1] == "modelname") {
name := ""
for i := 3; i < len(lineArr); i++ {
name += lineArr[i] + " "
}
output.Model = strings.TrimSpace(name)
continue
}
if len(lineArr) > 3 && (lineArr[0]+lineArr[1] == "cpucores") {
output.NoOfCores, _ = strconv.Atoi(strings.TrimSpace(lineArr[3]))
continue
}
if len(lineArr) > 3 && (lineArr[0]+lineArr[1] == "cpuMHz") {
output.Freq = strings.TrimSpace(lineArr[3]) + " MHz"
continue
}
if len(lineArr) > 3 && (lineArr[0]+lineArr[1] == "cachesize") {
name := ""
for i := 3; i < len(lineArr); i++ {
name += lineArr[i] + " "
}
output.Cache = strings.TrimSpace(name)
continue
}
}
output.Time = time.Now().Unix()
}
func processStatFileContents(output *CPU, statStr1 *string, statStr2 *string) {
statArr1 := processStatFile(statStr1)
statArr2 := processStatFile(statStr2)
for i := range statArr1 {
// user + system, and user+system+idle times
a1 := statArr1[i][0] + statArr1[i][1]
a2 := statArr1[i][0] + statArr1[i][1] + statArr1[i][2]
b := (statArr2[i][0] + statArr2[i][1] + statArr2[i][2] - a2)
if b > 0 {
usage := 100 * (statArr2[i][0] + statArr2[i][1] - a1) / b
if i == 0 {
output.LoadAvg = int(usage)
continue
}
output.CoreAvg = append(output.CoreAvg, int(usage))
} else {
output.CoreAvg = append(output.CoreAvg, 0)
}
}
}
func processStatFile(content *string) [][]uint64 {
statSplit := strings.Split(*content, "\n")
output := [][]uint64{}
r, _ := regexp.Compile("^cpu")
for _, line := range statSplit {
lineArr := strings.Fields(line)
if len(lineArr) > 0 && r.MatchString(lineArr[0]) {
output = append(output, []uint64{strops.ToUint64(lineArr[1]), strops.ToUint64(lineArr[3]), strops.ToUint64(lineArr[4])})
}
}
return output
}