2018-02-18 23:25:02 -08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-04-10 20:08:02 -07:00
|
|
|
"fmt"
|
2019-01-19 20:34:19 -08:00
|
|
|
"sync"
|
2018-02-18 23:25:02 -08:00
|
|
|
"time"
|
|
|
|
|
2020-05-12 18:42:45 -05:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2020-04-23 14:07:08 -05:00
|
|
|
"github.com/xxxserxxx/gotop/v4/devices"
|
2019-01-30 15:20:09 -08:00
|
|
|
|
2020-04-23 14:07:08 -05:00
|
|
|
ui "github.com/xxxserxxx/gotop/v4/termui"
|
2018-02-18 23:25:02 -08:00
|
|
|
)
|
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
type CPUWidget struct {
|
2018-02-18 23:25:02 -08:00
|
|
|
*ui.LineGraph
|
2020-04-27 20:33:41 -05:00
|
|
|
CPUCount int
|
2019-02-28 16:29:52 -08:00
|
|
|
ShowAverageLoad bool
|
2020-04-27 20:33:41 -05:00
|
|
|
ShowPerCPULoad bool
|
2019-02-28 16:29:52 -08:00
|
|
|
updateInterval time.Duration
|
|
|
|
updateLock sync.Mutex
|
2020-05-12 18:42:45 -05:00
|
|
|
cpuLoads map[string]float64
|
2018-02-18 23:25:02 -08:00
|
|
|
}
|
|
|
|
|
2020-02-27 18:51:28 -06:00
|
|
|
var cpuLabels []string
|
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCPULoad bool) *CPUWidget {
|
|
|
|
self := &CPUWidget{
|
2019-02-28 16:29:52 -08:00
|
|
|
LineGraph: ui.NewLineGraph(),
|
2020-04-27 20:33:41 -05:00
|
|
|
CPUCount: len(cpuLabels),
|
2019-02-28 16:29:52 -08:00
|
|
|
updateInterval: updateInterval,
|
|
|
|
ShowAverageLoad: showAverageLoad,
|
2020-04-27 20:33:41 -05:00
|
|
|
ShowPerCPULoad: showPerCPULoad,
|
2020-05-12 18:42:45 -05:00
|
|
|
cpuLoads: make(map[string]float64),
|
2018-03-03 17:05:52 -08:00
|
|
|
}
|
2018-12-31 16:55:50 -08:00
|
|
|
self.Title = " CPU Usage "
|
2019-01-12 16:31:37 -08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2018-08-01 00:24:44 +03:00
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
if !(self.ShowAverageLoad || self.ShowPerCPULoad) {
|
|
|
|
if self.CPUCount <= 8 {
|
|
|
|
self.ShowPerCPULoad = true
|
2018-08-01 00:40:51 +03:00
|
|
|
} else {
|
2019-02-28 16:29:52 -08:00
|
|
|
self.ShowAverageLoad = true
|
2018-08-01 00:40:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 16:29:52 -08:00
|
|
|
if self.ShowAverageLoad {
|
2018-08-16 16:44:44 -07:00
|
|
|
self.Data["AVRG"] = []float64{0}
|
2018-02-18 23:25:02 -08:00
|
|
|
}
|
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
if self.ShowPerCPULoad {
|
2020-02-28 10:03:41 -06:00
|
|
|
cpus := make(map[string]int)
|
2020-04-27 20:33:41 -05:00
|
|
|
devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCPULoad)
|
2020-02-27 18:51:28 -06:00
|
|
|
for k, v := range cpus {
|
2020-02-28 10:03:41 -06:00
|
|
|
self.Data[k] = []float64{float64(v)}
|
2018-08-01 00:24:44 +03:00
|
|
|
}
|
|
|
|
}
|
2018-04-12 20:00:34 -07:00
|
|
|
|
2018-12-18 18:12:22 -08:00
|
|
|
self.update()
|
2018-11-29 18:17:13 -08:00
|
|
|
|
2018-02-18 23:25:02 -08:00
|
|
|
go func() {
|
2019-02-28 16:29:52 -08:00
|
|
|
for range time.NewTicker(self.updateInterval).C {
|
2018-03-27 14:27:23 -07:00
|
|
|
self.update()
|
2018-02-18 23:25:02 -08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-27 14:27:23 -07:00
|
|
|
return self
|
2018-02-18 23:25:02 -08:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:42:45 -05:00
|
|
|
const AVRG = "AVRG"
|
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
func (cpu *CPUWidget) EnableMetric() {
|
|
|
|
if cpu.ShowAverageLoad {
|
2020-05-12 18:42:45 -05:00
|
|
|
metrics.NewGauge(makeName("cpu", " avg"), func() float64 {
|
|
|
|
return cpu.cpuLoads[AVRG]
|
2020-02-18 09:44:29 -06:00
|
|
|
})
|
|
|
|
} else {
|
2020-02-28 10:03:41 -06:00
|
|
|
cpus := make(map[string]int)
|
2020-04-27 20:33:41 -05:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, cpu.ShowPerCPULoad)
|
2020-02-27 18:51:28 -06:00
|
|
|
for key, perc := range cpus {
|
2020-05-12 18:42:45 -05:00
|
|
|
kc := key
|
|
|
|
cpu.cpuLoads[key] = float64(perc)
|
|
|
|
metrics.NewGauge(makeName("cpu", key), func() float64 {
|
|
|
|
return cpu.cpuLoads[kc]
|
2020-02-18 09:44:29 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
func (cpu *CPUWidget) Scale(i int) {
|
|
|
|
cpu.LineGraph.HorizontalScale = i
|
2020-02-12 20:15:49 -06:00
|
|
|
}
|
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
func (cpu *CPUWidget) update() {
|
|
|
|
if cpu.ShowAverageLoad {
|
2018-08-16 16:44:44 -07:00
|
|
|
go func() {
|
2020-02-28 10:03:41 -06:00
|
|
|
cpus := make(map[string]int)
|
2020-04-27 20:33:41 -05:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, false)
|
|
|
|
cpu.Lock()
|
|
|
|
defer cpu.Unlock()
|
|
|
|
cpu.updateLock.Lock()
|
|
|
|
defer cpu.updateLock.Unlock()
|
2020-02-27 18:51:28 -06:00
|
|
|
var val float64
|
|
|
|
for _, v := range cpus {
|
2020-02-28 10:03:41 -06:00
|
|
|
val = float64(v)
|
2020-02-27 18:51:28 -06:00
|
|
|
break
|
|
|
|
}
|
2020-05-12 18:42:45 -05:00
|
|
|
cpu.Data[AVRG] = append(cpu.Data[AVRG], val)
|
|
|
|
cpu.Labels[AVRG] = fmt.Sprintf("%3.0f%%", val)
|
|
|
|
cpu.cpuLoads[AVRG] = val
|
2018-08-16 16:44:44 -07:00
|
|
|
}()
|
2018-02-18 23:25:02 -08:00
|
|
|
}
|
2018-08-01 00:24:44 +03:00
|
|
|
|
2020-04-27 20:33:41 -05:00
|
|
|
if cpu.ShowPerCPULoad {
|
2018-08-16 16:44:44 -07:00
|
|
|
go func() {
|
2020-02-28 10:03:41 -06:00
|
|
|
cpus := make(map[string]int)
|
2020-04-27 20:33:41 -05:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, true)
|
|
|
|
cpu.Lock()
|
|
|
|
defer cpu.Unlock()
|
|
|
|
cpu.updateLock.Lock()
|
|
|
|
defer cpu.updateLock.Unlock()
|
2020-02-27 18:51:28 -06:00
|
|
|
for key, percent := range cpus {
|
2020-04-27 20:33:41 -05:00
|
|
|
cpu.Data[key] = append(cpu.Data[key], float64(percent))
|
|
|
|
cpu.Labels[key] = fmt.Sprintf("%d%%", percent)
|
2020-05-12 18:42:45 -05:00
|
|
|
cpu.cpuLoads[key] = float64(percent)
|
2018-08-16 16:44:44 -07:00
|
|
|
}
|
|
|
|
}()
|
2018-08-01 00:24:44 +03:00
|
|
|
}
|
2018-02-18 23:25:02 -08:00
|
|
|
}
|