Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Fix issues when using both percpu and averagecpu
Browse files Browse the repository at this point in the history
* Change 'Average' to 'AVRG'
* Run both updates with goroutines so they don't case each other to
block
  • Loading branch information
cjbassi committed Aug 16, 2018
1 parent 9f7e5ce commit c3603ea
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/widgets/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])
}
}()
}
}

0 comments on commit c3603ea

Please sign in to comment.