-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysinfo.go
More file actions
272 lines (237 loc) · 6.62 KB
/
Copy pathsysinfo.go
File metadata and controls
272 lines (237 loc) · 6.62 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bufio"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/shirou/gopsutil/cpu"
)
// MemoryInfo represents memory statistics
type MemoryInfo struct {
Total uint64
Used uint64
Free uint64
Cached uint64
Buffers uint64
}
// SwapInfo represents swap statistics (in kB, matching /proc/meminfo units)
type SwapInfo struct {
Total uint64
Used uint64
Free uint64
}
// CPUAggregateStats summarizes per-core stats into the CPU tab's stats strip
type CPUAggregateStats struct {
Threads int
AvgUtil float64
PeakCoreIndex int
PeakCoreUtil float64
MaxClock float64
}
// ComputeCPUAggregateStats computes aggregate stats from per-core utilization
// percentages and clock frequencies (as returned by cpu.Percent and
// GetCPUFrequencies).
func ComputeCPUAggregateStats(percent, freqs []float64) CPUAggregateStats {
stats := CPUAggregateStats{Threads: len(percent)}
if len(percent) == 0 {
return stats
}
var total float64
for i, p := range percent {
total += p
if p > stats.PeakCoreUtil {
stats.PeakCoreUtil = p
stats.PeakCoreIndex = i
}
}
stats.AvgUtil = total / float64(len(percent))
for _, f := range freqs {
if f > stats.MaxClock {
stats.MaxClock = f
}
}
return stats
}
// GetCPUFrequencies reads CPU frequencies from /proc/cpuinfo
func GetCPUFrequencies() ([]float64, error) {
file, err := os.Open("/proc/cpuinfo")
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var freqs []float64
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "cpu MHz") {
parts := strings.Split(line, ":")
if len(parts) == 2 {
freq, err := strconv.ParseFloat(strings.TrimSpace(parts[1]), 64)
if err == nil {
freqs = append(freqs, freq)
}
}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return freqs, nil
}
// GetMemoryInfo returns a MemoryInfo struct with the total, used, and free memory
func GetMemoryInfo() (MemoryInfo, error) {
file, err := os.Open("/proc/meminfo")
if err != nil {
return MemoryInfo{}, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var total, free uint64
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "MemTotal:") {
parts := strings.Fields(line)
if len(parts) == 3 {
total, _ = strconv.ParseUint(parts[1], 10, 64)
}
} else if strings.HasPrefix(line, "MemFree:") {
parts := strings.Fields(line)
if len(parts) == 3 {
free, _ = strconv.ParseUint(parts[1], 10, 64)
}
}
}
if err := scanner.Err(); err != nil {
return MemoryInfo{}, err
}
used := total - free
return MemoryInfo{
Total: total,
Used: used,
Free: free,
}, nil
}
// UpdateCPUInfo updates every core's state (feeding both the tile and list
// representations) and the CPU tab's aggregate stats strip.
func UpdateCPUInfo(cores []*CoreState, historySize int, onStats func(CPUAggregateStats)) {
percent, err := cpu.Percent(0, true)
if err != nil {
log.Printf("Error getting CPU percent: %v", err)
return
}
freqs, err := GetCPUFrequencies()
if err != nil {
log.Printf("Error getting CPU frequencies: %v", err)
return
}
for i, core := range cores {
if i >= len(percent) || i >= len(freqs) {
continue
}
core.Update(percent[i], freqs[i], historySize)
}
if onStats != nil {
onStats(ComputeCPUAggregateStats(percent, freqs))
}
}
// GetMemoryInfoDetailed returns detailed memory information (including
// cached/buffers, broken out separately) and swap information, both read
// from /proc/meminfo.
//
// Used is computed as total-free-cached-buffers (rather than total-available)
// so that Used+Cached+Buffers+Free sums to Total — required for the memory
// breakdown bars, which visualize those four components as parts of a whole.
func GetMemoryInfoDetailed() (MemoryInfo, SwapInfo, error) {
file, err := os.Open("/proc/meminfo")
if err != nil {
return MemoryInfo{}, SwapInfo{}, err
}
defer file.Close()
return parseMemInfo(file)
}
// parseMemInfo parses /proc/meminfo-formatted content into memory and swap
// stats. Split out from GetMemoryInfoDetailed so the parsing logic can be
// tested against a fixture without touching the filesystem.
func parseMemInfo(r io.Reader) (MemoryInfo, SwapInfo, error) {
scanner := bufio.NewScanner(r)
var total, free, cached, buffers, swapTotal, swapFree uint64
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "MemTotal:") {
parts := strings.Fields(line)
if len(parts) == 3 {
total, _ = strconv.ParseUint(parts[1], 10, 64)
}
} else if strings.HasPrefix(line, "MemFree:") {
parts := strings.Fields(line)
if len(parts) == 3 {
free, _ = strconv.ParseUint(parts[1], 10, 64)
}
} else if strings.HasPrefix(line, "Cached:") {
parts := strings.Fields(line)
if len(parts) == 3 {
cached, _ = strconv.ParseUint(parts[1], 10, 64)
}
} else if strings.HasPrefix(line, "Buffers:") {
parts := strings.Fields(line)
if len(parts) == 3 {
buffers, _ = strconv.ParseUint(parts[1], 10, 64)
}
} else if strings.HasPrefix(line, "SwapTotal:") {
parts := strings.Fields(line)
if len(parts) == 3 {
swapTotal, _ = strconv.ParseUint(parts[1], 10, 64)
}
} else if strings.HasPrefix(line, "SwapFree:") {
parts := strings.Fields(line)
if len(parts) == 3 {
swapFree, _ = strconv.ParseUint(parts[1], 10, 64)
}
}
}
if err := scanner.Err(); err != nil {
return MemoryInfo{}, SwapInfo{}, err
}
used := total - free - cached - buffers
memInfo := MemoryInfo{
Total: total,
Used: used,
Free: free,
Cached: cached,
Buffers: buffers,
}
swapInfo := SwapInfo{
Total: swapTotal,
Free: swapFree,
Used: swapTotal - swapFree,
}
return memInfo, swapInfo, nil
}
// UpdateMemoryInfo updates the memory dashboard with the latest memory and
// swap statistics.
func UpdateMemoryInfo(dashboard *MemoryDashboard, historySize int) {
memInfo, swapInfo, err := GetMemoryInfoDetailed()
if err != nil {
log.Printf("Error getting memory info: %v", err)
return
}
usagePercent := 0.0
if memInfo.Total > 0 {
usagePercent = float64(memInfo.Used) / float64(memInfo.Total) * 100.0
}
swapPercent := 0.0
if swapInfo.Total > 0 {
swapPercent = float64(swapInfo.Used) / float64(swapInfo.Total) * 100.0
}
dashboard.UsageHistory = append(dashboard.UsageHistory, usagePercent)
if len(dashboard.UsageHistory) > historySize {
dashboard.UsageHistory = dashboard.UsageHistory[1:]
}
dashboard.SwapHistory = append(dashboard.SwapHistory, swapPercent)
if len(dashboard.SwapHistory) > historySize {
dashboard.SwapHistory = dashboard.SwapHistory[1:]
}
dashboard.Update(memInfo, swapInfo, usagePercent, swapPercent)
}