目录
前言
物理CPU:主板上安装的CPU个数。
逻辑CPU:一般情况,认为一颗CPU可以有多个核,加上intel的超线程技术(HT), 可以在逻辑上再分一倍数量的CPU core出来。
一、使用cat /proc/cpuinfo查看cpu信息:
C:\Users\henry.xxx>adb shell
trinket:/ # cat /proc/cpuinfo
Processor : AArch64 Processor rev 4 (aarch64)
processor : 0
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x801
CPU revision : 4
processor : 1
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x801
CPU revision : 4
processor : 2
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x801
CPU revision : 4
processor : 3
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x801
CPU revision : 4
processor : 4
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x800
CPU revision : 2
processor : 5
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x800
CPU revision : 2
processor : 6
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x800
CPU revision : 2
processor : 7
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xa
CPU part : 0x800
CPU revision : 2
Hardware : Qualcomm Technologies, Inc TRINKET-IOT
trinket:/ #
二、查看物理CPU个数
cat /proc/cpuinfo| grep “physical id”| sort| uniq| wc -l
说明只有1个 0000 0001 1x2^0=0
三、查看逻辑CPU的个数
cat /proc/cpuinfo| grep “processor”| wc -l
8个
四、 查看进程当前运行在哪个cpu上
taskset -p pid
2即为 0000 0010 1x2^1
test_data_c运行在第二个cpu上
五、指定进程运行在cpu1上
taskset -p cpu-list pid
cpu-list可以是0,1这样的一个核,也可以是1-2这样的多个核,表示绑定在1和2上面
pid 表示进程号
由此可以看见成功将进程从cpu1转到cpu0上运行。
六、代码设置绑定进程到指定cpu
1.添加头文件
2.添加函数定义
注意:linux下可以直接调用pthread_setaffinity_np,将当前线程绑定在具体的cpu上,而android该API被屏蔽了,需要调用sched这个系统API,详情见下面代码:
#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(cpu_set_t mask) {
int err, syscallres;
pid_t pid = getpid();
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("pid = %d has setted affinity success",pid);
printf("pid = %d has setted affinity success\n",pid);
}
void get_cur_thread_affinity(cpu_set_t mask) {
int err, syscallres;
pid_t pid = getpid();
syscallres = syscall(__NR_sched_getaffinity, pid, sizeof(mask), &mask);
if (syscallres) {
err = errno;
LOGE("Error in the syscall getaffinity: mask = %d, err=%d",mask,errno);
}
LOGD("pid = %d has getted affinity success",pid);
printf("pid = %d has getted affinity success\n",pid);
}
3.在主函数下添加
进行绑定,与cpu2进行绑定。
测试结果为4 100 ,即cpu2。
参考链接:
线程/进程和核绑定(CPU亲和性)