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