console_codes.4: tfix
[man-pages.git] / man2 / sched_setattr.2
blob545558a4e9c9fcbd86f072fd4abfa5b89b7258be
1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2014 Peter Zijlstra <peterz@infradead.org>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH SCHED_SETATTR 2 2021-03-22 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 sched_setattr, sched_getattr \-
29 set and get scheduling policy and attributes
30 .SH SYNOPSIS
31 .nf
32 .B #include <sched.h>
33 .PP
34 .BI "int sched_setattr(pid_t " pid ", struct sched_attr *" attr ,
35 .BI "                  unsigned int " flags );
36 .BI "int sched_getattr(pid_t " pid ", struct sched_attr *" attr ,
37 .BI "                  unsigned int " size ", unsigned int " flags );
38 .fi
39 .\" FIXME . Add feature test macro requirements
40 .PP
41 .IR Note :
42 There are no glibc wrappers for these system calls; see NOTES.
43 .SH DESCRIPTION
44 .SS sched_setattr()
45 The
46 .BR sched_setattr ()
47 system call sets the scheduling policy and
48 associated attributes for the thread whose ID is specified in
49 .IR pid .
51 .I pid
52 equals zero,
53 the scheduling policy and attributes of the calling thread will be set.
54 .PP
55 Currently, Linux supports the following "normal"
56 (i.e., non-real-time) scheduling policies as values that may be specified in
57 .IR policy :
58 .TP 14
59 .BR SCHED_OTHER
60 the standard round-robin time-sharing policy;
61 .\" In the 2.6 kernel sources, SCHED_OTHER is actually called
62 .\" SCHED_NORMAL.
63 .TP
64 .BR SCHED_BATCH
65 for "batch" style execution of processes; and
66 .TP
67 .BR SCHED_IDLE
68 for running
69 .I very
70 low priority background jobs.
71 .PP
72 Various "real-time" policies are also supported,
73 for special time-critical applications that need precise control over
74 the way in which runnable threads are selected for execution.
75 For the rules governing when a process may use these policies, see
76 .BR sched (7).
77 The real-time policies that may be specified in
78 .IR policy
79 are:
80 .TP 14
81 .BR SCHED_FIFO
82 a first-in, first-out policy; and
83 .TP
84 .BR SCHED_RR
85 a round-robin policy.
86 .PP
87 Linux also provides the following policy:
88 .TP 14
89 .B SCHED_DEADLINE
90 a deadline scheduling policy; see
91 .BR sched (7)
92 for details.
93 .PP
94 The
95 .I attr
96 argument is a pointer to a structure that defines
97 the new scheduling policy and attributes for the specified thread.
98 This structure has the following form:
99 .PP
100 .in +4n
102 struct sched_attr {
103     u32 size;              /* Size of this structure */
104     u32 sched_policy;      /* Policy (SCHED_*) */
105     u64 sched_flags;       /* Flags */
106     s32 sched_nice;        /* Nice value (SCHED_OTHER,
107                               SCHED_BATCH) */
108     u32 sched_priority;    /* Static priority (SCHED_FIFO,
109                               SCHED_RR) */
110     /* Remaining fields are for SCHED_DEADLINE */
111     u64 sched_runtime;
112     u64 sched_deadline;
113     u64 sched_period;
118 The fields of the
119 .IR sched_attr
120 structure are as follows:
122 .B size
123 This field should be set to the size of the structure in bytes, as in
124 .IR "sizeof(struct sched_attr)" .
125 If the provided structure is smaller than the kernel structure,
126 any additional fields are assumed to be '0'.
127 If the provided structure is larger than the kernel structure,
128 the kernel verifies that all additional fields are 0;
129 if they are not,
130 .BR sched_setattr ()
131 fails with the error
132 .BR E2BIG
133 and updates
134 .I size
135 to contain the size of the kernel structure.
137 The above behavior when the size of the user-space
138 .I sched_attr
139 structure does not match the size of the kernel structure
140 allows for future extensibility of the interface.
141 Malformed applications that pass oversize structures
142 won't break in the future if the size of the kernel
143 .I sched_attr
144 structure is increased.
145 In the future,
146 it could also allow applications that know about a larger user-space
147 .I sched_attr
148 structure to determine whether they are running on an older kernel
149 that does not support the larger structure.
151 .I sched_policy
152 This field specifies the scheduling policy, as one of the
153 .BR SCHED_*
154 values listed above.
156 .I sched_flags
157 This field contains zero or more of the following flags
158 that are ORed together to control scheduling behavior:
161 .BR SCHED_FLAG_RESET_ON_FORK
162 Children created by
163 .BR fork (2)
164 do not inherit privileged scheduling policies.
166 .BR sched (7)
167 for details.
169 .BR SCHED_FLAG_RECLAIM " (since Linux 4.13)"
170 .\" 2d4283e9d583a3ee8cfb1cbb9c1270614df4c29d
171 This flag allows a
172 .BR SCHED_DEADLINE
173 thread to reclaim bandwidth unused by other real-time threads.
174 .\" Bandwidth reclaim is done via the GRUB algorithm; see
175 .\" Documentation/scheduler/sched-deadline.txt
177 .BR SCHED_FLAG_DL_OVERRUN " (since Linux 4.16)"
178 .\" commit 34be39305a77b8b1ec9f279163c7cdb6cc719b91
179 This flag allows an application to get informed about run-time overruns in
180 .BR SCHED_DEADLINE
181 threads.
182 Such overruns may be caused by (for example) coarse execution time accounting
183 or incorrect parameter assignment.
184 Notification takes the form of a
185 .B SIGXCPU
186 signal which is generated on each overrun.
188 This
189 .BR SIGXCPU
190 signal is
191 .I process-directed
192 (see
193 .BR signal (7))
194 rather than thread-directed.
195 This is probably a bug.
196 On the one hand,
197 .BR sched_setattr ()
198 is being used to set a per-thread attribute.
199 On the other hand, if the process-directed signal is delivered to
200 a thread inside the process other than the one that had a run-time overrun,
201 the application has no way of knowing which thread overran.
204 .I sched_nice
205 This field specifies the nice value to be set when specifying
206 .IR sched_policy
208 .BR SCHED_OTHER
210 .BR SCHED_BATCH .
211 The nice value is a number in the range \-20 (high priority)
212 to +19 (low priority); see
213 .BR sched (7).
215 .I sched_priority
216 This field specifies the static priority to be set when specifying
217 .IR sched_policy
219 .BR SCHED_FIFO
221 .BR SCHED_RR .
222 The allowed range of priorities for these policies can be determined using
223 .BR sched_get_priority_min (2)
225 .BR sched_get_priority_max (2).
226 For other policies, this field must be specified as 0.
228 .I sched_runtime
229 This field specifies the "Runtime" parameter for deadline scheduling.
230 The value is expressed in nanoseconds.
231 This field, and the next two fields,
232 are used only for
233 .BR SCHED_DEADLINE
234 scheduling; for further details, see
235 .BR sched (7).
237 .I sched_deadline
238 This field specifies the "Deadline" parameter for deadline scheduling.
239 The value is expressed in nanoseconds.
241 .I sched_period
242 This field specifies the "Period" parameter for deadline scheduling.
243 The value is expressed in nanoseconds.
246 .I flags
247 argument is provided to allow for future extensions to the interface;
248 in the current implementation it must be specified as 0.
251 .SS sched_getattr()
253 .BR sched_getattr ()
254 system call fetches the scheduling policy and the
255 associated attributes for the thread whose ID is specified in
256 .IR pid .
258 .I pid
259 equals zero,
260 the scheduling policy and attributes of the calling thread
261 will be retrieved.
264 .I size
265 argument should be set to the size of the
266 .I sched_attr
267 structure as known to user space.
268 The value must be at least as large as the size of the initially published
269 .I sched_attr
270 structure, or the call fails with the error
271 .BR EINVAL .
273 The retrieved scheduling attributes are placed in the fields of the
274 .I sched_attr
275 structure pointed to by
276 .IR attr .
277 The kernel sets
278 .I attr.size
279 to the size of its
280 .I sched_attr
281 structure.
283 If the caller-provided
284 .I attr
285 buffer is larger than the kernel's
286 .I sched_attr
287 structure,
288 the additional bytes in the user-space structure are not touched.
289 If the caller-provided structure is smaller than the kernel
290 .I sched_attr
291 structure, the kernel will silently not return any values which would be stored
292 outside the provided space.
293 As with
294 .BR sched_setattr (),
295 these semantics allow for future extensibility of the interface.
298 .I flags
299 argument is provided to allow for future extensions to the interface;
300 in the current implementation it must be specified as 0.
301 .SH RETURN VALUE
302 On success,
303 .BR sched_setattr ()
305 .BR sched_getattr ()
306 return 0.
307 On error, \-1 is returned, and
308 .I errno
309 is set to indicate the error.
310 .SH ERRORS
311 .BR sched_getattr ()
313 .BR sched_setattr ()
314 can both fail for the following reasons:
316 .B EINVAL
317 .I attr
318 is NULL; or
319 .I pid
320 is negative; or
321 .I flags
322 is not zero.
324 .B ESRCH
325 The thread whose ID is
326 .I pid
327 could not be found.
329 In addition,
330 .BR sched_getattr ()
331 can fail for the following reasons:
333 .B E2BIG
334 The buffer specified by
335 .I size
337 .I attr
338 is too small.
340 .B EINVAL
341 .I size
342 is invalid; that is, it is smaller than the initial version of the
343 .I sched_attr
344 structure (48 bytes) or larger than the system page size.
346 In addition,
347 .BR sched_setattr ()
348 can fail for the following reasons:
350 .B E2BIG
351 The buffer specified by
352 .I size
354 .I attr
355 is larger than the kernel structure,
356 and one or more of the excess bytes is nonzero.
358 .B EBUSY
359 .B SCHED_DEADLINE
360 admission control failure, see
361 .BR sched (7).
363 .B EINVAL
364 .I attr.sched_policy
365 is not one of the recognized policies;
366 .I attr.sched_flags
367 contains a flag other than
368 .BR SCHED_FLAG_RESET_ON_FORK ;
370 .I attr.sched_priority
371 is invalid; or
372 .I attr.sched_policy
374 .BR SCHED_DEADLINE
375 and the deadline scheduling parameters in
376 .I attr
377 are invalid.
379 .B EPERM
380 The caller does not have appropriate privileges.
382 .B EPERM
383 The CPU affinity mask of the thread specified by
384 .I pid
385 does not include all CPUs in the system
386 (see
387 .BR sched_setaffinity (2)).
388 .SH VERSIONS
389 These system calls first appeared in Linux 3.14.
390 .\" FIXME . Add glibc version
391 .SH CONFORMING TO
392 These system calls are nonstandard Linux extensions.
393 .SH NOTES
394 Glibc does not provide wrappers for these system calls; call them using
395 .BR syscall (2).
397 .BR sched_setattr ()
398 provides a superset of the functionality of
399 .BR sched_setscheduler (2),
400 .BR sched_setparam (2),
401 .BR nice (2),
402 and (other than the ability to set the priority of all processes
403 belonging to a specified user or all processes in a specified group)
404 .BR setpriority (2).
405 Analogously,
406 .BR sched_getattr ()
407 provides a superset of the functionality of
408 .BR sched_getscheduler (2),
409 .BR sched_getparam (2),
410 and (partially)
411 .BR getpriority (2).
412 .SH BUGS
413 In Linux versions up to
414 .\" FIXME . patch sent to Peter Zijlstra
415 3.15,
416 .BR sched_setattr ()
417 failed with the error
418 .BR EFAULT
419 instead of
420 .BR E2BIG
421 for the case described in ERRORS.
423 In Linux versions up to 5.3,
424 .BR sched_getattr ()
425 failed with the error
426 .BR EFBIG
427 if the in-kernel
428 .IR sched_attr
429 structure was larger than the
430 .IR size
431 passed by user space.
432 .\" In Linux versions up to up 3.15,
433 .\" FIXME . patch from Peter Zijlstra pending
434 .\" .BR sched_setattr ()
435 .\" allowed a negative
436 .\" .I attr.sched_policy
437 .\" value.
438 .SH SEE ALSO
439 .ad l
441 .BR chrt (1),
442 .BR nice (2),
443 .BR sched_get_priority_max (2),
444 .BR sched_get_priority_min (2),
445 .BR sched_getaffinity (2),
446 .BR sched_getparam (2),
447 .BR sched_getscheduler (2),
448 .BR sched_rr_get_interval (2),
449 .BR sched_setaffinity (2),
450 .BR sched_setparam (2),
451 .BR sched_setscheduler (2),
452 .BR sched_yield (2),
453 .BR setpriority (2),
454 .BR pthread_getschedparam (3),
455 .BR pthread_setschedparam (3),
456 .BR pthread_setschedprio (3),
457 .BR capabilities (7),
458 .BR cpuset (7),
459 .BR sched (7)