diff options
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/batterygauge.go | 4 | ||||
-rw-r--r-- | widgets/cpu.go | 60 | ||||
-rw-r--r-- | widgets/proc.go | 5 | ||||
-rw-r--r-- | widgets/proc_linux.go | 1 |
4 files changed, 33 insertions, 37 deletions
diff --git a/widgets/batterygauge.go b/widgets/batterygauge.go index 8dd681e..20d92d4 100644 --- a/widgets/batterygauge.go +++ b/widgets/batterygauge.go @@ -51,6 +51,10 @@ func (b *BatteryGauge) update() { } return } + if len(bats) < 1 { + b.Label = fmt.Sprintf("N/A") + return + } mx := 0.0 cu := 0.0 charging := "%d%% ⚡%s" diff --git a/widgets/cpu.go b/widgets/cpu.go index 4d2bc89..9983a10 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 = tr.Value("widget.label.cpu") 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.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 + } + }() } diff --git a/widgets/proc.go b/widgets/proc.go index ee94825..408906c 100644 --- a/widgets/proc.go +++ b/widgets/proc.go @@ -9,9 +9,8 @@ import ( "strings" "time" - psCPU "github.com/shirou/gopsutil/cpu" - tui "github.com/gizak/termui/v3" + "github.com/xxxserxxx/gotop/v4/devices" ui "github.com/xxxserxxx/gotop/v4/termui" "github.com/xxxserxxx/gotop/v4/utils" ) @@ -49,7 +48,7 @@ type ProcWidget struct { } func NewProcWidget() *ProcWidget { - cpuCount, err := psCPU.Counts(false) + cpuCount, err := devices.CpuCount() if err != nil { log.Println(tr.Value("error.proc.err.count", err.Error())) } diff --git a/widgets/proc_linux.go b/widgets/proc_linux.go index 938e5c2..b967c7b 100644 --- a/widgets/proc_linux.go +++ b/widgets/proc_linux.go @@ -19,6 +19,7 @@ func getProcs() ([]Proc, error) { procs := []Proc{} for _, line := range linesOfProcStrings { + log.Printf("line is '%s', pid is '%s', cpu is '%s', mem is '%s'", line, strings.TrimSpace(line[0:10]), strings.TrimSpace(line[63:68]), strings.TrimSpace(line[69:74])) pid, err := strconv.Atoi(strings.TrimSpace(line[0:10])) if err != nil { log.Println(tr.Value("widget.proc.err.pidconv", err.Error(), line)) |