用户进程和内核线程的CPU亲和性设置

一、概述

CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行。一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码可能会获得更好的性能.

进程描述结构体相关成员

struct task_struct {
    ...
    int nr_cpus_allowed; //此进程运行的处理器数量
    cpumask_t cpus_allowed; //允许的处理器掩码
    ...
};

二、用户空间绑核

1. 方法介绍

一个CPU的亲合力掩码用一个 cpu_set_t 结构体来表示一个CPU集合,下面的几个宏分别对这个掩码集进行操作:

CPU_ZERO()  //清空一个集合
CPU_SET()   //将一个给定的CPU号加到集合,CPU号直接是0 1 2 3 这样的数字
CPU_CLR()   //对将一个给定的CPU号从集合中去掉
CPU_ISSET() //检查一个CPU号是否在这个集合中.

下面两个函数就是用来设置获取线程CPU亲和力

#define _GNU_SOURCE
#include <sched.h>

/*设置亲和性*/
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);

具体使用方法见man的描述,设置进程为pid的这个进程,让它运行在mask所设定的CPU上.如果pid的值为0,则表示指定的是当前进程,cpusetsize是mask所指定的数的长度,通常设定为sizeof(cpu_set_t).
如果当前pid所指定的进程此时没有运行在mask所指定的任意一个CPU上,则该指定的进程会从其它CPU上迁移到mask的指定的一个CPU上运行.
成功返回0,失败返回errno.

2. 例子

创建CPU核数个进程,然后都绑定在CPU2上

#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 100  //1个CPU内的最多进程数

#define AFFNITY_CURRENT_TASK 0

int cpu_num = 0;  //cpu中核数


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

    printf("this tid is: %d
", tid);  //显示是第几个线程
    
    CPU_ZERO(&mask); //置空
    CPU_SET(2, &mask); //设置亲和力值,都设置在PU2上。直接设置1 2 3 4
    if (sched_setaffinity(AFFNITY_CURRENT_TASK, sizeof(mask), &mask))//设置线程CPU亲和力
    {
        printf("sched_setaffinity eror.
");
        return;
    }

    CPU_ZERO(&get);
    if (sched_getaffinity(AFFNITY_CURRENT_TASK, sizeof(get), &get))//获取线程CPU亲和力
    {
        printf("sched_getaffinity eror.
");
        return;
    }
    for (i = 0; i < cpu_num; i++)
    {
        if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力
        {
            printf("this thread %d is running on processor: %d
", tid, i);
        }
    }

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

    cpu_num = sysconf(_SC_NPROCESSORS_CONF);  //获取核数
    printf("system has %i processor(s).
", cpu_num);

    for(i = 0; i < cpu_num; i++)
    {
        tid[i] = i;  //每个线程必须有个tid[i]
        pthread_create(&thread[i],NULL,threadFun,(void*)&tid[i]);
    }
    for(i = 0; i < cpu_num; i++)
    {
        pthread_join(thread[i], NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束
    }

    return 0;
}
# gcc cpu_affinity_test.c -lpthread -o affnity_test
# ./affnity_test 
system has 4 processor(s).
this tid is: 3
this tid is: 0
this tid is: 1
this tid is: 2
this thread 3 is running on processor: 2
this thread 0 is running on processor: 2
this thread 1 is running on processor: 2
this thread 2 is running on processor: 2

三、内核线程绑核

1. 方法介绍

内核可以使用下面两个函数设置处理器亲和性掩码

/*kernel/kthread.c*/
void kthread_bind(struct task_struct *p, unsigned int cpu) //绑定时会将进程状态设置为 TASK_UNINTERRUPTIBLE
/*kernel/sched/core.c*/
int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)

2. 例子

static int rcutorture_booster_init(int cpu) //kernel/rcu/rcutorture.c
    kthread_bind(boost_tasks[cpu], cpu);
    wake_up_process(boost_tasks[cpu]);

static __init int test_ringbuffer(void) //kernel/trace/ring_buffer.c
    kthread_bind(rb_threads[cpu], cpu);
    wake_up_process(rb_threads[cpu]);

四、使用cgroup的cpuset进行绑定

1. 可以绑定一个或一组进程到一个或一组指定的CPU上,使用方法及例子见:https://www.cnblogs.com/hellokitty2/p/14288835.html

五、相关命令

1. # taskset -p <PID> 看PID的CPU亲和性

taskset -p 3  pid  //表示将进程pid绑定到第3个核上
//-p Set/get the affinity of given PID instead of a new command
//-a Set/get the affinity of all threads of the PID
原文地址:https://www.cnblogs.com/hellokitty2/p/14382901.html