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
|
package widgets
import (
"fmt"
"strconv"
"time"
"github.com/cjbassi/gotop/utils"
ui "github.com/cjbassi/termui"
psCPU "github.com/shirou/gopsutil/cpu"
)
type CPU struct {
*ui.LineGraph
Count int // number of cores
interval time.Duration
}
func NewCPU(interval time.Duration, zoom int) *CPU {
count, _ := psCPU.Counts(false)
self := &CPU{
LineGraph: ui.NewLineGraph(),
Count: count,
interval: interval,
}
self.Label = "CPU Usage"
self.Zoom = zoom
if self.Count <= 8 {
for i := 0; i < self.Count; i++ {
key := "CPU" + strconv.Itoa(i)
self.Data[key] = []float64{0}
}
} else {
self.Data["Average"] = []float64{0}
}
go self.update()
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
self.update()
}
}()
return self
}
// calculates the CPU usage over a 1 second interval and blocks for the duration
func (self *CPU) update() {
// show average cpu usage if more than 8 cores
if self.Count <= 8 {
percents, _ := psCPU.Percent(self.interval, true)
if len(percents) != self.Count {
count, _ := psCPU.Counts(false)
utils.Error("CPU percentages",
fmt.Sprint(
"self.Count: ", self.Count, "\n",
"gopsutil.Counts(): ", count, "\n",
"len(percents): ", len(percents), "\n",
"percents: ", percents, "\n",
"self.interval: ", self.interval,
))
}
for i := 0; i < self.Count; i++ {
key := "CPU" + strconv.Itoa(i)
percent := percents[i]
self.Data[key] = append(self.Data[key], percent)
}
} else {
percent, _ := psCPU.Percent(self.interval, false)
self.Data["Average"] = append(self.Data["Average"], percent[0])
}
}
|