]>
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 |
b16695a3 | 36 | .SS API summary |
bac6ef74 MK |
37 | Linux provides the following system calls for controlling |
38 | the CPU scheduling behavior, policy, and priority of processes | |
39 | (or, more precisely, threads). | |
b16695a3 MK |
40 | .TP |
41 | .BR sched_setscheduler (2) | |
42 | Set the scheduling policy and parameters of a specified thread. | |
43 | .TP | |
44 | .BR sched_getscheduler (2) | |
45 | Return the scheduling policy of a specified thread. | |
46 | .TP | |
47 | .BR sched_setparam (2) | |
48 | Set the scheduling parameters of a specified thread. | |
49 | .TP | |
50 | .BR sched_getparam (2) | |
51 | Fetch the scheduling parameters of a specified thread. | |
52 | .TP | |
53 | .BR sched_get_priority_max (2) | |
275e3c16 | 54 | Return the maximum priority available in a specified scheduling policy. |
b16695a3 MK |
55 | .TP |
56 | .BR sched_get_priority_min (2) | |
275e3c16 | 57 | Return the minimum priority available in a specified scheduling policy. |
b16695a3 | 58 | .TP |
5813ff92 | 59 | .BR sched_rr_get_interval (2) |
b16695a3 MK |
60 | Fetch the quantum used for threads that are scheduled under |
61 | the "round-robin" scheduling policy. | |
62 | .TP | |
63 | .BR sched_yield (2) | |
64 | Cause the caller to relinquish the CPU, | |
65 | so that some other thread be executed. | |
66 | .TP | |
67 | .BR sched_setaffinity (2) | |
68 | (Linux-specific) | |
69 | Set the CPU affinity of a specified thread. | |
70 | .TP | |
71 | .BR sched_getaffinity (2) | |
72 | (Linux-specific) | |
91f5e870 | 73 | Get the CPU affinity of a specified thread. |
b16695a3 MK |
74 | .TP |
75 | .BR sched_setattr (2) | |
77dab50a MK |
76 | Set the scheduling policy and parameters of a specified thread. |
77 | This (Linux-specific) system call provides a superset of the functionality of | |
78 | .BR sched_setscheduler (2) | |
79 | and | |
80 | .BR sched_setparam (2). | |
b16695a3 MK |
81 | .TP |
82 | .BR sched_getattr (2) | |
77dab50a MK |
83 | Fetch the scheduling policy and parameters of a specified thread. |
84 | This (Linux-specific) system call provides a superset of the functionality of | |
85 | .BR sched_getscheduler (2) | |
86 | and | |
87 | .BR sched_getparam (2). | |
b16695a3 | 88 | .\" |
59c06be3 MK |
89 | .SS Scheduling policies |
90 | The scheduler is the kernel component that decides which runnable thread | |
91 | will be executed by the CPU next. | |
92 | Each thread has an associated scheduling policy and a \fIstatic\fP | |
2e868ccc MK |
93 | scheduling priority, |
94 | .IR sched_priority . | |
961df2a8 | 95 | The scheduler makes its decisions based on knowledge of the scheduling |
59c06be3 MK |
96 | policy and static priority of all threads on the system. |
97 | ||
98 | For threads scheduled under one of the normal scheduling policies | |
99 | (\fBSCHED_OTHER\fP, \fBSCHED_IDLE\fP, \fBSCHED_BATCH\fP), | |
100 | \fIsched_priority\fP is not used in scheduling | |
101 | decisions (it must be specified as 0). | |
102 | ||
103 | Processes scheduled under one of the real-time policies | |
104 | (\fBSCHED_FIFO\fP, \fBSCHED_RR\fP) have a | |
105 | \fIsched_priority\fP value in the range 1 (low) to 99 (high). | |
106 | (As the numbers imply, real-time threads always have higher priority | |
107 | than normal threads.) | |
cc401eea | 108 | Note well: POSIX.1 requires an implementation to support only a |
59c06be3 MK |
109 | minimum 32 distinct priority levels for the real-time policies, |
110 | and some systems supply just this minimum. | |
111 | Portable programs should use | |
112 | .BR sched_get_priority_min (2) | |
113 | and | |
114 | .BR sched_get_priority_max (2) | |
115 | to find the range of priorities supported for a particular policy. | |
116 | ||
117 | Conceptually, the scheduler maintains a list of runnable | |
118 | threads for each possible \fIsched_priority\fP value. | |
119 | In order to determine which thread runs next, the scheduler looks for | |
120 | the nonempty list with the highest static priority and selects the | |
121 | thread at the head of this list. | |
122 | ||
123 | A thread's scheduling policy determines | |
124 | where it will be inserted into the list of threads | |
125 | with equal static priority and how it will move inside this list. | |
126 | ||
127 | All scheduling is preemptive: if a thread with a higher static | |
128 | priority becomes ready to run, the currently running thread | |
129 | will be preempted and | |
130 | returned to the wait list for its static priority level. | |
131 | The scheduling policy determines the | |
132 | ordering only within the list of runnable threads with equal static | |
133 | priority. | |
134 | .SS SCHED_FIFO: First in-first out scheduling | |
135 | \fBSCHED_FIFO\fP can be used only with static priorities higher than | |
136 | 0, which means that when a \fBSCHED_FIFO\fP threads becomes runnable, | |
137 | it will always immediately preempt any currently running | |
138 | \fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP thread. | |
139 | \fBSCHED_FIFO\fP is a simple scheduling | |
140 | algorithm without time slicing. | |
141 | For threads scheduled under the | |
142 | \fBSCHED_FIFO\fP policy, the following rules apply: | |
143 | .IP * 3 | |
144 | A \fBSCHED_FIFO\fP thread that has been preempted by another thread of | |
145 | higher priority will stay at the head of the list for its priority and | |
146 | will resume execution as soon as all threads of higher priority are | |
147 | blocked again. | |
148 | .IP * | |
149 | When a \fBSCHED_FIFO\fP thread becomes runnable, it | |
150 | will be inserted at the end of the list for its priority. | |
151 | .IP * | |
152 | A call to | |
4c2eb0c2 MK |
153 | .BR sched_setscheduler (2), |
154 | .BR sched_setparam (2), | |
59c06be3 | 155 | or |
4c2eb0c2 | 156 | .BR sched_setattr (2) |
59c06be3 MK |
157 | will put the |
158 | \fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) thread identified by | |
159 | \fIpid\fP at the start of the list if it was runnable. | |
160 | As a consequence, it may preempt the currently running thread if | |
161 | it has the same priority. | |
cc401eea | 162 | (POSIX.1 specifies that the thread should go to the end |
59c06be3 MK |
163 | of the list.) |
164 | .\" In 2.2.x and 2.4.x, the thread is placed at the front of the queue | |
165 | .\" In 2.0.x, the Right Thing happened: the thread went to the back -- MTK | |
166 | .IP * | |
167 | A thread calling | |
168 | .BR sched_yield (2) | |
169 | will be put at the end of the list. | |
170 | .PP | |
171 | No other events will move a thread | |
172 | scheduled under the \fBSCHED_FIFO\fP policy in the wait list of | |
173 | runnable threads with equal static priority. | |
174 | ||
175 | A \fBSCHED_FIFO\fP | |
176 | thread runs until either it is blocked by an I/O request, it is | |
177 | preempted by a higher priority thread, or it calls | |
178 | .BR sched_yield (2). | |
179 | .SS SCHED_RR: Round-robin scheduling | |
180 | \fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP. | |
181 | Everything | |
182 | described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP, | |
183 | except that each thread is allowed to run only for a maximum time | |
184 | quantum. | |
185 | If a \fBSCHED_RR\fP thread has been running for a time | |
186 | period equal to or longer than the time quantum, it will be put at the | |
187 | end of the list for its priority. | |
188 | A \fBSCHED_RR\fP thread that has | |
189 | been preempted by a higher priority thread and subsequently resumes | |
190 | execution as a running thread will complete the unexpired portion of | |
191 | its round-robin time quantum. | |
192 | The length of the time quantum can be | |
193 | retrieved using | |
194 | .BR sched_rr_get_interval (2). | |
195 | .\" On Linux 2.4, the length of the RR interval is influenced | |
196 | .\" by the process nice value -- MTK | |
197 | .\" | |
7a0d1838 | 198 | .SS SCHED_DEADLINE: Sporadic task model deadline scheduling |
9cc1fa25 MK |
199 | Since version 3.14, Linux provides a deadline scheduling policy |
200 | .RB ( SCHED_DEADLINE ). | |
201 | This policy is currently implemented using | |
202 | GEDF (Global Earliest Deadline First) | |
203 | in conjunction with CBS (Constant Bandwidth Server). | |
204 | To set and fetch this policy and associated attributes, | |
205 | one must use the Linux-specific | |
206 | .BR sched_setattr (2) | |
207 | and | |
208 | .BR sched_getattr (2) | |
209 | system calls. | |
7a0d1838 PZ |
210 | |
211 | A sporadic task is one that has a sequence of jobs, where each | |
91c98da6 | 212 | job is activated at most once per period. |
9cc1fa25 MK |
213 | Each job also has a |
214 | .IR "relative deadline" , | |
91c98da6 | 215 | before which it should finish execution, and a |
9cc1fa25 MK |
216 | .IR "computation time" , |
217 | which is the CPU time necessary for executing the job. | |
218 | The moment when a task wakes up | |
219 | because a new job has to be executed is called the | |
220 | .IR "arrival time" | |
221 | (also referred to as the request time or release time). | |
222 | The | |
223 | .IR "start time" | |
224 | is the time at which a task starts its execution. | |
225 | The | |
0da5e58a | 226 | .I "absolute deadline" |
9cc1fa25 | 227 | is thus obtained by adding the relative deadline to the arrival time. |
7a0d1838 PZ |
228 | |
229 | The following diagram clarifies these terms: | |
230 | ||
91c98da6 | 231 | .in +4n |
7a0d1838 | 232 | .nf |
7bd7f43e MK |
233 | arrival/wakeup absolute deadline |
234 | | start time | | |
235 | | | | | |
236 | v v v | |
237 | -----x--------xooooooooooooooooo--------x--------x--- | |
0756f58f | 238 | |<- comp. time ->| |
7bd7f43e MK |
239 | |<------- relative deadline ------>| |
240 | |<-------------- period ------------------->| | |
7a0d1838 | 241 | .fi |
91c98da6 | 242 | .in |
7a0d1838 | 243 | |
9cc1fa25 | 244 | When setting a |
91c98da6 | 245 | .B SCHED_DEADLINE |
9cc1fa25 MK |
246 | policy for a thread using |
247 | .BR sched_setattr (2), | |
248 | one can specify three parameters: | |
249 | .IR Runtime , | |
250 | .IR Deadline , | |
251 | and | |
252 | .IR Period . | |
253 | These parameters do not necessarily correspond to the aforementioned terms: | |
254 | usual practice is to set Runtime to something bigger than the average | |
255 | computation time (or worst-case execution time for hard real-time tasks), | |
256 | Deadline to the relative deadline, and Period to the period of the task. | |
257 | Thus, for | |
258 | .BR SCHED_DEADLINE | |
259 | scheduling, we have: | |
7a0d1838 | 260 | |
91c98da6 | 261 | .in +4n |
7a0d1838 | 262 | .nf |
7bd7f43e MK |
263 | arrival/wakeup absolute deadline |
264 | | start time | | |
265 | | | | | |
266 | v v v | |
267 | -----x--------xooooooooooooooooo--------x--------x--- | |
268 | |<-- Runtime ------->| | |
269 | |<----------- Deadline ----------->| | |
270 | |<-------------- Period ------------------->| | |
7a0d1838 | 271 | .fi |
91c98da6 | 272 | .in |
7a0d1838 | 273 | |
9cc1fa25 MK |
274 | The three deadline-scheduling parameters correspond to the |
275 | .IR sched_runtime , | |
276 | .IR sched_deadline , | |
277 | and | |
278 | .IR sched_period | |
279 | fields of the | |
280 | .I sched_attr | |
281 | structure; see | |
282 | .BR sched_setattr (2). | |
a68beb35 | 283 | These fields express values in nanoseconds. |
9cc1fa25 | 284 | .\" FIXME It looks as though specifying sched_period as 0 means |
47a2bb17 MK |
285 | .\" "make sched_period the same as sched_deadline". |
286 | .\" This needs to be documented. | |
9cc1fa25 MK |
287 | If |
288 | .IR sched_period | |
289 | is specified as 0, then it is made the same as | |
290 | .IR sched_deadline . | |
291 | ||
292 | The kernel requires that: | |
293 | ||
294 | sched_runtime <= sched_deadline <= sched_period | |
295 | ||
296 | .\" See __checkparam_dl in kernel/sched/core.c | |
297 | In addition, under the current implementation, | |
298 | all of the parameter values must be at least 1024 | |
299 | (i.e., just over one microsecond, | |
7bd7f43e | 300 | which is the resolution of the implementation), and less than 2^63. |
9cc1fa25 MK |
301 | If any of these checks fails, |
302 | .BR sched_setattr (2) | |
303 | fails with the error | |
304 | .BR EINVAL . | |
7a0d1838 PZ |
305 | |
306 | The CBS guarantees non-interference between tasks, by throttling | |
9cc1fa25 | 307 | threads that attempt to over-run their specified Runtime. |
7a0d1838 | 308 | |
9cc1fa25 | 309 | To ensure deadline scheduling guarantees, |
0da5e58a | 310 | the kernel must prevent situations where the set of |
91c98da6 | 311 | .B SCHED_DEADLINE |
9cc1fa25 MK |
312 | threads is not feasible (schedulable) within the given constraints. |
313 | The kernel thus performs an admittance test when setting or changing | |
91c98da6 | 314 | .B SCHED_DEADLINE |
9cc1fa25 MK |
315 | policy and attributes. |
316 | This admission test calculates whether the change is feasible; | |
8e8cd193 | 317 | if it is not, |
91c98da6 | 318 | .BR sched_setattr (2) |
9cc1fa25 MK |
319 | fails with the error |
320 | .BR EBUSY . | |
7a0d1838 PZ |
321 | |
322 | For example, it is required (but not necessarily sufficient) for | |
9cc1fa25 MK |
323 | the total utilization to be less than or equal to the total number of |
324 | CPUs available, where, since each thread can maximally run for | |
325 | Runtime per Period, that thread's utilization is its | |
326 | Runtime divided by its Period. | |
7a0d1838 | 327 | |
88e28f78 | 328 | In order to fulfill the guarantees that are made when |
9cc1fa25 MK |
329 | a thread is admitted to the |
330 | .BR SCHED_DEADLINE | |
331 | policy, | |
91c98da6 | 332 | .BR SCHED_DEADLINE |
9cc1fa25 MK |
333 | threads are the highest priority (user controllable) threads in the |
334 | system; if any | |
91c98da6 | 335 | .BR SCHED_DEADLINE |
9cc1fa25 MK |
336 | thread is runnable, |
337 | it will preempt any thread scheduled under one of the other policies. | |
7a0d1838 | 338 | |
9cc1fa25 | 339 | A call to |
91c98da6 | 340 | .BR fork (2) |
9cc1fa25 MK |
341 | by a thread scheduled under the |
342 | .B SCHED_DEADLINE | |
343 | policy will fail with the error | |
344 | .BR EAGAIN , | |
345 | unless the thread has its reset-on-fork flag set (see below). | |
7a0d1838 | 346 | |
91c98da6 MK |
347 | A |
348 | .B SCHED_DEADLINE | |
9cc1fa25 | 349 | thread that calls |
91c98da6 | 350 | .BR sched_yield (2) |
9cc1fa25 MK |
351 | will yield the current job and wait for a new period to begin. |
352 | .\" | |
353 | .\" FIXME Calling sched_getparam() on a SCHED_DEADLINE thread | |
47a2bb17 MK |
354 | .\" fails with EINVAL, but sched_getscheduler() succeeds. |
355 | .\" Is that intended? (Why?) | |
91c98da6 | 356 | .\" |
59c06be3 MK |
357 | .SS SCHED_OTHER: Default Linux time-sharing scheduling |
358 | \fBSCHED_OTHER\fP can be used at only static priority 0. | |
359 | \fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is | |
360 | intended for all threads that do not require the special | |
361 | real-time mechanisms. | |
362 | The thread to run is chosen from the static | |
363 | priority 0 list based on a \fIdynamic\fP priority that is determined only | |
364 | inside this list. | |
365 | The dynamic priority is based on the nice value (set by | |
1608b671 MK |
366 | .BR nice (2), |
367 | .BR setpriority (2), | |
59c06be3 | 368 | or |
1608b671 | 369 | .BR sched_setattr (2)) |
59c06be3 MK |
370 | and increased for each time quantum the thread is ready to run, |
371 | but denied to run by the scheduler. | |
372 | This ensures fair progress among all \fBSCHED_OTHER\fP threads. | |
373 | .\" | |
374 | .SS SCHED_BATCH: Scheduling batch processes | |
375 | (Since Linux 2.6.16.) | |
376 | \fBSCHED_BATCH\fP can be used only at static priority 0. | |
377 | This policy is similar to \fBSCHED_OTHER\fP in that it schedules | |
378 | the thread according to its dynamic priority | |
379 | (based on the nice value). | |
380 | The difference is that this policy | |
381 | will cause the scheduler to always assume | |
382 | that the thread is CPU-intensive. | |
383 | Consequently, the scheduler will apply a small scheduling | |
a1fa36af | 384 | penalty with respect to wakeup behavior, |
59c06be3 MK |
385 | so that this thread is mildly disfavored in scheduling decisions. |
386 | ||
387 | .\" The following paragraph is drawn largely from the text that | |
388 | .\" accompanied Ingo Molnar's patch for the implementation of | |
389 | .\" SCHED_BATCH. | |
390 | .\" commit b0a9499c3dd50d333e2aedb7e894873c58da3785 | |
391 | This policy is useful for workloads that are noninteractive, | |
392 | but do not want to lower their nice value, | |
393 | and for workloads that want a deterministic scheduling policy without | |
394 | interactivity causing extra preemptions (between the workload's tasks). | |
395 | .\" | |
396 | .SS SCHED_IDLE: Scheduling very low priority jobs | |
397 | (Since Linux 2.6.23.) | |
398 | \fBSCHED_IDLE\fP can be used only at static priority 0; | |
399 | the process nice value has no influence for this policy. | |
400 | ||
401 | This policy is intended for running jobs at extremely low | |
402 | priority (lower even than a +19 nice value with the | |
403 | .B SCHED_OTHER | |
404 | or | |
405 | .B SCHED_BATCH | |
406 | policies). | |
407 | .\" | |
408 | .SS Resetting scheduling policy for child processes | |
005eaa8f MK |
409 | Each thread has a reset-on-fork scheduling flag. |
410 | When this flag is set, children created by | |
411 | .BR fork (2) | |
412 | do not inherit privileged scheduling policies. | |
413 | The reset-on-fork flag can be set by either: | |
414 | .IP * 3 | |
415 | ORing the | |
59c06be3 | 416 | .B SCHED_RESET_ON_FORK |
005eaa8f | 417 | flag into the |
59c06be3 | 418 | .I policy |
005eaa8f MK |
419 | argument when calling |
420 | .BR sched_setscheduler (2) | |
421 | (since Linux 2.6.32); | |
422 | or | |
423 | .IP * | |
424 | specifying the | |
425 | .B SCHED_FLAG_RESET_ON_FORK | |
426 | flag in | |
427 | .IR attr.sched_flags | |
59c06be3 | 428 | when calling |
005eaa8f MK |
429 | .BR sched_setattr (2). |
430 | .PP | |
431 | Note that the constants used with these two APIs have different names. | |
432 | The state of the reset-on-fork flag can analogously be retrieved using | |
433 | .BR sched_getscheduler (2) | |
434 | and | |
435 | .BR sched_getattr (2). | |
436 | ||
437 | The reset-on-fork feature is intended for media-playback applications, | |
59c06be3 MK |
438 | and can be used to prevent applications evading the |
439 | .BR RLIMIT_RTTIME | |
440 | resource limit (see | |
441 | .BR getrlimit (2)) | |
442 | by creating multiple child processes. | |
443 | ||
005eaa8f | 444 | More precisely, if the reset-on-fork flag is set, |
59c06be3 MK |
445 | the following rules apply for subsequently created children: |
446 | .IP * 3 | |
447 | If the calling thread has a scheduling policy of | |
448 | .B SCHED_FIFO | |
449 | or | |
450 | .BR SCHED_RR , | |
451 | the policy is reset to | |
452 | .BR SCHED_OTHER | |
453 | in child processes. | |
454 | .IP * | |
455 | If the calling process has a negative nice value, | |
456 | the nice value is reset to zero in child processes. | |
457 | .PP | |
005eaa8f | 458 | After the reset-on-fork flag has been enabled, |
59c06be3 MK |
459 | it can be reset only if the thread has the |
460 | .BR CAP_SYS_NICE | |
461 | capability. | |
462 | This flag is disabled in child processes created by | |
463 | .BR fork (2). | |
59c06be3 MK |
464 | .\" |
465 | .SS Privileges and resource limits | |
466 | In Linux kernels before 2.6.12, only privileged | |
467 | .RB ( CAP_SYS_NICE ) | |
468 | threads can set a nonzero static priority (i.e., set a real-time | |
469 | scheduling policy). | |
470 | The only change that an unprivileged thread can make is to set the | |
471 | .B SCHED_OTHER | |
759e1210 | 472 | policy, and this can be done only if the effective user ID of the caller |
59c06be3 MK |
473 | matches the real or effective user ID of the target thread |
474 | (i.e., the thread specified by | |
475 | .IR pid ) | |
476 | whose policy is being changed. | |
477 | ||
9cc1fa25 MK |
478 | A thread must be privileged |
479 | .RB ( CAP_SYS_NICE ) | |
0da5e58a | 480 | in order to set or modify a |
9cc1fa25 MK |
481 | .BR SCHED_DEADLINE |
482 | policy. | |
483 | ||
59c06be3 MK |
484 | Since Linux 2.6.12, the |
485 | .B RLIMIT_RTPRIO | |
486 | resource limit defines a ceiling on an unprivileged thread's | |
487 | static priority for the | |
488 | .B SCHED_RR | |
489 | and | |
490 | .B SCHED_FIFO | |
491 | policies. | |
492 | The rules for changing scheduling policy and priority are as follows: | |
493 | .IP * 3 | |
494 | If an unprivileged thread has a nonzero | |
495 | .B RLIMIT_RTPRIO | |
496 | soft limit, then it can change its scheduling policy and priority, | |
497 | subject to the restriction that the priority cannot be set to a | |
498 | value higher than the maximum of its current priority and its | |
499 | .B RLIMIT_RTPRIO | |
500 | soft limit. | |
501 | .IP * | |
502 | If the | |
503 | .B RLIMIT_RTPRIO | |
504 | soft limit is 0, then the only permitted changes are to lower the priority, | |
505 | or to switch to a non-real-time policy. | |
506 | .IP * | |
507 | Subject to the same rules, | |
508 | another unprivileged thread can also make these changes, | |
509 | as long as the effective user ID of the thread making the change | |
510 | matches the real or effective user ID of the target thread. | |
511 | .IP * | |
512 | Special rules apply for the | |
f7a858b4 MK |
513 | .BR SCHED_IDLE |
514 | policy. | |
59c06be3 MK |
515 | In Linux kernels before 2.6.39, |
516 | an unprivileged thread operating under this policy cannot | |
517 | change its policy, regardless of the value of its | |
518 | .BR RLIMIT_RTPRIO | |
519 | resource limit. | |
520 | In Linux kernels since 2.6.39, | |
521 | .\" commit c02aa73b1d18e43cfd79c2f193b225e84ca497c8 | |
522 | an unprivileged thread can switch to either the | |
523 | .BR SCHED_BATCH | |
524 | or the | |
85b6211a | 525 | .BR SCHED_OTHER |
59c06be3 MK |
526 | policy so long as its nice value falls within the range permitted by its |
527 | .BR RLIMIT_NICE | |
528 | resource limit (see | |
529 | .BR getrlimit (2)). | |
530 | .PP | |
531 | Privileged | |
532 | .RB ( CAP_SYS_NICE ) | |
533 | threads ignore the | |
534 | .B RLIMIT_RTPRIO | |
535 | limit; as with older kernels, | |
536 | they can make arbitrary changes to scheduling policy and priority. | |
537 | See | |
538 | .BR getrlimit (2) | |
539 | for further information on | |
540 | .BR RLIMIT_RTPRIO . | |
0c055c75 MK |
541 | .SS Limiting the CPU usage of real-time and deadline processes |
542 | A nonblocking infinite loop in a thread scheduled under the | |
543 | .BR SCHED_FIFO , | |
544 | .BR SCHED_RR , | |
545 | or | |
546 | .BR SCHED_DEADLINE | |
df312a96 MK |
547 | policy can potentially block all other threads from accessing |
548 | the CPU forever. | |
0c055c75 MK |
549 | Prior to Linux 2.6.25, the only way of preventing a runaway real-time |
550 | process from freezing the system was to run (at the console) | |
551 | a shell scheduled under a higher static priority than the tested application. | |
552 | This allows an emergency kill of tested | |
553 | real-time applications that do not block or terminate as expected. | |
554 | ||
555 | Since Linux 2.6.25, there are other techniques for dealing with runaway | |
556 | real-time and deadline processes. | |
557 | One of these is to use the | |
558 | .BR RLIMIT_RTTIME | |
559 | resource limit to set a ceiling on the CPU time that | |
560 | a real-time process may consume. | |
561 | See | |
562 | .BR getrlimit (2) | |
563 | for details. | |
564 | ||
565 | Since version 2.6.25, Linux also provides two | |
566 | .I /proc | |
567 | files that can be used to reserve a certain amount of CPU time | |
568 | to be used by non-real-time processes. | |
0b1ce085 | 569 | Reserving CPU time in this fashion allows some CPU time to be |
0c055c75 MK |
570 | allocated to (say) a root shell that can be used to kill a runaway process. |
571 | Both of these files specify time values in microseconds: | |
572 | .TP | |
573 | .IR /proc/sys/kernel/sched_rt_period_us | |
574 | This file specifies a scheduling period that is equivalent to | |
575 | 100% CPU bandwidth. | |
576 | The value in this file can range from 1 to | |
577 | .BR INT_MAX , | |
578 | giving an operating range of 1 microsecond to around 35 minutes. | |
579 | The default value in this file is 1,000,000 (1 second). | |
580 | .TP | |
581 | .IR /proc/sys/kernel/sched_rt_runtime_us | |
582 | The value in this file specifies how much of the "period" time | |
583 | can be used by all real-time and deadline scheduled processes | |
584 | on the system. | |
585 | The value in this file can range from \-1 to | |
586 | .BR INT_MAX \-1. | |
587 | Specifying \-1 makes the runtime the same as the period; | |
588 | that is, no CPU time is set aside for non-real-time processes | |
589 | (which was the Linux behavior before kernel 2.6.25). | |
590 | The default value in this file is 950,000 (0.95 seconds), | |
591 | meaning that 5% of the CPU time is reserved for processes that | |
592 | don't run under a real-time or deadline scheduling policy. | |
593 | .PP | |
59c06be3 | 594 | .SS Response time |
1154a064 | 595 | A blocked high priority thread waiting for I/O has a certain |
59c06be3 MK |
596 | response time before it is scheduled again. |
597 | The device driver writer | |
598 | can greatly reduce this response time by using a "slow interrupt" | |
599 | interrupt handler. | |
600 | .\" as described in | |
601 | .\" .BR request_irq (9). | |
602 | .SS Miscellaneous | |
603 | Child processes inherit the scheduling policy and parameters across a | |
604 | .BR fork (2). | |
605 | The scheduling policy and parameters are preserved across | |
606 | .BR execve (2). | |
607 | ||
608 | Memory locking is usually needed for real-time processes to avoid | |
609 | paging delays; this can be done with | |
610 | .BR mlock (2) | |
611 | or | |
612 | .BR mlockall (2). | |
59c06be3 | 613 | .SH NOTES |
1f7fb9c0 MK |
614 | The |
615 | .BR cgroups (7) | |
616 | CPU controller can be used to limit the CPU consumption of | |
617 | groups of processes. | |
59c06be3 MK |
618 | .PP |
619 | Originally, Standard Linux was intended as a general-purpose operating | |
620 | system being able to handle background processes, interactive | |
621 | applications, and less demanding real-time applications (applications that | |
622 | need to usually meet timing deadlines). | |
623 | Although the Linux kernel 2.6 | |
624 | allowed for kernel preemption and the newly introduced O(1) scheduler | |
625 | ensures that the time needed to schedule is fixed and deterministic | |
626 | irrespective of the number of active tasks, true real-time computing | |
627 | was not possible up to kernel version 2.6.17. | |
628 | .SS Real-time features in the mainline Linux kernel | |
629 | .\" FIXME . Probably this text will need some minor tweaking | |
84dd1325 | 630 | .\" ask Carsten Emde about this. |
59c06be3 MK |
631 | From kernel version 2.6.18 onward, however, Linux is gradually |
632 | becoming equipped with real-time capabilities, | |
633 | most of which are derived from the former | |
634 | .I realtime-preempt | |
94875d76 | 635 | patch set. |
59c06be3 | 636 | Until the patches have been completely merged into the |
84dd1325 | 637 | mainline kernel, |
59c06be3 MK |
638 | they must be installed to achieve the best real-time performance. |
639 | These patches are named: | |
640 | .in +4n | |
641 | .nf | |
642 | ||
643 | patch-\fIkernelversion\fP-rt\fIpatchversion\fP | |
644 | .fi | |
645 | .in | |
646 | .PP | |
647 | and can be downloaded from | |
648 | .UR http://www.kernel.org\:/pub\:/linux\:/kernel\:/projects\:/rt/ | |
649 | .UE . | |
650 | ||
651 | Without the patches and prior to their full inclusion into the mainline | |
652 | kernel, the kernel configuration offers only the three preemption classes | |
653 | .BR CONFIG_PREEMPT_NONE , | |
654 | .BR CONFIG_PREEMPT_VOLUNTARY , | |
655 | and | |
656 | .B CONFIG_PREEMPT_DESKTOP | |
657 | which respectively provide no, some, and considerable | |
658 | reduction of the worst-case scheduling latency. | |
659 | ||
660 | With the patches applied or after their full inclusion into the mainline | |
661 | kernel, the additional configuration item | |
662 | .B CONFIG_PREEMPT_RT | |
663 | becomes available. | |
664 | If this is selected, Linux is transformed into a regular | |
665 | real-time operating system. | |
759e1210 | 666 | The FIFO and RR scheduling policies are then used to run a thread |
59c06be3 | 667 | with true real-time priority and a minimum worst-case scheduling latency. |
59c06be3 MK |
668 | .SH SEE ALSO |
669 | .ad l | |
670 | .nh | |
671 | .BR chrt (1), | |
f19db853 | 672 | .BR taskset (1), |
59c06be3 MK |
673 | .BR getpriority (2), |
674 | .BR mlock (2), | |
675 | .BR mlockall (2), | |
676 | .BR munlock (2), | |
677 | .BR munlockall (2), | |
678 | .BR nice (2), | |
679 | .BR sched_get_priority_max (2), | |
680 | .BR sched_get_priority_min (2), | |
681 | .BR sched_getaffinity (2), | |
682 | .BR sched_getparam (2), | |
584c8ee0 | 683 | .BR sched_getscheduler (2), |
59c06be3 MK |
684 | .BR sched_rr_get_interval (2), |
685 | .BR sched_setaffinity (2), | |
686 | .BR sched_setparam (2), | |
584c8ee0 | 687 | .BR sched_setscheduler (2), |
59c06be3 MK |
688 | .BR sched_yield (2), |
689 | .BR setpriority (2), | |
720a5280 MK |
690 | .BR pthread_getaffinity_np (3), |
691 | .BR pthread_setaffinity_np (3), | |
692 | .BR sched_getcpu (3), | |
59c06be3 MK |
693 | .BR capabilities (7), |
694 | .BR cpuset (7) | |
695 | .ad | |
696 | .PP | |
697 | .I Programming for the real world \- POSIX.4 | |
698 | by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0. | |
699 | .PP | |
b963d0e3 MK |
700 | The Linux kernel source files |
701 | .IR Documentation/scheduler/sched-deadline.txt , | |
702 | .IR Documentation/scheduler/sched-rt-group.txt , | |
458689ed | 703 | .IR Documentation/scheduler/sched-design-CFS.txt , |
b963d0e3 | 704 | and |
d630434e | 705 | .IR Documentation/scheduler/sched-nice-design.txt |