CPU_SET

CPU_SET(3)                 Linux Programmer's Manual                CPU_SET(3)



åå
       CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT, CPU_AND, CPU_OR,
       CPU_XOR, CPU_EQUAL, CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE, CPU_SET_S,
       CPU_CLR_S, CPU_ISSET_S, CPU_ZERO_S, CPU_COUNT_S, CPU_AND_S, CPU_OR_S,
       CPU_XOR_S, CPU_EQUAL_S - CPU éåãæä½ããããã®ãã¯ã

æ¸å¼
       #define _GNU_SOURCE             /* feature_test_macros(7) åç§ */
       #include <sched.h>

       void CPU_ZERO(cpu_set_t *set);

       void CPU_SET(int cpu, cpu_set_t *set);
       void CPU_CLR(int cpu, cpu_set_t *set);
       int  CPU_ISSET(int cpu, cpu_set_t *set);

       int  CPU_COUNT(cpu_set_t *set);

       void CPU_AND(cpu_set_t *destset,
                    cpu_set_t *srcset1, cpu_set_t *srcset2);
       void CPU_OR(cpu_set_t *destset,
                    cpu_set_t *srcset1, cpu_set_t *srcset2);
       void CPU_XOR(cpu_set_t *destset,
                    cpu_set_t *srcset1, cpu_set_t *srcset2);

       int  CPU_EQUAL(cpu_set_t *set1, cpu_set_t *set2);

       cpu_set_t *CPU_ALLOC(int num_cpus);
       void CPU_FREE(cpu_set_t *set);
       size_t CPU_ALLOC_SIZE(int num_cpus);

       void CPU_ZERO_S(size_t setsize, cpu_set_t *set);

       void CPU_SET_S(int cpu, size_t setsize, cpu_set_t *set);
       void CPU_CLR_S(int cpu, size_t setsize, cpu_set_t *set);
       int  CPU_ISSET_S(int cpu, size_t setsize, cpu_set_t *set);

       int  CPU_COUNT_S(size_t setsize, cpu_set_t *set);

       void CPU_AND_S(size_t setsize, cpu_set_t *destset,
                    cpu_set_t *srcset1, cpu_set_t *srcset2);
       void CPU_OR_S(size_t setsize, cpu_set_t *destset,
                    cpu_set_t *srcset1, cpu_set_t *srcset2);
       void CPU_XOR_S(size_t setsize, cpu_set_t *destset,
                    cpu_set_t *srcset1, cpu_set_t *srcset2);

       int  CPU_EQUAL_S(size_t setsize, cpu_set_t *set1, cpu_set_t *set2);

