xmtop/widgets/cpu.go

121 lines
2.7 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"
2018-02-19 15:25:02 +08:00
"time"
"github.com/VictoriaMetrics/metrics"
"github.com/VividCortex/ewma"
2020-04-24 03:07:08 +08:00
"github.com/xxxserxxx/gotop/v4/devices"
2019-01-31 07:20:09 +08:00
"github.com/gizak/termui/v3"
2020-04-24 03:07:08 +08:00
ui "github.com/xxxserxxx/gotop/v4/termui"
2018-02-19 15:25:02 +08:00
)
2020-04-28 09:33:41 +08:00
type CPUWidget struct {
2018-02-19 15:25:02 +08:00
*ui.LineGraph
2020-04-28 09:33:41 +08:00
CPUCount int
2019-03-01 08:29:52 +08:00
ShowAverageLoad bool
2020-04-28 09:33:41 +08:00
ShowPerCPULoad bool
2019-03-01 08:29:52 +08:00
updateInterval time.Duration
cpuLoads map[string]float64
average ewma.MovingAverage
2018-02-19 15:25:02 +08:00
}
var cpuLabels []string
2020-04-28 09:33:41 +08:00
func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCPULoad bool) *CPUWidget {
self := &CPUWidget{
2019-03-01 08:29:52 +08:00
LineGraph: ui.NewLineGraph(),
2020-04-28 09:33:41 +08:00
CPUCount: len(cpuLabels),
2019-03-01 08:29:52 +08:00
updateInterval: updateInterval,
ShowAverageLoad: showAverageLoad,
2020-04-28 09:33:41 +08:00
ShowPerCPULoad: showPerCPULoad,
cpuLoads: make(map[string]float64),
average: ewma.NewMovingAverage(),
2018-03-04 09:05:52 +08:00
}
self.LabelStyles[AVRG] = termui.ModifierBold
2020-06-19 08:51:01 +08:00
self.Title = tr.Value("widget.label.cpu")
2019-01-13 08:31:37 +08:00
self.HorizontalScale = horizontalScale
2018-08-01 05:24:44 +08:00
2020-04-28 09:33:41 +08:00
if !(self.ShowAverageLoad || self.ShowPerCPULoad) {
if self.CPUCount <= 8 {
self.ShowPerCPULoad = true
2018-08-01 05:40:51 +08:00
} else {
2019-03-01 08:29:52 +08:00
self.ShowAverageLoad = true
2018-08-01 05:40:51 +08:00
}
}
2019-03-01 08:29:52 +08:00
if self.ShowAverageLoad {
self.Data[AVRG] = []float64{0}
2018-02-19 15:25:02 +08:00
}
2020-04-28 09:33:41 +08:00
if self.ShowPerCPULoad {
2020-02-29 00:03:41 +08:00
cpus := make(map[string]int)
2020-04-28 09:33:41 +08:00
devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCPULoad)
for k, v := range cpus {
2020-02-29 00:03:41 +08:00
self.Data[k] = []float64{float64(v)}
2018-08-01 05:24:44 +08:00
}
}
2018-04-13 11:00:34 +08:00
2018-12-19 10:12:22 +08:00
self.update()
2018-02-19 15:25:02 +08:00
go func() {
2019-03-01 08:29:52 +08:00
for range time.NewTicker(self.updateInterval).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
}
const AVRG = "AVRG"
2020-04-28 09:33:41 +08:00
func (cpu *CPUWidget) EnableMetric() {
if cpu.ShowAverageLoad {
metrics.NewGauge(makeName("cpu", " avg"), func() float64 {
return cpu.cpuLoads[AVRG]
})
} else {
2020-02-29 00:03:41 +08:00
cpus := make(map[string]int)
2020-04-28 09:33:41 +08:00
devices.UpdateCPU(cpus, cpu.updateInterval, cpu.ShowPerCPULoad)
for key, perc := range cpus {
kc := key
cpu.cpuLoads[key] = float64(perc)
metrics.NewGauge(makeName("cpu", key), func() float64 {
return cpu.cpuLoads[kc]
})
}
}
}
2020-04-28 09:33:41 +08:00
func (cpu *CPUWidget) Scale(i int) {
cpu.LineGraph.HorizontalScale = i
}
2020-04-28 09:33:41 +08:00
func (cpu *CPUWidget) update() {
go func() {
cpus := make(map[string]int)
devices.UpdateCPU(cpus, cpu.updateInterval, true)
cpu.Lock()
defer cpu.Unlock()
// AVG = ((AVG*i)+n)/(i+1)
var sum int
for key, percent := range cpus {
sum += percent
if cpu.ShowPerCPULoad {
2020-04-28 09:33:41 +08:00
cpu.Data[key] = append(cpu.Data[key], float64(percent))
2020-07-21 18:49:51 +08:00
cpu.Labels[key] = fmt.Sprintf("%3d%%", percent)
cpu.cpuLoads[key] = float64(percent)
}
}
if cpu.ShowAverageLoad {
cpu.average.Add(float64(sum) / float64(len(cpus)))
avg := cpu.average.Value()
cpu.Data[AVRG] = append(cpu.Data[AVRG], avg)
cpu.Labels[AVRG] = fmt.Sprintf("%3.0f%%", avg)
cpu.cpuLoads[AVRG] = avg
}
}()
2018-02-19 15:25:02 +08:00
}