This enhancement comes from investigating the issue from Brandon BerHent who back-port the -gc branch to 3.10 for android system and build customized kernel for nexus 6. It's very cool thing and I got to said "Hello Moto", which recall the memory of my first cell-phone.
The android system, unlike pc platform, seems use a lot cpu hotplug
mechanism for power-saving functionality. When I look at the cpu hotplug code, I notice the below behaviors.
p5qe ~ # schedtool -a 0x02 1388
p5qe ~ # schedtool 1388
PID 1388: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0x2
p5qe ~ # cat /sys/devices/system/cpu/cpu1/online
1
p5qe ~ # echo 0 > /sys/devices/system/cpu/cpu1/online
p5qe ~ # schedtool 1388
PID 1388: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0x1
p5qe ~ # cat /sys/devices/system/cpu/cpu1/online
0
p5qe ~ # echo 1 > /sys/devices/system/cpu/cpu1/online
p5qe ~ # schedtool 1388
PID 1388: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0x3
As you can see, after cpu 1 offline then online, task's affinity changes from 0x2 to 0x3, which include the new online cpu 1, but not the original design what the task to run on. And the most
interesting thing is, it is not only the behaviors of BFS, it's same for mainline CFS.
Normally, for pc platform, it's not a big problem, as there is not much cpu hotplug events unless suspend/resume. But if just a small enhancement that can maintenance task's original affinity intend, why not? Below is the behaviors with the enhancement.
p5qe ~ # schedtool 1375
PID 1375: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0xf
p5qe ~ # schedtool -a 0x2 1375
p5qe ~ # schedtool 1375
PID 1375: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0x2
p5qe ~ # echo 0 > /sys/devices/system/cpu/cpu1/online
p5qe ~ # schedtool 1375
PID 1375: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0x1
p5qe ~ # dmesg | tail
[ 9.771522] zram3: detected capacity change from 0 to 268435456
[ 9.783513] Adding 262140k swap on /dev/zram0. Priority:10 extents:1 across:262140k SSFS
[ 9.785789] Adding 262140k swap on /dev/zram1. Priority:10 extents:1 across:262140k SSFS
[ 9.788066] Adding 262140k swap on /dev/zram2. Priority:10 extents:1 across:262140k SSFS
[ 9.790311] Adding 262140k swap on /dev/zram3. Priority:10 extents:1 across:262140k SSFS
[ 12.103469] sky2 0000:02:00.0 eth1: Link is up at 1000 Mbps, full duplex, flow control both
[ 25.360122] random: nonblocking pool is initialized
[ 105.757001] Renew affinity for 198 processes to cpu 1
[ 105.757001] kvm: disabling virtualization on CPU1
[ 105.757140] smpboot: CPU 1 is now offline
p5qe ~ # echo 1 > /sys/devices/system/cpu/cpu1/online
p5qe ~ # schedtool 1375
PID 1375: PRIO 0, POLICY N: SCHED_NORMAL , NICE 0, AFFINITY 0x2
p5qe ~ # dmesg | tail
[ 9.790311] Adding 262140k swap on /dev/zram3. Priority:10 extents:1 across:262140k SSFS
[ 12.103469] sky2 0000:02:00.0 eth1: Link is up at 1000 Mbps, full duplex, flow control both
[ 25.360122] random: nonblocking pool is initialized
[ 105.757001] Renew affinity for 198 processes to cpu 1
[ 105.757001] kvm: disabling virtualization on CPU1
[ 105.757140] smpboot: CPU 1 is now offline
[ 137.348718] x86: Booting SMP configuration:
[ 137.348722] smpboot: Booting Node 0 Processor 1 APIC 0x1
[ 137.359727] kvm: enabling virtualization on CPU1
[ 137.363338] Renew affinity for 203 processes to cpu 1
This enhancement changes the default behaviors of the kernel/system, I have tested it for a while with different use cases, all looks good. So I mark this changes version 1, if you have any comments/concert, please let me know. I'll look into it.
Here is the
commit of this enhancement.
BR Alfred
Edit: Just push a
minor fix when CONFIG_HOTPLUG_CPU is not enabled.