説æ
       cpu_set_t ãã¼ã¿æ§é ä½ã¯ CPU éåã表ç¾ãã¦ããã CPU éåã¯
       sched_setaffinity(2) ãåæ§ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ä½¿ç¨ããã¦ããã

       cpu_set_t ãã¼ã¿åã¯ãããéåã¨ãã¦å®è£ããã¦ããã ãããã ãã¼ã¿æ§é ä½ã¯ãã®å®è£ãæè‐
       ããã«æ±ããã®ã¨ããã¦ããã CPU éåã®ãã¹ã¦ã®æä½ã¯ã ãã®ãã¼ã¸ã§èª¬æããã¦ãããã¯ã‐
       ãéãã¦è¡ãã¹ãã§ããã

       以ä¸ã®ãã¯ãã CPU éå set ãæä½ããããã«æä¾ããã¦ããã

       CPU_ZERO()       set ãã¯ãªã¢ããã éåã«ã¯ä½ã CPU ãå«ã¾ããªãç¶æã¨ãªãã

       CPU_SET()        set ã« cpu ã追å ããã

       CPU_CLR()        set ãã cpu ãåé¤ããã

       CPU_ISSET()      CPU cpu ã set ã®ã¡ã³ãã¼ã§ããããæ¤æ»ããã

       CPU_COUNT()      set ã«å«ã¾ãã CPU æ°ãè¿ãã

       cpu å¼ãæ°ãæå®ããå ´åã ãã®å¼ãæ°ã¯å¯ä½ç¨ãä¼´ãã¹ãã§ã¯ãªãã ä¸è¨ã®ãã¯ã‐
       ã¯å¼ãæ°ãè¤æ°åè©ä¾¡ããå¯è½æ§ãããããã§ããã

       ãã®ã·ã¹ãã ã§å©ç¨å¯è½ãªæåã® CPU ã cpu å¤ 0 ã«å¯¾å¿ãã 次㮠CPU ã cpu å¤
       1 ã«å¯¾å¿ãã 以éãåæ§ã§ããã å®æ° CPU_SETSIZE (ç¾å¨ã®ã¨ãã 1024) ã¯
       cpu_set_t ã«æ ¼ç´ã§ããæ大 CPU æ°ããã大ããªå¤ã§ããã

       以ä¸ã®ãã¯ã㯠CPU éåã©ããã®è«çæä½ãè¡ãã

       CPU_AND()        éå srcset1 㨠srcset2 ã®ç©éåã destset ã«æ ¼ç´ãã
                        (åã®éåã®ããããã destset ã¨ãã¦ä½¿ç¨ãããå ´åããã)ã

       CPU_OR()         éå srcset1 㨠srcset2 ã®åéåã destset ã«æ ¼ç´ãã
                        (åã®éåã®ããããã destset ã¨ãã¦ä½¿ç¨ãããå ´åããã)ã

       CPU_XOR()        éå srcset1 㨠srcset2 ã® XOR ã destset ã«æ ¼ç´ãã
                        (åã®éåã®ããããã destset ã¨ãã¦ä½¿ç¨ãããå ´åããã)ã XOR ã¨ã¯ã
                        srcset1 ã srcset2
                        ã®ããããã«å«ã¾ãããã両æ¹ã«ã¯å«ã¾ããªãéåã®ãã¨ã§ããã

       CPU_EQUAL()      äºã¤ã® CPU éåãå¨ãåã CPU ãå«ãã§ããããæ¤æ»ããã

   åçã«å¤§ããã決ã¾ã CPU éå
       ããã¤ãã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ CPU éåã®å¤§ãããåçã«æ±ºããè½å (ä¾ãã°ã æ¨æºã®
       cpu_set_t ãã¼ã¿åã§å®ç¾©ãããããã大ããéåãå²ãå½ã¦ããªã©) ãå¿è¦ã¨ãªããã¨ãããããã
       ç¾å¨ glibc ã¯ããã«å¯¾å¿ããããã«ããã¤ãã®ãã¯ããæä¾ãã¦ããã

       以ä¸ã®ãã¯ãã使ã㨠CPU éåã®å²ãå½ã¦ã¨è§£æ¾ãã§ããã

       CPU_ALLOC()      0 ãã num_cpus-1 ã¾ã§ã®ç¯å²ã® CPU ãä¿æããã®ã«ååãªå¤§ããã®
                        CPU éåãå²ãå½ã¦ãã

       CPU_ALLOC_SIZE() 0 ãã num_cpus-1 ã¾ã§ã®ç¯å²ã® CPU ãä¿æããã®ã«å¿è¦ãª CPU
                        éåã®å¤§ããããã¤ãæ°ã§è¿ãã ãã®ãã¯ããè¿ãå¤ã¯ã å¾è¿°ã®
                        CPU_*_S() ãã¯ãã® setsize å¼ãæ°ã¨ãã¦ä½¿ç¨ã§ããã

       CPU_FREE()       以åã« CPU_ALLOC() ã§å²ãå½ã¦ããã CPU éåã解æ¾ããã

       ååã "_S" ã§çµãããã¯ã㯠"_S" ãªãã®åãååã®ãã¯ãã¨åçã§ããã ãããã®ãã¯ã㯠"_S"
       ãªãã®ãã®ã¨åãåä½ããããã åçã«å²ãå½ã¦ãããã 大ããã setsize ãã¤ãã® CPU
       éåã«å¯¾ãã¦æä½ãè¡ãç¹ãç°ãªãã

è¿ãå¤
       CPU_ISSET() 㨠CPU_ISSET_S() ã¯ã cpu ã set ã«å«ã¾ãã¦ããã° 0
       以å¤ãè¿ããå«ã¾ããªãå ´å 0 ãè¿ãã

       CPU_COUNT() 㨠CPU_COUNT_S() 㯠set ã«å«ã¾ãã CPU æ°ãè¿ãã

       CPU_EQUAL() 㨠CPU_EQUAL_S() ã¯ã äºã¤ã® CPU éåãçãããã° 0 以å¤ãè¿ãã ç‐
       ãããªãå ´å 0 ãè¿ãã

       CPU_ALLOC() ã¯æåããã¨ãã¤ã³ã¿ã¼ãè¿ãã 失æãã㨠NULL ãè¿ã (ã¨ã©ã¼ã¯
       malloc(3) ã¨åãã§ãã)ã

       CPU_ALLOC_SIZE() ã¯æå®ããã大ããã® CPU éåãæ ¼ç´ããã®ã«å¿è¦ãªãã¤ãæ°ãè¿ãã

       ä»ã®é¢æ°ã¯å¤ãè¿ããªãã

ãã¼ã¸ã§ã³
       ãã¯ã CPU_ZERO(), CPU_SET(), CPU_CLR(), CPU_ISSET() 㯠glibc 2.3.3
       ã§è¿½å ãããã

       CPU_COUNT() 㯠glibc 2.6 ã§åãã¦ç»å ´ããã

       CPU_AND(), CPU_OR(), CPU_XOR(), CPU_EQUAL(), CPU_ALLOC(),
       CPU_ALLOC_SIZE(), CPU_FREE(), CPU_ZERO_S(), CPU_SET_S(), CPU_CLR_S(),
       CPU_ISSET_S(), CPU_AND_S(), CPU_OR_S(), CPU_XOR_S(), CPU_EQUAL_S() ã¯
       glibc 2.7 ã§åãã¦ç»å ´ããã

