Set_cpu_affinity not pinning the thread to a core

Hi team,

A customer is trying a multithreaded approach where they want to set different threads to different cores using the set_cpu_affinity function. They state that in x86 the function works correctly but when they execute it in our module, the core is not correctly choosen. I’ve attached an example of the source code used.

I’ve did the test myself and confirmed with htop that indeed, every thread is correctly associated to different cores (CPU column on the left).

$ gcc main_edited.c -o test -lpthread
$ gcc --version
gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0

[upload|oM3xqz4eSmcGjnCrq45hp/lfTrg=]

Then did the same proccedure cross-compiling for the T30:

$ /home/alvaro/gcc-linaro/bin/arm-linux-gnueabihf-gcc main_edited.c -o testarm -lpthread
$ /home/alvaro/gcc-linaro/bin/arm-linux-gnueabihf-gcc --version
arm-linux-gnueabihf-gcc (Linaro GCC 5.2-2015.11-2) 5.2.1 20151005

But at running the application, I got the following error and got all the threads running in the same core (Also, the 0% in the other 3 cores give it away as well).
[upload|IaOHNPj30xGu2/QuHN1LLoWW+Ak=]

pthread_setaffinity_np: Invalid argument
a_cpu = 1
pthread_setaffinity_np: Invalid argument
a_cpu = 2

I’ve tried with the newest version of the toolchain (7.3.1 I believe) with the same outcome.

Do you know if there is an issue with this approach? Do you know if there is an alternative solution?

Thanks and best regards,
Alvaro.

Hi Alvaro

As part of the power management, with low computing requirements unused CPUs are automatically set offline. I guess that the CPUs to which you want migrate a task to must be online but they aren’t, resulting in the error message.

Can you try to first disable the automatic hotpluging and then enable all CPUs and then start your test application?

echo 0 > /sys/module/cpu_tegra3/parameters/auto_hotplug
echo 1 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu2/online
echo 1 > /sys/devices/system/cpu/cpu3/online

Max

Thanks Max! The customer confirmed this solved the issue.