Debian Codegraph @

upstream/debian-unstable/man7/sched.7

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