summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2018-01-18 15:54:16 -0500
committerAtomic Bot <atomic-devel@projectatomic.io>2018-01-19 17:00:15 +0000
commit70306466d08c0e52a5a70c6d8eb8b021f95d22de (patch)
tree26497a9a1efbbb33b1f3e960abbbc49d95a0ded4
parenta4701b56311d5d934543e2b4306b08baa844ec3f (diff)
downloadpodman-70306466d08c0e52a5a70c6d8eb8b021f95d22de.tar.gz
podman-70306466d08c0e52a5a70c6d8eb8b021f95d22de.tar.bz2
podman-70306466d08c0e52a5a70c6d8eb8b021f95d22de.zip
Cleanup of podman stats
Fix errors when containers are not running. --all, --latest, containers can not be used at same time. Should match the output of docker stats, 0 values replaced by "--" Should return stats right away if container is not running. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Closes: #244 Approved by: TomSweeneyRedHat
-rw-r--r--cmd/podman/stats.go49
-rw-r--r--libpod/stats.go6
2 files changed, 39 insertions, 16 deletions
diff --git a/cmd/podman/stats.go b/cmd/podman/stats.go
index d2ffd8a75..871eb9e2f 100644
--- a/cmd/podman/stats.go
+++ b/cmd/podman/stats.go
@@ -22,7 +22,7 @@ type statsOutputParams struct {
MemPerc string `json:"mem_percent"`
NetIO string `json:"netio"`
BlockIO string `json:"blocki"`
- PIDS uint64 `json:"pids"`
+ PIDS string `json:"pids"`
}
var (
@@ -62,14 +62,22 @@ func statsCmd(c *cli.Context) error {
}
all := c.Bool("all")
-
- if c.Bool("latest") && all {
- return errors.Errorf("--all and --latest cannot be used together")
+ latest := c.Bool("latest")
+ ctr := 0
+ if all {
+ ctr += 1
+ }
+ if latest {
+ ctr += 1
+ }
+ if len(c.Args()) > 0 {
+ ctr += 1
}
- if c.Bool("latest") && len(c.Args()) > 0 {
- return errors.Errorf("no container names are allowed with --latest")
+ if ctr > 1 {
+ return errors.Errorf("--all, --latest and containers cannot be used together")
}
+
runtime, err := getRuntime(c)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
@@ -91,19 +99,19 @@ func statsCmd(c *cli.Context) error {
format = genStatsFormat()
}
+ containerFunc = runtime.GetRunningContainers
if len(c.Args()) > 0 {
containerFunc = func() ([]*libpod.Container, error) { return runtime.GetContainersByList(c.Args()) }
- } else if c.Bool("latest") {
+ } else if latest {
containerFunc = func() ([]*libpod.Container, error) {
- var ctrs []*libpod.Container
lastCtr, err := runtime.GetLatestContainer()
- ctrs = append(ctrs, lastCtr)
- return ctrs, err
+ if err != nil {
+ return nil, err
+ }
+ return []*libpod.Container{lastCtr}, nil
}
} else if all {
containerFunc = runtime.GetAllContainers
- } else {
- containerFunc = runtime.GetRunningContainers
}
ctrs, err = containerFunc()
@@ -215,18 +223,29 @@ func (i *statsOutputParams) headerMap() map[string]string {
}
func combineHumanValues(a, b uint64) string {
+ if a == 0 && b == 0 {
+ return "-- / --"
+ }
return fmt.Sprintf("%s / %s", units.HumanSize(float64(a)), units.HumanSize(float64(b)))
}
func floatToPercentString(f float64) string {
strippedFloat, err := libpod.RemoveScientificNotationFromFloat(f)
- if err != nil {
+ if err != nil || strippedFloat == 0 {
// If things go bazinga, return a safe value
- return "0.00 %"
+ return "--"
}
return fmt.Sprintf("%.2f", strippedFloat) + "%"
}
+func pidsToString(pid uint64) string {
+ if pid == 0 {
+ // If things go bazinga, return a safe value
+ return "--"
+ }
+ return fmt.Sprintf("%d", pid)
+}
+
func getStatsOutputParams(stats *libpod.ContainerStats) statsOutputParams {
return statsOutputParams{
Container: stats.ContainerID[:12],
@@ -236,6 +255,6 @@ func getStatsOutputParams(stats *libpod.ContainerStats) statsOutputParams {
MemPerc: floatToPercentString(stats.MemPerc),
NetIO: combineHumanValues(stats.NetInput, stats.NetOutput),
BlockIO: combineHumanValues(stats.BlockInput, stats.BlockOutput),
- PIDS: stats.PIDs,
+ PIDS: pidsToString(stats.PIDs),
}
}
diff --git a/libpod/stats.go b/libpod/stats.go
index 86da0679e..e87654277 100644
--- a/libpod/stats.go
+++ b/libpod/stats.go
@@ -29,11 +29,16 @@ type ContainerStats struct {
// GetContainerStats gets the running stats for a given container
func (c *Container) GetContainerStats(previousStats *ContainerStats) (*ContainerStats, error) {
stats := new(ContainerStats)
+ stats.ContainerID = c.ID()
c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {
return stats, errors.Wrapf(err, "error updating container %s state", c.ID())
}
+ if c.state.State != ContainerStateRunning {
+ return stats, nil
+ }
+
cgroup, err := cgroups.Load(cgroups.V1, c.CGroupPath())
if err != nil {
return stats, errors.Wrapf(err, "unable to load cgroup at %+v", c.CGroupPath())
@@ -50,7 +55,6 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container
previousCPU := previousStats.CPUNano
previousSystem := previousStats.SystemNano
- stats.ContainerID = c.ID()
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, previousSystem)
stats.MemUsage = cgroupStats.Memory.Usage.Usage
stats.MemLimit = getMemLimit(cgroupStats.Memory.Usage.Limit)