2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-04-11 11:08:02 +08:00
|
|
|
"fmt"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2018-03-30 06:48:43 +08:00
|
|
|
ui "github.com/cjbassi/termui"
|
2018-03-04 09:05:52 +08:00
|
|
|
psCPU "github.com/shirou/gopsutil/cpu"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type CPU struct {
|
|
|
|
*ui.LineGraph
|
2018-12-02 13:00:17 +08:00
|
|
|
Count int // number of cores
|
|
|
|
Average bool // show average load
|
|
|
|
PerCPU bool // show per-core load
|
|
|
|
interval time.Duration
|
|
|
|
formatString string
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:44 +08:00
|
|
|
func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
|
2018-12-05 13:44:25 +08:00
|
|
|
count, err := psCPU.Counts(false)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get CPU count from gopsutil: %v", err)
|
|
|
|
}
|
2018-12-02 13:00:17 +08:00
|
|
|
formatString := "CPU%1d"
|
|
|
|
if count > 10 {
|
|
|
|
formatString = "CPU%02d"
|
|
|
|
}
|
2018-03-28 05:27:23 +08:00
|
|
|
self := &CPU{
|
2018-12-02 13:00:17 +08:00
|
|
|
LineGraph: ui.NewLineGraph(),
|
|
|
|
Count: count,
|
|
|
|
interval: interval,
|
|
|
|
Average: average,
|
|
|
|
PerCPU: percpu,
|
|
|
|
formatString: formatString,
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
2018-03-28 05:27:23 +08:00
|
|
|
self.Label = "CPU Usage"
|
|
|
|
self.Zoom = zoom
|
2018-08-01 05:24:44 +08:00
|
|
|
|
2018-08-01 05:40:51 +08:00
|
|
|
if !(self.Average || self.PerCPU) {
|
|
|
|
if self.Count <= 8 {
|
|
|
|
self.PerCPU = true
|
|
|
|
} else {
|
|
|
|
self.Average = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:44 +08:00
|
|
|
if self.Average {
|
2018-08-17 07:44:44 +08:00
|
|
|
self.Data["AVRG"] = []float64{0}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:44 +08:00
|
|
|
if self.PerCPU {
|
|
|
|
for i := 0; i < self.Count; i++ {
|
2018-12-02 13:00:17 +08:00
|
|
|
k := fmt.Sprintf(formatString, i)
|
2018-08-01 05:24:44 +08:00
|
|
|
self.Data[k] = []float64{0}
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 11:00:34 +08:00
|
|
|
|
2018-12-19 10:12:22 +08:00
|
|
|
self.update()
|
2018-11-30 10:17:13 +08:00
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
go func() {
|
2018-11-30 10:17:13 +08:00
|
|
|
ticker := time.NewTicker(self.interval)
|
2018-02-19 15:25:02 +08:00
|
|
|
for range ticker.C {
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
return self
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
func (self *CPU) update() {
|
2018-08-01 05:24:44 +08:00
|
|
|
if self.Average {
|
2018-08-17 07:44:44 +08:00
|
|
|
go func() {
|
2018-12-05 13:44:25 +08:00
|
|
|
percent, err := psCPU.Percent(self.interval, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get average CPU usage percent from gopsutil: %v. self.interval: %v. percpu: %v", err, self.interval, false)
|
2018-12-14 13:59:45 +08:00
|
|
|
} else {
|
|
|
|
self.Data["AVRG"] = append(self.Data["AVRG"], percent[0])
|
|
|
|
self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", percent[0])
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2018-08-17 07:44:44 +08:00
|
|
|
}()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-08-01 05:24:44 +08:00
|
|
|
|
|
|
|
if self.PerCPU {
|
2018-08-17 07:44:44 +08:00
|
|
|
go func() {
|
2018-12-05 13:44:25 +08:00
|
|
|
percents, err := psCPU.Percent(self.interval, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get CPU usage percents from gopsutil: %v. self.interval: %v. percpu: %v", err, self.interval, true)
|
2018-12-06 09:43:46 +08:00
|
|
|
} else {
|
2018-12-14 13:59:45 +08:00
|
|
|
if len(percents) != self.Count {
|
|
|
|
log.Printf("error: number of CPU usage percents from gopsutil doesn't match CPU count. percents: %v. self.Count: %v", percents, self.Count)
|
|
|
|
} else {
|
|
|
|
for i, percent := range percents {
|
|
|
|
k := fmt.Sprintf(self.formatString, i)
|
|
|
|
self.Data[k] = append(self.Data[k], percent)
|
|
|
|
self.Labels[k] = fmt.Sprintf("%3.0f%%", percent)
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 07:44:44 +08:00
|
|
|
}
|
|
|
|
}()
|
2018-08-01 05:24:44 +08:00
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|