add scale-cpu switch

This commit is contained in:
Bradley Cicenas 2018-01-11 15:19:00 +00:00
parent 6d37a4e333
commit d785b263f4
4 changed files with 29 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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