tryResetCPUAffinity tries to reset the CPU affinity of the process identified by pid to include all possible CPUs (notwithstanding cgroup cpuset restrictions, isolated CPUs and CPU online status).
(pid int)
| 183 | // identified by pid to include all possible CPUs (notwithstanding cgroup |
| 184 | // cpuset restrictions, isolated CPUs and CPU online status). |
| 185 | func tryResetCPUAffinity(pid int) { |
| 186 | // When resetting the CPU affinity, we want to allow all |
| 187 | // possible CPUs in the system, including those not in |
| 188 | // cpuset.cpus, online or even present (hot-plugged) at call |
| 189 | // time. Using a cpumask any tighter this that may disallow |
| 190 | // using those CPUs if they are added to cpuset.cpus later. |
| 191 | // |
| 192 | // Note that sched_setaffinity(2) will implicitly: |
| 193 | // |
| 194 | // * Clamp the cpumask so that it matches the number of CPUs |
| 195 | // supported by the kernel. |
| 196 | // |
| 197 | // * Mask out any CPUs that are not a member of the target task's |
| 198 | // configured cgroup cpuset. This is for task's effective affinity, |
| 199 | // without forgetting masked-out CPUs should the cgroup cpuset |
| 200 | // change later. |
| 201 | // |
| 202 | // Therefore, preparing the cpumask, we can avoid reading |
| 203 | // /sys/devices/system/cpu/possible and kernel_max. |
| 204 | // Instead, we use a huge buffer similarly to go 1.25 runtime in |
| 205 | // getCPUCount(). |
| 206 | const maxCPUs = 64 * 1024 |
| 207 | buf := bytes.Repeat([]byte{0xff}, maxCPUs/8) |
| 208 | if err := linux.SchedSetaffinity(pid, buf); err != nil { |
| 209 | logrus.WithError(err).Warnf("resetting the CPU affinity of pid %d failed -- the container process may inherit runc's CPU affinity", pid) |
| 210 | return |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Starts setns process with specified initial CPU affinity. |
| 215 | func (p *setnsProcess) startWithCPUAffinity() error { |
no test coverage detected
searching dependent graphs…