Setting CPU affinity - Kernel support

Can someone tell me what the dependencies are, for a Linux installation to support setting the CPU affinity of a process via sched_setaffinity(…), or thread via pthread_setaffinity_np(…)?

In the manpages of sched_setaffinity, this page is referenced: cpuset. There it says, there should be a “nodev cpuset” in /proc/filesystems, if CPUSET is supported.

On both systems I tested, the iMX6 (kernel 4.1) and the TK1 (kernel 3.10), it’s not there. Then I found that the option CONFIG_CPUSET is disabled in the linux config (according to /proc/config.gz), of both systems.

So it should be not supported on both, right?
But on the iMX6, both setaffinity calls above succeed and I do see the effect in htop, the second core doig much more than without the call.
But on the TK1, the call returns unsuccessfully.

Normally my guess would be I’d need to build a new Linux image with that option enabled. But the relation between the presence indicator for CPUSET support, and it actually working seems to be different than I so far gathered from the manpages.
Hence, I’d be glad if someone could tell me definitely what’s really going on here, before I go to such efforts in vain :wink:

Hi

I believe from the manpage of sched_setaffinity that CONFIG_CPUSETS is not needed for that systemcall. i.e. the manpage says:

The system may further restrict the set of CPUs on which the process runs if the “cpuset” mechanism described in cpuset(7) is being used.

So with CONFIG_CPUSETS you get an additional way to restrict the CPUs on which a process is allowed to run.


At least the example from here works for me on an Apalis iMX6 Dual with kernel 4.1.44-2.7.4+gb1555bf and an Apalis TK1.

root@apalis-imx6:~# ./hello_affinity                                            
sched_getaffinity = 1 1                                                         
sched_getcpu = 0                                                                
sched_getaffinity = 0 1                                                         
sched_getcpu = 1                                                                

code and binary

Max

Thanks for that test program.
What Linux image did you use on the TK1?
I am using the L4T one (the smaller of the two, without the computervision stuff).
And am getting “Invalid argument” with perror("") after sched_setaffinity(…) returns a negative number, while this works fine on the iMX6 with LXDE image.

The test program you posted has the same:

sched_getaffinity = 1
sched_getcpu = 0
sched_setaffinity: Invalid argument
sched_getaffinity = 1
sched_getcpu = 0

I used the 2.8b5 mainline image.

If you use an image with the downstream kernel usually the cpu hotplug feature is used in a way that in idle all but CPU 0 is switched off to save power.

You have to make sure that at least one of the CPUs to which you want migrate to are switched on. The following would disable the automatic hotplug feature and enable additionally CPU 1-3. After that the hello_affinity works.

root@apalis-tk1:~# echo > 0 /sys/devices/system/cpu/cpuquiet/tegra_cpuquiet/enab
le                                                                              
root@apalis-tk1:~# echo 1 > /sys/devices/system/cpu/cpu1/online                 
root@apalis-tk1:~# echo 1 > /sys/devices/system/cpu/cpu2/online                 
root@apalis-tk1:~# echo 1 > /sys/devices/system/cpu/cpu3/online                 

root@apalis-tk1:~# ./hello_affinity 
sched_getaffinity = 1 1 1 1                                                     
sched_getcpu = 3                                                                
sched_getaffinity = 0 1 0 0                                                     
sched_getcpu = 1

Hehe! Good to know, that’s almost as good as “works better with power cord plugged in”. Thanks.

You are welcome.