1SCHED_SETAFFINITY(2)       Linux Programmer's Manual      SCHED_SETAFFINITY(2)
2
3
4

NAME

6       sched_setaffinity,  sched_getaffinity  -  set  and  get a process's CPU
7       affinity mask
8

SYNOPSIS

10       #define _GNU_SOURCE
11       #include <sched.h>
12
13       int sched_setaffinity(pid_t pid, size_t cpusetsize,
14                             cpu_set_t *mask);
15
16       int sched_getaffinity(pid_t pid, size_t cpusetsize,
17                             cpu_set_t *mask);
18

DESCRIPTION

20       A process's CPU affinity mask determines the set of CPUs on which it is
21       eligible  to run.  On a multiprocessor system, setting the CPU affinity
22       mask can be used to obtain performance benefits.  For example, by dedi‐
23       cating one CPU to a particular process (i.e., setting the affinity mask
24       of that process to specify a single CPU, and setting the affinity  mask
25       of  all  other processes to exclude that CPU), it is possible to ensure
26       maximum execution speed for that process.  Restricting a process to run
27       on  a  single  CPU also avoids the performance cost caused by the cache
28       invalidation that occurs when a process ceases to execute  on  one  CPU
29       and then recommences execution on a different CPU.
30
31       A  CPU  affinity mask is represented by the cpu_set_t structure, a "CPU
32       set", pointed to by mask.  A set of macros for manipulating CPU sets is
33       described in CPU_SET(3).
34
35       sched_setaffinity()  sets the CPU affinity mask of the process whose ID
36       is pid to the value specified by mask.  If pid is zero, then the  call‐
37       ing  process is used.  The argument cpusetsize is the length (in bytes)
38       of the data pointed to by mask.  Normally this argument would be speci‐
39       fied as sizeof(cpu_set_t).
40
41       If  the process specified by pid is not currently running on one of the
42       CPUs specified in mask, then that process is migrated  to  one  of  the
43       CPUs specified in mask.
44
45       sched_getaffinity() writes the affinity mask of the process whose ID is
46       pid into the cpu_set_t structure pointed to by  mask.   The  cpusetsize
47       argument  specifies  the size (in bytes) of mask.  If pid is zero, then
48       the mask of the calling process is returned.
49

RETURN VALUE

51       On success, sched_setaffinity() and sched_getaffinity() return  0.   On
52       error, -1 is returned, and errno is set appropriately.
53

ERRORS

55       EFAULT A supplied memory address was invalid.
56
57       EINVAL The  affinity bit mask mask contains no processors that are cur‐
58              rently physically on the system and  permitted  to  the  process
59              according  to  any  restrictions  that  may  be  imposed  by the
60              "cpuset" mechanism described in cpuset(7).
61
62       EINVAL (sched_getaffinity()   and,    in    kernels    before    2.6.9,
63              sched_setaffinity())  cpusetsize is smaller than the size of the
64              affinity mask used by the kernel.
65
66       EPERM  (sched_setaffinity()) The calling process does not  have  appro‐
67              priate  privileges.  The caller needs an effective user ID equal
68              to the user ID or effective user ID of the process identified by
69              pid, or it must possess the CAP_SYS_NICE capability.
70
71       ESRCH  The process whose ID is pid could not be found.
72

VERSIONS

74       The  CPU  affinity  system calls were introduced in Linux kernel 2.5.8.
75       The system call wrappers were introduced in glibc 2.3.  Initially,  the
76       glibc interfaces included a cpusetsize argument, typed as unsigned int.
77       In glibc 2.3.3, the cpusetsize  argument  was  removed,  but  was  then
78       restored in glibc 2.3.4, with type size_t.
79

CONFORMING TO

81       These system calls are Linux-specific.
82

NOTES

