I am developing a C++ code using android-ndk-15c and trying to run a thread on a specific core available on the processor that has 10 ARM cores (not all cores are the same; Big.little architecture). However, not all cores are active all the time. If I try to call sched_setaffinity with a cpu that is inactive, the call returns error message. Here is the sample code.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
void getCpus() {
cpu_set_t my_set;
int syscallres = sched_getaffinity(0, sizeof(cpu_set_t), &my_set);
if( syscallres ) {
int err = errno;
printf("Error in the syscall getaffinity: err=%d\n", err);
}
for(unsigned cpu = 0; cpu < 10; cpu++ ) {
if( CPU_ISSET(cpu, &my_set) ) {
printf( "cpu %d available!!\n", cpu );
}
}
}
void setCpu( int cpu ) {
cpu_set_t my_set;
CPU_ZERO(&my_set);
CPU_SET( cpu, &my_set);
int syscallres = sched_setaffinity(0, sizeof(cpu_set_t), &my_set);
if( syscallres ) {
int err = errno;
printf("Error in the syscall setaffinity: cpu=%d err=%d\n", cpu, err);
}
}
int main () {
getCpus();
setCpu(3);
}
Sample outputs:
cpu 0 available!!
cpu 1 available!!
Error in the syscall setaffinity: cpu=3 err=22
Another output when cpu 3 was active (not due to my code; android may activate some cores depending on load).
cpu 0 available!!
cpu 1 available!!
cpu 2 available!!
cpu 3 available!!
cpu 4 available!!
How to activate a specific core via ndk system calls?
I don't think individual cores can be activated, but it looks like...
android : PowerManager allows you to see if there is a sustained performance mode, which can be set on a Window by calling
setSustainedPerformanceMode
This should wake up the CPUs for your usage. Also WakeLocks look like they warn Android that you want to access more than "idle" resources.