A Puzzle

如果先调用sched_setaffinity将线程绑定到CPU1上,再将CPU1逻辑关闭(offline),会发生什么?在Linux系统中,要回答这个问题,先要搞清楚Linux下的3个机制:

  • CPU hotplug
  • cpuset
  • scheduling affinity

CPU hotplug

CPU hotplug的意思是, Linux允许Logically ShutdownBring Up CPU, 用户可以通过sysfs接口操作

shutdown
1
echo 0 > /sys/devices/system/cpu/cpu1/online
bring up
1
echo 1 > /sys/devices/system/cpu/cpu1/online

What if the cpu is shutdown

/proc/interrupts, /proc/cpuinfo, top都将看不到它

What happened if the cpu is shutdown

我只列出跟上面的疑问有关系的:

  • All processes are migrated away from this outgoing CPU to new CPUs. The new CPU is chosen from each process’ current cpuset, which may be a subset of all online CPUs.

当关闭CPU1后,原来运行在CPU1上的进程将被移到新的CPU们上调度,具体哪个CPU是从这个进程当前的cpuset中选择,所以疑问转换成了“sched_setaffinity对进程当前的cpuset做了什么?”

cpuset