MCPcopy Create free account
hub / github.com/containers/common / GetSystemCPUUsage

Function GetSystemCPUUsage

pkg/cgroups/cpu.go:125–160  ·  view source on GitHub ↗

GetSystemCPUUsage returns the system usage for all the cgroups

()

Source from the content-addressed store, hash-verified

123
124// GetSystemCPUUsage returns the system usage for all the cgroups
125func GetSystemCPUUsage() (uint64, error) {
126 cgroupv2, err := IsCgroup2UnifiedMode()
127 if err != nil {
128 return 0, err
129 }
130 if !cgroupv2 {
131 p := filepath.Join(cgroupRoot, CPUAcct, "cpuacct.usage")
132 return readFileAsUint64(p)
133 }
134
135 files, err := ioutil.ReadDir(cgroupRoot)
136 if err != nil {
137 return 0, err
138 }
139 var total uint64
140 for _, file := range files {
141 if !file.IsDir() {
142 continue
143 }
144 p := filepath.Join(cgroupRoot, file.Name(), "cpu.stat")
145
146 values, err := readCgroup2MapPath(p)
147 if err != nil {
148 return 0, err
149 }
150
151 if val, found := values["usage_usec"]; found {
152 v, err := strconv.ParseUint(cleanString(val[0]), 10, 64)
153 if err != nil {
154 return 0, err
155 }
156 total += v * 1000
157 }
158 }
159 return total, nil
160}

Callers

nothing calls this directly

Calls 4

readFileAsUint64Function · 0.85
readCgroup2MapPathFunction · 0.85
cleanStringFunction · 0.85
IsCgroup2UnifiedModeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…