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