请问andoid 是否支持isolcpus将arm的某个核隔离?
我想实现在arm的某个指定核上跑一个单独的线程,该核为该线程独占。
另外,在实现过程中,需要用cpu亲和性去绑定特定线程,发现android NDK不支持thread_setaffinity_np接口,请问哪位有解决办法?
dongshengcui@vizum-VirtualBox:~/qwz/bind$ aarch64-linux-android-g++ bindThread.c -o bindThread -lpthreadbindThread.c: In function 'void* myfun(void*)':bindThread.c:23:71: error: 'pthread_setaffinity_np' was not declared in this scope if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) { ^bindThread.c:27:69: error: 'pthread_getaffinity_np' was not declared in this scope if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) { ^dongshengcui@vizum-VirtualBox:~/qwz/bind$
我找到了一种方式,可以替代android np接口不支持的问题。 代码如下:
//#define _GNU_SOURCE#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <pthread.h>#include <sched.h>#include <sys/syscall.h>#include <unistd.h>
int set_cur_thread_affinity(cpu_set_t mask){ int err, syscallres; pid_t pid = gettid();
syscallres = syscall(__NR_sched_setaffinity,pid, sizeof(mask), &mask); if(syscallres){ err = errno; printf("Error in the syscall setaffinity: mask = %d, err =%d\n", mask, errno); return -1; } printf("tid = %d has setted affinity sucess\n", pid); return 0;}
void *myfun(void *arg){ cpu_set_t mask; cpu_set_t get; char buf[256]; int i; int j; int num = sysconf(_SC_NPROCESSORS_CONF); printf("system has %d processor(s)\n", num); CPU_ZERO(&mask); printf("*((int *)arg) is %d \n", *((int *)arg));
CPU_SET(*((int *)arg), &mask); if (set_cur_thread_affinity(mask) < 0) { fprintf(stderr, "set thread affinity failed\n"); }
while(1) { ; } pthread_exit(NULL);} int main(int argc, char *argv[]){
pthread_t tid; int temp = atol(argv[1]); printf("argv[1] is %d\n", temp);
if (pthread_create(&tid, NULL, myfun, &temp) != 0) { fprintf(stderr, "thread create failed\n"); return -1; } pthread_join(tid, NULL); return 0;}