xmtop/widgets/mem.go

42 lines
739 B
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
"time"
ui "github.com/cjbassi/gotop/termui"
2018-03-04 09:05:52 +08:00
psMem "github.com/shirou/gopsutil/mem"
2018-02-19 15:25:02 +08:00
)
type Mem struct {
*ui.LineGraph
interval time.Duration
}
2018-03-10 08:29:05 +08:00
func NewMem(interval time.Duration, zoom int) *Mem {
2018-03-04 09:05:52 +08:00
m := &Mem{
LineGraph: ui.NewLineGraph(),
2018-03-09 16:27:46 +08:00
interval: interval,
2018-03-04 09:05:52 +08:00
}
2018-02-19 15:25:02 +08:00
m.Label = "Memory Usage"
2018-03-10 08:29:05 +08:00
m.Zoom = zoom
2018-02-23 16:42:39 +08:00
m.Data["Main"] = []float64{0}
2018-02-19 15:25:02 +08:00
m.Data["Swap"] = []float64{0}
go m.update()
ticker := time.NewTicker(m.interval)
go func() {
for range ticker.C {
m.update()
}
}()
return m
}
func (m *Mem) update() {
2018-03-04 09:05:52 +08:00
main, _ := psMem.VirtualMemory()
swap, _ := psMem.SwapMemory()
2018-02-19 15:25:02 +08:00
m.Data["Main"] = append(m.Data["Main"], main.UsedPercent)
m.Data["Swap"] = append(m.Data["Swap"], swap.UsedPercent)
}