summaryrefslogtreecommitdiff
path: root/cmd/podman/top.go
diff options
context:
space:
mode:
authorValentin Rothberg <vrothberg@suse.com>2018-07-19 14:41:58 +0200
committerAtomic Bot <atomic-devel@projectatomic.io>2018-07-19 20:47:52 +0000
commitba1871dac033783ab0329c9b3c9113a34a90992f (patch)
treef0be2944ff09d857306ea42864c7b24ae86ae9bd /cmd/podman/top.go
parent98703eb204923f06555605c648fc165a55214520 (diff)
downloadpodman-ba1871dac033783ab0329c9b3c9113a34a90992f.tar.gz
podman-ba1871dac033783ab0329c9b3c9113a34a90992f.tar.bz2
podman-ba1871dac033783ab0329c9b3c9113a34a90992f.zip
podman-top: use containers/psgo
Use github.com/containers/psgo instead of execing `ps (1)`. The psgo library enables a much more flexible interface with respect to which data to be printed (e.g., capabilities, seccomp mode, PID, PCPU, etc.) while the output can be parsed reliably. The library does not use ps (1) but parses /proc and /dev instead. To list the processes of a given container, psgo will join the mount namespace of the given container and extract all data from there. Notice that this commit breaks compatibility with docker-top. Signed-off-by: Valentin Rothberg <vrothberg@suse.com> Closes: #1113 Approved by: rhatdan
Diffstat (limited to 'cmd/podman/top.go')
-rw-r--r--cmd/podman/top.go31
1 files changed, 24 insertions, 7 deletions
diff --git a/cmd/podman/top.go b/cmd/podman/top.go
index d848723e2..749d97505 100644
--- a/cmd/podman/top.go
+++ b/cmd/podman/top.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
+ "strings"
"text/tabwriter"
"github.com/pkg/errors"
@@ -14,11 +15,15 @@ import (
var (
topFlags = []cli.Flag{
LatestFlag,
+ cli.BoolFlag{
+ Name: "list-descriptors",
+ Hidden: true,
+ },
}
- topDescription = `
- podman top
-
- Display the running processes of the container.
+ topDescription = `Display the running processes of the container. Specify format descriptors
+to alter the output. You may run "podman top -l pid pcpu seccomp" to print
+the process ID, the CPU percentage and the seccomp mode of each process of
+the latest container.
`
topCommand = cli.Command{
@@ -27,7 +32,7 @@ var (
Description: topDescription,
Flags: topFlags,
Action: topCmd,
- ArgsUsage: "CONTAINER-NAME",
+ ArgsUsage: "CONTAINER-NAME [format descriptors]",
SkipArgReorder: true,
}
)
@@ -37,6 +42,15 @@ func topCmd(c *cli.Context) error {
var err error
args := c.Args()
+ if c.Bool("list-descriptors") {
+ descriptors, err := libpod.GetContainerPidInformationDescriptors()
+ if err != nil {
+ return err
+ }
+ fmt.Println(strings.Join(descriptors, "\n"))
+ return nil
+ }
+
if len(args) < 1 && !c.Bool("latest") {
return errors.Errorf("you must provide the name or id of a running container")
}
@@ -50,9 +64,12 @@ func topCmd(c *cli.Context) error {
}
defer runtime.Shutdown(false)
+ var descriptors []string
if c.Bool("latest") {
+ descriptors = args
container, err = runtime.GetLatestContainer()
} else {
+ descriptors = args[1:]
container, err = runtime.LookupContainer(args[0])
}
@@ -67,12 +84,12 @@ func topCmd(c *cli.Context) error {
return errors.Errorf("top can only be used on running containers")
}
- psOutput, err := container.GetContainerPidInformation([]string{})
+ psOutput, err := container.GetContainerPidInformation(descriptors)
if err != nil {
return err
}
- w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
+ w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
for _, proc := range psOutput {
fmt.Fprintln(w, proc)
}