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