2016-07-18 14:30:15 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2016-09-22 08:54:41 -04:00
|
|
|
units "src/github.com/docker/go-units"
|
2016-07-18 14:30:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-09-22 08:54:41 -04:00
|
|
|
winOSType = "windows"
|
|
|
|
defaultStatsTableFormat = "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}"
|
|
|
|
winDefaultStatsTableFormat = "table {{.Container}}\t{{.CPUPerc}}\t{{{.MemUsage}}\t{.NetIO}}\t{{.BlockIO}}"
|
2016-07-18 14:30:15 -04:00
|
|
|
emptyStatsTableFormat = "Waiting for statistics..."
|
|
|
|
|
|
|
|
containerHeader = "CONTAINER"
|
2016-09-22 08:54:41 -04:00
|
|
|
cpuPercHeader = "CPU %"
|
2016-07-18 14:30:15 -04:00
|
|
|
netIOHeader = "NET I/O"
|
|
|
|
blockIOHeader = "BLOCK I/O"
|
2016-09-22 08:54:41 -04:00
|
|
|
winMemPercHeader = "PRIV WORKING SET" // Used only on Window
|
|
|
|
memPercHeader = "MEM %" // Used only on Linux
|
2016-07-18 14:30:15 -04:00
|
|
|
memUseHeader = "MEM USAGE / LIMIT" // Used only on Linux
|
|
|
|
pidsHeader = "PIDS" // Used only on Linux
|
|
|
|
)
|
|
|
|
|
2016-09-22 08:54:41 -04:00
|
|
|
// StatsEntry represents represents the statistics data collected from a container
|
|
|
|
type StatsEntry struct {
|
2016-07-18 14:30:15 -04:00
|
|
|
Name string
|
|
|
|
CPUPercentage float64
|
|
|
|
Memory float64 // On Windows this is the private working set
|
|
|
|
MemoryLimit float64 // Not used on Windows
|
|
|
|
MemoryPercentage float64 // Not used on Windows
|
|
|
|
NetworkRx float64
|
|
|
|
NetworkTx float64
|
|
|
|
BlockRead float64
|
|
|
|
BlockWrite float64
|
|
|
|
PidsCurrent uint64 // Not used on Windows
|
2016-09-22 08:54:41 -04:00
|
|
|
IsInvalid bool
|
|
|
|
OSType string
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
|
2016-09-22 08:54:41 -04:00
|
|
|
// ContainerStats represents an entity to store containers statistics synchronously
|
2016-07-18 14:30:15 -04:00
|
|
|
type ContainerStats struct {
|
2016-09-22 08:54:41 -04:00
|
|
|
mutex sync.Mutex
|
|
|
|
StatsEntry
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetError returns the container statistics error.
|
|
|
|
// This is used to determine whether the statistics are valid or not
|
|
|
|
func (cs *ContainerStats) GetError() error {
|
|
|
|
cs.mutex.Lock()
|
|
|
|
defer cs.mutex.Unlock()
|
|
|
|
return cs.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetErrorAndReset zeroes all the container statistics and store the error.
|
|
|
|
// It is used when receiving time out error during statistics collecting to reduce lock overhead
|
|
|
|
func (cs *ContainerStats) SetErrorAndReset(err error) {
|
|
|
|
cs.mutex.Lock()
|
|
|
|
defer cs.mutex.Unlock()
|
|
|
|
cs.CPUPercentage = 0
|
|
|
|
cs.Memory = 0
|
|
|
|
cs.MemoryPercentage = 0
|
|
|
|
cs.MemoryLimit = 0
|
|
|
|
cs.NetworkRx = 0
|
|
|
|
cs.NetworkTx = 0
|
|
|
|
cs.BlockRead = 0
|
|
|
|
cs.BlockWrite = 0
|
|
|
|
cs.PidsCurrent = 0
|
|
|
|
cs.err = err
|
|
|
|
cs.IsInvalid = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetError sets container statistics error
|
|
|
|
func (cs *ContainerStats) SetError(err error) {
|
|
|
|
cs.mutex.Lock()
|
|
|
|
defer cs.mutex.Unlock()
|
|
|
|
cs.err = err
|
|
|
|
if err != nil {
|
|
|
|
cs.IsInvalid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStatistics set the container statistics
|
|
|
|
func (cs *ContainerStats) SetStatistics(s StatsEntry) {
|
|
|
|
cs.mutex.Lock()
|
|
|
|
defer cs.mutex.Unlock()
|
|
|
|
s.Name = cs.Name
|
|
|
|
s.OSType = cs.OSType
|
|
|
|
cs.StatsEntry = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStatistics returns container statistics with other meta data such as the container name
|
|
|
|
func (cs *ContainerStats) GetStatistics() StatsEntry {
|
|
|
|
cs.mutex.Lock()
|
|
|
|
defer cs.mutex.Unlock()
|
|
|
|
return cs.StatsEntry
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatsFormat returns a format for rendering an CStatsContext
|
|
|
|
func NewStatsFormat(source, osType string) Format {
|
|
|
|
if source == TableFormatKey {
|
2016-09-22 08:54:41 -04:00
|
|
|
if osType == winOSType {
|
2016-07-18 14:30:15 -04:00
|
|
|
return Format(winDefaultStatsTableFormat)
|
|
|
|
}
|
|
|
|
return Format(defaultStatsTableFormat)
|
|
|
|
}
|
|
|
|
return Format(source)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContainerStats returns a new ContainerStats entity and sets in it the given name
|
|
|
|
func NewContainerStats(name, osType string) *ContainerStats {
|
|
|
|
return &ContainerStats{
|
2016-09-22 08:54:41 -04:00
|
|
|
StatsEntry: StatsEntry{Name: name, OSType: osType},
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatsWrite renders the context for a list of containers statistics
|
2016-09-22 08:54:41 -04:00
|
|
|
func ContainerStatsWrite(ctx Context, containerStats []StatsEntry) error {
|
2016-07-18 14:30:15 -04:00
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
for _, cstats := range containerStats {
|
|
|
|
containerStatsCtx := &containerStatsContext{
|
2016-09-22 08:54:41 -04:00
|
|
|
s: cstats,
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
if err := format(containerStatsCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(&containerStatsContext{}, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerStatsContext struct {
|
|
|
|
HeaderContext
|
2016-09-22 08:54:41 -04:00
|
|
|
s StatsEntry
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerStatsContext) Container() string {
|
|
|
|
c.AddHeader(containerHeader)
|
|
|
|
return c.s.Name
|
|
|
|
}
|
|
|
|
|
2016-09-22 08:54:41 -04:00
|
|
|
func (c *containerStatsContext) CPUPerc() string {
|
|
|
|
c.AddHeader(cpuPercHeader)
|
|
|
|
if c.s.IsInvalid {
|
|
|
|
return fmt.Sprintf("--")
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
return fmt.Sprintf("%.2f%%", c.s.CPUPercentage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerStatsContext) MemUsage() string {
|
|
|
|
c.AddHeader(memUseHeader)
|
2016-09-22 08:54:41 -04:00
|
|
|
if c.s.IsInvalid || c.s.OSType == winOSType {
|
|
|
|
return fmt.Sprintf("-- / --")
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
2016-09-22 08:54:41 -04:00
|
|
|
return fmt.Sprintf("%s / %s", units.BytesSize(c.s.Memory), units.BytesSize(c.s.MemoryLimit))
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
|
2016-09-22 08:54:41 -04:00
|
|
|
func (c *containerStatsContext) MemPerc() string {
|
|
|
|
header := memPercHeader
|
|
|
|
if c.s.OSType == winOSType {
|
|
|
|
header = winMemPercHeader
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
c.AddHeader(header)
|
2016-09-22 08:54:41 -04:00
|
|
|
if c.s.IsInvalid {
|
|
|
|
return fmt.Sprintf("--")
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
return fmt.Sprintf("%.2f%%", c.s.MemoryPercentage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerStatsContext) NetIO() string {
|
|
|
|
c.AddHeader(netIOHeader)
|
2016-09-22 08:54:41 -04:00
|
|
|
if c.s.IsInvalid {
|
|
|
|
return fmt.Sprintf("--")
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
return fmt.Sprintf("%s / %s", units.HumanSizeWithPrecision(c.s.NetworkRx, 3), units.HumanSizeWithPrecision(c.s.NetworkTx, 3))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerStatsContext) BlockIO() string {
|
|
|
|
c.AddHeader(blockIOHeader)
|
2016-09-22 08:54:41 -04:00
|
|
|
if c.s.IsInvalid {
|
|
|
|
return fmt.Sprintf("--")
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
return fmt.Sprintf("%s / %s", units.HumanSizeWithPrecision(c.s.BlockRead, 3), units.HumanSizeWithPrecision(c.s.BlockWrite, 3))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerStatsContext) PIDs() string {
|
|
|
|
c.AddHeader(pidsHeader)
|
2016-09-22 08:54:41 -04:00
|
|
|
if c.s.IsInvalid || c.s.OSType == winOSType {
|
|
|
|
return fmt.Sprintf("--")
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
2016-09-22 08:54:41 -04:00
|
|
|
return fmt.Sprintf("%d", c.s.PidsCurrent)
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|