2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-05-24 09:52:35 +08:00
|
|
|
"fmt"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2020-02-18 23:44:29 +08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-01-31 07:20:09 +08:00
|
|
|
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4/devices"
|
|
|
|
ui "github.com/xxxserxxx/gotop/v4/termui"
|
|
|
|
"github.com/xxxserxxx/gotop/v4/utils"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
type MemWidget struct {
|
2018-02-19 15:25:02 +08:00
|
|
|
*ui.LineGraph
|
2019-03-01 08:29:52 +08:00
|
|
|
updateInterval time.Duration
|
2020-02-28 08:51:28 +08:00
|
|
|
metrics map[string]prometheus.Gauge
|
2019-05-29 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-08 15:43:10 +08:00
|
|
|
func NewMemWidget(updateInterval time.Duration, horizontalScale int) *MemWidget {
|
2019-03-01 08:29:52 +08:00
|
|
|
self := &MemWidget{
|
|
|
|
LineGraph: ui.NewLineGraph(),
|
|
|
|
updateInterval: updateInterval,
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
self.Title = " Memory Usage "
|
2019-01-13 08:31:37 +08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2020-02-28 08:51:28 +08:00
|
|
|
mems := make(map[string]devices.MemoryInfo)
|
|
|
|
devices.UpdateMem(mems)
|
|
|
|
for name, mem := range mems {
|
2020-02-29 00:03:41 +08:00
|
|
|
self.Data[name] = []float64{0}
|
2020-02-28 08:51:28 +08:00
|
|
|
self.renderMemInfo(name, mem)
|
|
|
|
}
|
2018-04-13 11:00:34 +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 {
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Lock()
|
2020-02-28 08:51:28 +08:00
|
|
|
devices.UpdateMem(mems)
|
|
|
|
for label, mi := range mems {
|
|
|
|
self.renderMemInfo(label, mi)
|
|
|
|
if self.metrics != nil && self.metrics[label] != nil {
|
|
|
|
self.metrics[label].Set(mi.UsedPercent)
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Unlock()
|
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
|
|
|
}
|
2020-02-13 10:15:49 +08:00
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (mem *MemWidget) EnableMetric() {
|
|
|
|
mem.metrics = make(map[string]prometheus.Gauge)
|
2020-02-28 08:51:28 +08:00
|
|
|
mems := make(map[string]devices.MemoryInfo)
|
|
|
|
devices.UpdateMem(mems)
|
2020-04-28 09:33:41 +08:00
|
|
|
for l, m := range mems {
|
|
|
|
mem.metrics[l] = prometheus.NewGauge(prometheus.GaugeOpts{
|
2020-02-28 08:51:28 +08:00
|
|
|
Namespace: "gotop",
|
|
|
|
Subsystem: "memory",
|
|
|
|
Name: l,
|
|
|
|
})
|
2020-04-28 09:33:41 +08:00
|
|
|
mem.metrics[l].Set(m.UsedPercent)
|
|
|
|
prometheus.MustRegister(mem.metrics[l])
|
2020-02-18 23:44:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (mem *MemWidget) Scale(i int) {
|
|
|
|
mem.LineGraph.HorizontalScale = i
|
2020-02-13 10:15:49 +08:00
|
|
|
}
|
2020-02-28 08:51:28 +08:00
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (mem *MemWidget) renderMemInfo(line string, memoryInfo devices.MemoryInfo) {
|
|
|
|
mem.Data[line] = append(mem.Data[line], memoryInfo.UsedPercent)
|
2020-02-28 08:51:28 +08:00
|
|
|
memoryTotalBytes, memoryTotalMagnitude := utils.ConvertBytes(memoryInfo.Total)
|
|
|
|
memoryUsedBytes, memoryUsedMagnitude := utils.ConvertBytes(memoryInfo.Used)
|
2020-04-28 09:33:41 +08:00
|
|
|
mem.Labels[line] = fmt.Sprintf("%3.0f%% %5.1f%s/%.0f%s",
|
2020-02-28 08:51:28 +08:00
|
|
|
memoryInfo.UsedPercent,
|
|
|
|
memoryUsedBytes,
|
|
|
|
memoryUsedMagnitude,
|
|
|
|
memoryTotalBytes,
|
|
|
|
memoryTotalMagnitude,
|
|
|
|
)
|
|
|
|
}
|