Is it possible to set affinity with sched_setaffinity in Android?

允我心安 提交于 2020-01-10 10:19:10

问题


Is it possible to set CPU affinity in native C code compiled with the Android NDK? Since the system is using a Linux kernel, 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.


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/7467848/is-it-possible-to-set-affinity-with-sched-setaffinity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!