#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 `-i` | invert default colors
`-r` | reverse container sort order `-r` | reverse container sort order
`-s` | select initial container sort field `-s` | select initial container sort field
`-scale-cpu` | show cpu as % of system total
`-v` | output version information and exit `-v` | output version information and exit
### Keybindings ### Keybindings

View File

@ -26,6 +26,11 @@ var defaultColumns = []Column{
Label: "CPU Usage", Label: "CPU Usage",
Enabled: true, Enabled: true,
}, },
Column{
Name: "cpus",
Label: "CPU Usage as % of system total",
Enabled: false,
},
Column{ Column{
Name: "mem", Name: "mem",
Label: "Memory Usage", Label: "Memory Usage",

View File

@ -22,11 +22,6 @@ var defaultSwitches = []*Switch{
Val: true, Val: true,
Label: "Enable status header", Label: "Enable status header",
}, },
&Switch{
Key: "scaleCpu",
Val: false,
Label: "Show CPU as %% of system total",
},
} }
type Switch struct { type Switch struct {

View File

@ -1,7 +1,6 @@
package collector package collector
import ( import (
"github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/models" "github.com/bcicen/ctop/models"
api "github.com/fsouza/go-dockerclient" api "github.com/fsouza/go-dockerclient"
) )
@ -16,15 +15,13 @@ type Docker struct {
done chan bool done chan bool
lastCpu float64 lastCpu float64
lastSysCpu float64 lastSysCpu float64
scaleCpu bool
} }
func NewDocker(client *api.Client, id string) *Docker { func NewDocker(client *api.Client, id string) *Docker {
return &Docker{ return &Docker{
Metrics: models.Metrics{}, Metrics: models.Metrics{},
id: id, id: id,
client: client, client: client,
scaleCpu: config.GetSwitchVal("scaleCpu"),
} }
} }
@ -79,18 +76,15 @@ func (c *Docker) Stop() {
} }
func (c *Docker) ReadCPU(stats *api.Stats) { 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) total := float64(stats.CPUStats.CPUUsage.TotalUsage)
system := float64(stats.CPUStats.SystemCPUUsage) system := float64(stats.CPUStats.SystemCPUUsage)
cpudiff := total - c.lastCpu cpudiff := total - c.lastCpu
syscpudiff := system - c.lastSysCpu syscpudiff := system - c.lastSysCpu
if c.scaleCpu { c.NCpus = ncpus
c.CPUUtil = percent(cpudiff, syscpudiff) c.CPUUtil = percent(cpudiff, syscpudiff)
} else {
c.CPUUtil = percent(ncpus*cpudiff, syscpudiff)
}
c.lastCpu = total c.lastCpu = total
c.lastSysCpu = system c.lastSysCpu = system
c.Pids = int(stats.PidsStats.Current) c.Pids = int(stats.PidsStats.Current)

View File

@ -9,7 +9,6 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/types" "github.com/opencontainers/runc/types"
"github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/models" "github.com/bcicen/ctop/models"
) )
@ -24,7 +23,6 @@ type Runc struct {
interval int // collection interval, in seconds interval int // collection interval, in seconds
lastCpu float64 lastCpu float64
lastSysCpu float64 lastSysCpu float64
scaleCpu bool
} }
func NewRunc(libc libcontainer.Container) *Runc { func NewRunc(libc libcontainer.Container) *Runc {
@ -33,7 +31,6 @@ func NewRunc(libc libcontainer.Container) *Runc {
id: libc.ID(), id: libc.ID(),
libc: libc, libc: libc,
interval: 1, interval: 1,
scaleCpu: config.GetSwitchVal("scaleCpu"),
} }
return c return c
} }
@ -89,18 +86,15 @@ func (c *Runc) run() {
func (c *Runc) ReadCPU(stats *cgroups.Stats) { func (c *Runc) ReadCPU(stats *cgroups.Stats) {
u := stats.CpuStats.CpuUsage u := stats.CpuStats.CpuUsage
ncpus := float64(len(u.PercpuUsage)) ncpus := uint8(len(u.PercpuUsage))
total := float64(u.TotalUsage) total := float64(u.TotalUsage)
system := float64(getSysCPUUsage()) system := float64(getSysCPUUsage())
cpudiff := total - c.lastCpu cpudiff := total - c.lastCpu
syscpudiff := system - c.lastSysCpu syscpudiff := system - c.lastSysCpu
if c.scaleCpu { c.NCpus = ncpus
c.CPUUtil = percent(cpudiff, syscpudiff) c.CPUUtil = percent(cpudiff, syscpudiff)
} else {
c.CPUUtil = percent(ncpus*cpudiff, syscpudiff)
}
c.lastCpu = total c.lastCpu = total
c.lastSysCpu = system c.lastSysCpu = system
c.Pids = int(stats.PidsStats.Current) c.Pids = int(stats.PidsStats.Current)

View File

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

View File

@ -11,14 +11,22 @@ import (
type CPUCol struct { type CPUCol struct {
*GaugeCol *GaugeCol
scaleCpu bool
} }
func NewCPUCol() CompactCol { 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) { func (w *CPUCol) SetMetrics(m models.Metrics) {
val := m.CPUUtil val := m.CPUUtil
if !w.scaleCpu {
val = val * int(m.NCpus)
}
w.BarColor = colorScale(val) w.BarColor = colorScale(val)
w.Label = fmt.Sprintf("%d%%", val) w.Label = fmt.Sprintf("%d%%", val)

View File

@ -44,7 +44,6 @@ func main() {
sortFieldFlag = flag.String("s", "", "select container sort field") sortFieldFlag = flag.String("s", "", "select container sort field")
reverseSortFlag = flag.Bool("r", false, "reverse container sort order") reverseSortFlag = flag.Bool("r", false, "reverse container sort order")
invertFlag = flag.Bool("i", false, "invert default colors") 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") connectorFlag = flag.String("connector", "docker", "container connector to use")
) )
flag.Parse() flag.Parse()
@ -86,10 +85,6 @@ func main() {
config.Toggle("sortReversed") config.Toggle("sortReversed")
} }
if *scaleCpu {
config.Toggle("scaleCpu")
}
// init ui // init ui
if *invertFlag { if *invertFlag {
InvertColorMap() InvertColorMap()

View File

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