diff --git a/config/switch.go b/config/switch.go index 0a2f6f8..3eb343a 100644 --- a/config/switch.go +++ b/config/switch.go @@ -17,6 +17,11 @@ var switches = []*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 12006b4..b2377ee 100644 --- a/connector/collector/docker.go +++ b/connector/collector/docker.go @@ -1,6 +1,7 @@ package collector import ( + "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/models" api "github.com/fsouza/go-dockerclient" ) @@ -15,13 +16,15 @@ 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, + Metrics: models.Metrics{}, + id: id, + client: client, + scaleCpu: config.GetSwitchVal("scaleCpu"), } } @@ -82,7 +85,11 @@ func (c *Docker) ReadCPU(stats *api.Stats) { cpudiff := total - c.lastCpu syscpudiff := system - c.lastSysCpu - c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus) + if c.scaleCpu { + c.CPUUtil = round((cpudiff / syscpudiff * 100)) + } else { + c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus) + } 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 21b6af4..33de589 100644 --- a/connector/collector/runc.go +++ b/connector/collector/runc.go @@ -5,6 +5,7 @@ package collector import ( "time" + "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/models" "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/cgroups" @@ -21,6 +22,7 @@ type Runc struct { interval int // collection interval, in seconds lastCpu float64 lastSysCpu float64 + scaleCpu bool } func NewRunc(libc libcontainer.Container) *Runc { @@ -29,6 +31,7 @@ func NewRunc(libc libcontainer.Container) *Runc { id: libc.ID(), libc: libc, interval: 1, + scaleCpu: config.GetSwitchVal("scaleCpu"), } return c } @@ -90,7 +93,11 @@ func (c *Runc) ReadCPU(stats *cgroups.Stats) { cpudiff := total - c.lastCpu syscpudiff := system - c.lastSysCpu - c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus) + if c.scaleCpu { + c.CPUUtil = round((cpudiff / syscpudiff * 100)) + } else { + c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus) + } c.lastCpu = total c.lastSysCpu = system c.Pids = int(stats.PidsStats.Current) diff --git a/main.go b/main.go index 86a8543..63deda6 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ 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() @@ -79,6 +80,10 @@ func main() { config.Toggle("sortReversed") } + if *scaleCpu { + config.Toggle("scaleCpu") + } + // init ui if *invertFlag { InvertColorMap()