]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/sched.7
sched.7: Add a subsection on group scheduling
[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.\"
b8efb414 32.TH SCHED 7 2016-10-08 "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
MK
112policy and static priority of all threads on the system.
113
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).
118
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.
132
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.
138
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.
142
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:
159.IP * 3
160A \fBSCHED_FIFO\fP thread that has been preempted by another thread of
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.
164.IP *
165When a \fBSCHED_FIFO\fP thread becomes runnable, it
166will be inserted at the end of the list for its priority.
167.IP *
168A call to
4c2eb0c2
MK
169.BR sched_setscheduler (2),
170.BR sched_setparam (2),
59c06be3 171or
4c2eb0c2 172.BR sched_setattr (2)
59c06be3
MK
173will put the
174\fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) thread identified by
175\fIpid\fP at the start of the list if it was runnable.
176As a consequence, it may preempt the currently running thread if
177it has the same priority.
cc401eea 178(POSIX.1 specifies that the thread should go to the end
59c06be3
MK
179of the list.)
180.\" In 2.2.x and 2.4.x, the thread is placed at the front of the queue
181.\" In 2.0.x, the Right Thing happened: the thread went to the back -- MTK
182.IP *
183A thread calling
184.BR sched_yield (2)
185will be put at the end of the list.
186.PP
187No other events will move a thread
188scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
189runnable threads with equal static priority.
190
191A \fBSCHED_FIFO\fP
192thread runs until either it is blocked by an I/O request, it is
193preempted by a higher priority thread, or it calls
194.BR sched_yield (2).
195.SS SCHED_RR: Round-robin scheduling
196\fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
197Everything
198described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
199except that each thread is allowed to run only for a maximum time
200quantum.
201If a \fBSCHED_RR\fP thread has been running for a time
202period equal to or longer than the time quantum, it will be put at the
203end of the list for its priority.
204A \fBSCHED_RR\fP thread that has
205been preempted by a higher priority thread and subsequently resumes
206execution as a running thread will complete the unexpired portion of
207its round-robin time quantum.
208The length of the time quantum can be
209retrieved using
210.BR sched_rr_get_interval (2).
211.\" On Linux 2.4, the length of the RR interval is influenced
212.\" by the process nice value -- MTK
213.\"
7a0d1838 214.SS SCHED_DEADLINE: Sporadic task model deadline scheduling
9cc1fa25
MK
215Since version 3.14, Linux provides a deadline scheduling policy
216.RB ( SCHED_DEADLINE ).
217This policy is currently implemented using
218GEDF (Global Earliest Deadline First)
219in conjunction with CBS (Constant Bandwidth Server).
220To set and fetch this policy and associated attributes,
221one must use the Linux-specific
222.BR sched_setattr (2)
223and
224.BR sched_getattr (2)
225system calls.
7a0d1838
PZ
226
227A sporadic task is one that has a sequence of jobs, where each
91c98da6 228job is activated at most once per period.
9cc1fa25
MK
229Each job also has a
230.IR "relative deadline" ,
91c98da6 231before which it should finish execution, and a
9cc1fa25
MK
232.IR "computation time" ,
233which is the CPU time necessary for executing the job.
234The moment when a task wakes up
235because a new job has to be executed is called the
236.IR "arrival time"
237(also referred to as the request time or release time).
238The
239.IR "start time"
240is the time at which a task starts its execution.
241The
0da5e58a 242.I "absolute deadline"
9cc1fa25 243is thus obtained by adding the relative deadline to the arrival time.
7a0d1838
PZ
244
245The following diagram clarifies these terms:
246
91c98da6 247.in +4n
7a0d1838 248.nf
7bd7f43e
MK
249arrival/wakeup absolute deadline
250 | start time |
251 | | |
252 v v v
253-----x--------xooooooooooooooooo--------x--------x---
0756f58f 254 |<- comp. time ->|
7bd7f43e
MK
255 |<------- relative deadline ------>|
256 |<-------------- period ------------------->|
7a0d1838 257.fi
91c98da6 258.in
7a0d1838 259
9cc1fa25 260When setting a
91c98da6 261.B SCHED_DEADLINE
9cc1fa25
MK
262policy for a thread using
263.BR sched_setattr (2),
264one can specify three parameters:
265.IR Runtime ,
266.IR Deadline ,
267and
268.IR Period .
269These parameters do not necessarily correspond to the aforementioned terms:
270usual practice is to set Runtime to something bigger than the average
271computation time (or worst-case execution time for hard real-time tasks),
272Deadline to the relative deadline, and Period to the period of the task.
273Thus, for
274.BR SCHED_DEADLINE
275scheduling, we have:
7a0d1838 276
91c98da6 277.in +4n
7a0d1838 278.nf
7bd7f43e
MK
279arrival/wakeup absolute deadline
280 | start time |
281 | | |
282 v v v
283-----x--------xooooooooooooooooo--------x--------x---
284 |<-- Runtime ------->|
285 |<----------- Deadline ----------->|
286 |<-------------- Period ------------------->|
7a0d1838 287.fi
91c98da6 288.in
7a0d1838 289
9cc1fa25
MK
290The three deadline-scheduling parameters correspond to the
291.IR sched_runtime ,
292.IR sched_deadline ,
293and
294.IR sched_period
295fields of the
296.I sched_attr
297structure; see
298.BR sched_setattr (2).
a68beb35 299These fields express values in nanoseconds.
9cc1fa25 300.\" FIXME It looks as though specifying sched_period as 0 means
47a2bb17
MK
301.\" "make sched_period the same as sched_deadline".
302.\" This needs to be documented.
9cc1fa25
MK
303If
304.IR sched_period
305is specified as 0, then it is made the same as
306.IR sched_deadline .
307
308The kernel requires that:
309
310 sched_runtime <= sched_deadline <= sched_period
311
312.\" See __checkparam_dl in kernel/sched/core.c
313In addition, under the current implementation,
314all of the parameter values must be at least 1024
315(i.e., just over one microsecond,
7bd7f43e 316which is the resolution of the implementation), and less than 2^63.
9cc1fa25
MK
317If any of these checks fails,
318.BR sched_setattr (2)
319fails with the error
320.BR EINVAL .
7a0d1838
PZ
321
322The CBS guarantees non-interference between tasks, by throttling
9cc1fa25 323threads that attempt to over-run their specified Runtime.
7a0d1838 324
9cc1fa25 325To ensure deadline scheduling guarantees,
0da5e58a 326the kernel must prevent situations where the set of
91c98da6 327.B SCHED_DEADLINE
9cc1fa25
MK
328threads is not feasible (schedulable) within the given constraints.
329The kernel thus performs an admittance test when setting or changing
91c98da6 330.B SCHED_DEADLINE
9cc1fa25
MK
331policy and attributes.
332This admission test calculates whether the change is feasible;
8e8cd193 333if it is not,
91c98da6 334.BR sched_setattr (2)
9cc1fa25
MK
335fails with the error
336.BR EBUSY .
7a0d1838
PZ
337
338For example, it is required (but not necessarily sufficient) for
9cc1fa25
MK
339the total utilization to be less than or equal to the total number of
340CPUs available, where, since each thread can maximally run for
341Runtime per Period, that thread's utilization is its
342Runtime divided by its Period.
7a0d1838 343
88e28f78 344In order to fulfill the guarantees that are made when
9cc1fa25
MK
345a thread is admitted to the
346.BR SCHED_DEADLINE
347policy,
91c98da6 348.BR SCHED_DEADLINE
9cc1fa25
MK
349threads are the highest priority (user controllable) threads in the
350system; if any
91c98da6 351.BR SCHED_DEADLINE
9cc1fa25
MK
352thread is runnable,
353it will preempt any thread scheduled under one of the other policies.
7a0d1838 354
9cc1fa25 355A call to
91c98da6 356.BR fork (2)
9cc1fa25
MK
357by a thread scheduled under the
358.B SCHED_DEADLINE
359policy will fail with the error
360.BR EAGAIN ,
361unless the thread has its reset-on-fork flag set (see below).
7a0d1838 362
91c98da6
MK
363A
364.B SCHED_DEADLINE
9cc1fa25 365thread that calls
91c98da6 366.BR sched_yield (2)
9cc1fa25
MK
367will yield the current job and wait for a new period to begin.
368.\"
369.\" FIXME Calling sched_getparam() on a SCHED_DEADLINE thread
47a2bb17
MK
370.\" fails with EINVAL, but sched_getscheduler() succeeds.
371.\" Is that intended? (Why?)
91c98da6 372.\"
59c06be3 373.SS SCHED_OTHER: Default Linux time-sharing scheduling
b8986eae
MK
374\fBSCHED_OTHER\fP can be used at only static priority 0
375(i.e., threads under real-time policies always have priority over
376.B SCHED_OTHER
377processes).
59c06be3
MK
378\fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
379intended for all threads that do not require the special
380real-time mechanisms.
f677bcfb 381
59c06be3
MK
382The thread to run is chosen from the static
383priority 0 list based on a \fIdynamic\fP priority that is determined only
384inside this list.
2be50a32 385The dynamic priority is based on the nice value (see below)
927d0dfa 386and is increased for each time quantum the thread is ready to run,
59c06be3
MK
387but denied to run by the scheduler.
388This ensures fair progress among all \fBSCHED_OTHER\fP threads.
389.\"
45fcd0e2 390.SS The nice value
bcbb240c 391The nice value is an attribute
d145138e
MK
392that can be used to influence the CPU scheduler to
393favor or disfavor a process in scheduling decisions.
394It affects the scheduling of
395.BR SCHED_OTHER
396and
397.BR SCHED_BATCH
bcbb240c 398(see below) processes.
2be50a32
MK
399The nice value can be modified using
400.BR nice (2),
401.BR setpriority (2),
402or
403.BR sched_setattr (2).
d145138e 404
bcbb240c
MK
405According to POSIX.1, the nice value is a per-process attribute;
406that is, the threads in a process should share a nice value.
407However, on Linux, the nice value is a per-thread attribute:
408different threads in the same process may have different nice values.
409
45fcd0e2
MK
410The range of the nice value
411varies across UNIX systems.
412On modern Linux, the range is \-20 (high priority) to +19 (low priority).
413On some other systems, the range is \-20..20.
414Very early Linux kernels (Before Linux 2.0) had the range \-infinity..15.
415.\" Linux before 1.3.36 had \-infinity..15.
416.\" Since kernel 1.3.43, Linux has the range \-20..19.
417
418The degree to which the nice value affects the relative scheduling of
419.BR SCHED_OTHER
420processes likewise varies across UNIX systems and
421across Linux kernel versions.
422
423With the advent of the CFS scheduler in kernel 2.6.23,
424Linux adopted an algorithm that causes
425relative differences in nice values to have a much stronger effect.
115366c6
MK
426In the current implementation, each unit of difference in the
427nice values of two processes results in a factor of 1.25
428in the degree to which the scheduler favors the higher priority process.
45fcd0e2
MK
429This causes very low nice values (+19) to truly provide little CPU
430to a process whenever there is any other
431higher priority load on the system,
432and makes high nice values (\-20) deliver most of the CPU to applications
433that require it (e.g., some audio applications).
50e12810
MK
434
435On Linux, the
436.BR RLIMIT_NICE
437resource limit can be used to define a limit to which
438an unprivileged process's nice value can be raised; see
439.BR setrlimit (2)
440for details.
45fcd0e2 441.\"
e92070f8
MK
442.SS The nice value and group scheduling
443When scheduling non-real-time processes (i.e., those scheduled under the
444.BR SCHED_OTHER ,
445.BR SCHED_BATCH ,
446and
447.BR SCHED_IDLE
448policies), the CFS scheduler employs a technique known as "group scheduling",
449if the kernel was configured with the
450.BR CONFIG_GROUP_SCHED
451option (which is typical).
452Scheduling groups (also known as task groups)
453are formed in the following circumstances:
454.IP * 3
455All of the processes in a non-root CPU cgroup form a scheduling group.
456Thus, each non-root CPU cgroup is a scheduling group.
457.IP *
458All of the processes that are (implicitly) placed in an autogroup
459(i.e., the same session, as created by
460.BR setsid (2))
461form a scheduling group.
462Each new autogroup is thus a separate scheduling group.
463.IP *
464If autogrouping is disabled, then all processes in the root CPU cgroup
465form a scheduling group (sometimes called the "root task group").
466.PP
467Under group scheduling,
468the nice value has an effect for scheduling decisions
469.IR "only relative to other threads in the same group" .
470This has some surprising consequences in terms of the traditional semantics
471of the nice value on UNIX systems.
472In particular, if autogrouping
473is enabled (which is the default), then employing
474.BR setpriority (2)
475or
476.BR nice (1)
477on a process has an effect only for scheduling relative
478to other processes executed in the same session
479(typically: the same terminal window).
480Conversely, for two process that are (for example) the sole CPU-bound processes
481in different sessions (different terminal windows),
482.IR "modifying the nice value of the process in one of the sessions"
483.IR "has no effect"
484in terms of the scheduler's decisions relative to the
485process in the other session.
486.\" More succinctly: the nice(1) command is nearly a no-op since Linux 2.6.38.
487.\"
59c06be3
MK
488.SS SCHED_BATCH: Scheduling batch processes
489(Since Linux 2.6.16.)
490\fBSCHED_BATCH\fP can be used only at static priority 0.
491This policy is similar to \fBSCHED_OTHER\fP in that it schedules
492the thread according to its dynamic priority
493(based on the nice value).
494The difference is that this policy
495will cause the scheduler to always assume
496that the thread is CPU-intensive.
497Consequently, the scheduler will apply a small scheduling
a1fa36af 498penalty with respect to wakeup behavior,
59c06be3
MK
499so that this thread is mildly disfavored in scheduling decisions.
500
501.\" The following paragraph is drawn largely from the text that
502.\" accompanied Ingo Molnar's patch for the implementation of
503.\" SCHED_BATCH.
504.\" commit b0a9499c3dd50d333e2aedb7e894873c58da3785
505This policy is useful for workloads that are noninteractive,
506but do not want to lower their nice value,
507and for workloads that want a deterministic scheduling policy without
508interactivity causing extra preemptions (between the workload's tasks).
509.\"
510.SS SCHED_IDLE: Scheduling very low priority jobs
511(Since Linux 2.6.23.)
512\fBSCHED_IDLE\fP can be used only at static priority 0;
513the process nice value has no influence for this policy.
514
515This policy is intended for running jobs at extremely low
516priority (lower even than a +19 nice value with the
517.B SCHED_OTHER
518or
519.B SCHED_BATCH
520policies).
521.\"
522.SS Resetting scheduling policy for child processes
005eaa8f
MK
523Each thread has a reset-on-fork scheduling flag.
524When this flag is set, children created by
525.BR fork (2)
526do not inherit privileged scheduling policies.
527The reset-on-fork flag can be set by either:
528.IP * 3
529ORing the
59c06be3 530.B SCHED_RESET_ON_FORK
005eaa8f 531flag into the
59c06be3 532.I policy
005eaa8f
MK
533argument when calling
534.BR sched_setscheduler (2)
535(since Linux 2.6.32);
536or
537.IP *
538specifying the
539.B SCHED_FLAG_RESET_ON_FORK
540flag in
541.IR attr.sched_flags
59c06be3 542when calling
005eaa8f
MK
543.BR sched_setattr (2).
544.PP
545Note that the constants used with these two APIs have different names.
546The state of the reset-on-fork flag can analogously be retrieved using
547.BR sched_getscheduler (2)
548and
549.BR sched_getattr (2).
550
551The reset-on-fork feature is intended for media-playback applications,
59c06be3
MK
552and can be used to prevent applications evading the
553.BR RLIMIT_RTTIME
554resource limit (see
555.BR getrlimit (2))
556by creating multiple child processes.
557
005eaa8f 558More precisely, if the reset-on-fork flag is set,
59c06be3
MK
559the following rules apply for subsequently created children:
560.IP * 3
561If the calling thread has a scheduling policy of
562.B SCHED_FIFO
563or
564.BR SCHED_RR ,
565the policy is reset to
566.BR SCHED_OTHER
567in child processes.
568.IP *
569If the calling process has a negative nice value,
570the nice value is reset to zero in child processes.
571.PP
005eaa8f 572After the reset-on-fork flag has been enabled,
59c06be3
MK
573it can be reset only if the thread has the
574.BR CAP_SYS_NICE
575capability.
576This flag is disabled in child processes created by
577.BR fork (2).
59c06be3
MK
578.\"
579.SS Privileges and resource limits
580In Linux kernels before 2.6.12, only privileged
581.RB ( CAP_SYS_NICE )
582threads can set a nonzero static priority (i.e., set a real-time
583scheduling policy).
584The only change that an unprivileged thread can make is to set the
585.B SCHED_OTHER
759e1210 586policy, and this can be done only if the effective user ID of the caller
59c06be3
MK
587matches the real or effective user ID of the target thread
588(i.e., the thread specified by
589.IR pid )
590whose policy is being changed.
591
9cc1fa25
MK
592A thread must be privileged
593.RB ( CAP_SYS_NICE )
0da5e58a 594in order to set or modify a
9cc1fa25
MK
595.BR SCHED_DEADLINE
596policy.
597
59c06be3
MK
598Since Linux 2.6.12, the
599.B RLIMIT_RTPRIO
600resource limit defines a ceiling on an unprivileged thread's
601static priority for the
602.B SCHED_RR
603and
604.B SCHED_FIFO
605policies.
606The rules for changing scheduling policy and priority are as follows:
607.IP * 3
608If an unprivileged thread has a nonzero
609.B RLIMIT_RTPRIO
610soft limit, then it can change its scheduling policy and priority,
611subject to the restriction that the priority cannot be set to a
612value higher than the maximum of its current priority and its
613.B RLIMIT_RTPRIO
614soft limit.
615.IP *
616If the
617.B RLIMIT_RTPRIO
618soft limit is 0, then the only permitted changes are to lower the priority,
619or to switch to a non-real-time policy.
620.IP *
621Subject to the same rules,
622another unprivileged thread can also make these changes,
623as long as the effective user ID of the thread making the change
624matches the real or effective user ID of the target thread.
625.IP *
626Special rules apply for the
f7a858b4
MK
627.BR SCHED_IDLE
628policy.
59c06be3
MK
629In Linux kernels before 2.6.39,
630an unprivileged thread operating under this policy cannot
631change its policy, regardless of the value of its
632.BR RLIMIT_RTPRIO
633resource limit.
634In Linux kernels since 2.6.39,
635.\" commit c02aa73b1d18e43cfd79c2f193b225e84ca497c8
636an unprivileged thread can switch to either the
637.BR SCHED_BATCH
638or the
85b6211a 639.BR SCHED_OTHER
59c06be3
MK
640policy so long as its nice value falls within the range permitted by its
641.BR RLIMIT_NICE
642resource limit (see
643.BR getrlimit (2)).
644.PP
645Privileged
646.RB ( CAP_SYS_NICE )
647threads ignore the
648.B RLIMIT_RTPRIO
649limit; as with older kernels,
650they can make arbitrary changes to scheduling policy and priority.
651See
652.BR getrlimit (2)
653for further information on
654.BR RLIMIT_RTPRIO .
0c055c75
MK
655.SS Limiting the CPU usage of real-time and deadline processes
656A nonblocking infinite loop in a thread scheduled under the
657.BR SCHED_FIFO ,
658.BR SCHED_RR ,
659or
660.BR SCHED_DEADLINE
df312a96
MK
661policy can potentially block all other threads from accessing
662the CPU forever.
0c055c75
MK
663Prior to Linux 2.6.25, the only way of preventing a runaway real-time
664process from freezing the system was to run (at the console)
665a shell scheduled under a higher static priority than the tested application.
666This allows an emergency kill of tested
667real-time applications that do not block or terminate as expected.
668
669Since Linux 2.6.25, there are other techniques for dealing with runaway
670real-time and deadline processes.
671One of these is to use the
672.BR RLIMIT_RTTIME
673resource limit to set a ceiling on the CPU time that
674a real-time process may consume.
675See
676.BR getrlimit (2)
677for details.
678
679Since version 2.6.25, Linux also provides two
680.I /proc
681files that can be used to reserve a certain amount of CPU time
682to be used by non-real-time processes.
0b1ce085 683Reserving CPU time in this fashion allows some CPU time to be
0c055c75
MK
684allocated to (say) a root shell that can be used to kill a runaway process.
685Both of these files specify time values in microseconds:
686.TP
687.IR /proc/sys/kernel/sched_rt_period_us
688This file specifies a scheduling period that is equivalent to
689100% CPU bandwidth.
690The value in this file can range from 1 to
691.BR INT_MAX ,
692giving an operating range of 1 microsecond to around 35 minutes.
693The default value in this file is 1,000,000 (1 second).
694.TP
695.IR /proc/sys/kernel/sched_rt_runtime_us
696The value in this file specifies how much of the "period" time
697can be used by all real-time and deadline scheduled processes
698on the system.
699The value in this file can range from \-1 to
700.BR INT_MAX \-1.
701Specifying \-1 makes the runtime the same as the period;
702that is, no CPU time is set aside for non-real-time processes
703(which was the Linux behavior before kernel 2.6.25).
704The default value in this file is 950,000 (0.95 seconds),
705meaning that 5% of the CPU time is reserved for processes that
706don't run under a real-time or deadline scheduling policy.
707.PP
59c06be3 708.SS Response time
1154a064 709A blocked high priority thread waiting for I/O has a certain
59c06be3
MK
710response time before it is scheduled again.
711The device driver writer
712can greatly reduce this response time by using a "slow interrupt"
713interrupt handler.
714.\" as described in
715.\" .BR request_irq (9).
716.SS Miscellaneous
717Child processes inherit the scheduling policy and parameters across a
718.BR fork (2).
719The scheduling policy and parameters are preserved across
720.BR execve (2).
721
722Memory locking is usually needed for real-time processes to avoid
723paging delays; this can be done with
724.BR mlock (2)
725or
726.BR mlockall (2).
ed520068
MK
727.\"
728.SS The autogroup feature
729.\" commit 5091faa449ee0b7d73bc296a93bca9540fc51d0a
730Since Linux 2.6.38,
731the kernel provides a feature known as autogrouping to improve interactive
ee1f3c18 732desktop performance in the face of multiprocess, CPU-intensive
ed520068
MK
733workloads such as building the Linux kernel with large numbers of
734parallel build processes (i.e., the
735.BR make (1)
736.BR \-j
737flag).
738
739This feature operates in conjunction with the
740CFS scheduler and requires a kernel that is configured with
741.BR CONFIG_SCHED_AUTOGROUP .
742On a running system, this feature is enabled or disabled via the file
743.IR /proc/sys/kernel/sched_autogroup_enabled ;
744a value of 0 disables the feature, while a value of 1 enables it.
745The default value in this file is 1, unless the kernel was booted with the
746.IR noautogroup
747parameter.
748
ee1f3c18
MK
749A new autogroup is created created when a new session is created via
750.BR setsid (2);
751this happens, for example, when a new terminal window is started.
752A new process created by
753.BR fork (2)
754inherits its parent's autogroup membership.
755Thus, all of the processes in a session are members of the same autogroup.
756An autogroup is automatically destroyed when the last process
ed520068
MK
757in the group terminates.
758
ee1f3c18
MK
759When autogrouping is enabled, all of the members of an autogroup
760are placed in the same kernel scheduler "task group".
761The CFS scheduler employs an algorithm that equalizes the
762distribution of CPU cycles across task groups.
763The benefits of this for interactive desktop performance
764can be described via the following example.
765
626dca36
MK
766Suppose that there are two autogroups competing for the same CPU
767(i.e., presume either a single CPU system or the use of
768.BR taskset (1)
769to confine all the processes to the same CPU on an SMP system).
ee1f3c18
MK
770The first group contains ten CPU-bound processes from
771a kernel build started with
772.IR "make\ \-j10" .
773The other contains a single CPU-bound process: a video player.
774The effect of autogrouping is that the two groups will
775each receive half of the CPU cycles.
776That is, the video player will receive 50% of the CPU cycles,
c11d0670 777rather than just 9% of the cycles,
ee1f3c18 778which would likely lead to degraded video playback.
626dca36
MK
779The situation on an SMP system is more complex,
780.\" Mike Galbraith, 25 Nov 2016:
781.\" I'd say something more wishy-washy here, like cycles are
782.\" distributed fairly across groups and leave it at that, as your
783.\" detailed example is incorrect due to SMP fairness (which I don't
784.\" like much because [very unlikely] worst case scenario
785.\" renders a box sized group incapable of utilizing more that
786.\" a single CPU total). For example, if a group of NR_CPUS
787.\" size competes with a singleton, load balancing will try to give
788.\" the singleton a full CPU of its very own. If groups intersect for
789.\" whatever reason on say my quad lappy, distribution is 80/20 in
790.\" favor of the singleton.
791but the general effect is the same:
792the scheduler distributes CPU cycles across task groups such that
ee1f3c18 793an autogroup that contains a large number of CPU-bound processes
1dd83d2e 794does not end up hogging CPU cycles at the expense of the other
ee1f3c18
MK
795jobs on the system.
796
797A process's autogroup (task group) membership can be viewed via the file
ed520068
MK
798.IR /proc/[pid]/autogroup :
799
800.nf
801.in +4n
802$ \fBcat /proc/1/autogroup\fP
803/autogroup-1 nice 0
804.in
805.fi
806
807This file can also be used to modify the CPU bandwidth allocated
ee1f3c18 808to an autogroup.
ed520068 809This is done by writing a number in the "nice" range to the file
ee1f3c18 810to set the autogroup's nice value.
58627ec0
MK
811The allowed range is from +19 (low priority) to \-20 (high priority).
812(Writing values outside of this range causes
813.BR write (2)
814to fail with the error
815.BR EINVAL .)
816The setting has the same effect as modifying the nice level via
ee1f3c18 817.BR getpriority (2).
576b74ee
MK
818(For a discussion of the nice value, see
819.BR getpriority (2).)
ee1f3c18 820.\" FIXME .
576b74ee
MK
821.\" Because of a bug introduced in Linux 4.7
822.\" (commit 2159197d66770ec01f75c93fb11dc66df81fd45b made changes
823.\" that exposed the fact that autogroup didn't call scale_load()),
824.\" it happened that *all* values in this range caused a task group
825.\" to be further disfavored by the scheduler, with \-20 resulting
826.\" in the scheduler mildy disfavoring the task group and +19 greatly
827.\" disfavoring it.
ed520068 828.\"
576b74ee
MK
829.\" A patch was posted on 23 Nov 2016
830.\" ("sched/autogroup: Fix 64bit kernel nice adjustment";
831.\" check later to see in which kernel version it lands.
ee1f3c18
MK
832.\"
833.\" FIXME How do the nice value of a process and the nice value of
834.\" an autogroup interact? Which has priority?
45922aa8
MK
835.\"
836.\" It *appears* that the autogroup nice value is used for CPU distribution
837.\" between task groups, and that the process nice value has no effect there.
838.\" (I.e., suppose two autogroups each contain a CPU-bound process,
839.\" with one process having nice==0 and the other having nice==19.
840.\" It appears that they each get 50% of the CPU.)
841.\" It appears that the process nice value has effect only with respect to
842.\" scheduling relative to other processes in the *same* autogroup.
843.\" Is this correct?
ed520068 844
ed520068
MK
845The use of the
846.BR cgroups (7)
e9c1649a
MK
847CPU controller to place processes in cgroups other than the
848root CPU cgroup overrides the effect of autogrouping.
ee1f3c18
MK
849
850The autogroup feature does not group processes
851that are scheduled under a real-time and deadline policies.
852Those processes are scheduled according to the rules described earlier.
59c06be3 853.SH NOTES
1f7fb9c0
MK
854The
855.BR cgroups (7)
856CPU controller can be used to limit the CPU consumption of
857groups of processes.
59c06be3
MK
858.PP
859Originally, Standard Linux was intended as a general-purpose operating
860system being able to handle background processes, interactive
861applications, and less demanding real-time applications (applications that
862need to usually meet timing deadlines).
863Although the Linux kernel 2.6
864allowed for kernel preemption and the newly introduced O(1) scheduler
865ensures that the time needed to schedule is fixed and deterministic
866irrespective of the number of active tasks, true real-time computing
867was not possible up to kernel version 2.6.17.
868.SS Real-time features in the mainline Linux kernel
869.\" FIXME . Probably this text will need some minor tweaking
84dd1325 870.\" ask Carsten Emde about this.
6ad8b4d0 871Since kernel version 2.6.18, Linux is gradually
59c06be3
MK
872becoming equipped with real-time capabilities,
873most of which are derived from the former
874.I realtime-preempt
94875d76 875patch set.
59c06be3 876Until the patches have been completely merged into the
84dd1325 877mainline kernel,
59c06be3
MK
878they must be installed to achieve the best real-time performance.
879These patches are named:
880.in +4n
881.nf
882
883patch-\fIkernelversion\fP-rt\fIpatchversion\fP
884.fi
885.in
886.PP
887and can be downloaded from
888.UR http://www.kernel.org\:/pub\:/linux\:/kernel\:/projects\:/rt/
889.UE .
890
891Without the patches and prior to their full inclusion into the mainline
892kernel, the kernel configuration offers only the three preemption classes
893.BR CONFIG_PREEMPT_NONE ,
894.BR CONFIG_PREEMPT_VOLUNTARY ,
895and
896.B CONFIG_PREEMPT_DESKTOP
897which respectively provide no, some, and considerable
898reduction of the worst-case scheduling latency.
899
900With the patches applied or after their full inclusion into the mainline
901kernel, the additional configuration item
902.B CONFIG_PREEMPT_RT
903becomes available.
904If this is selected, Linux is transformed into a regular
905real-time operating system.
759e1210 906The FIFO and RR scheduling policies are then used to run a thread
59c06be3 907with true real-time priority and a minimum worst-case scheduling latency.
59c06be3
MK
908.SH SEE ALSO
909.ad l
910.nh
911.BR chrt (1),
f19db853 912.BR taskset (1),
59c06be3
MK
913.BR getpriority (2),
914.BR mlock (2),
915.BR mlockall (2),
916.BR munlock (2),
917.BR munlockall (2),
918.BR nice (2),
919.BR sched_get_priority_max (2),
920.BR sched_get_priority_min (2),
921.BR sched_getaffinity (2),
922.BR sched_getparam (2),
584c8ee0 923.BR sched_getscheduler (2),
59c06be3
MK
924.BR sched_rr_get_interval (2),
925.BR sched_setaffinity (2),
926.BR sched_setparam (2),
584c8ee0 927.BR sched_setscheduler (2),
59c06be3
MK
928.BR sched_yield (2),
929.BR setpriority (2),
720a5280
MK
930.BR pthread_getaffinity_np (3),
931.BR pthread_setaffinity_np (3),
932.BR sched_getcpu (3),
59c06be3
MK
933.BR capabilities (7),
934.BR cpuset (7)
935.ad
936.PP
937.I Programming for the real world \- POSIX.4
938by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0.
939.PP
b963d0e3
MK
940The Linux kernel source files
941.IR Documentation/scheduler/sched-deadline.txt ,
942.IR Documentation/scheduler/sched-rt-group.txt ,
458689ed 943.IR Documentation/scheduler/sched-design-CFS.txt ,
b963d0e3 944and
d630434e 945.IR Documentation/scheduler/sched-nice-design.txt