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

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

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"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"
) )
@ -49,7 +50,8 @@ func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
ticker := time.NewTicker(self.interval) ticker := time.NewTicker(self.interval)
go func() { go func() {
self.update() // update asynchronously because of 1 second blocking period
go self.update()
for range ticker.C { for range ticker.C {
self.update() self.update()
} }
@ -68,6 +70,17 @@ func (self *CPU) update() {
if self.PerCPU { if self.PerCPU {
percents, _ := psCPU.Percent(self.interval, true) 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++ { for i := 0; i < self.Count; i++ {
k := fmt.Sprintf("CPU%d", i) k := fmt.Sprintf("CPU%d", i)
self.Data[k] = append(self.Data[k], percents[i]) self.Data[k] = append(self.Data[k], percents[i])