android将线程绑定在指定CPU
2016-12-26 12:51
761 查看
linux下可以直接调用pthread_setaffinity_np,将当前线程绑定在具体的cpu上,而android该API被屏蔽了,需要调用sched这个系统API,详情见下面代码:
package nativelibs;
public class Affinity {
static {
System.loadLibrary("Affinity");
}
public static native void bindToCpu(int cpu);
}
ok,具体的JNI代码如下:
#include <jni.h>
#include <android/log.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/syscall.h>
#define TAG "Affinity"
#define DEBUG 1
#ifndef CPU_ZERO
#define CPU_SETSIZE 1024
#define __NCPUBITS (8 * sizeof (unsigned long))
typedef struct
{
unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;
#define CPU_SET(cpu, cpusetp) \
((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) \
memset((cpusetp), 0, sizeof(cpu_set_t))
#else
#define CPU_SET(cpu,cpustep) ((void)0)
#define CPU_ZERO(cpu,cpustep) ((void)0)
#endif
#ifdef DEBUG
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
#else
#define LOGD(...) ((void)0)
#define LOGE(...) ((void)0)
#endif
void set_cur_thread_affinity(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, err=%d",mask,errno); } LOGD("tid = %d has setted affinity success",pid); }
static int getCores() {
return sysconf(_SC_NPROCESSORS_CONF);
}
JNIEXPORT int JNICALL Java_nativelibs_Affinity_getCores(JNIEnv *env, jclass type) {
return getCores();
}
JNIEXPORT void JNICALL Java_nativelibs_Affinity_bindToCpu(JNIEnv *env, jclass type, jint cpu) {
int cores = getCores();
LOGD("get cpu number = %d\n",cores);
if (cpu >= cores) {
LOGE("your set cpu is beyond the cores,exit...");
return;
}
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu,&mask);
set_cur_thread_affinity((int)(&mask));
LOGD("set affinity to %d success",cpu);
}
package nativelibs;
public class Affinity {
static {
System.loadLibrary("Affinity");
}
public static native void bindToCpu(int cpu);
}
void set_cur_thread_affinity(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, err=%d",mask,errno); } LOGD("tid = %d has setted affinity success",pid); }
ok,具体的JNI代码如下:
#include <jni.h>
#include <android/log.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/syscall.h>
#define TAG "Affinity"
#define DEBUG 1
#ifndef CPU_ZERO
#define CPU_SETSIZE 1024
#define __NCPUBITS (8 * sizeof (unsigned long))
typedef struct
{
unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;
#define CPU_SET(cpu, cpusetp) \
((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) \
memset((cpusetp), 0, sizeof(cpu_set_t))
#else
#define CPU_SET(cpu,cpustep) ((void)0)
#define CPU_ZERO(cpu,cpustep) ((void)0)
#endif
#ifdef DEBUG
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
#else
#define LOGD(...) ((void)0)
#define LOGE(...) ((void)0)
#endif
void set_cur_thread_affinity(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, err=%d",mask,errno); } LOGD("tid = %d has setted affinity success",pid); }
static int getCores() {
return sysconf(_SC_NPROCESSORS_CONF);
}
JNIEXPORT int JNICALL Java_nativelibs_Affinity_getCores(JNIEnv *env, jclass type) {
return getCores();
}
JNIEXPORT void JNICALL Java_nativelibs_Affinity_bindToCpu(JNIEnv *env, jclass type, jint cpu) {
int cores = getCores();
LOGD("get cpu number = %d\n",cores);
if (cpu >= cores) {
LOGE("your set cpu is beyond the cores,exit...");
return;
}
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu,&mask);
set_cur_thread_affinity((int)(&mask));
LOGD("set affinity to %d success",cpu);
}
相关文章推荐
- Linux_多核系统下绑定进程或线程到指定CPU_核执行
- windows下绑定线程(进程)到指定的CPU核心 http://blog.csdn.net/beyond_cn/article/details/15813361
- 设置cpu亲和性---即 绑定特定的进程线程到指定的cpu
- Linux_多核系统下绑定进程或线程到指定CPU_核执行
- windows下绑定线程(进程)到指定的CPU核心
- Linux_多核系统下绑定进程或线程到指定CPU_核执行和调优
- Visual C++学习总结——进程或线程与指定CPU绑定
- LINUX将制定的线程号、进程号绑定到指定CPU核上
- 设置cpu亲和性---即 绑定特定的进程线程到指定的cpu
- windows下绑定线程(进程)到指定的CPU核心
- C#线程绑定到指定cpu
- linux线程绑定到指定cpu运行
- 多线程编程中,将线程绑定到指定CPU
- windows下绑定线程(进程)到指定的CPU核心
- windows下绑定线程(进程)到指定CPU
- windows下绑定线程(进程)到指定的CPU核心
- 进程线程与cpu绑定
- 控制线程在指定CPU上运行
- 负载均衡-指定进程/线程运行的CPU-(affinity setup)
- posix多线程有感--线程高级编程(均衡负载CPU绑定)