#207 Replace scaleCpu option with dedicated column CPU Scaled

The new column is disabled by default.
This commit is contained in:
Sergey Ponomarev 2020-11-20 23:08:19 +02:00
parent 2792e72d18
commit f377dcaee2
9 changed files with 25 additions and 33 deletions

View File

@ -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

View File

@ -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",

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -13,6 +13,7 @@ var (
"name": NewNameCol,
"id": NewCIDCol,
"cpu": NewCPUCol,
"cpus": NewCpuScaledCol,
"mem": NewMemCol,
"net": NewNetCol,
"io": NewIOCol,

View File

@ -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)

View File

@ -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()

View File

@ -31,6 +31,7 @@ func (m Meta) Get(k string) string {
}
type Metrics struct {
NCpus uint8
CPUUtil int
NetTx int64
NetRx int64