• Linux:把进程/线程绑定到特定cpu核上运行


    查看cpu有几个核

    使用cat /proc/cpuinfo查看cpu信息,如下两个信息:

    • processor,指明第几个cpu处理器
    • cpu cores,指明每个处理器的核心数

    也可以使用系统调用sysconf获取cpu核心数:

    #include

    int sysconf(_SC_NPROCESSORS_CONF);/* 返回系统可以使用的核数,但是其值会包括系统中禁用的核的数目,因 此该值并不代表当前系统中可用的核数 */
    int sysconf(_SC_NPROCESSORS_ONLN);/* 返回值真正的代表了系统当前可用的核数 */

    /* 以下两个函数与上述类似 */
    #include

    int get_nprocs_conf (void);/* 可用核数 */
    int get_nprocs (void);/* 真正的反映了当前可用核数 */

    使用taskset指令

    • 获取进程pid

    -> % ps
      PID TTY          TIME CMD
     2683 pts/1    00:00:00 zsh
     2726 pts/1    00:00:00 dgram_servr
     2930 pts/1    00:00:00 ps

    • 查看进程当前运行在哪个cpu上

    -> % taskset -p 2726
    pid 2726's current affinity mask: 3

    显示的十进制数字3转换为2进制为最低两个是1,每个1对应一个cpu,所以进程运行在2个cpu上。

    • 指定进程运行在cpu1上

    -> % taskset -pc 1 2726
    pid 2726's current affinity list: 0,1
    pid 2726's new affinity list: 1

    注意,cpu的标号是从0开始的,所以cpu1表示第二个cpu(第一个cpu的标号是0)。

    至此,就把应用程序绑定到了cpu1上运行,查看如下:

    -> % taskset -p 2726
    pid 2726's current affinity mask: 2

    • 启动程序时绑定cpu

    #启动时绑定到第二个cpu
    -> % taskset -c 1 ./dgram_servr&
    [1] 3011

    #查看确认绑定情况
    -> % taskset -p 3011
    pid 3011's current affinity mask: 2

    使用sched_setaffinity系统调用

    sched_setaffinity可以将某个进程绑定到一个特定的CPU。

    #define _GNU_SOURCE             /* See feature_test_macros(7) */
    #include

    /* 设置进程号为pid的进程运行在mask所设定的CPU上
     * 第二个参数cpusetsize是mask所指定的数的长度
     * 通常设定为sizeof(cpu_set_t)

     * 如果pid的值为0,则表示指定的是当前进程 
     */
    int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);

    int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);/* 获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中 */

    • 实例
    1. #include<stdlib.h>
    2. #include<stdio.h>
    3. #include<sys/types.h>
    4. #include<sys/sysinfo.h>
    5. #include<unistd.h>
    6. #define __USE_GNU
    7. #include<sched.h>
    8. #include<ctype.h>
    9. #include<string.h>
    10. #include<pthread.h>
    11. #define THREAD_MAX_NUM 200 //1个CPU内的最多进程数
    12. int num=0; //cpu中核数
    13. void* threadFun(void* arg) //arg 传递线程标号(自己定义)
    14. {
    15. cpu_set_t mask; //CPU核的集合
    16. cpu_set_t get; //获取在集合中的CPU
    17. int *a = (int *)arg;
    18. int i;
    19. printf("the thread is:%d\n",*a); //显示是第几个线程
    20. CPU_ZERO(&mask); //置空
    21. CPU_SET(*a,&mask); //设置亲和力值
    22. if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力
    23. {
    24. printf("warning: could not set CPU affinity, continuing...\n");
    25. }
    26. CPU_ZERO(&get);
    27. if (sched_getaffinity(0, sizeof(get), &get) == -1)//获取线程CPU亲和力
    28. {
    29. printf("warning: cound not get thread affinity, continuing...\n");
    30. }
    31. for (i = 0; i < num; i++)
    32. {
    33. if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力
    34. {
    35. printf("this thread %d is running processor : %d\n", i,i);
    36. }
    37. }
    38. return NULL;
    39. }
    40. int main(int argc, char* argv[])
    41. {
    42. int tid[THREAD_MAX_NUM];
    43. int i;
    44. pthread_t thread[THREAD_MAX_NUM];
    45. num = sysconf(_SC_NPROCESSORS_CONF); //获取核数
    46. if (num > THREAD_MAX_NUM) {
    47. printf("num of cores[%d] is bigger than THREAD_MAX_NUM[%d]!\n", num, THREAD_MAX_NUM);
    48. return -1;
    49. }
    50. printf("system has %i processor(s). \n", num);
    51. for(i=0;i<num;i++)
    52. {
    53. tid[i] = i; //每个线程必须有个tid[i]
    54. pthread_create(&thread[i],NULL,threadFun,(void*)&tid[i]);
    55. }
    56. for(i=0; i< num; i++)
    57. {
    58. pthread_join(thread[i],NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束
    59. }
    60. return 0;
    61. }
    • 运行结果

    -> % ./a.out
    system has 2 processor(s). 
    the thread is:0
    the thread is:1
    this thread 0 is running processor : 0
    this thread 1 is running processor : 1

    绑定线程到cpu核上运行

    • 绑定线程到cpu核上使用pthread_setaffinity_np函数,其原型定义如下:

    #define _GNU_SOURCE             /* See feature_test_macros(7) */
    #include

    int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
    int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);

    Compile and link with -pthread.

    • 各参数的意义与sched_setaffinity相似。

    • 实例

    1. #define _GNU_SOURCE
    2. #include <pthread.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include <errno.h>
    6. #define handle_error_en(en, msg) \
    7. do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
    8. int
    9. main(int argc, char *argv[])
    10. {
    11. int s, j;
    12. cpu_set_t cpuset;
    13. pthread_t thread;
    14. thread = pthread_self();
    15. /* Set affinity mask to include CPUs 0 to 7 */
    16. CPU_ZERO(&cpuset);
    17. for (j = 0; j < 8; j++)
    18. CPU_SET(j, &cpuset);
    19. s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    20. if (s != 0)
    21. handle_error_en(s, "pthread_setaffinity_np");
    22. /* Check the actual affinity mask assigned to the thread */
    23. s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    24. if (s != 0)
    25. handle_error_en(s, "pthread_getaffinity_np");
    26. printf("Set returned by pthread_getaffinity_np() contained:\n");
    27. for (j = 0; j < CPU_SETSIZE; j++)
    28. if (CPU_ISSET(j, &cpuset))
    29. printf(" CPU %d\n", j);
    30. exit(EXIT_SUCCESS);
    31. }
    • 运行结果

    -> % ./a.out 
    Set returned by pthread_getaffinity_np() contained:
        CPU 0
        CPU 1

    扩展

    taskset

  • 相关阅读:
    2019史上最全java面试题题库大全800题含答案(面试宝典)
    ROS机器人RIKIBOT硬件介绍
    Academic accumulation|社会创业研究:过去的成就和未来的承诺
    Qt ffmpeg音视频转换工具
    【电源专题】案例:直接用LDO或Buck不香?为什么非要用Buck降压再转LDO?
    奶茶行业竞争加剧,奈雪全自动化制作,亲身示范如何“内卷”
    并发编程基础底层原理学习(一)
    UE5笔记【八】导入FBX网格和和材质到UE5
    探索编译软件栈新范式;高端GPU禁售的影响;陈天奇DL系统免费课程|AI系统前沿动态
    k8s1.24 部署springboot项目
  • 原文地址:https://blog.csdn.net/hhd1988/article/details/133742482