]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/sched.7
e8960635dba6eac1dcc310bc9a7d72b50c00fd49
[thirdparty/man-pages.git] / man7 / sched.7
1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2014 Peter Zijlstra <peterz@infradead.org>
3 .\" and Copyright (C) 2014 Juri Lelli <juri.lelli@gmail.com>
4 .\" Various pieces from the old sched_setscheduler(2) page
5 .\" Copyright (C) Tom Bjorkholm, Markus Kuhn & David A. Wheeler 1996-1999
6 .\" and Copyright (C) 2007 Carsten Emde <Carsten.Emde@osadl.org>
7 .\" and Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
8 .\"
9 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
10 .\" This is free documentation; you can redistribute it and/or
11 .\" modify it under the terms of the GNU General Public License as
12 .\" published by the Free Software Foundation; either version 2 of
13 .\" the License, or (at your option) any later version.
14 .\"
15 .\" The GNU General Public License's references to "object code"
16 .\" and "executables" are to be interpreted as the output of any
17 .\" document formatting or typesetting system, including
18 .\" intermediate and printed output.
19 .\"
20 .\" This manual is distributed in the hope that it will be useful,
21 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
22 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 .\" GNU General Public License for more details.
24 .\"
25 .\" You should have received a copy of the GNU General Public
26 .\" License along with this manual; if not, see
27 .\" <http://www.gnu.org/licenses/>.
28 .\" %%%LICENSE_END
29 .\"
30 .\" Worth looking at: http://rt.wiki.kernel.org/index.php
31 .\"
32 .TH SCHED 7 2019-03-06 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 sched \- overview of CPU scheduling
35 .SH DESCRIPTION
36 Since Linux 2.6.23, the default scheduler is CFS,
37 the "Completely Fair Scheduler".
38 The CFS scheduler replaced the earlier "O(1)" scheduler.
39 .\"
40 .SS API summary
41 Linux provides the following system calls for controlling
42 the CPU scheduling behavior, policy, and priority of processes
43 (or, more precisely, threads).
44 .TP
45 .BR nice (2)
46 Set a new nice value for the calling thread,
47 and return the new nice value.
48 .TP
49 .BR getpriority (2)
50 Return the nice value of a thread, a process group,
51 or the set of threads owned by a specified user.
52 .TP
53 .BR setpriority (2)
54 Set the nice value of a thread, a process group,
55 or the set of threads owned by a specified user.
56 .TP
57 .BR sched_setscheduler (2)
58 Set the scheduling policy and parameters of a specified thread.
59 .TP
60 .BR sched_getscheduler (2)
61 Return the scheduling policy of a specified thread.
62 .TP
63 .BR sched_setparam (2)
64 Set the scheduling parameters of a specified thread.
65 .TP
66 .BR sched_getparam (2)
67 Fetch the scheduling parameters of a specified thread.
68 .TP
69 .BR sched_get_priority_max (2)
70 Return the maximum priority available in a specified scheduling policy.
71 .TP
72 .BR sched_get_priority_min (2)
73 Return the minimum priority available in a specified scheduling policy.
74 .TP
75 .BR sched_rr_get_interval (2)
76 Fetch the quantum used for threads that are scheduled under
77 the "round-robin" scheduling policy.
78 .TP
79 .BR sched_yield (2)
80 Cause the caller to relinquish the CPU,
81 so that some other thread be executed.
82 .TP
83 .BR sched_setaffinity (2)
84 (Linux-specific)
85 Set the CPU affinity of a specified thread.
86 .TP
87 .BR sched_getaffinity (2)
88 (Linux-specific)
89 Get the CPU affinity of a specified thread.
90 .TP
91 .BR sched_setattr (2)
92 Set the scheduling policy and parameters of a specified thread.
93 This (Linux-specific) system call provides a superset of the functionality of
94 .BR sched_setscheduler (2)
95 and
96 .BR sched_setparam (2).
97 .TP
98 .BR sched_getattr (2)
99 Fetch the scheduling policy and parameters of a specified thread.
100 This (Linux-specific) system call provides a superset of the functionality of
101 .BR sched_getscheduler (2)
102 and
103 .BR sched_getparam (2).
104 .\"
105 .SS Scheduling policies
106 The scheduler is the kernel component that decides which runnable thread
107 will be executed by the CPU next.
108 Each thread has an associated scheduling policy and a \fIstatic\fP
109 scheduling priority,
110 .IR sched_priority .
111 The scheduler makes its decisions based on knowledge of the scheduling
112 policy and static priority of all threads on the system.
113 .PP
114 For threads scheduled under one of the normal scheduling policies
115 (\fBSCHED_OTHER\fP, \fBSCHED_IDLE\fP, \fBSCHED_BATCH\fP),
116 \fIsched_priority\fP is not used in scheduling
117 decisions (it must be specified as 0).
118 .PP
119 Processes scheduled under one of the real-time policies
120 (\fBSCHED_FIFO\fP, \fBSCHED_RR\fP) have a
121 \fIsched_priority\fP value in the range 1 (low) to 99 (high).
122 (As the numbers imply, real-time threads always have higher priority
123 than normal threads.)
124 Note well: POSIX.1 requires an implementation to support only a
125 minimum 32 distinct priority levels for the real-time policies,
126 and some systems supply just this minimum.
127 Portable programs should use
128 .BR sched_get_priority_min (2)
129 and
130 .BR sched_get_priority_max (2)
131 to find the range of priorities supported for a particular policy.
132 .PP
133 Conceptually, the scheduler maintains a list of runnable
134 threads for each possible \fIsched_priority\fP value.
135 In order to determine which thread runs next, the scheduler looks for
136 the nonempty list with the highest static priority and selects the
137 thread at the head of this list.
138 .PP
139 A thread's scheduling policy determines
140 where it will be inserted into the list of threads
141 with equal static priority and how it will move inside this list.
142 .PP
143 All scheduling is preemptive: if a thread with a higher static
144 priority becomes ready to run, the currently running thread
145 will be preempted and
146 returned to the wait list for its static priority level.
147 The scheduling policy determines the
148 ordering only within the list of runnable threads with equal static
149 priority.
150 .SS SCHED_FIFO: First in-first out scheduling
151 \fBSCHED_FIFO\fP can be used only with static priorities higher than
152 0, which means that when a \fBSCHED_FIFO\fP threads becomes runnable,
153 it will always immediately preempt any currently running
154 \fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP thread.
155 \fBSCHED_FIFO\fP is a simple scheduling
156 algorithm without time slicing.
157 For threads scheduled under the
158 \fBSCHED_FIFO\fP policy, the following rules apply:
159 .IP 1) 3
160 A running \fBSCHED_FIFO\fP thread that has been preempted by another thread of
161 higher priority will stay at the head of the list for its priority and
162 will resume execution as soon as all threads of higher priority are
163 blocked again.
164 .IP 2)
165 When a blocked \fBSCHED_FIFO\fP thread becomes runnable, it
166 will be inserted at the end of the list for its priority.
167 .IP 3)
168 If a call to
169 .BR sched_setscheduler (2),
170 .BR sched_setparam (2),
171 .BR sched_setattr (2),
172 .BR pthread_setschedparam (3),
173 or
174 .BR pthread_setschedprio (3)
175 changes the priority of the running or runnable
176 .B SCHED_FIFO
177 thread identified by
178 .I pid
179 the effect on the thread's position in the list depends on
180 the direction of the change to threads priority:
181 .RS
182 .IP \(bu 3
183 If the thread's priority is raised,
184 it is placed at the end of the list for its new priority.
185 As a consequence,
186 it may preempt a currently running thread with the same priority.
187 .IP \(bu
188 If the thread's priority is unchanged,
189 its position in the run list is unchanged.
190 .IP \(bu
191 If the thread's priority is lowered,
192 it is placed at the front of the list for its new priority.
193 .RE
194 .IP
195 According to POSIX.1-2008,
196 changes to a thread's priority (or policy) using any mechanism other than
197 .BR pthread_setschedprio (3)
198 should result in the thread being placed at the end of
199 the list for its priority.
200 .\" In 2.2.x and 2.4.x, the thread is placed at the front of the queue
201 .\" In 2.0.x, the Right Thing happened: the thread went to the back -- MTK
202 .IP 4)
203 A thread calling
204 .BR sched_yield (2)
205 will be put at the end of the list.
206 .PP
207 No other events will move a thread
208 scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
209 runnable threads with equal static priority.
210 .PP
211 A \fBSCHED_FIFO\fP
212 thread runs until either it is blocked by an I/O request, it is
213 preempted by a higher priority thread, or it calls
214 .BR sched_yield (2).
215 .SS SCHED_RR: Round-robin scheduling
216 \fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
217 Everything
218 described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
219 except that each thread is allowed to run only for a maximum time
220 quantum.
221 If a \fBSCHED_RR\fP thread has been running for a time
222 period equal to or longer than the time quantum, it will be put at the
223 end of the list for its priority.
224 A \fBSCHED_RR\fP thread that has
225 been preempted by a higher priority thread and subsequently resumes
226 execution as a running thread will complete the unexpired portion of
227 its round-robin time quantum.
228 The length of the time quantum can be
229 retrieved using
230 .BR sched_rr_get_interval (2).
231 .\" On Linux 2.4, the length of the RR interval is influenced
232 .\" by the process nice value -- MTK
233 .\"
234 .SS SCHED_DEADLINE: Sporadic task model deadline scheduling
235 Since version 3.14, Linux provides a deadline scheduling policy
236 .RB ( SCHED_DEADLINE ).
237 This policy is currently implemented using
238 GEDF (Global Earliest Deadline First)
239 in conjunction with CBS (Constant Bandwidth Server).
240 To set and fetch this policy and associated attributes,
241 one must use the Linux-specific
242 .BR sched_setattr (2)
243 and
244 .BR sched_getattr (2)
245 system calls.
246 .PP
247 A sporadic task is one that has a sequence of jobs, where each
248 job is activated at most once per period.
249 Each job also has a
250 .IR "relative deadline" ,
251 before which it should finish execution, and a
252 .IR "computation time" ,
253 which is the CPU time necessary for executing the job.
254 The moment when a task wakes up
255 because a new job has to be executed is called the
256 .IR "arrival time"
257 (also referred to as the request time or release time).
258 The
259 .IR "start time"
260 is the time at which a task starts its execution.
261 The
262 .I "absolute deadline"
263 is thus obtained by adding the relative deadline to the arrival time.
264 .PP
265 The following diagram clarifies these terms:
266 .PP
267 .in +4n
268 .EX
269 arrival/wakeup absolute deadline
270 | start time |
271 | | |
272 v v v
273 -----x--------xooooooooooooooooo--------x--------x---
274 |<- comp. time ->|
275 |<------- relative deadline ------>|
276 |<-------------- period ------------------->|
277 .EE
278 .in
279 .PP
280 When setting a
281 .B SCHED_DEADLINE
282 policy for a thread using
283 .BR sched_setattr (2),
284 one can specify three parameters:
285 .IR Runtime ,
286 .IR Deadline ,
287 and
288 .IR Period .
289 These parameters do not necessarily correspond to the aforementioned terms:
290 usual practice is to set Runtime to something bigger than the average
291 computation time (or worst-case execution time for hard real-time tasks),
292 Deadline to the relative deadline, and Period to the period of the task.
293 Thus, for
294 .BR SCHED_DEADLINE
295 scheduling, we have:
296 .PP
297 .in +4n
298 .EX
299 arrival/wakeup absolute deadline
300 | start time |
301 | | |
302 v v v
303 -----x--------xooooooooooooooooo--------x--------x---
304 |<-- Runtime ------->|
305 |<----------- Deadline ----------->|
306 |<-------------- Period ------------------->|
307 .EE
308 .in
309 .PP
310 The three deadline-scheduling parameters correspond to the
311 .IR sched_runtime ,
312 .IR sched_deadline ,
313 and
314 .IR sched_period
315 fields of the
316 .I sched_attr
317 structure; see
318 .BR sched_setattr (2).
319 These fields express values in nanoseconds.
320 .\" FIXME It looks as though specifying sched_period as 0 means
321 .\" "make sched_period the same as sched_deadline".
322 .\" This needs to be documented.
323 If
324 .IR sched_period
325 is specified as 0, then it is made the same as
326 .IR sched_deadline .
327 .PP
328 The kernel requires that:
329 .PP
330 sched_runtime <= sched_deadline <= sched_period
331 .PP
332 .\" See __checkparam_dl in kernel/sched/core.c
333 In addition, under the current implementation,
334 all of the parameter values must be at least 1024
335 (i.e., just over one microsecond,
336 which is the resolution of the implementation), and less than 2^63.
337 If any of these checks fails,
338 .BR sched_setattr (2)
339 fails with the error
340 .BR EINVAL .
341 .PP
342 The CBS guarantees non-interference between tasks, by throttling
343 threads that attempt to over-run their specified Runtime.
344 .PP
345 To ensure deadline scheduling guarantees,
346 the kernel must prevent situations where the set of
347 .B SCHED_DEADLINE
348 threads is not feasible (schedulable) within the given constraints.
349 The kernel thus performs an admittance test when setting or changing
350 .B SCHED_DEADLINE
351 policy and attributes.
352 This admission test calculates whether the change is feasible;
353 if it is not,
354 .BR sched_setattr (2)
355 fails with the error
356 .BR EBUSY .
357 .PP
358 For example, it is required (but not necessarily sufficient) for
359 the total utilization to be less than or equal to the total number of
360 CPUs available, where, since each thread can maximally run for
361 Runtime per Period, that thread's utilization is its
362 Runtime divided by its Period.
363 .PP
364 In order to fulfill the guarantees that are made when
365 a thread is admitted to the
366 .BR SCHED_DEADLINE
367 policy,
368 .BR SCHED_DEADLINE
369 threads are the highest priority (user controllable) threads in the
370 system; if any
371 .BR SCHED_DEADLINE
372 thread is runnable,
373 it will preempt any thread scheduled under one of the other policies.
374 .PP
375 A call to
376 .BR fork (2)
377 by a thread scheduled under the
378 .B SCHED_DEADLINE
379 policy fails with the error
380 .BR EAGAIN ,
381 unless the thread has its reset-on-fork flag set (see below).
382 .PP
383 A
384 .B SCHED_DEADLINE
385 thread that calls
386 .BR sched_yield (2)
387 will yield the current job and wait for a new period to begin.
388 .\"
389 .\" FIXME Calling sched_getparam() on a SCHED_DEADLINE thread
390 .\" fails with EINVAL, but sched_getscheduler() succeeds.
391 .\" Is that intended? (Why?)
392 .\"
393 .SS SCHED_OTHER: Default Linux time-sharing scheduling
394 \fBSCHED_OTHER\fP can be used at only static priority 0
395 (i.e., threads under real-time policies always have priority over
396 .B SCHED_OTHER
397 processes).
398 \fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
399 intended for all threads that do not require the special
400 real-time mechanisms.
401 .PP
402 The thread to run is chosen from the static
403 priority 0 list based on a \fIdynamic\fP priority that is determined only
404 inside this list.
405 The dynamic priority is based on the nice value (see below)
406 and is increased for each time quantum the thread is ready to run,
407 but denied to run by the scheduler.
408 This ensures fair progress among all \fBSCHED_OTHER\fP threads.
409 .PP
410 In the Linux kernel source code, the
411 .B SCHED_OTHER
412 policy is actually named
413 .BR SCHED_NORMAL .
414 .\"
415 .SS The nice value
416 The nice value is an attribute
417 that can be used to influence the CPU scheduler to
418 favor or disfavor a process in scheduling decisions.
419 It affects the scheduling of
420 .BR SCHED_OTHER
421 and
422 .BR SCHED_BATCH
423 (see below) processes.
424 The nice value can be modified using
425 .BR nice (2),
426 .BR setpriority (2),
427 or
428 .BR sched_setattr (2).
429 .PP
430 According to POSIX.1, the nice value is a per-process attribute;
431 that is, the threads in a process should share a nice value.
432 However, on Linux, the nice value is a per-thread attribute:
433 different threads in the same process may have different nice values.
434 .PP
435 The range of the nice value
436 varies across UNIX systems.
437 On modern Linux, the range is \-20 (high priority) to +19 (low priority).
438 On some other systems, the range is \-20..20.
439 Very early Linux kernels (Before Linux 2.0) had the range \-infinity..15.
440 .\" Linux before 1.3.36 had \-infinity..15.
441 .\" Since kernel 1.3.43, Linux has the range \-20..19.
442 .PP
443 The degree to which the nice value affects the relative scheduling of
444 .BR SCHED_OTHER
445 processes likewise varies across UNIX systems and
446 across Linux kernel versions.
447 .PP
448 With the advent of the CFS scheduler in kernel 2.6.23,
449 Linux adopted an algorithm that causes
450 relative differences in nice values to have a much stronger effect.
451 In the current implementation, each unit of difference in the
452 nice values of two processes results in a factor of 1.25
453 in the degree to which the scheduler favors the higher priority process.
454 This causes very low nice values (+19) to truly provide little CPU
455 to a process whenever there is any other
456 higher priority load on the system,
457 and makes high nice values (\-20) deliver most of the CPU to applications
458 that require it (e.g., some audio applications).
459 .PP
460 On Linux, the
461 .BR RLIMIT_NICE
462 resource limit can be used to define a limit to which
463 an unprivileged process's nice value can be raised; see
464 .BR setrlimit (2)
465 for details.
466 .PP
467 For further details on the nice value, see the subsections on
468 the autogroup feature and group scheduling, below.
469 .\"
470 .SS SCHED_BATCH: Scheduling batch processes
471 (Since Linux 2.6.16.)
472 \fBSCHED_BATCH\fP can be used only at static priority 0.
473 This policy is similar to \fBSCHED_OTHER\fP in that it schedules
474 the thread according to its dynamic priority
475 (based on the nice value).
476 The difference is that this policy
477 will cause the scheduler to always assume
478 that the thread is CPU-intensive.
479 Consequently, the scheduler will apply a small scheduling
480 penalty with respect to wakeup behavior,
481 so that this thread is mildly disfavored in scheduling decisions.
482 .PP
483 .\" The following paragraph is drawn largely from the text that
484 .\" accompanied Ingo Molnar's patch for the implementation of
485 .\" SCHED_BATCH.
486 .\" commit b0a9499c3dd50d333e2aedb7e894873c58da3785
487 This policy is useful for workloads that are noninteractive,
488 but do not want to lower their nice value,
489 and for workloads that want a deterministic scheduling policy without
490 interactivity causing extra preemptions (between the workload's tasks).
491 .\"
492 .SS SCHED_IDLE: Scheduling very low priority jobs
493 (Since Linux 2.6.23.)
494 \fBSCHED_IDLE\fP can be used only at static priority 0;
495 the process nice value has no influence for this policy.
496 .PP
497 This policy is intended for running jobs at extremely low
498 priority (lower even than a +19 nice value with the
499 .B SCHED_OTHER
500 or
501 .B SCHED_BATCH
502 policies).
503 .\"
504 .SS Resetting scheduling policy for child processes
505 Each thread has a reset-on-fork scheduling flag.
506 When this flag is set, children created by
507 .BR fork (2)
508 do not inherit privileged scheduling policies.
509 The reset-on-fork flag can be set by either:
510 .IP * 3
511 ORing the
512 .B SCHED_RESET_ON_FORK
513 flag into the
514 .I policy
515 argument when calling
516 .BR sched_setscheduler (2)
517 (since Linux 2.6.32);
518 or
519 .IP *
520 specifying the
521 .B SCHED_FLAG_RESET_ON_FORK
522 flag in
523 .IR attr.sched_flags
524 when calling
525 .BR sched_setattr (2).
526 .PP
527 Note that the constants used with these two APIs have different names.
528 The state of the reset-on-fork flag can analogously be retrieved using
529 .BR sched_getscheduler (2)
530 and
531 .BR sched_getattr (2).
532 .PP
533 The reset-on-fork feature is intended for media-playback applications,
534 and can be used to prevent applications evading the
535 .BR RLIMIT_RTTIME
536 resource limit (see
537 .BR getrlimit (2))
538 by creating multiple child processes.
539 .PP
540 More precisely, if the reset-on-fork flag is set,
541 the following rules apply for subsequently created children:
542 .IP * 3
543 If the calling thread has a scheduling policy of
544 .B SCHED_FIFO
545 or
546 .BR SCHED_RR ,
547 the policy is reset to
548 .BR SCHED_OTHER
549 in child processes.
550 .IP *
551 If the calling process has a negative nice value,
552 the nice value is reset to zero in child processes.
553 .PP
554 After the reset-on-fork flag has been enabled,
555 it can be reset only if the thread has the
556 .BR CAP_SYS_NICE
557 capability.
558 This flag is disabled in child processes created by
559 .BR fork (2).
560 .\"
561 .SS Privileges and resource limits
562 In Linux kernels before 2.6.12, only privileged
563 .RB ( CAP_SYS_NICE )
564 threads can set a nonzero static priority (i.e., set a real-time
565 scheduling policy).
566 The only change that an unprivileged thread can make is to set the
567 .B SCHED_OTHER
568 policy, and this can be done only if the effective user ID of the caller
569 matches the real or effective user ID of the target thread
570 (i.e., the thread specified by
571 .IR pid )
572 whose policy is being changed.
573 .PP
574 A thread must be privileged
575 .RB ( CAP_SYS_NICE )
576 in order to set or modify a
577 .BR SCHED_DEADLINE
578 policy.
579 .PP
580 Since Linux 2.6.12, the
581 .B RLIMIT_RTPRIO
582 resource limit defines a ceiling on an unprivileged thread's
583 static priority for the
584 .B SCHED_RR
585 and
586 .B SCHED_FIFO
587 policies.
588 The rules for changing scheduling policy and priority are as follows:
589 .IP * 3
590 If an unprivileged thread has a nonzero
591 .B RLIMIT_RTPRIO
592 soft limit, then it can change its scheduling policy and priority,
593 subject to the restriction that the priority cannot be set to a
594 value higher than the maximum of its current priority and its
595 .B RLIMIT_RTPRIO
596 soft limit.
597 .IP *
598 If the
599 .B RLIMIT_RTPRIO
600 soft limit is 0, then the only permitted changes are to lower the priority,
601 or to switch to a non-real-time policy.
602 .IP *
603 Subject to the same rules,
604 another unprivileged thread can also make these changes,
605 as long as the effective user ID of the thread making the change
606 matches the real or effective user ID of the target thread.
607 .IP *
608 Special rules apply for the
609 .BR SCHED_IDLE
610 policy.
611 In Linux kernels before 2.6.39,
612 an unprivileged thread operating under this policy cannot
613 change its policy, regardless of the value of its
614 .BR RLIMIT_RTPRIO
615 resource limit.
616 In Linux kernels since 2.6.39,
617 .\" commit c02aa73b1d18e43cfd79c2f193b225e84ca497c8
618 an unprivileged thread can switch to either the
619 .BR SCHED_BATCH
620 or the
621 .BR SCHED_OTHER
622 policy so long as its nice value falls within the range permitted by its
623 .BR RLIMIT_NICE
624 resource limit (see
625 .BR getrlimit (2)).
626 .PP
627 Privileged
628 .RB ( CAP_SYS_NICE )
629 threads ignore the
630 .B RLIMIT_RTPRIO
631 limit; as with older kernels,
632 they can make arbitrary changes to scheduling policy and priority.
633 See
634 .BR getrlimit (2)
635 for further information on
636 .BR RLIMIT_RTPRIO .
637 .SS Limiting the CPU usage of real-time and deadline processes
638 A nonblocking infinite loop in a thread scheduled under the
639 .BR SCHED_FIFO ,
640 .BR SCHED_RR ,
641 or
642 .BR SCHED_DEADLINE
643 policy can potentially block all other threads from accessing
644 the CPU forever.
645 Prior to Linux 2.6.25, the only way of preventing a runaway real-time
646 process from freezing the system was to run (at the console)
647 a shell scheduled under a higher static priority than the tested application.
648 This allows an emergency kill of tested
649 real-time applications that do not block or terminate as expected.
650 .PP
651 Since Linux 2.6.25, there are other techniques for dealing with runaway
652 real-time and deadline processes.
653 One of these is to use the
654 .BR RLIMIT_RTTIME
655 resource limit to set a ceiling on the CPU time that
656 a real-time process may consume.
657 See
658 .BR getrlimit (2)
659 for details.
660 .PP
661 Since version 2.6.25, Linux also provides two
662 .I /proc
663 files that can be used to reserve a certain amount of CPU time
664 to be used by non-real-time processes.
665 Reserving CPU time in this fashion allows some CPU time to be
666 allocated to (say) a root shell that can be used to kill a runaway process.
667 Both of these files specify time values in microseconds:
668 .TP
669 .IR /proc/sys/kernel/sched_rt_period_us
670 This file specifies a scheduling period that is equivalent to
671 100% CPU bandwidth.
672 The value in this file can range from 1 to
673 .BR INT_MAX ,
674 giving an operating range of 1 microsecond to around 35 minutes.
675 The default value in this file is 1,000,000 (1 second).
676 .TP
677 .IR /proc/sys/kernel/sched_rt_runtime_us
678 The value in this file specifies how much of the "period" time
679 can be used by all real-time and deadline scheduled processes
680 on the system.
681 The value in this file can range from \-1 to
682 .BR INT_MAX \-1.
683 Specifying \-1 makes the run time the same as the period;
684 that is, no CPU time is set aside for non-real-time processes
685 (which was the Linux behavior before kernel 2.6.25).
686 The default value in this file is 950,000 (0.95 seconds),
687 meaning that 5% of the CPU time is reserved for processes that
688 don't run under a real-time or deadline scheduling policy.
689 .PP
690 .SS Response time
691 A blocked high priority thread waiting for I/O has a certain
692 response time before it is scheduled again.
693 The device driver writer
694 can greatly reduce this response time by using a "slow interrupt"
695 interrupt handler.
696 .\" as described in
697 .\" .BR request_irq (9).
698 .SS Miscellaneous
699 Child processes inherit the scheduling policy and parameters across a
700 .BR fork (2).
701 The scheduling policy and parameters are preserved across
702 .BR execve (2).
703 .PP
704 Memory locking is usually needed for real-time processes to avoid
705 paging delays; this can be done with
706 .BR mlock (2)
707 or
708 .BR mlockall (2).
709 .\"
710 .SS The autogroup feature
711 .\" commit 5091faa449ee0b7d73bc296a93bca9540fc51d0a
712 Since Linux 2.6.38,
713 the kernel provides a feature known as autogrouping to improve interactive
714 desktop performance in the face of multiprocess, CPU-intensive
715 workloads such as building the Linux kernel with large numbers of
716 parallel build processes (i.e., the
717 .BR make (1)
718 .BR \-j
719 flag).
720 .PP
721 This feature operates in conjunction with the
722 CFS scheduler and requires a kernel that is configured with
723 .BR CONFIG_SCHED_AUTOGROUP .
724 On a running system, this feature is enabled or disabled via the file
725 .IR /proc/sys/kernel/sched_autogroup_enabled ;
726 a value of 0 disables the feature, while a value of 1 enables it.
727 The default value in this file is 1, unless the kernel was booted with the
728 .IR noautogroup
729 parameter.
730 .PP
731 A new autogroup is created when a new session is created via
732 .BR setsid (2);
733 this happens, for example, when a new terminal window is started.
734 A new process created by
735 .BR fork (2)
736 inherits its parent's autogroup membership.
737 Thus, all of the processes in a session are members of the same autogroup.
738 An autogroup is automatically destroyed when the last process
739 in the group terminates.
740 .PP
741 When autogrouping is enabled, all of the members of an autogroup
742 are placed in the same kernel scheduler "task group".
743 The CFS scheduler employs an algorithm that equalizes the
744 distribution of CPU cycles across task groups.
745 The benefits of this for interactive desktop performance
746 can be described via the following example.
747 .PP
748 Suppose that there are two autogroups competing for the same CPU
749 (i.e., presume either a single CPU system or the use of
750 .BR taskset (1)
751 to confine all the processes to the same CPU on an SMP system).
752 The first group contains ten CPU-bound processes from
753 a kernel build started with
754 .IR "make\ \-j10" .
755 The other contains a single CPU-bound process: a video player.
756 The effect of autogrouping is that the two groups will
757 each receive half of the CPU cycles.
758 That is, the video player will receive 50% of the CPU cycles,
759 rather than just 9% of the cycles,
760 which would likely lead to degraded video playback.
761 The situation on an SMP system is more complex,
762 .\" Mike Galbraith, 25 Nov 2016:
763 .\" I'd say something more wishy-washy here, like cycles are
764 .\" distributed fairly across groups and leave it at that, as your
765 .\" detailed example is incorrect due to SMP fairness (which I don't
766 .\" like much because [very unlikely] worst case scenario
767 .\" renders a box sized group incapable of utilizing more that
768 .\" a single CPU total). For example, if a group of NR_CPUS
769 .\" size competes with a singleton, load balancing will try to give
770 .\" the singleton a full CPU of its very own. If groups intersect for
771 .\" whatever reason on say my quad lappy, distribution is 80/20 in
772 .\" favor of the singleton.
773 but the general effect is the same:
774 the scheduler distributes CPU cycles across task groups such that
775 an autogroup that contains a large number of CPU-bound processes
776 does not end up hogging CPU cycles at the expense of the other
777 jobs on the system.
778 .PP
779 A process's autogroup (task group) membership can be viewed via the file
780 .IR /proc/[pid]/autogroup :
781 .PP
782 .in +4n
783 .EX
784 $ \fBcat /proc/1/autogroup\fP
785 /autogroup-1 nice 0
786 .EE
787 .in
788 .PP
789 This file can also be used to modify the CPU bandwidth allocated
790 to an autogroup.
791 This is done by writing a number in the "nice" range to the file
792 to set the autogroup's nice value.
793 The allowed range is from +19 (low priority) to \-20 (high priority).
794 (Writing values outside of this range causes
795 .BR write (2)
796 to fail with the error
797 .BR EINVAL .)
798 .\" FIXME .
799 .\" Because of a bug introduced in Linux 4.7
800 .\" (commit 2159197d66770ec01f75c93fb11dc66df81fd45b made changes
801 .\" that exposed the fact that autogroup didn't call scale_load()),
802 .\" it happened that *all* values in this range caused a task group
803 .\" to be further disfavored by the scheduler, with \-20 resulting
804 .\" in the scheduler mildly disfavoring the task group and +19 greatly
805 .\" disfavoring it.
806 .\"
807 .\" A patch was posted on 23 Nov 2016
808 .\" ("sched/autogroup: Fix 64bit kernel nice adjustment";
809 .\" check later to see in which kernel version it lands.
810 .PP
811 The autogroup nice setting has the same meaning as the process nice value,
812 but applies to distribution of CPU cycles to the autogroup as a whole,
813 based on the relative nice values of other autogroups.
814 For a process inside an autogroup, the CPU cycles that it receives
815 will be a product of the autogroup's nice value
816 (compared to other autogroups)
817 and the process's nice value
818 (compared to other processes in the same autogroup.
819 .PP
820 The use of the
821 .BR cgroups (7)
822 CPU controller to place processes in cgroups other than the
823 root CPU cgroup overrides the effect of autogrouping.
824 .PP
825 The autogroup feature groups only processes scheduled under
826 non-real-time policies
827 .RB ( SCHED_OTHER ,
828 .BR SCHED_BATCH ,
829 and
830 .BR SCHED_IDLE ).
831 It does not group processes scheduled under real-time and
832 deadline policies.
833 Those processes are scheduled according to the rules described earlier.
834 .\"
835 .SS The nice value and group scheduling
836 When scheduling non-real-time processes (i.e., those scheduled under the
837 .BR SCHED_OTHER ,
838 .BR SCHED_BATCH ,
839 and
840 .BR SCHED_IDLE
841 policies), the CFS scheduler employs a technique known as "group scheduling",
842 if the kernel was configured with the
843 .BR CONFIG_FAIR_GROUP_SCHED
844 option (which is typical).
845 .PP
846 Under group scheduling, threads are scheduled in "task groups".
847 Task groups have a hierarchical relationship,
848 rooted under the initial task group on the system,
849 known as the "root task group".
850 Task groups are formed in the following circumstances:
851 .IP * 3
852 All of the threads in a CPU cgroup form a task group.
853 The parent of this task group is the task group of the
854 corresponding parent cgroup.
855 .IP *
856 If autogrouping is enabled,
857 then all of the threads that are (implicitly) placed in an autogroup
858 (i.e., the same session, as created by
859 .BR setsid (2))
860 form a task group.
861 Each new autogroup is thus a separate task group.
862 The root task group is the parent of all such autogroups.
863 .IP *
864 If autogrouping is enabled, then the root task group consists of
865 all processes in the root CPU cgroup that were not
866 otherwise implicitly placed into a new autogroup.
867 .IP *
868 If autogrouping is disabled, then the root task group consists of
869 all processes in the root CPU cgroup.
870 .IP *
871 If group scheduling was disabled (i.e., the kernel was configured without
872 .BR CONFIG_FAIR_GROUP_SCHED ),
873 then all of the processes on the system are notionally placed
874 in a single task group.
875 .PP
876 Under group scheduling,
877 a thread's nice value has an effect for scheduling decisions
878 .IR "only relative to other threads in the same task group" .
879 This has some surprising consequences in terms of the traditional semantics
880 of the nice value on UNIX systems.
881 In particular, if autogrouping
882 is enabled (which is the default in various distributions), then employing
883 .BR setpriority (2)
884 or
885 .BR nice (1)
886 on a process has an effect only for scheduling relative
887 to other processes executed in the same session
888 (typically: the same terminal window).
889 .PP
890 Conversely, for two processes that are (for example)
891 the sole CPU-bound processes in different sessions
892 (e.g., different terminal windows,
893 each of whose jobs are tied to different autogroups),
894 .IR "modifying the nice value of the process in one of the sessions"
895 .IR "has no effect"
896 in terms of the scheduler's decisions relative to the
897 process in the other session.
898 .\" More succinctly: the nice(1) command is in many cases a no-op since
899 .\" Linux 2.6.38.
900 .\"
901 A possibly useful workaround here is to use a command such as
902 the following to modify the autogroup nice value for
903 .I all
904 of the processes in a terminal session:
905 .PP
906 .in +4n
907 .EX
908 $ \fBecho 10 > /proc/self/autogroup\fP
909 .EE
910 .in
911 .SS Real-time features in the mainline Linux kernel
912 .\" FIXME . Probably this text will need some minor tweaking
913 .\" ask Carsten Emde about this.
914 Since kernel version 2.6.18, Linux is gradually
915 becoming equipped with real-time capabilities,
916 most of which are derived from the former
917 .I realtime-preempt
918 patch set.
919 Until the patches have been completely merged into the
920 mainline kernel,
921 they must be installed to achieve the best real-time performance.
922 These patches are named:
923 .PP
924 .in +4n
925 .EX
926 patch-\fIkernelversion\fP-rt\fIpatchversion\fP
927 .EE
928 .in
929 .PP
930 and can be downloaded from
931 .UR http://www.kernel.org\:/pub\:/linux\:/kernel\:/projects\:/rt/
932 .UE .
933 .PP
934 Without the patches and prior to their full inclusion into the mainline
935 kernel, the kernel configuration offers only the three preemption classes
936 .BR CONFIG_PREEMPT_NONE ,
937 .BR CONFIG_PREEMPT_VOLUNTARY ,
938 and
939 .B CONFIG_PREEMPT_DESKTOP
940 which respectively provide no, some, and considerable
941 reduction of the worst-case scheduling latency.
942 .PP
943 With the patches applied or after their full inclusion into the mainline
944 kernel, the additional configuration item
945 .B CONFIG_PREEMPT_RT
946 becomes available.
947 If this is selected, Linux is transformed into a regular
948 real-time operating system.
949 The FIFO and RR scheduling policies are then used to run a thread
950 with true real-time priority and a minimum worst-case scheduling latency.
951 .SH NOTES
952 The
953 .BR cgroups (7)
954 CPU controller can be used to limit the CPU consumption of
955 groups of processes.
956 .PP
957 Originally, Standard Linux was intended as a general-purpose operating
958 system being able to handle background processes, interactive
959 applications, and less demanding real-time applications (applications that
960 need to usually meet timing deadlines).
961 Although the Linux kernel 2.6
962 allowed for kernel preemption and the newly introduced O(1) scheduler
963 ensures that the time needed to schedule is fixed and deterministic
964 irrespective of the number of active tasks, true real-time computing
965 was not possible up to kernel version 2.6.17.
966 .SH SEE ALSO
967 .ad l
968 .nh
969 .BR chcpu (1),
970 .BR chrt (1),
971 .BR lscpu (1),
972 .BR ps (1),
973 .BR taskset (1),
974 .BR top (1),
975 .BR getpriority (2),
976 .BR mlock (2),
977 .BR mlockall (2),
978 .BR munlock (2),
979 .BR munlockall (2),
980 .BR nice (2),
981 .BR sched_get_priority_max (2),
982 .BR sched_get_priority_min (2),
983 .BR sched_getaffinity (2),
984 .BR sched_getparam (2),
985 .BR sched_getscheduler (2),
986 .BR sched_rr_get_interval (2),
987 .BR sched_setaffinity (2),
988 .BR sched_setparam (2),
989 .BR sched_setscheduler (2),
990 .BR sched_yield (2),
991 .BR setpriority (2),
992 .BR pthread_getschedparam (3),
993 .BR pthread_getaffinity_np (3),
994 .BR pthread_setaffinity_np (3),
995 .BR sched_getcpu (3),
996 .BR capabilities (7),
997 .BR cpuset (7)
998 .ad
999 .PP
1000 .I Programming for the real world \- POSIX.4
1001 by Bill O.\& Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0.
1002 .PP
1003 The Linux kernel source files
1004 .IR Documentation/scheduler/sched-deadline.txt ,
1005 .IR Documentation/scheduler/sched-rt-group.txt ,
1006 .IR Documentation/scheduler/sched-design-CFS.txt ,
1007 and
1008 .IR Documentation/scheduler/sched-nice-design.txt