In cgroups v2 there isn't a dedicated "cpu" directory under /sys/fs/cgroup, so the approximation of the number of CPUs may use the cpu.max file. The format is the following: $MAX $PERIOD Which indicates that the group may consume up to $MAX in each $PERIOD duration. The "max" value could be either the string "max" or a number. In the first case our approximation doesn't work so we can bail out e
| 84 | our approximation doesn't work so we can bail out earlier. |
| 85 | */ |
| 86 | static inline double CgroupV2Cpus(const TString& cpuMaxPath) { |
| 87 | try { |
| 88 | TVector<TString> cgroupCpuMax = StringSplitter(TFileInput(cpuMaxPath).ReadAll()).Split(' ').Take(2); |
| 89 | double max = FromString<int32_t>(StripString(cgroupCpuMax[0])); |
| 90 | double period = FromString<int32_t>(StripString(cgroupCpuMax[1])); |
| 91 | |
| 92 | if (max <= 0 || period <= 0) { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | return max / period; |
| 97 | } catch (...) { |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static inline double CgroupCpus() { |
| 103 | static const TString cpuMaxPath("/sys/fs/cgroup/cpu.max"); |
no test coverage detected