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"
|
2019-01-20 12:34:19 +08:00
|
|
|
"sync"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
psCpu "github.com/shirou/gopsutil/cpu"
|
2019-01-31 07:20:09 +08:00
|
|
|
|
|
|
|
ui "github.com/cjbassi/gotop/src/termui"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
type CpuWidget struct {
|
2018-02-19 15:25:02 +08:00
|
|
|
*ui.LineGraph
|
2019-03-01 08:29:52 +08:00
|
|
|
CpuCount int
|
|
|
|
ShowAverageLoad bool
|
|
|
|
ShowPerCpuLoad bool
|
|
|
|
updateInterval time.Duration
|
|
|
|
formatString string
|
|
|
|
renderLock *sync.RWMutex
|
|
|
|
updateLock sync.Mutex
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
func NewCpuWidget(renderLock *sync.RWMutex, updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCpuLoad bool) *CpuWidget {
|
|
|
|
cpuCount, err := psCpu.Counts(false)
|
2018-12-05 13:44:25 +08:00
|
|
|
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"
|
2019-03-01 08:29:52 +08:00
|
|
|
if cpuCount > 10 {
|
2018-12-02 13:00:17 +08:00
|
|
|
formatString = "CPU%02d"
|
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
self := &CpuWidget{
|
|
|
|
LineGraph: ui.NewLineGraph(),
|
|
|
|
CpuCount: cpuCount,
|
|
|
|
updateInterval: updateInterval,
|
|
|
|
ShowAverageLoad: showAverageLoad,
|
|
|
|
ShowPerCpuLoad: showPerCpuLoad,
|
|
|
|
formatString: formatString,
|
|
|
|
renderLock: renderLock,
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
self.Title = " CPU Usage "
|
2019-01-13 08:31:37 +08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2018-08-01 05:24:44 +08:00
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if !(self.ShowAverageLoad || self.ShowPerCpuLoad) {
|
|
|
|
if self.CpuCount <= 8 {
|
|
|
|
self.ShowPerCpuLoad = true
|
2018-08-01 05:40:51 +08:00
|
|
|
} else {
|
2019-03-01 08:29:52 +08:00
|
|
|
self.ShowAverageLoad = true
|
2018-08-01 05:40:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if self.ShowAverageLoad {
|
2018-08-17 07:44:44 +08:00
|
|
|
self.Data["AVRG"] = []float64{0}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if self.ShowPerCpuLoad {
|
|
|
|
for i := 0; i < int(self.CpuCount); i++ {
|
|
|
|
key := fmt.Sprintf(formatString, i)
|
|
|
|
self.Data[key] = []float64{0}
|
2018-08-01 05:24:44 +08:00
|
|
|
}
|
|
|
|
}
|
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() {
|
2019-03-01 08:29:52 +08:00
|
|
|
for range time.NewTicker(self.updateInterval).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
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
func (self *CpuWidget) update() {
|
|
|
|
if self.ShowAverageLoad {
|
2018-08-17 07:44:44 +08:00
|
|
|
go func() {
|
2019-03-01 08:29:52 +08:00
|
|
|
percent, err := psCpu.Percent(self.updateInterval, false)
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-03-01 08:29:52 +08:00
|
|
|
log.Printf("failed to get average CPU usage percent from gopsutil: %v. self.updateInterval: %v. percpu: %v", err, self.updateInterval, false)
|
2018-12-14 13:59:45 +08:00
|
|
|
} else {
|
2019-01-27 06:09:32 +08:00
|
|
|
self.renderLock.RLock()
|
|
|
|
defer self.renderLock.RUnlock()
|
|
|
|
self.updateLock.Lock()
|
|
|
|
defer self.updateLock.Unlock()
|
2018-12-14 13:59:45 +08:00
|
|
|
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
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if self.ShowPerCpuLoad {
|
2018-08-17 07:44:44 +08:00
|
|
|
go func() {
|
2019-03-01 08:29:52 +08:00
|
|
|
percents, err := psCpu.Percent(self.updateInterval, true)
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-03-01 08:29:52 +08:00
|
|
|
log.Printf("failed to get CPU usage percents from gopsutil: %v. self.updateInterval: %v. percpu: %v", err, self.updateInterval, true)
|
2018-12-06 09:43:46 +08:00
|
|
|
} else {
|
2019-03-01 08:29:52 +08:00
|
|
|
if len(percents) != int(self.CpuCount) {
|
|
|
|
log.Printf("error: number of CPU usage percents from gopsutil doesn't match CPU count. percents: %v. self.Count: %v", percents, self.CpuCount)
|
2018-12-14 13:59:45 +08:00
|
|
|
} else {
|
2019-01-27 06:09:32 +08:00
|
|
|
self.renderLock.RLock()
|
|
|
|
defer self.renderLock.RUnlock()
|
|
|
|
self.updateLock.Lock()
|
|
|
|
defer self.updateLock.Unlock()
|
2018-12-14 13:59:45 +08:00
|
|
|
for i, percent := range percents {
|
2019-03-01 08:29:52 +08:00
|
|
|
key := fmt.Sprintf(self.formatString, i)
|
|
|
|
self.Data[key] = append(self.Data[key], percent)
|
|
|
|
self.Labels[key] = fmt.Sprintf("%3.0f%%", percent)
|
2018-12-14 13:59:45 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 07:44:44 +08:00
|
|
|
}
|
|
|
|
}()
|
2018-08-01 05:24:44 +08:00
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|