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-05-13 07:42:45 +08:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
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
|
2019-05-29 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-08 15:43:10 +08:00
|
|
|
func NewMemWidget(updateInterval time.Duration, horizontalScale int) *MemWidget {
|
2020-05-13 07:42:45 +08:00
|
|
|
widg := &MemWidget{
|
2019-03-01 08:29:52 +08:00
|
|
|
LineGraph: ui.NewLineGraph(),
|
|
|
|
updateInterval: updateInterval,
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
2020-06-19 08:51:01 +08:00
|
|
|
widg.Title = tr.Value("widget.label.mem")
|
2020-05-13 07:42:45 +08:00
|
|
|
widg.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-11-22 19:19:02 +08:00
|
|
|
if mem.Total > 0 {
|
|
|
|
widg.Data[name] = []float64{0}
|
|
|
|
widg.renderMemInfo(name, mem)
|
|
|
|
}
|
2020-02-28 08:51:28 +08:00
|
|
|
}
|
2018-04-13 11:00:34 +08:00
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
go func() {
|
2020-05-13 07:42:45 +08:00
|
|
|
for range time.NewTicker(widg.updateInterval).C {
|
|
|
|
widg.Lock()
|
2020-02-28 08:51:28 +08:00
|
|
|
devices.UpdateMem(mems)
|
|
|
|
for label, mi := range mems {
|
2020-11-22 19:19:02 +08:00
|
|
|
if mi.Total > 0 {
|
|
|
|
widg.renderMemInfo(label, mi)
|
|
|
|
}
|
2020-02-28 08:51:28 +08:00
|
|
|
}
|
2020-05-13 07:42:45 +08:00
|
|
|
widg.Unlock()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-13 07:42:45 +08:00
|
|
|
return widg
|
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() {
|
2020-02-28 08:51:28 +08:00
|
|
|
mems := make(map[string]devices.MemoryInfo)
|
|
|
|
devices.UpdateMem(mems)
|
2020-11-22 19:19:02 +08:00
|
|
|
for l := range mems {
|
2020-05-13 07:42:45 +08:00
|
|
|
lc := l
|
|
|
|
metrics.NewGauge(makeName("memory", l), func() float64 {
|
|
|
|
if ds, ok := mem.Data[lc]; ok {
|
|
|
|
return ds[len(ds)-1]
|
|
|
|
}
|
|
|
|
return 0.0
|
2020-02-28 08:51:28 +08:00
|
|
|
})
|
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,
|
|
|
|
)
|
|
|
|
}
|