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, 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,6 +1,7 @@
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"
) )
@ -15,13 +16,15 @@ 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"),
} }
} }
@ -82,7 +85,11 @@ func (c *Docker) ReadCPU(stats *api.Stats) {
cpudiff := total - c.lastCpu cpudiff := total - c.lastCpu
syscpudiff := system - c.lastSysCpu 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.lastCpu = total
c.lastSysCpu = system c.lastSysCpu = system
c.Pids = int(stats.PidsStats.Current) c.Pids = int(stats.PidsStats.Current)

View File

@ -5,6 +5,7 @@ package collector
import ( import (
"time" "time"
"github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/models" "github.com/bcicen/ctop/models"
"github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
@ -21,6 +22,7 @@ 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 {
@ -29,6 +31,7 @@ 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
} }
@ -90,7 +93,11 @@ func (c *Runc) ReadCPU(stats *cgroups.Stats) {
cpudiff := total - c.lastCpu cpudiff := total - c.lastCpu
syscpudiff := system - c.lastSysCpu 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.lastCpu = total
c.lastSysCpu = system c.lastSysCpu = system
c.Pids = int(stats.PidsStats.Current) c.Pids = int(stats.PidsStats.Current)

View File

@ -41,6 +41,7 @@ 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()
@ -79,6 +80,10 @@ func main() {
config.Toggle("sortReversed") config.Toggle("sortReversed")
} }
if *scaleCpu {
config.Toggle("scaleCpu")
}
// init ui // init ui
if *invertFlag { if *invertFlag {
InvertColorMap() InvertColorMap()