aboutsummaryrefslogtreecommitdiff
path: root/pkg/cgroups/cpu.go
diff options
context:
space:
mode:
authorGiuseppe Scrivano <gscrivan@redhat.com>2020-02-12 11:58:29 +0100
committerGiuseppe Scrivano <gscrivan@redhat.com>2020-02-12 11:58:29 +0100
commit510fa4ebc8271f929753275428ae2b6047ecc8c7 (patch)
tree1e1770ea9ec95ce4a3a984b4d9dab44732a5b12f /pkg/cgroups/cpu.go
parent6215e1bb218a86c217a66e34f2abd043feca8582 (diff)
downloadpodman-510fa4ebc8271f929753275428ae2b6047ecc8c7.tar.gz
podman-510fa4ebc8271f929753275428ae2b6047ecc8c7.tar.bz2
podman-510fa4ebc8271f929753275428ae2b6047ecc8c7.zip
stats: add SystemUsage
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Diffstat (limited to 'pkg/cgroups/cpu.go')
-rw-r--r--pkg/cgroups/cpu.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/pkg/cgroups/cpu.go b/pkg/cgroups/cpu.go
index a43a76b22..5f0a18031 100644
--- a/pkg/cgroups/cpu.go
+++ b/pkg/cgroups/cpu.go
@@ -121,3 +121,42 @@ func (c *cpuHandler) Stat(ctr *CgroupControl, m *Metrics) error {
m.CPU = CPUMetrics{Usage: usage}
return nil
}
+
+// GetSystemCPUUsage returns the system usage for all the cgroups
+func GetSystemCPUUsage() (uint64, error) {
+ cgroupv2, err := IsCgroup2UnifiedMode()
+ if err != nil {
+ return 0, err
+ }
+ if !cgroupv2 {
+ p := filepath.Join(cgroupRoot, CPUAcct, "cpuacct.usage")
+ return readFileAsUint64(p)
+ }
+
+ files, err := ioutil.ReadDir(cgroupRoot)
+ if err != nil {
+ return 0, errors.Wrapf(err, "read directory %q", cgroupRoot)
+ }
+ var total uint64
+ for _, file := range files {
+ if !file.IsDir() {
+ continue
+ }
+ p := filepath.Join(cgroupRoot, file.Name(), "cpu.stat")
+
+ values, err := readCgroup2MapPath(p)
+ if err != nil {
+ return 0, err
+ }
+
+ if val, found := values["usage_usec"]; found {
+ v, err := strconv.ParseUint(cleanString(val[0]), 10, 0)
+ if err != nil {
+ return 0, err
+ }
+ total += v * 1000
+ }
+
+ }
+ return total, nil
+}