GetSystemCPUUsage returns the system usage for all the cgroups
()
| 123 | |
| 124 | // GetSystemCPUUsage returns the system usage for all the cgroups |
| 125 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…