summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2017-12-11 09:34:58 -0600
committerAtomic Bot <atomic-devel@projectatomic.io>2017-12-14 22:22:56 +0000
commit3ba2c3e11791b9da11661ea45d966d2db621a6ac (patch)
treeba83d175492e64b2949b27113cc4bdb98cc8f98a /libpod
parentbbe6b21cb802737f933305f7c2013a86349ba43c (diff)
downloadpodman-3ba2c3e11791b9da11661ea45d966d2db621a6ac.tar.gz
podman-3ba2c3e11791b9da11661ea45d966d2db621a6ac.tar.bz2
podman-3ba2c3e11791b9da11661ea45d966d2db621a6ac.zip
kpod top
Display information about processes in a running container. Signed-off-by: baude <bbaude@redhat.com> Closes: #121 Approved by: rhatdan
Diffstat (limited to 'libpod')
-rw-r--r--libpod/container_top.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/libpod/container_top.go b/libpod/container_top.go
new file mode 100644
index 000000000..7d6dad2b4
--- /dev/null
+++ b/libpod/container_top.go
@@ -0,0 +1,57 @@
+package libpod
+
+import (
+ "fmt"
+ "io/ioutil"
+ "path/filepath"
+ "strings"
+
+ "github.com/pkg/errors"
+ "github.com/projectatomic/libpod/utils"
+ "github.com/sirupsen/logrus"
+)
+
+// GetContainerPids reads sysfs to obtain the pids associated with the container's cgroup
+// and uses locking
+func (c *Container) GetContainerPids() ([]string, error) {
+ c.lock.Lock()
+ defer c.lock.Unlock()
+ if err := c.syncContainer(); err != nil {
+ return []string{}, errors.Wrapf(err, "error updating container %s state", c.ID())
+ }
+ return c.getContainerPids()
+}
+
+// Gets the pids for a container without locking. should only be called from a func where
+// locking has already been established.
+func (c *Container) getContainerPids() ([]string, error) {
+ taskFile := filepath.Join("/sys/fs/cgroup/pids", CGroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()), c.ID(), "tasks")
+ logrus.Debug("reading pids from ", taskFile)
+ content, err := ioutil.ReadFile(taskFile)
+ if err != nil {
+ return []string{}, errors.Wrapf(err, "unable to read pids from %s", taskFile)
+ }
+ return strings.Fields(string(content)), nil
+
+}
+
+// GetContainerPidInformation calls ps with the appropriate options and returns
+// the results as a string
+func (c *Container) GetContainerPidInformation(args []string) ([]string, error) {
+ c.lock.Lock()
+ defer c.lock.Unlock()
+ if err := c.syncContainer(); err != nil {
+ return []string{}, errors.Wrapf(err, "error updating container %s state", c.ID())
+ }
+ pids, err := c.getContainerPids()
+ if err != nil {
+ return []string{}, errors.Wrapf(err, "unable to obtain pids for ", c.ID())
+ }
+ args = append(args, "-p", strings.Join(pids, ","))
+ logrus.Debug("Executing: ", strings.Join(args, " "))
+ results, err := utils.ExecCmd("ps", args...)
+ if err != nil {
+ return []string{}, errors.Wrapf(err, "unable to obtain information about pids")
+ }
+ return strings.Split(results, "\n"), nil
+}