zoukankan      html  css  js  c++  java
  • 进程线程与cpu绑定

    CPU Affinity


    CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行.
    一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码可能会获得更好的性能.
    一个CPU的亲合力掩码用一个cpu_set_t结构体来表示一个CPU集合,下面的几个宏分别对这个掩码集进行操作:
    CPU_ZERO() 清空一个集合
    CPU_SET()与CPU_CLR()分别对将一个给定的CPU号加到一个集合或者从一个集合中去掉.
    CPU_ISSET()检查一个CPU号是否在这个集合中.
    其实这几个的用法与select()函数那几个调用差不多.
    下面两个函数就是最主要的了:
    sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)
    该函数设置进程为pid的这个进程,让它运行在mask所设定的CPU上.如果pid的值为0,则表示指定的是当前进程,使当前进程运行在mask所设定的那些CPU上.第二个参数cpusetsize是


    mask所指定的数的长度.通常设定为sizeof(cpu_set_t).如果当前pid所指定的CPU此时没有运行在mask所指定的任意一个CPU上,则该指定的进程会从其它CPU上迁移到mask的指定的


    一个CPU上运行.
    sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)

    该函数获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中.即获得指定pid当前可以运行在哪些CPU上.同样,如果pid的值为0.也表示的是当前进程.


    # define __CPU_SETSIZE  1024
    # define __NCPUBITS     (8 * sizeof (__cpu_mask))
    
    typedef unsigned long int __cpu_mask;
    
    # define __CPUELT(cpu)  ((cpu) / __NCPUBITS)
    # define __CPUMASK(cpu) ((__cpu_mask) 1 << ((cpu) % __NCPUBITS))
    
    typedef struct
    {
      __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];
    } cpu_set_t;
    
    # define __CPU_ZERO(cpusetp) \
      do {                                                                        \
        unsigned int __i;                                                         \
        cpu_set_t *__arr = (cpusetp);                                             \
        for (__i = 0; __i < sizeof (cpu_set_t) / sizeof (__cpu_mask); ++__i)      \
          __arr->__bits[__i] = 0;                                                 \
      } while (0)
    # define __CPU_SET(cpu, cpusetp) \
      ((cpusetp)->__bits[__CPUELT (cpu)] |= __CPUMASK (cpu))
    # define __CPU_CLR(cpu, cpusetp) \
      ((cpusetp)->__bits[__CPUELT (cpu)] &= ~__CPUMASK (cpu))
    # define __CPU_ISSET(cpu, cpusetp) \
      (((cpusetp)->__bits[__CPUELT (cpu)] & __CPUMASK (cpu)) != 0)


    例子:

    #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>
    
    int main(int argc, char* argv[])
    {
            int num = sysconf(_SC_NPROCESSORS_CONF);
            int created_thread = 0;
            int myid;
            int i;
            int j = 0;
    
            cpu_set_t mask;
            cpu_set_t get;
    
            if (argc != 2)
            {
                    printf("usage : ./cpu num\n");
                    exit(1);
            }
    
            myid = atoi(argv[1]);
    
            printf("system has %i processor(s). \n", num);
    
            CPU_ZERO(&mask);
            CPU_SET(myid, &mask);
    
            if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
            {
                    printf("warning: could not set CPU affinity, continuing...\n");
            }
            while (1)
            {
    
                    CPU_ZERO(&get);
                    if (sched_getaffinity(0, sizeof(get), &get) == -1)
                    {
                            printf("warning: cound not get cpu affinity, continuing...\n");
                    }
                    for (i = 0; i < num; i++)
                    {
                            if (CPU_ISSET(i, &get))
                            {
                                    printf("this process %d is running processor : %d\n",getpid(), i);
                            }
                    }
            }
            return 0;
    }
    




      与进程的情况相似,线程亲和性的设置和获取主要通过下面两个函数来实现:
        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);


    #define _GNU_SOURCE
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <sched.h>
    
    void *myfun(void *arg)
    {
        cpu_set_t mask;
        cpu_set_t get;
        char buf[256];
        int i;
        int j;
        int num = sysconf(_SC_NPROCESSORS_CONF);
        printf("system has %d processor(s)\n", num);
    
        for (i = 0; i < num; i++) {
            CPU_ZERO(&mask);
            CPU_SET(i, &mask);
            if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
                fprintf(stderr, "set thread affinity failed\n");
            }
            CPU_ZERO(&get);
            if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
                fprintf(stderr, "get thread affinity failed\n");
            }
            for (j = 0; j < num; j++) {
                if (CPU_ISSET(j, &get)) {
                    printf("thread %d is running in processor %d\n", (int)pthread_self(), j);
                }
            }
            j = 0;
            while (j++ < 100000000) {
                memset(buf, 0, sizeof(buf));
            }
        }
        pthread_exit(NULL);
    }
    
    int main(int argc, char *argv[])
    {
        pthread_t tid;
        if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0) {
            fprintf(stderr, "thread create failed\n");
            return -1;
        }
        pthread_join(tid, NULL);
        return 0;
    }


  • 相关阅读:
    模板
    kuangbin带你飞
    BZOJ开荒记
    模板
    洛谷
    模板
    [蓝桥杯][2013年第四届真题]危险系数
    数位DP入门题
    备战2019蓝桥杯
    常用的数学符号
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3063500.html
Copyright © 2011-2022 走看看