84       After  a  call  to  sched_setaffinity(),  the  set of CPUs on which the
85       process will actually run is the intersection of the set  specified  in
86       the  mask  argument and the set of CPUs actually present on the system.
87       The system may further restrict the set of CPUs on  which  the  process
88       runs  if  the  "cpuset" mechanism described in cpuset(7) is being used.
89       These restrictions on the actual set of CPUs on which the process  will
90       run are silently imposed by the kernel.
91
92       sched_setscheduler(2) has a description of the Linux scheduling scheme.
93
94       The  affinity  mask  is  actually  a  per-thread  attribute that can be
95       adjusted independently for each of the threads in a thread group.   The
96       value  returned  from a call to gettid(2) can be passed in the argument
97       pid.  Specifying pid as 0  will  set  the  attribute  for  the  calling
98       thread,  and  passing  the value returned from a call to getpid(2) will
99       set the attribute for the main thread of the thread group.  (If you are
100       using  the  POSIX  threads  API,  then  use  pthread_setaffinity_np (3)
101       instead of sched_setaffinity().)
102
103       A child created via fork(2) inherits its parent's  CPU  affinity  mask.
104       The affinity mask is preserved across an execve(2).
105
106       This  manual  page  describes  the glibc interface for the CPU affinity
107       calls.  The actual system call interface is  slightly  different,  with
108       the  mask  being typed as unsigned long *, reflecting the fact that the
109       underlying implementation of CPU sets is a simple bit  mask.   On  suc‐
110       cess,  the  raw  sched_getaffinity()  system  call returns the size (in
111       bytes) of the cpumask_t data type that is used internally by the kernel
112       to represent the CPU set bit mask.
113
114       The cpu_set_t affinity mask size provided by glibc only allows for upto
115       1024 CPUs. It is possible to build Linux kernels with greater than 1024
116       CPUs.  Any  application  using the statically sized cpu_set_t will fail
117       with EINVAL on such kernels. It is thus recommended  that  applications
118       avoid  using  the  statically sized cpu_set_t type, and instead dynami‐
119       cally allocate a  mask  using  the  CPU_*_S  macros  described  in  the
120       CPU_SET(3)  man  page.  Since  it is not possible to determine ahead of
121       time what NR_CPUS value the kernel was built with, applications must be
122       prepared  to  catch EINVAL, and retry the command with a larger dynami‐
123       cally allocated mask.  The example that  follows  illustrates  portable
124       usage.
125
126

EXAMPLE

128          #define _GNU_SOURCE
129
130          #include <sched.h>
131          #include <stdio.h>
132          #include <errno.h>
133
134          int main(void)
135          {
136               cpu_set_t *mask;
137               size_t size;
138               int i;
139               int nrcpus = 1024;
140
141       realloc:
142               mask = CPU_ALLOC(nrcpus);
143               size = CPU_ALLOC_SIZE(nrcpus);
144               CPU_ZERO_S(size, mask);
145               if ( sched_getaffinity(0, size, mask) == -1 ) {
146                       CPU_FREE(mask);
147                       if (errno == EINVAL &&
148                           nrcpus < (1024 << 8)) {
149                              nrcpus = nrcpus << 2;
150                              goto realloc;
151                       }
152                       perror("sched_getaffinity");
153                       return -1;
154               }
155
156               for ( i = 0; i < nrcpus; i++ ) {
157                       if ( CPU_ISSET_S(i, size, mask) ) {
158                               printf("CPU %d is set\n", (i+1));
159                       }
160               }
161
162               CPU_FREE(mask);
163
164               return 0;
165          }
166
167

SEE ALSO

169       clone(2), getcpu(2), getpriority(2), gettid(2), nice(2), sched_get_pri‐
170       ority_max(2),     sched_get_priority_min(2),     sched_getscheduler(2),
171       sched_setscheduler(2),   setpriority(2),  CPU_SET(3),  sched_getcpu(3),
172       capabilities(7), pthread_setaffinity_np(3), cpuset(7)
173

COLOPHON

175       This page is part of release 3.22 of the Linux  man-pages  project.   A
176       description  of  the project, and information about reporting bugs, can
177       be found at http://www.kernel.org/doc/man-pages/.
178
179
180
181Linux                             2008-11-14              SCHED_SETAFFINITY(2)
Impressum