【linux】把进程/线程绑定到特定cpu核上运行

目录

一、目的

二、命令绑定

1、查看绑定情况

2、启动时绑定

3、启动后绑定

三、代码绑定

绑定进程到cpu核上运行

绑定线程到cpu核上运行

四、判断进程在哪个CPU核上运行


一、目的

某个进程需要较高的运行效率时,就有必要考虑将其绑定到单独的核上运行,以减小由于在不同的核上调度造成的开销。

把某个进程/线程绑定到特定的cpu核上后,该进程就会一直在此核上运行,不会再被操作系统调度到其他核上。但绑定的这个核还是可能会被调度运行其他应用程序的。

二、命令绑定

1、查看绑定情况

查看进程pid现在的绑核情况

taskset -p pid

pid 2726's current affinity mask: 6

显示的十进制数字6--->转换为2进制是110,每个1对应一个cpu,所以进程运行在#1,#2cpu上(cpu从0开始)

2、启动时绑定

#启动时绑定到第二个cpu

taskset -c 1 ./dgram_servr&

#启动时绑定到第1个cpu,第3个cpu

taskset -c 0,2 ./dgram_servr&

(cpu从0开始)

3、启动后绑定

2.1 按CPU数直接绑核

taskset -cp 1,2,5,11 9865  将进程9864绑定到#1、#2、#5、#11号核上面。

taskset -cp 1,2,5-11 9865  将进程9864绑定到#1、#2、#5~#11号核上面。

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


taskset -cp cpu-list pid
其中cpu-list是数字化的cpu列表,从1开始。多个不连续的cpu可用逗号连接,连续的可用短现连接,比如1,2,5-11等。

2.2 掩码形式绑核

taskset -p mask pid


按照二进制形式,从最低位到最高位代表物理CPU的#1、#2、……、#n号核。
比如:0x00000001代表CPU的0号核,0x00000003代表CPU的0号和3号核。
需要注意的是,并非掩码中给出的CPU核就一定会存在,比如0x00000400理论上代表CPU的第10号核,但是该核在真正的计算机上面并不一定是存在的。而且,如果我们试图将物理上并不存的核绑定给某个进程时,会返回错误。掩码形式的绑核命令为:
taskset -p mask pid
 

三、代码绑定

(原文:https://blog.csdn.net/guotianqing/article/details/80958281

绑定进程到cpu核上运行

查看cpu有几个核

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

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

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

#include <unistd.h>

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

/* 以下两个函数与上述类似 */
#include <sys/sysinfo.h>

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

我使用的是虚拟机,有2个处理器,每个处理器只有一个核,等同于一个处理器两个核心。

使用sched_setaffinity系统调用

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

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sched.h>

/* 设置进程号为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所指向的结构中 */
  • 实例
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>

#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
#include<pthread.h>
#define THREAD_MAX_NUM 200  //1个CPU内的最多进程数

int num=0;  //cpu中核数
void* threadFun(void* arg)  //arg  传递线程标号(自己定义)
{
         cpu_set_t mask;  //CPU核的集合
         cpu_set_t get;   //获取在集合中的CPU
         int *a = (int *)arg; 
         int i;

         printf("the thread is:%d\n",*a);  //显示是第几个线程
         CPU_ZERO(&mask);    //置空
         CPU_SET(*a,&mask);   //设置亲和力值
         if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力
         {
                   printf("warning: could not set CPU affinity, continuing...\n");
         }

           CPU_ZERO(&get);
           if (sched_getaffinity(0, sizeof(get), &get) == -1)//获取线程CPU亲和力
           {
                    printf("warning: cound not get thread affinity, continuing...\n");
           }
           for (i = 0; i < num; i++)
           {
                    if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力
                    {
                             printf("this thread %d is running processor : %d\n", i,i);
                    }
           }

         return NULL;
}

int main(int argc, char* argv[])
{
         int tid[THREAD_MAX_NUM];
         int i;
         pthread_t thread[THREAD_MAX_NUM];

         num = sysconf(_SC_NPROCESSORS_CONF);  //获取核数
         if (num > THREAD_MAX_NUM) {
            printf("num of cores[%d] is bigger than THREAD_MAX_NUM[%d]!\n", num, THREAD_MAX_NUM);
            return -1;
         }
         printf("system has %i processor(s). \n", num);

         for(i=0;i<num;i++)
         {
                   tid[i] = i;  //每个线程必须有个tid[i]
                   pthread_create(&thread[i],NULL,threadFun,(void*)&tid[i]);
         }
         for(i=0; i< num; i++)
         {
                   pthread_join(thread[i],NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束
         }
         return 0;
}
  • 运行结果
-> % ./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 <pthread.h>

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相似。

  • 实例

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define handle_error_en(en, msg) \
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

int
main(int argc, char *argv[])
{
    int s, j;
    cpu_set_t cpuset;
    pthread_t thread;

    thread = pthread_self();

    /* Set affinity mask to include CPUs 0 to 7 */

    CPU_ZERO(&cpuset);
    for (j = 0; j < 8; j++)
        CPU_SET(j, &cpuset);

    s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
        handle_error_en(s, "pthread_setaffinity_np");

    /* Check the actual affinity mask assigned to the thread */

    s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
        handle_error_en(s, "pthread_getaffinity_np");

    printf("Set returned by pthread_getaffinity_np() contained:\n");
    for (j = 0; j < CPU_SETSIZE; j++)
        if (CPU_ISSET(j, &cpuset))
            printf("    CPU %d\n", j);

    exit(EXIT_SUCCESS);
}
  • 运行结果
-> % ./a.out 
Set returned by pthread_getaffinity_np() contained:
    CPU 0
    CPU 1

四、判断进程在哪个CPU核上运行

taskset -c -p <pid>

taskset -c -p 5357

pid 5357's current affinity list:5,输出显示这个进程被固定在5号CPU上

但如果你没有明确固定该进程到任何CPU上,会得到类似下面的亲和力列表:

pid 5357's current affinity list:0-11,表明该进程可能会被安排在从0到11中的任何一个CPU上

ps -o pid,psr,comm -p <pid>

ps 命令可以告诉你每个进程/线程目前分配到的(在“PSR”列)CPU ID。

ps -o pid,psr,comm -p <pid>

运行结果:

PID PSR COMM

5357  10  prog

输出表示进程的 PID 为 5357(名为”prog”)目前在编号为 10的CPU 上运行着。如果该过程没有被固定,PSR 列会根据内核可能调度该进程到不同CPU而改变显示。

更多方法见:http://t.csdn.cn/dxe8M

【参考资料】
多核技术导论之操作系统对多核处理器的支持方法
线程绑定CPU核-sched_setaffinity
PTHREAD_SETAFFINITY_NP(3) Linux Programmer’s ManualPTHREAD_SETAFFINITY_NP(3)

 

  • 9
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值