Linux教程

Linux下将进程/线程绑定到指定CPU核运行

本文主要是介绍Linux下将进程/线程绑定到指定CPU核运行,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

如何查看Linux核数

$ 总核数 = 物理CPU个数 X 每颗物理CPU的核数
$ 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数

$ 查看物理CPU个数
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

$ 查看每个物理CPU中core的个数(即核数)
cat /proc/cpuinfo| grep "cpu cores"| uniq

$ 查看逻辑CPU的个数
cat /proc/cpuinfo| grep "processor"| wc -l

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);/* 真正的反映了当前可用核数 */
 

什么是绑核

        所谓绑核,其实就是设定某个进程/线程与某个CPU核的亲和力(affinity)。设定以后,Linux调度器就会让这个进程/线程只在所绑定的核上面去运行。但并不是说该进程/线程就独占这个CPU的核,其他的进程/线程还是可以在这个核上面运行的。

掩码形式绑核

        将掩码转换为二进制形式,从最低位到最高位代表物理CPU的#0、#1、……、#n号核。某位的值为0表示不绑该核,1表示绑。比如:0x00000001的二进制为0000...0001,只有第0号核的位置是1,所以表示只绑0号核;0x00000003的二进制为0000...0011,第0和1号核的位置是1,所以表示绑CPU的0号和1号核;再比如0xFFFFFFFF的二进制为1111...1111,所有32个核的位置都为1,所以表示绑CPU的0~31核。

掩码形式的绑核命令为:

taskset -p mask pid

列表形式

        列表形式指直接指定要绑的CPU核的列表,列表中可以有一个或多个核。具体语法如下:

taskset -cp cpu-list pid

其中cpu-list是数字化的cpu列表,从0开始。多个不连续的cpu可用逗号连接,连续的可用短现连接,比如0,1,3-6等。

比如taskset -cp 0,1,3-6 2677 命令表示将进程2677 绑定到#0#1#3~#6号核上面。

多进程和多线程在cpu核上运行分析:

        每个 CPU 核运行一个进程的时候,由于每个进程的资源都独立,所以 CPU 核心之间切换的时候无需考虑上下文。
        每个 CPU 核运行一个线程的时候,有时线程之间需要共享资源,所以这些资源必须从 CPU 的一个核心被复制到另外一个核心,这会造成额外的开销。

举例如下:

1, 使用taskset指令绑定进程到特定CPU步骤

(1)获取进程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

(2) 查看进程当前运行在哪个cpu上

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

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

(3)指定进程运行在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上运行

(4)确认绑定命令

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

2,启动程序时绑定cpu

(1)启动时绑定到第二个cpu
$ taskset -c 1 ./dgram_servr&
[1] 3011

(2)查看确认绑定情况
$ taskset -p 3011
pid 3011's current affinity mask: 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核上运行。

在具体使用中,要根据使用场景和需求决定使用何种方式。
 

这篇关于Linux下将进程/线程绑定到指定CPU核运行的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!