Closes #32. Average is really average, over time. Boldify the AVRG label. Remove spurious sync lock.

This commit is contained in:
Sean E. Russell 2020-07-25 07:21:15 -05:00
parent f9f330a910
commit c25e58e79c
7 changed files with 36 additions and 38 deletions

View File

@ -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() {

View File

@ -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

View File

@ -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 := psCpu.Counts(l)

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -25,6 +25,7 @@ type LineGraph struct {
HorizontalScale int
LineColors map[string]Color
LabelStyles map[string]Modifier
DefaultLineColor Color
}
@ -38,6 +39,7 @@ func NewLineGraph() *LineGraph {
HorizontalScale: 5,
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),
)
}

View File

@ -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()
// 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("%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
}
}()
}