From c3603ea1d5c6f785e913ae13366a9de682e0b732 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Thu, 16 Aug 2018 16:44:44 -0700 Subject: [PATCH] Fix issues when using both percpu and averagecpu * Change 'Average' to 'AVRG' * Run both updates with goroutines so they don't case each other to block --- src/widgets/cpu.go | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/widgets/cpu.go b/src/widgets/cpu.go index 573eede..043aa01 100644 --- a/src/widgets/cpu.go +++ b/src/widgets/cpu.go @@ -38,7 +38,7 @@ func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU { } if self.Average { - self.Data["Average"] = []float64{0} + self.Data["AVRG"] = []float64{0} } if self.PerCPU { @@ -63,28 +63,32 @@ func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU { // calculates the CPU usage over a 1 second interval and blocks for the duration func (self *CPU) update() { if self.Average { - percent, _ := psCPU.Percent(self.interval, false) - self.Data["Average"] = append(self.Data["Average"], percent[0]) - self.Labels["Average"] = fmt.Sprintf("%3.0f%%", percent[0]) + go func() { + percent, _ := psCPU.Percent(self.interval, false) + self.Data["AVRG"] = append(self.Data["AVRG"], percent[0]) + self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", percent[0]) + }() } if self.PerCPU { - percents, _ := psCPU.Percent(self.interval, true) - if len(percents) != self.Count { - count, _ := psCPU.Counts(false) - utils.Error("CPU percentages", - fmt.Sprint( - "self.Count: ", self.Count, "\n", - "gopsutil.Counts(): ", count, "\n", - "len(percents): ", len(percents), "\n", - "percents: ", percents, "\n", - "self.interval: ", self.interval, - )) - } - for i := 0; i < self.Count; i++ { - k := fmt.Sprintf("CPU%d", i) - self.Data[k] = append(self.Data[k], percents[i]) - self.Labels[k] = fmt.Sprintf("%3.0f%%", percents[i]) - } + go func() { + percents, _ := psCPU.Percent(self.interval, true) + if len(percents) != self.Count { + count, _ := psCPU.Counts(false) + utils.Error("CPU percentages", + fmt.Sprint( + "self.Count: ", self.Count, "\n", + "gopsutil.Counts(): ", count, "\n", + "len(percents): ", len(percents), "\n", + "percents: ", percents, "\n", + "self.interval: ", self.interval, + )) + } + for i := 0; i < self.Count; i++ { + k := fmt.Sprintf("CPU%d", i) + self.Data[k] = append(self.Data[k], percents[i]) + self.Labels[k] = fmt.Sprintf("%3.0f%%", percents[i]) + } + }() } }