diff --git a/container.go b/container.go index 0aed60c..b6d61a8 100644 --- a/container.go +++ b/container.go @@ -45,7 +45,7 @@ func (c *Container) Collect(client *docker.Client) { for s := range c.stats { c.reader.Read(s) c.widgets.SetCPU(c.reader.CPUUtil) - c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit) + c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit, c.reader.MemPercent) c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx) } }() diff --git a/reader.go b/reader.go index 796e2ac..7e39f1c 100644 --- a/reader.go +++ b/reader.go @@ -1,16 +1,18 @@ package main import ( + "math" + "github.com/fsouza/go-dockerclient" ) type StatReader struct { - CPUUtil float64 - NetTx int64 - NetRx int64 - MemUsage int64 - MemLimit int64 - //MemPercent int64 + CPUUtil int + NetTx int64 + NetRx int64 + MemLimit int64 + MemPercent int + MemUsage int64 lastCpu float64 lastSysCpu float64 } @@ -28,7 +30,7 @@ func (s *StatReader) ReadCPU(stats *docker.Stats) { cpudiff := total - s.lastCpu syscpudiff := system - s.lastSysCpu - s.CPUUtil = (cpudiff / syscpudiff * 100) * ncpus + s.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus) s.lastCpu = total s.lastSysCpu = system } @@ -36,7 +38,7 @@ func (s *StatReader) ReadCPU(stats *docker.Stats) { func (s *StatReader) ReadMem(stats *docker.Stats) { s.MemUsage = int64(stats.MemoryStats.Usage) s.MemLimit = int64(stats.MemoryStats.Limit) - //s.MemPercent = round((float64(cur) / float64(limit)) * 100) + s.MemPercent = round((float64(s.MemUsage) / float64(s.MemLimit)) * 100) } func (s *StatReader) ReadNet(stats *docker.Stats) { @@ -46,3 +48,7 @@ func (s *StatReader) ReadNet(stats *docker.Stats) { s.NetRx += int64(network.RxBytes) } } + +func round(num float64) int { + return int(num + math.Copysign(0.5, num)) +} diff --git a/sort.go b/sort.go index 4f3b956..6fb7988 100644 --- a/sort.go +++ b/sort.go @@ -4,7 +4,7 @@ import ( "sort" ) -var SortFields = []string{"id", "name", "cpu", "mem"} +var SortFields = []string{"id", "name", "cpu", "mem", "mem %"} // Sort array of containers by field func SortContainers(field string, containers []*Container) { @@ -17,6 +17,8 @@ func SortContainers(field string, containers []*Container) { sort.Sort(sort.Reverse(ByCPU(containers))) case "mem": sort.Sort(sort.Reverse(ByMem(containers))) + case "mem %": + sort.Sort(sort.Reverse(ByMemPercent(containers))) default: sort.Sort(ByID(containers)) } @@ -45,3 +47,9 @@ type ByMem []*Container func (a ByMem) Len() int { return len(a) } func (a ByMem) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByMem) Less(i, j int) bool { return a[i].reader.MemUsage < a[j].reader.MemUsage } + +type ByMemPercent []*Container + +func (a ByMemPercent) Len() int { return len(a) } +func (a ByMemPercent) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByMemPercent) Less(i, j int) bool { return a[i].reader.MemPercent < a[j].reader.MemPercent } diff --git a/widgets/compact.go b/widgets/compact.go index 59ae881..adf6f1c 100644 --- a/widgets/compact.go +++ b/widgets/compact.go @@ -35,23 +35,21 @@ func (w *Compact) Row() *ui.Row { ) } -func (w *Compact) SetCPU(val float64) { - intVal := round(val) - w.Cpu.BarColor = colorScale(intVal) - w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(intVal)) - if intVal < 5 { - intVal = 5 +func (w *Compact) SetCPU(val int) { + w.Cpu.BarColor = colorScale(val) + w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val)) + if val < 5 { + val = 5 w.Cpu.BarColor = ui.ColorBlack } - w.Cpu.Percent = intVal + w.Cpu.Percent = val } func (w *Compact) SetNet(rx int64, tx int64) { w.Net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx)) } -func (w *Compact) SetMem(val int64, limit int64) { - percent := round((float64(val) / float64(limit)) * 100) +func (w *Compact) SetMem(val int64, limit int64, percent int) { w.Memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit)) if percent < 5 { percent = 5 diff --git a/widgets/util.go b/widgets/util.go index e409a38..86a6793 100644 --- a/widgets/util.go +++ b/widgets/util.go @@ -2,7 +2,6 @@ package widgets import ( "fmt" - "math" "strconv" ui "github.com/gizak/termui" @@ -30,10 +29,6 @@ func byteFormat(n int64) string { return fmt.Sprintf("%sG", strconv.FormatInt(n, 10)) } -func round(num float64) int { - return int(num + math.Copysign(0.5, num)) -} - func compactPar(s string) *ui.Par { p := ui.NewPar(s) p.Border = false