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

Commit

Permalink
Added cli options for cpu load
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Aug 16, 2018
1 parent c71cd76 commit 8de8367
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
zoomInterval = 3

averageLoad = false
percpuLoad = false
percpuLoad = false

cpu *w.CPU
mem *w.Mem
Expand All @@ -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
Expand All @@ -86,6 +88,9 @@ Colorschemes:
} else {
interval = time.Second / time.Duration(rate)
}

averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
}

func handleColorscheme(cs string) {
Expand Down
21 changes: 17 additions & 4 deletions src/widgets/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand All @@ -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])
Expand Down

0 comments on commit 8de8367

Please sign in to comment.