sched_setaffinity

SCHED_SETAFFINITY(2)       Linux Programmer's Manual      SCHED_SETAFFINITY(2)



åå
       sched_setaffinity, sched_getaffinity - ããã»ã¹ã® CPU affinity ãã¹ã¯ãè¨‐
       å®ã»åå¾ãã

æ¸å¼
       #define _GNU_SOURCE             /* feature_test_macros(7) åç§ */
       #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);

説æ
       ããã»ã¹ã® CPU affinity (親å度) ãã¹ã¯ã¯ããã®ããã»ã¹ã å®è¡ã許å¯ããã¦ãã CPU
       ã®éåã決å®ããã ãã«ãããã»ããµã»ã·ã¹ãã ã§ã¯ãCPU affinity ãã¹ã¯ãè¨å®ãããã¨ã§
       æ§è½ä¸ã®ã¡ãªãããå¾ãããå¯è½æ§ãããã ä¾ãã°ãç¹å®ã®ããã»ã¹ãä¸ã¤ã® CPU ã«æ¬ãä»ã
       (ããªãã¡ããã®ããã»ã¹ã® affinity ãã¹ã¯ãä¸ã¤ã® CPU ã«è¨å®ã)ã ä»ã®å¨ã¦ã®ãã‐
       ã»ã¹ã® affinity ãã¹ã¯ãããã® CPU ãé¤å¤ãããã¨ã§ã 確å®ã«ãã®ãã‐
       ã»ã¹ã®å®è¡é度ãæ大ã«ãããã¨ãã§ããã ã¾ããããããã»ã¹ã®å®è¡ãä¸ã¤ã® CPU
       ã«éå®ãããã¨ã§ã ä¸ã¤ã® CPU ã§ã®å®è¡ãåæ¢ãã¦ããå¥ã® CPU
       ã§å®è¡ãåéããã¨ãã«çºçãã ãã£ãã·ã¥ç¡å¹å (cache invalidation)
       ã«ããæ§è½é¢ã®å£åãé¿ãããã¨ãã§ããã

       CPU affinity ãã¹ã¯ã¯ãCPU ã®éåãã表ã cpu_set_t æ§é ä½ã§è¡¨ç¾ããã
       cpu_set_t ã¸ã®ãã¤ã³ã¿ mask ã§æå®ãããã CPU éåãæä½ããããã®ãã¯ã群ã«ã¤ãã¦ã¯
       CPU_SET(3)  ã§è¨è¼ããã¦ããã

       sched_setaffinity()  ã¯ãããã»ã¹ID ã pid ã®ããã»ã¹ã® CPU affinity ãã¹ã¯ã
       mask ã§æå®ãããå¤ã«è¨å®ããã pid ã 0 ã®å ´åãå¼ã³åºãåããã»ã¹ã使ãããã
       cpusetsize å¼ãæ°ã«ã¯ mask ãæããã¼ã¿ã®é·ã (ãã¤ãåä½) ã§ããã
       é常ã¯ããã®å¼ãæ°ã«ã¯ sizeof(cpu_set_t) ãæå®ããã°ããã

       pid ã§æå®ãããããã»ã¹ã mask ã§æå®ããã CPU ã®ããããã§ç¾å¨å®è¡ããã¦ããªãå ´åã
       ãã®ããã»ã¹ã¯ mask ã§æå®ããã CPU ã®ããããã«ç§»åãããã

       sched_getaffinity()  ã¯ã ããã»ã¹ID ã pid ã®ããã»ã¹ã® affinity ãã¹ã¯ã mask
       ãæã cpu_set_t æ§é ä½ã«æ¸ãè¾¼ãã cpusetsize å¼ãæ°ã«ã¯ mask ã® (ãã¤ãåä½ã®)
       大ãããæå®ããã

è¿ãå¤
       æåããå ´åã sched_setaffinity()  㨠sched_getaffinity()  㯠0 ãè¿ãã
       ã¨ã©ã¼ã®å ´å㯠-1 ãè¿ãã errno ãé©åã«è¨å®ããã

ã¨ã©ã¼
       EFAULT æå®ãããã¡ã¢ãªçªå°ãä¸æ£ã§ããã

       EINVAL ã·ã¹ãã ä¸ã«ç¾å¨å®éã«åå¨ããã㤠"cpuset" æ©æ§ã課ãå¶éã«ããã¦ãã®ãã‐
              ã»ã¹ã«å¯¾ãã¦è¨±å¯ããã¦ãã ããã»ããµãã affinity ããããã¹ã¯ mask
              ã«å«ã¾ãã¦ããªãã "cpuset" æ©æ§ã«ã¤ãã¦ã¯ cpuset(7)  ãåç§ã

       EINVAL (sched_getaffinity()  ã¨ãã«ã¼ãã« 2.6.9 以åã®
              sched_setaffinity()) cpusetsize ãã«ã¼ãã«ã§ä½¿ããã¦ãã affinity
              ãã¹ã¯ã®ãµã¤ãºããå°ããã

       EPERM  (sched_setaffinity())  å¼ã³åºãåã®ããã»ã¹ã«é©åãªç¹æ¨©ããªãã£ãã
              å¼ã³åºãåã¯ãå®å¹ã¦ã¼ã¶ ID ã pid ã§èå¥ãããããã»ã¹ã®å®ã¦ã¼ã¶ ID
              ã¾ãã¯å®å¹ã¦ã¼ã¶ ID ã¨åãã§ãããã CAP_SYS_NICE ã±ã¼ãããªãã£
              (capability) ãæããªããã°ãªããªãã

       ESRCH  ããã»ã¹ ID pid ã®ããã»ã¹ãè¦ã¤ãããªãã£ãã

