Remove 8 cores limit in CPU widget

This commit is contained in:
Ivan Trubach 2018-08-01 00:24:44 +03:00
parent eb17f69606
commit 12527db13f
No known key found for this signature in database
GPG Key ID: FC2AF342E98E6BA2
2 changed files with 36 additions and 40 deletions

20
main.go
View File

@ -37,6 +37,9 @@ var (
zoom = 7 zoom = 7
zoomInterval = 3 zoomInterval = 3
averageLoad = true
percpuLoad = true
cpu *w.CPU cpu *w.CPU
mem *w.Mem mem *w.Mem
proc *w.Proc proc *w.Proc
@ -173,15 +176,16 @@ func widgetColors() {
mem.LineColor["Main"] = ui.Color(colorscheme.MainMem) mem.LineColor["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem) mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
LineColor := make(map[string]ui.Color) i := 0
if cpu.Count <= 8 { for k := range cpu.Data {
for i := 0; i < len(cpu.Data); i++ { if i >= len(colorscheme.CPULines) {
LineColor[fmt.Sprintf("CPU%d", i)] = ui.Color(colorscheme.CPULines[i]) // assuming colorscheme for CPU lines is not empty
i = 0
} }
} else { c := colorscheme.CPULines[i]
LineColor["Average"] = ui.Color(colorscheme.CPULines[0]) cpu.LineColor[k] = ui.Color(c)
i++
} }
cpu.LineColor = LineColor
if !minimal { if !minimal {
temp.TempLow = ui.Color(colorscheme.TempLow) temp.TempLow = ui.Color(colorscheme.TempLow)
@ -194,7 +198,7 @@ func initWidgets() {
wg.Add(widgetCount) wg.Add(widgetCount)
go func() { go func() {
cpu = w.NewCPU(interval, zoom) cpu = w.NewCPU(interval, zoom, averageLoad, percpuLoad)
wg.Done() wg.Done()
}() }()
go func() { go func() {

View File

@ -2,10 +2,8 @@ package widgets
import ( import (
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/cjbassi/gotop/src/utils"
ui "github.com/cjbassi/termui" ui "github.com/cjbassi/termui"
psCPU "github.com/shirou/gopsutil/cpu" psCPU "github.com/shirou/gopsutil/cpu"
) )
@ -13,32 +11,37 @@ import (
type CPU struct { type CPU struct {
*ui.LineGraph *ui.LineGraph
Count int // number of cores Count int // number of cores
Average bool // show average load
PerCPU bool // show per-core load
interval time.Duration interval time.Duration
} }
func NewCPU(interval time.Duration, zoom int) *CPU { func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
count, _ := psCPU.Counts(false) count, _ := psCPU.Counts(false)
self := &CPU{ self := &CPU{
LineGraph: ui.NewLineGraph(), LineGraph: ui.NewLineGraph(),
Count: count, Count: count,
interval: interval, interval: interval,
Average: average,
PerCPU: percpu,
} }
self.Label = "CPU Usage" self.Label = "CPU Usage"
self.Zoom = zoom self.Zoom = zoom
if self.Count <= 8 {
for i := 0; i < self.Count; i++ { if self.Average {
key := "CPU" + strconv.Itoa(i)
self.Data[key] = []float64{0}
}
} else {
self.Data["Average"] = []float64{0} self.Data["Average"] = []float64{0}
} }
// update asynchronously because of 1 second blocking period if self.PerCPU {
go self.update() for i := 0; i < self.Count; i++ {
k := fmt.Sprintf("CPU%d", i)
self.Data[k] = []float64{0}
}
}
ticker := time.NewTicker(self.interval) ticker := time.NewTicker(self.interval)
go func() { go func() {
self.update()
for range ticker.C { for range ticker.C {
self.update() self.update()
} }
@ -49,29 +52,18 @@ func NewCPU(interval time.Duration, zoom int) *CPU {
// calculates the CPU usage over a 1 second interval and blocks for the duration // calculates the CPU usage over a 1 second interval and blocks for the duration
func (self *CPU) update() { func (self *CPU) update() {
// show average cpu usage if more than 8 cores if self.Average {
if self.Count <= 8 {
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++ {
key := "CPU" + strconv.Itoa(i)
percent := percents[i]
self.Data[key] = append(self.Data[key], percent)
self.Labels[key] = fmt.Sprintf("%3.0f%%", percent)
}
} else {
percent, _ := psCPU.Percent(self.interval, false) percent, _ := psCPU.Percent(self.interval, false)
self.Data["Average"] = append(self.Data["Average"], percent[0]) self.Data["Average"] = append(self.Data["Average"], percent[0])
self.Labels["Average"] = fmt.Sprintf("%3.0f%%", percent[0]) self.Labels["Average"] = fmt.Sprintf("%3.0f%%", percent[0])
} }
if self.PerCPU {
percents, _ := psCPU.Percent(self.interval, true)
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])
}
}
} }