æºæ
       ãããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ Linux åºæã§ããã

注æ
       CPU éåãè¤è£½ããã«ã¯ã memcpy(3) ã使ç¨ããã

       CPU éåã¯ãã³ã°ã¯ã¼ãåä½ã«å²ãå½ã¦ããããããéåãªã®ã§ã åçã«å²ãå½ã¦ããã CPU
       éåã®å®éã® CPU æ°ã¯ sizeof(unsigned long) ã®æ¬¡ã®åæ°ã«åãä¸ãããããã¨ã«ãªãã
       ã¢ããªã±ã¼ã·ã§ã³ã¯ã ãããã®ä½åãªãããã®å容ã¯ä¸å®ã¨èããã¹ãã§ããã

       ååã¯ä¼¼ã¦ãããã å®æ° CPU_SETSIZE 㯠cpu_set_t ãã¼ã¿åã«å«ã¾ãã CPU æ°
       (ã¤ã¾ããäºå®ä¸ãããéååã®ãããã«ã¦ã³ã) ã示ãã®ã«å¯¾ãã¦ã ãã¯ã CPU_*_S() ã®
       setsize å¼ãæ°ã¯ãã¤ãåä½ã®ãµã¤ãºã§ããç¹ã«æ³¨æãããã¨ã

       ãæ¸å¼ãã«æ¸ããã¦ããå¼ãæ°ã¨è¿ãå¤ã®ãã¼ã¿åã¯ãããããã®å ´åã§ã©ããªåãæå¾ããããã®ãã³ãã§ããã
       ããããªããã ãããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ãã¯ãã¨ãã¦å®è£ããã¦ããããã
       ãã®ãã³ããå®ããªãã£ãå ´åã«ã ã³ã³ãã¤ã©ãå¿ãããå¨ã¦ã®åã¨ã©ã¼ãææã§ããã¨ã¯éããªãã

ãã°
       glibc 2.8 以åã® 32 ããããã©ãããã©ã¼ã ã§ã¯ã CPU_ALLOC()
       ã¯å¿è¦ãªç©ºéã®å²ãå½ã¦ãäºåº¦è¡ãã CPU_ALLOC_SIZE()
       ã¯æ¬æ¥ããã¹ãå¤ã®äºåã®å¤ãè¿ãã ãã®ãã°ã¯ãã‐
       ã°ã©ã ã®åä½ã«ã¯å½±é¿ãä¸ããªãã¯ãã ãã ç¡é§ã«ã¡ã¢ãªã¼ãæ¶è²»ãã
       åçã«å²ãå½ã¦ããã CPU éåã«å¯¾ãã¦æä½ãè¡ããã¯ãã®åä½ã®å¹çãä¸ããçµæã¨ãªãã
       ãããã®ãã°ã¯ glibc 2.9 ã§ä¿®æ£ãããã

ä¾
       以ä¸ã®ããã°ã©ã ã¯ãåçã«å²ãå½ã¦ã CPU éåã«å¯¾ãã¦ããã¤ãã®ãã¯ã‐
       ã使ç¨ããä¾ã示ãã¦ããã

       #define _GNU_SOURCE
       #include <sched.h>
       #include <stdlib.h>
       #include <unistd.h>
       #include <stdio.h>
       #include <assert.h>

       int
       main(int argc, char *argv[])
       {
           cpu_set_t *cpusetp;
           size_t size;
           int num_cpus, cpu;

           if (argc < 2) {
               fprintf(stderr, "Usage: %s <num-cpus>\n", argv[0]);
               exit(EXIT_FAILURE);
           }

           num_cpus = atoi(argv[1]);

           cpusetp = CPU_ALLOC(num_cpus);
           if (cpusetp == NULL) {
               perror("CPU_ALLOC");
               exit(EXIT_FAILURE);
           }

           size = CPU_ALLOC_SIZE(num_cpus);

           CPU_ZERO_S(size, cpusetp);
           for (cpu = 0; cpu < num_cpus; cpu += 2)
               CPU_SET_S(cpu, size, cpusetp);

           printf("CPU_COUNT() of set:    %d\n", CPU_COUNT_S(size, cpusetp));

           CPU_FREE(cpusetp);
           exit(EXIT_SUCCESS);
       }

é¢é£é ç®
       sched_setaffinity(2), pthread_attr_setaffinity_np(3),
       pthread_setaffinity_np(3), cpuset(7)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.79 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2012-03-15                        CPU_SET(3)