绑定进程到特定cpu核(实战)


前言

物理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亲和性)

  • 23
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值