xmtop/src/widgets/cpu.go

102 lines
2.4 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
2018-04-11 11:08:02 +08:00
"fmt"
"log"
2018-02-19 15:25:02 +08:00
"time"
2018-03-30 06:48:43 +08:00
ui "github.com/cjbassi/termui"
2018-03-04 09:05:52 +08:00
psCPU "github.com/shirou/gopsutil/cpu"
2018-02-19 15:25:02 +08:00
)
type CPU struct {
*ui.LineGraph
2018-12-02 13:00:17 +08:00
Count int // number of cores
Average bool // show average load
PerCPU bool // show per-core load
interval time.Duration
formatString string
2018-02-19 15:25:02 +08:00
}
2018-08-01 05:24:44 +08:00
func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
count, err := psCPU.Counts(false)
if err != nil {
log.Printf("failed to get CPU count from gopsutil: %v", err)
}
2018-12-02 13:00:17 +08:00
formatString := "CPU%1d"
if count > 10 {
formatString = "CPU%02d"
}
2018-03-28 05:27:23 +08:00
self := &CPU{
2018-12-02 13:00:17 +08:00
LineGraph: ui.NewLineGraph(),
Count: count,
interval: interval,
Average: average,
PerCPU: percpu,
formatString: formatString,
2018-03-04 09:05:52 +08:00
}
2018-03-28 05:27:23 +08:00
self.Label = "CPU Usage"
self.Zoom = zoom
2018-08-01 05:24:44 +08:00
2018-08-01 05:40:51 +08:00
if !(self.Average || self.PerCPU) {
if self.Count <= 8 {
self.PerCPU = true
} else {
self.Average = true
}
}
2018-08-01 05:24:44 +08:00
if self.Average {
self.Data["AVRG"] = []float64{0}
2018-02-19 15:25:02 +08:00
}
2018-08-01 05:24:44 +08:00
if self.PerCPU {
for i := 0; i < self.Count; i++ {
2018-12-02 13:00:17 +08:00
k := fmt.Sprintf(formatString, i)
2018-08-01 05:24:44 +08:00
self.Data[k] = []float64{0}
}
}
2018-04-13 11:00:34 +08:00
go self.update() // update asynchronously because of 1 second blocking period
2018-02-19 15:25:02 +08:00
go func() {
ticker := time.NewTicker(self.interval)
2018-02-19 15:25:02 +08:00
for range ticker.C {
2018-03-28 05:27:23 +08:00
self.update()
2018-02-19 15:25:02 +08:00
}
}()
2018-03-28 05:27:23 +08:00
return self
2018-02-19 15:25:02 +08:00
}
2018-04-11 12:03:13 +08:00
// calculates the CPU usage over a 1 second interval and blocks for the duration
2018-03-28 05:27:23 +08:00
func (self *CPU) update() {
2018-08-01 05:24:44 +08:00
if self.Average {
go func() {
percent, err := psCPU.Percent(self.interval, false)
if err != nil {
log.Printf("failed to get average CPU usage percent from gopsutil: %v. self.interval: %v. percpu: %v", err, self.interval, false)
}
self.Data["AVRG"] = append(self.Data["AVRG"], percent[0])
self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", percent[0])
}()
2018-02-19 15:25:02 +08:00
}
2018-08-01 05:24:44 +08:00
if self.PerCPU {
go func() {
percents, err := psCPU.Percent(self.interval, true)
if err != nil {
log.Printf("failed to get CPU usage percents from gopsutil: %v. self.interval: %v. percpu: %v", err, self.interval, true)
}
if len(percents) != self.Count {
log.Printf("error: number of CPU usage percents from gopsutil doesn't match CPU count. percents: %v. self.Count: %v", percents, self.Count)
}
for i := 0; i < self.Count; i++ {
2018-12-02 13:00:17 +08:00
k := fmt.Sprintf(self.formatString, i)
self.Data[k] = append(self.Data[k], percents[i])
self.Labels[k] = fmt.Sprintf("%3.0f%%", percents[i])
}
}()
2018-08-01 05:24:44 +08:00
}
2018-02-19 15:25:02 +08:00
}