summaryrefslogtreecommitdiffstats
path: root/widgets/cpu.go
blob: c0f25bc264c24b853763050b6f97b198032fc361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package widgets

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	psCpu "github.com/shirou/gopsutil/cpu"
	"github.com/xxxserxxx/gotop/devices"

	ui "github.com/xxxserxxx/gotop/termui"
)

type CpuWidget struct {
	*ui.LineGraph
	CpuCount        int
	ShowAverageLoad bool
	ShowPerCpuLoad  bool
	updateInterval  time.Duration
	formatString    string
	updateLock      sync.Mutex
	metric          []prometheus.Gauge
}

func NewCpuWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCpuLoad bool) *CpuWidget {
	cpuCount, err := devices.Counts(false)
	if err != nil {
		log.Printf("failed to get CPU count: %v", err)
	}
	formatString := "CPU%1d"
	if cpuCount > 10 {
		formatString = "CPU%02d"
	}
	self := &CpuWidget{
		LineGraph:       ui.NewLineGraph(),
		CpuCount:        cpuCount,
		updateInterval:  updateInterval,
		ShowAverageLoad: showAverageLoad,
		ShowPerCpuLoad:  showPerCpuLoad,
		formatString:    formatString,
	}
	self.Title = " CPU Usage "
	self.HorizontalScale = horizontalScale

	if !(self.ShowAverageLoad || self.ShowPerCpuLoad) {
		if self.CpuCount <= 8 {
			self.ShowPerCpuLoad = true
		} else {
			self.ShowAverageLoad = true
		}
	}

	if self.ShowAverageLoad {
		self.Data["AVRG"] = []float64{0}
	}

	if self.ShowPerCpuLoad {
		for i := 0; i < int(self.CpuCount); i++ {
			key := fmt.Sprintf(formatString, i)
			self.Data[key] = []float64{0}
		}
	}

	self.update()

	go func() {
		for range time.NewTicker(self.updateInterval).C {
			self.update()
		}
	}()

	return self
}

func (self *CpuWidget) EnableMetric() {
	if self.ShowAverageLoad {
		self.metric = make([]prometheus.Gauge, 1)
		self.metric[0] = prometheus.NewGauge(prometheus.GaugeOpts{
			Subsystem: "cpu",
			Name:      "avg",
		})
	} else {
		ctx, ccl := context.WithTimeout(context.Background(), time.Second*5)
		defer ccl()
		percents, err := psCpu.PercentWithContext(ctx, self.updateInterval, true)
		if err != nil {
			log.Printf("error setting up metrics: %v", err)
			return
		}
		self.metric = make([]prometheus.Gauge, self.CpuCount)
		for i, perc := range percents {
			gauge := prometheus.NewGauge(prometheus.GaugeOpts{
				Namespace: "gotop",
				Subsystem: "cpu",
				Name:      fmt.Sprintf("%d", i),
			})
			gauge.Set(perc)
			prometheus.MustRegister(gauge)
			self.metric[i] = gauge
		}
	}
}

func (b *CpuWidget) Scale(i int) {
	b.LineGraph.HorizontalScale = i
}

func (self *CpuWidget) update() {
	if self.ShowAverageLoad {
		go func() {
			percent, err := devices.Percent(self.updateInterval, false)
			if err != nil {
				log.Printf("failed to get average CPU usage percent: %v. self.updateInterval: %v. percpu: %v", err, self.updateInterval, false)
			} else {
				self.Lock()
				defer self.Unlock()
				self.updateLock.Lock()
				defer self.updateLock.Unlock()
				self.Data["AVRG"] = append(self.Data["AVRG"], percent[0])
				self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", percent[0])
				if self.metric != nil {
					self.metric[0].Set(percent[0])
				}
			}
		}()
	}

	if self.ShowPerCpuLoad {
		go func() {
			percents, err := devices.Percent(self.updateInterval, true)
			if err != nil {
				log.Printf("failed to get CPU usage percents: %v. self.updateInterval: %v. percpu: %v", err, self.updateInterval, true)
			} else {
				if len(percents) != int(self.CpuCount) {
					log.Printf("error: number of CPU usage percents doesn't match CPU count. percents: %v. self.Count: %v", percents, self.CpuCount)
				} else {
					self.Lock()
					defer self.Unlock()
					self.updateLock.Lock()
					defer self.updateLock.Unlock()
					for i, percent := range percents {
						key := fmt.Sprintf(self.formatString, i)
						self.Data[key] = append(self.Data[key], percent)
						self.Labels[key] = fmt.Sprintf("%3.0f%%", percent)
						if self.metric != nil {
							if self.metric[i] == nil {
								log.Printf("ERROR: not enough metrics %d", i)
							} else {
								self.metric[i].Set(percent)
							}
						}
					}
				}
			}
		}()
	}
}