diff options
Diffstat (limited to 'pkg/cgroups/cpu.go')
-rw-r--r-- | pkg/cgroups/cpu.go | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/pkg/cgroups/cpu.go b/pkg/cgroups/cpu.go index d27f1257f..3f969fd3c 100644 --- a/pkg/cgroups/cpu.go +++ b/pkg/cgroups/cpu.go @@ -55,13 +55,13 @@ func (c *cpuHandler) Apply(ctr *CgroupControl, res *spec.LinuxResources) error { if res.CPU == nil { return nil } - return fmt.Errorf("function not implemented yet") + return fmt.Errorf("cpu apply not implemented yet") } // Create the cgroup func (c *cpuHandler) Create(ctr *CgroupControl) (bool, error) { if ctr.cgroup2 { - return false, fmt.Errorf("function not implemented yet") + return false, fmt.Errorf("cpu create not implemented for cgroup v2") } return ctr.createCgroupDirectory(CPU) } @@ -73,24 +73,39 @@ func (c *cpuHandler) Destroy(ctr *CgroupControl) error { // Stat fills a metrics structure with usage stats for the controller func (c *cpuHandler) Stat(ctr *CgroupControl, m *Metrics) error { - if ctr.cgroup2 { - return fmt.Errorf("function not implemented yet") - } - var err error usage := CPUUsage{} - - usage.Total, err = readAcct(ctr, "cpuacct.usage") - if err != nil { - return err - } - usage.Kernel, err = readAcct(ctr, "cpuacct.usage_sys") - if err != nil { - return err - } - usage.PerCPU, err = readAcctList(ctr, "cpuacct.usage_percpu") - if err != nil { - return err + if ctr.cgroup2 { + values, err := readCgroup2MapFile(ctr, "cpu.stat") + if err != nil { + return err + } + if val, found := values["usage_usec"]; found { + usage.Kernel, err = strconv.ParseUint(cleanString(val[0]), 10, 0) + if err != nil { + return err + } + } + if val, found := values["system_usec"]; found { + usage.Total, err = strconv.ParseUint(cleanString(val[0]), 10, 0) + if err != nil { + return err + } + } + // FIXME: How to read usage.PerCPU? + } else { + usage.Total, err = readAcct(ctr, "cpuacct.usage") + if err != nil { + return err + } + usage.Kernel, err = readAcct(ctr, "cpuacct.usage_sys") + if err != nil { + return err + } + usage.PerCPU, err = readAcctList(ctr, "cpuacct.usage_percpu") + if err != nil { + return err + } } m.CPU = CPUMetrics{Usage: usage} return nil |