ãã¼ã¸ã§ã³
       CPU affinity ã·ã¹ãã ã³ã¼ã«ã¯ Linux kernel 2.5.8 ã§å°å¥ãããã
       ãããã®ã·ã¹ãã ã³ã¼ã«ã®ã©ããã¼é¢æ°ã¯ glibc 2.3 ã§å°å¥ãããã æåã¯ãglibc
       ã®ã¤ã³ã¿ãã§ã¼ã¹ã«ã¯ unsigned int åã® cpusetsize å¼ãæ°ãå¥ã£ã¦ããã glibc
       2.3.3 ã§ã¯ cpusetsize å¼ãæ°ãåé¤ãããããglibc 2.3.4 㧠size_t åã§å¾©æ´»ããã

æºæ
       ãããã®ã·ã¹ãã ã³ã¼ã«ã¯ Linux åºæã§ããã

注æ
       sched_setaffinity()  ãå¼ã³åºããå¾ã¯ãããã»ã¹ãå®éã«å®è¡ããã CPU ã®éåã¯ã mask
       å¼ãæ°ã§æå®ãããéåã¨ãã·ã¹ãã ä¸ã«å®éã«åå¨ãã CPU ã®éåã® å±ééå (AND) ã¨ãªãã
       "cpuset" æ©æ§ã使ç¨ããã¦ããå ´åã«ã¯ãããã»ã¹ãåä½ãã CPU éå
       ã«å¯¾ãã¦ã·ã¹ãã ã¯ããã«å¶éãå ãããããããªã ("cpuset" æ©æ§ã«ã¤ãã¦ã¯ cpuset(7)
       ãåç§)ã ããã»ã¹ãåä½ããå®éã® CPU éåã«å¯¾ããå¶éã¯ã«ã¼ãã«ã«ãã
       æé»ã®ãã¡ã«é©ç¨ãããã

       sched_setscheduler(2) ã« Linux ã®ã¹ã±ã¸ã¥ã¼ãªã³ã°æ¹å¼ã«ã¤ãã¦ã®èª¬æ ãããã

       å®éã«ã¯ affinity ãã¹ã¯ã¯ã¹ã¬ããåä½ã®å±æ§ã§ãã¹ã¬ããã°ã«ã¼ãã®
       åã¹ã¬ããåä½ã«ç¬ç«ãã¦èª¿æ´ãããã¨ãã§ããã gettid(2)
       ã³ã¼ã«ããã®è¿ãå¤ããã®ã³ã¼ã«ã® pid å¼ãæ°ã¨ãã¦æ¸¡ããã¨ãã§ããã pid ã« 0
       ãæå®ããã¨å¼ã³åºãåã®ã¹ã¬ããã®å±æ§ãè¨å®ããã getpid(2)  ã³ã¼ã«ããã®è¿ãå¤ã pid
       ã«æå®ããã¨ã¹ã¬ããã°ã«ã¼ãã®ã¡ã¤ã³ã¹ã¬ããã®å±æ§ãè¨å®ããã (POSIX ã¹ã¬ãã API
       ã使ç¨ãã¦ããå ´åã sched_setaffinity()  ã®ä»£ããã«
       pthread_setaffinity_np(3) ã使ç¨ãããã¨)ã

       fork(2)  çµç±ã§çæãããåããã»ã¹ã¯è¦ªããã»ã¹ã® CPU affinity ãã¹ã¯ãç¶æ¿ããã
       affinity ãã¹ã¯ã¯ execve(2)  ã®åå¾ã§ä¿åãããã

       ãã®ããã¥ã¢ã«ãã¼ã¸ã§ã¯ CPU affinity ã³ã¼ã«ã® glibc ã¤ã³ã¿ãã§ã¼ã¹ã
       説æãã¦ãããå®éã®ã·ã¹ãã ã³ã¼ã«ã»ã¤ã³ã¿ãã§ã¼ã¹ã¯å°ãéã£ã¦ããã å®éã®å®è£ã§ã¯
       CPU éåã¯ç°¡åãªããããã¹ã¯ã§ããã¨ããå®ç¶ãåæ ãã mask ã®åã unsigned long *
       ã¨ãªã£ã¦ããã æåæã«ã¯ãçã® sched_getaffinity() ã·ã¹ãã ã³ã¼ã«èªèº«ã¯
       cpumask_t ãã¼ã¿åã® (ãã¤ãåä½ã®) 大ãããè¿ãã cpumask_t ã¯ã«ã¼ãã«åé¨ã§ CPU
       éåã®ããããã¹ã¯ã表ç¾ããã®ã« 使ããã¦ãããã¼ã¿åã§ããã

é¢é£é ç®
       taskset(1), clone(2), getcpu(2), getpriority(2), gettid(2), nice(2),
       sched_get_priority_max(2), sched_get_priority_min(2),
       sched_getscheduler(2), sched_setscheduler(2), setpriority(2),
       CPU_SET(3), pthread_setaffinity_np(3), sched_getcpu(3),
       capabilities(7), cpuset(7)

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



Linux                             2013-02-11              SCHED_SETAFFINITY(2)