進程線程與cpu綁定

來源:互聯網
上載者:User

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;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.