-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.go
157 lines (122 loc) · 3.93 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
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
package main
/**
* CPU Information
*/
import (
"fmt"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
ui "github.com/gizak/termui"
)
////////////////////////////////////////////
// Widget: CPU
////////////////////////////////////////////
const CPUWidgetUpdateInterval = 7 * time.Second
type CPUWidget struct {
widget *ui.LineChart
lastUpdated *time.Time
numProcessors int
lastStat linuxproc.CPUStat
curStat linuxproc.CPUStat
cpuPercent float64
loadLast1Min []float64
loadLast5Min []float64
timestamps []string
mostRecent1MinLoad float64
mostRecent5MinLoad float64
}
func NewCPUWidget() *CPUWidget {
// Create base element
e := ui.NewLineChart()
e.Height = 20
e.Border = true
e.PaddingTop = 1
e.LineColor["cpu"] = ui.ColorBlue | ui.AttrBold
e.AxesColor = ui.ColorYellow
// Create widget
w := &CPUWidget{
widget: e,
cpuPercent: 0,
loadLast1Min: make([]float64, 0),
loadLast5Min: make([]float64, 0),
timestamps: make([]string, 0),
}
w.update()
w.resize()
return w
}
func (w *CPUWidget) getGridWidget() ui.GridBufferer {
return w.widget
}
func (w *CPUWidget) update() {
if shouldUpdate(w) {
w.loadProcessorStats()
loadPercent := float64(w.mostRecent5MinLoad) / float64(w.numProcessors)
cpuColorString := percentToAttributeString(int(100.0*w.cpuPercent), 0, 100, true)
loadColor := percentToAttribute(int(100.0*loadPercent), 0, 100, true)
loadColorString := percentToAttributeString(int(100.0*loadPercent), 0, 100, true)
w.widget.BorderLabel = fmt.Sprintf("[CPU: %0.2f%%](%s)[───](fg-white)[5m Load: %0.2f](%s)", w.cpuPercent*100, cpuColorString, w.mostRecent5MinLoad, loadColorString)
w.widget.Data["cpu"] = w.loadLast1Min
w.widget.DataLabels = w.timestamps
// Adjust graph axes color by Load value (never bold)
w.widget.AxesColor = loadColor
}
}
func (w *CPUWidget) resize() {
// Update
}
func (w *CPUWidget) loadProcessorStats() {
// Read /proc/stat for the overall CPU percentage
stats, statErr := linuxproc.ReadStat("/proc/stat")
if statErr == nil {
// Save last two stats records
w.lastStat = w.curStat
w.curStat = stats.CPUStatAll
w.numProcessors = len(stats.CPUStats)
// Calculate usage percentage
// from: https://stackoverflow.com/a/23376195
prevIdle := w.lastStat.Idle + w.lastStat.IOWait
curIdle := w.curStat.Idle + w.curStat.IOWait
prevNonIdle := w.lastStat.User + w.lastStat.Nice + w.lastStat.System + w.lastStat.IRQ + w.lastStat.SoftIRQ + w.lastStat.Steal
curNonIdle := w.curStat.User + w.curStat.Nice + w.curStat.System + w.curStat.IRQ + w.curStat.SoftIRQ + w.curStat.Steal
prevTotal := prevIdle + prevNonIdle
curTotal := curIdle + curNonIdle
// differentiate: actual value minus the previous one
totald := curTotal - prevTotal
idled := curIdle - prevIdle
w.cpuPercent = (float64(totald - idled)) / float64(totald)
}
// Read load average
loadavg, loadErr := linuxproc.ReadLoadAvg("/proc/loadavg")
if loadErr == nil {
w.mostRecent1MinLoad = loadavg.Last1Min
w.mostRecent5MinLoad = loadavg.Last5Min
now := time.Now()
ts := fmt.Sprintf("%02d:%02d", now.Hour(), now.Minute())
// Record, keep a fixed number around
if len(w.loadLast1Min) > (w.widget.Width * 2) {
w.loadLast1Min = append(w.loadLast1Min[1:], loadavg.Last1Min)
} else {
w.loadLast1Min = append(w.loadLast1Min, loadavg.Last1Min)
}
if len(w.loadLast5Min) > (w.widget.Width * 2) {
w.loadLast5Min = append(w.loadLast5Min[1:], loadavg.Last5Min)
} else {
w.loadLast5Min = append(w.loadLast5Min, loadavg.Last5Min)
}
if len(w.timestamps) > (w.widget.Width * 2) {
w.timestamps = append(w.timestamps[1:], ts)
} else {
w.timestamps = append(w.timestamps, ts)
}
}
}
func (w *CPUWidget) getUpdateInterval() time.Duration {
return CPUWidgetUpdateInterval
}
func (w *CPUWidget) getLastUpdated() *time.Time {
return w.lastUpdated
}
func (w *CPUWidget) setLastUpdated(t time.Time) {
w.lastUpdated = &t
}