From f377dcaee215072956d76732918f137dab20ef8a Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Fri, 20 Nov 2020 23:08:19 +0200 Subject: [PATCH] #207 Replace scaleCpu option with dedicated column CPU Scaled The new column is disabled by default. --- README.md | 1 - config/columns.go | 5 +++++ config/switch.go | 5 ----- connector/collector/docker.go | 18 ++++++------------ connector/collector/runc.go | 12 +++--------- cwidgets/compact/column.go | 1 + cwidgets/compact/gauge.go | 10 +++++++++- main.go | 5 ----- models/main.go | 1 + 9 files changed, 25 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index cb45a48..0c1315f 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,6 @@ Option | Description `-i` | invert default colors `-r` | reverse container sort order `-s` | select initial container sort field -`-scale-cpu` | show cpu as % of system total `-v` | output version information and exit ### Keybindings diff --git a/config/columns.go b/config/columns.go index b59a50e..06c4849 100644 --- a/config/columns.go +++ b/config/columns.go @@ -26,6 +26,11 @@ var defaultColumns = []Column{ Label: "CPU Usage", Enabled: true, }, + Column{ + Name: "cpus", + Label: "CPU Usage as % of system total", + Enabled: false, + }, Column{ Name: "mem", Label: "Memory Usage", diff --git a/config/switch.go b/config/switch.go index dca9bc4..bff8f0f 100644 --- a/config/switch.go +++ b/config/switch.go @@ -22,11 +22,6 @@ var defaultSwitches = []*Switch{ Val: true, Label: "Enable status header", }, - &Switch{ - Key: "scaleCpu", - Val: false, - Label: "Show CPU as %% of system total", - }, } type Switch struct { diff --git a/connector/collector/docker.go b/connector/collector/docker.go index 39a2b44..68a1274 100644 --- a/connector/collector/docker.go +++ b/connector/collector/docker.go @@ -1,7 +1,6 @@ package collector import ( - "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/models" api "github.com/fsouza/go-dockerclient" ) @@ -16,15 +15,13 @@ type Docker struct { done chan bool lastCpu float64 lastSysCpu float64 - scaleCpu bool } func NewDocker(client *api.Client, id string) *Docker { return &Docker{ - Metrics: models.Metrics{}, - id: id, - client: client, - scaleCpu: config.GetSwitchVal("scaleCpu"), + Metrics: models.Metrics{}, + id: id, + client: client, } } @@ -79,18 +76,15 @@ func (c *Docker) Stop() { } func (c *Docker) ReadCPU(stats *api.Stats) { - ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage)) + ncpus := uint8(len(stats.CPUStats.CPUUsage.PercpuUsage)) total := float64(stats.CPUStats.CPUUsage.TotalUsage) system := float64(stats.CPUStats.SystemCPUUsage) cpudiff := total - c.lastCpu syscpudiff := system - c.lastSysCpu - if c.scaleCpu { - c.CPUUtil = percent(cpudiff, syscpudiff) - } else { - c.CPUUtil = percent(ncpus*cpudiff, syscpudiff) - } + c.NCpus = ncpus + c.CPUUtil = percent(cpudiff, syscpudiff) c.lastCpu = total c.lastSysCpu = system c.Pids = int(stats.PidsStats.Current) diff --git a/connector/collector/runc.go b/connector/collector/runc.go index 7a7621d..9886423 100644 --- a/connector/collector/runc.go +++ b/connector/collector/runc.go @@ -9,7 +9,6 @@ import ( "github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/types" - "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/models" ) @@ -24,7 +23,6 @@ type Runc struct { interval int // collection interval, in seconds lastCpu float64 lastSysCpu float64 - scaleCpu bool } func NewRunc(libc libcontainer.Container) *Runc { @@ -33,7 +31,6 @@ func NewRunc(libc libcontainer.Container) *Runc { id: libc.ID(), libc: libc, interval: 1, - scaleCpu: config.GetSwitchVal("scaleCpu"), } return c } @@ -89,18 +86,15 @@ func (c *Runc) run() { func (c *Runc) ReadCPU(stats *cgroups.Stats) { u := stats.CpuStats.CpuUsage - ncpus := float64(len(u.PercpuUsage)) + ncpus := uint8(len(u.PercpuUsage)) total := float64(u.TotalUsage) system := float64(getSysCPUUsage()) cpudiff := total - c.lastCpu syscpudiff := system - c.lastSysCpu - if c.scaleCpu { - c.CPUUtil = percent(cpudiff, syscpudiff) - } else { - c.CPUUtil = percent(ncpus*cpudiff, syscpudiff) - } + c.NCpus = ncpus + c.CPUUtil = percent(cpudiff, syscpudiff) c.lastCpu = total c.lastSysCpu = system c.Pids = int(stats.PidsStats.Current) diff --git a/cwidgets/compact/column.go b/cwidgets/compact/column.go index c997a68..4ff0793 100644 --- a/cwidgets/compact/column.go +++ b/cwidgets/compact/column.go @@ -13,6 +13,7 @@ var ( "name": NewNameCol, "id": NewCIDCol, "cpu": NewCPUCol, + "cpus": NewCpuScaledCol, "mem": NewMemCol, "net": NewNetCol, "io": NewIOCol, diff --git a/cwidgets/compact/gauge.go b/cwidgets/compact/gauge.go index 2ef7f32..9dd09a8 100644 --- a/cwidgets/compact/gauge.go +++ b/cwidgets/compact/gauge.go @@ -11,14 +11,22 @@ import ( type CPUCol struct { *GaugeCol + scaleCpu bool } func NewCPUCol() CompactCol { - return &CPUCol{NewGaugeCol("CPU")} + return &CPUCol{NewGaugeCol("CPU"), false} +} + +func NewCpuScaledCol() CompactCol { + return &CPUCol{NewGaugeCol("CPUS"), true} } func (w *CPUCol) SetMetrics(m models.Metrics) { val := m.CPUUtil + if !w.scaleCpu { + val = val * int(m.NCpus) + } w.BarColor = colorScale(val) w.Label = fmt.Sprintf("%d%%", val) diff --git a/main.go b/main.go index 71b7dde..659074e 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,6 @@ func main() { sortFieldFlag = flag.String("s", "", "select container sort field") reverseSortFlag = flag.Bool("r", false, "reverse container sort order") invertFlag = flag.Bool("i", false, "invert default colors") - scaleCpu = flag.Bool("scale-cpu", false, "show cpu as % of system total") connectorFlag = flag.String("connector", "docker", "container connector to use") ) flag.Parse() @@ -86,10 +85,6 @@ func main() { config.Toggle("sortReversed") } - if *scaleCpu { - config.Toggle("scaleCpu") - } - // init ui if *invertFlag { InvertColorMap() diff --git a/models/main.go b/models/main.go index aa6e539..684ab39 100644 --- a/models/main.go +++ b/models/main.go @@ -31,6 +31,7 @@ func (m Meta) Get(k string) string { } type Metrics struct { + NCpus uint8 CPUUtil int NetTx int64 NetRx int64