开发者

Is it possible to set affinity with sched_setaffinity in Android?

开发者 https://www.devze.com 2023-04-06 00:04 出处:网络
Is it possible to set CPU affinity in native C code compiled with the Android NDK? Since the system is using a Linux kern开发者_如何学运维el, it should be possible to use the sched_setaffinity/sched_g

Is it possible to set CPU affinity in native C code compiled with the Android NDK? Since the system is using a Linux kern开发者_如何学运维el, it should be possible to use the sched_setaffinity/sched_getaffinity functions, but when I compile with the NDK, I get errors complaining that the cpu_set_t type is unknown (which is used as an argument to the functions). Is there any other way to accomplish this? When I compile with CodeSourcerys ARM compiler (arm-none-linux-gnueabi-gcc) this does not seem to be a problem, so the system obviously supports the required syscalls.


The following code works well with NDK r5 or newer:

#include <sys/syscall.h>
#include <pthread.h>
void setCurrentThreadAffinityMask(int mask)
{
    int err, syscallres;
    pid_t pid = gettid();
    syscallres = syscall(__NR_sched_setaffinity, pid, sizeof(mask), &mask);
    if (syscallres)
    {
        err = errno;
        LOGE("Error in the syscall setaffinity: mask=%d=0x%x err=%d=0x%x", mask, mask, err, err);
    }
}


For cpu_set_t, I was able to compile with -D_GNU_SOURCE=1 option for Android NDK.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号