Average cpu usage when more than 8 cores

This commit is contained in:
Caleb Bassi 2018-03-27 14:00:32 -07:00
parent bfd6382f6b
commit 86c5c7155a
2 changed files with 24 additions and 12 deletions

View File

@ -167,8 +167,12 @@ func widgetColors() {
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
LineColor := make(map[string]ui.Color)
for i := 0; i < len(cpu.Data); i++ {
LineColor[fmt.Sprintf("CPU%d", i+1)] = ui.Color(colorscheme.CPULines[i])
if cpu.Count <= 8 {
for i := 0; i < len(cpu.Data); i++ {
LineColor[fmt.Sprintf("CPU%d", i+1)] = ui.Color(colorscheme.CPULines[i])
}
} else {
LineColor["Average"] = ui.Color(colorscheme.CPULines[0])
}
cpu.LineColor = LineColor

View File

@ -10,7 +10,7 @@ import (
type CPU struct {
*ui.LineGraph
count int // number of CPUs
Count int // number of CPUs
interval time.Duration
}
@ -18,14 +18,18 @@ func NewCPU(interval time.Duration, zoom int) *CPU {
count, _ := psCPU.Counts(false)
c := &CPU{
LineGraph: ui.NewLineGraph(),
count: count,
Count: count,
interval: interval,
}
c.Label = "CPU Usage"
c.Zoom = zoom
for i := 0; i < c.count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = []float64{0}
if c.Count <= 8 {
for i := 0; i < c.Count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = []float64{0}
}
} else {
c.Data["Average"] = []float64{0}
}
go c.update()
@ -41,10 +45,14 @@ func NewCPU(interval time.Duration, zoom int) *CPU {
func (c *CPU) update() {
// psutil calculates the CPU usage over a 1 second interval, therefore it blocks for 1 second
// `true` makes it so psutil doesn't group CPU usage percentages
percent, _ := psCPU.Percent(c.interval, true)
for i := 0; i < c.count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = append(c.Data[key], percent[i])
if c.Count <= 8 {
percent, _ := psCPU.Percent(c.interval, true)
for i := 0; i < c.Count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = append(c.Data[key], percent[i])
}
} else {
percent, _ := psCPU.Percent(c.interval, false)
c.Data["Average"] = append(c.Data["Average"], percent[0])
}
}