Closes #32. Average is really average, over time. Boldify the AVRG label. Remove spurious sync lock.
This commit is contained in:
parent
f9f330a910
commit
c25e58e79c
|
@ -332,7 +332,6 @@ func eventLoop(c gotop.Config, grid *layout.MyGrid) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: state:merge #135 linux console font (cmatsuoka/console-font)
|
|
||||||
func main() {
|
func main() {
|
||||||
// TODO: Make this an option, for performance testing
|
// TODO: Make this an option, for performance testing
|
||||||
//go func() {
|
//go func() {
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"github.com/xxxserxxx/gotop/v4/widgets"
|
"github.com/xxxserxxx/gotop/v4/widgets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FIXME This is totally broken since the updates
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
i string
|
i string
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
psCpu "github.com/shirou/gopsutil/cpu"
|
psCpu "github.com/shirou/gopsutil/cpu"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FIXME: broken % under Linux. Doesn't reflect reality *at all*.
|
|
||||||
func init() {
|
func init() {
|
||||||
f := func(cpus map[string]int, l bool) map[string]error {
|
f := func(cpus map[string]int, l bool) map[string]error {
|
||||||
cpuCount, err := psCpu.Counts(l)
|
cpuCount, err := psCpu.Counts(l)
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module github.com/xxxserxxx/gotop/v4
|
||||||
require (
|
require (
|
||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||||
github.com/VictoriaMetrics/metrics v1.11.2
|
github.com/VictoriaMetrics/metrics v1.11.2
|
||||||
|
github.com/VividCortex/ewma v1.1.1
|
||||||
github.com/distatus/battery v0.9.0
|
github.com/distatus/battery v0.9.0
|
||||||
github.com/gizak/termui/v3 v3.1.0
|
github.com/gizak/termui/v3 v3.1.0
|
||||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||||
|
|
2
go.sum
2
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/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 h1:t/ceLP6SvagUqypCKU7cI7+tQn54+TIV/tGoxihHvx8=
|
||||||
github.com/VictoriaMetrics/metrics v1.11.2/go.mod h1:LU2j9qq7xqZYXz8tF3/RQnB2z2MbZms5TDiIg9/NHiQ=
|
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 h1:XtfPmj9tQRilnrEmI1HjQhxXWRhEM+m8CACtaMJE/kM=
|
||||||
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4=
|
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=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
|
|
@ -25,6 +25,7 @@ type LineGraph struct {
|
||||||
HorizontalScale int
|
HorizontalScale int
|
||||||
|
|
||||||
LineColors map[string]Color
|
LineColors map[string]Color
|
||||||
|
LabelStyles map[string]Modifier
|
||||||
DefaultLineColor Color
|
DefaultLineColor Color
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ func NewLineGraph() *LineGraph {
|
||||||
HorizontalScale: 5,
|
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 {
|
if !ok {
|
||||||
seriesLineColor = self.DefaultLineColor
|
seriesLineColor = self.DefaultLineColor
|
||||||
}
|
}
|
||||||
|
seriesLabelStyle, ok := self.LabelStyles[seriesName]
|
||||||
|
if !ok {
|
||||||
|
seriesLabelStyle = ModifierClear
|
||||||
|
}
|
||||||
|
|
||||||
// render key ontop, but let braille be drawn over space characters
|
// render key ontop, but let braille be drawn over space characters
|
||||||
str := seriesName + " " + self.Labels[seriesName]
|
str := seriesName + " " + self.Labels[seriesName]
|
||||||
|
@ -145,7 +151,7 @@ func (self *LineGraph) Draw(buf *Buffer) {
|
||||||
for k, char := range str {
|
for k, char := range str {
|
||||||
if char != ' ' {
|
if char != ' ' {
|
||||||
buf.SetCell(
|
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),
|
image.Pt(xoff+self.Inner.Min.X+2+k, yoff+self.Inner.Min.Y+i+1),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,13 @@ package widgets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/VictoriaMetrics/metrics"
|
"github.com/VictoriaMetrics/metrics"
|
||||||
|
"github.com/VividCortex/ewma"
|
||||||
"github.com/xxxserxxx/gotop/v4/devices"
|
"github.com/xxxserxxx/gotop/v4/devices"
|
||||||
|
|
||||||
|
"github.com/gizak/termui/v3"
|
||||||
ui "github.com/xxxserxxx/gotop/v4/termui"
|
ui "github.com/xxxserxxx/gotop/v4/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,8 +18,8 @@ type CPUWidget struct {
|
||||||
ShowAverageLoad bool
|
ShowAverageLoad bool
|
||||||
ShowPerCPULoad bool
|
ShowPerCPULoad bool
|
||||||
updateInterval time.Duration
|
updateInterval time.Duration
|
||||||
updateLock sync.Mutex
|
|
||||||
cpuLoads map[string]float64
|
cpuLoads map[string]float64
|
||||||
|
average ewma.MovingAverage
|
||||||
}
|
}
|
||||||
|
|
||||||
var cpuLabels []string
|
var cpuLabels []string
|
||||||
|
@ -31,7 +32,9 @@ func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverage
|
||||||
ShowAverageLoad: showAverageLoad,
|
ShowAverageLoad: showAverageLoad,
|
||||||
ShowPerCPULoad: showPerCPULoad,
|
ShowPerCPULoad: showPerCPULoad,
|
||||||
cpuLoads: make(map[string]float64),
|
cpuLoads: make(map[string]float64),
|
||||||
|
average: ewma.NewMovingAverage(),
|
||||||
}
|
}
|
||||||
|
self.LabelStyles[AVRG] = termui.ModifierBold
|
||||||
self.Title = " CPU Usage "
|
self.Title = " CPU Usage "
|
||||||
self.HorizontalScale = horizontalScale
|
self.HorizontalScale = horizontalScale
|
||||||
|
|
||||||
|
@ -44,7 +47,7 @@ func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverage
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.ShowAverageLoad {
|
if self.ShowAverageLoad {
|
||||||
self.Data["AVRG"] = []float64{0}
|
self.Data[AVRG] = []float64{0}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.ShowPerCPULoad {
|
if self.ShowPerCPULoad {
|
||||||
|
@ -91,38 +94,27 @@ func (cpu *CPUWidget) Scale(i int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cpu *CPUWidget) update() {
|
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() {
|
go func() {
|
||||||
cpus := make(map[string]int)
|
cpus := make(map[string]int)
|
||||||
devices.UpdateCPU(cpus, cpu.updateInterval, true)
|
devices.UpdateCPU(cpus, cpu.updateInterval, true)
|
||||||
cpu.Lock()
|
cpu.Lock()
|
||||||
defer cpu.Unlock()
|
defer cpu.Unlock()
|
||||||
cpu.updateLock.Lock()
|
// AVG = ((AVG*i)+n)/(i+1)
|
||||||
defer cpu.updateLock.Unlock()
|
var sum int
|
||||||
for key, percent := range cpus {
|
for key, percent := range cpus {
|
||||||
|
sum += percent
|
||||||
|
if cpu.ShowPerCPULoad {
|
||||||
cpu.Data[key] = append(cpu.Data[key], float64(percent))
|
cpu.Data[key] = append(cpu.Data[key], float64(percent))
|
||||||
cpu.Labels[key] = fmt.Sprintf("%3d%%", percent)
|
cpu.Labels[key] = fmt.Sprintf("%3d%%", percent)
|
||||||
cpu.cpuLoads[key] = float64(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
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user