Added cli options for cpu load

This commit is contained in:
Caleb Bassi 2018-08-16 16:03:29 -07:00
parent c71cd760e6
commit 8de8367084
2 changed files with 23 additions and 5 deletions

View File

@ -38,7 +38,7 @@ var (
zoomInterval = 3
averageLoad = false
percpuLoad = false
percpuLoad = false
cpu *w.CPU
mem *w.Mem
@ -60,6 +60,8 @@ Options:
-m, --minimal Only show CPU, Mem and Process widgets.
-r, --rate=RATE Number of times per second to update CPU and Mem widgets [default: 1].
-v, --version Show version.
-p, --percpu Show each CPU in the CPU widget.
-a, --averagecpu Show average CPU in the CPU widget.
Colorschemes:
default
@ -86,6 +88,9 @@ Colorschemes:
} else {
interval = time.Second / time.Duration(rate)
}
averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
}
func handleColorscheme(cs string) {

View File

@ -4,15 +4,16 @@ import (
"fmt"
"time"
"github.com/cjbassi/gotop/src/utils"
ui "github.com/cjbassi/termui"
psCPU "github.com/shirou/gopsutil/cpu"
)
type CPU struct {
*ui.LineGraph
Count int // number of cores
Average bool // show average load
PerCPU bool // show per-core load
Count int // number of cores
Average bool // show average load
PerCPU bool // show per-core load
interval time.Duration
}
@ -49,7 +50,8 @@ func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
ticker := time.NewTicker(self.interval)
go func() {
self.update()
// update asynchronously because of 1 second blocking period
go self.update()
for range ticker.C {
self.update()
}
@ -68,6 +70,17 @@ func (self *CPU) update() {
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])