From fa9357d1ed084529e75fe7e04457c45d30843601 Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Sat, 25 Jul 2020 07:21:15 -0500 Subject: Closes #32. Average is really average, over time. Boldify the AVRG label. Remove spurious sync lock. --- cmd/gotop/main.go | 1 - config_test.go | 1 - devices/cpu_cpu.go | 1 - go.mod | 1 + go.sum | 2 ++ termui/linegraph.go | 10 +++++++-- widgets/cpu.go | 58 +++++++++++++++++++++++------------------------------ 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/cmd/gotop/main.go b/cmd/gotop/main.go index c917fc7..f9f6618 100644 --- a/cmd/gotop/main.go +++ b/cmd/gotop/main.go @@ -332,7 +332,6 @@ func eventLoop(c gotop.Config, grid *layout.MyGrid) { } } -// TODO: state:merge #135 linux console font (cmatsuoka/console-font) func main() { // TODO: Make this an option, for performance testing //go func() { diff --git a/config_test.go b/config_test.go index 4afb44d..1f4ecb0 100644 --- a/config_test.go +++ b/config_test.go @@ -9,7 +9,6 @@ import ( "github.com/xxxserxxx/gotop/v4/widgets" ) -// FIXME This is totally broken since the updates func TestParse(t *testing.T) { tests := []struct { i string diff --git a/devices/cpu_cpu.go b/devices/cpu_cpu.go index bd2f891..9847572 100644 --- a/devices/cpu_cpu.go +++ b/devices/cpu_cpu.go @@ -6,7 +6,6 @@ import ( psCpu "github.com/shirou/gopsutil/cpu" ) -// FIXME: broken % under Linux. Doesn't reflect reality *at all*. func init() { f := func(cpus map[string]int, l bool) map[string]error { cpuCount, err := CpuCount() diff --git a/go.mod b/go.mod index 70e6a96..1e07313 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/xxxserxxx/gotop/v4 require ( github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/VictoriaMetrics/metrics v1.11.2 + github.com/VividCortex/ewma v1.1.1 github.com/distatus/battery v0.9.0 github.com/gizak/termui/v3 v3.1.0 github.com/go-ole/go-ole v1.2.4 // indirect diff --git a/go.sum b/go.sum index fd648f6..575a44b 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUW github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/VictoriaMetrics/metrics v1.11.2 h1:t/ceLP6SvagUqypCKU7cI7+tQn54+TIV/tGoxihHvx8= github.com/VictoriaMetrics/metrics v1.11.2/go.mod h1:LU2j9qq7xqZYXz8tF3/RQnB2z2MbZms5TDiIg9/NHiQ= +github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM= +github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd h1:XtfPmj9tQRilnrEmI1HjQhxXWRhEM+m8CACtaMJE/kM= github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/termui/linegraph.go b/termui/linegraph.go index db15822..fa8a3ae 100644 --- a/termui/linegraph.go +++ b/termui/linegraph.go @@ -25,6 +25,7 @@ type LineGraph struct { HorizontalScale int LineColors map[string]Color + LabelStyles map[string]Modifier DefaultLineColor Color } @@ -37,7 +38,8 @@ func NewLineGraph() *LineGraph { HorizontalScale: 5, - LineColors: make(map[string]Color), + LineColors: make(map[string]Color), + LabelStyles: make(map[string]Modifier), } } @@ -136,6 +138,10 @@ func (self *LineGraph) Draw(buf *Buffer) { if !ok { seriesLineColor = self.DefaultLineColor } + seriesLabelStyle, ok := self.LabelStyles[seriesName] + if !ok { + seriesLabelStyle = ModifierClear + } // render key ontop, but let braille be drawn over space characters str := seriesName + " " + self.Labels[seriesName] @@ -145,7 +151,7 @@ func (self *LineGraph) Draw(buf *Buffer) { for k, char := range str { if char != ' ' { buf.SetCell( - NewCell(char, NewStyle(seriesLineColor)), + NewCell(char, NewStyle(seriesLineColor, ColorClear, seriesLabelStyle)), image.Pt(xoff+self.Inner.Min.X+2+k, yoff+self.Inner.Min.Y+i+1), ) } diff --git a/widgets/cpu.go b/widgets/cpu.go index d8d9a50..2668b3a 100644 --- a/widgets/cpu.go +++ b/widgets/cpu.go @@ -2,12 +2,13 @@ package widgets import ( "fmt" - "sync" "time" "github.com/VictoriaMetrics/metrics" + "github.com/VividCortex/ewma" "github.com/xxxserxxx/gotop/v4/devices" + "github.com/gizak/termui/v3" ui "github.com/xxxserxxx/gotop/v4/termui" ) @@ -17,8 +18,8 @@ type CPUWidget struct { ShowAverageLoad bool ShowPerCPULoad bool updateInterval time.Duration - updateLock sync.Mutex cpuLoads map[string]float64 + average ewma.MovingAverage } var cpuLabels []string @@ -31,7 +32,9 @@ func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverage ShowAverageLoad: showAverageLoad, ShowPerCPULoad: showPerCPULoad, cpuLoads: make(map[string]float64), + average: ewma.NewMovingAverage(), } + self.LabelStyles[AVRG] = termui.ModifierBold self.Title = " CPU Usage " self.HorizontalScale = horizontalScale @@ -44,7 +47,7 @@ func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverage } if self.ShowAverageLoad { - self.Data["AVRG"] = []float64{0} + self.Data[AVRG] = []float64{0} } if self.ShowPerCPULoad { @@ -91,38 +94,27 @@ func (cpu *CPUWidget) Scale(i int) { } func (cpu *CPUWidget) update() { - if cpu.ShowAverageLoad { - go func() { - cpus := make(map[string]int) - devices.UpdateCPU(cpus, cpu.updateInterval, false) - cpu.Lock() - defer cpu.Unlock() - cpu.updateLock.Lock() - defer cpu.updateLock.Unlock() - var val float64 - for _, v := range cpus { - val = float64(v) - break - } - cpu.Data[AVRG] = append(cpu.Data[AVRG], val) - cpu.Labels[AVRG] = fmt.Sprintf("%3.0f%%", val) - cpu.cpuLoads[AVRG] = val - }() - } - - if cpu.ShowPerCPULoad { - go func() { - cpus := make(map[string]int) - devices.UpdateCPU(cpus, cpu.updateInterval, true) - cpu.Lock() - defer cpu.Unlock() - cpu.updateLock.Lock() - defer cpu.updateLock.Unlock() - for key, percent := range cpus { + 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 { cpu.Data[key] = append(cpu.Data[key], float64(percent)) cpu.Labels[key] = fmt.Sprintf("%d%%", 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 + } + }() } -- cgit v1.2.3