Commit | Line | Data |
---|---|---|
712e5e34 DF |
1 | Deadline Task Scheduling |
2 | ------------------------ | |
3 | ||
4 | CONTENTS | |
5 | ======== | |
6 | ||
7 | 0. WARNING | |
8 | 1. Overview | |
9 | 2. Scheduling algorithm | |
10 | 3. Scheduling Real-Time Tasks | |
11 | 4. Bandwidth management | |
12 | 4.1 System-wide settings | |
13 | 4.2 Task interface | |
14 | 4.3 Default behavior | |
15 | 5. Tasks CPU affinity | |
16 | 5.1 SCHED_DEADLINE and cpusets HOWTO | |
17 | 6. Future plans | |
f5801933 | 18 | A. Test suite |
13924d2a | 19 | B. Minimal main() |
712e5e34 DF |
20 | |
21 | ||
22 | 0. WARNING | |
23 | ========== | |
24 | ||
25 | Fiddling with these settings can result in an unpredictable or even unstable | |
26 | system behavior. As for -rt (group) scheduling, it is assumed that root users | |
27 | know what they're doing. | |
28 | ||
29 | ||
30 | 1. Overview | |
31 | =========== | |
32 | ||
33 | The SCHED_DEADLINE policy contained inside the sched_dl scheduling class is | |
34 | basically an implementation of the Earliest Deadline First (EDF) scheduling | |
35 | algorithm, augmented with a mechanism (called Constant Bandwidth Server, CBS) | |
36 | that makes it possible to isolate the behavior of tasks between each other. | |
37 | ||
38 | ||
39 | 2. Scheduling algorithm | |
40 | ================== | |
41 | ||
42 | SCHED_DEADLINE uses three parameters, named "runtime", "period", and | |
b56bfc6c | 43 | "deadline", to schedule tasks. A SCHED_DEADLINE task should receive |
712e5e34 DF |
44 | "runtime" microseconds of execution time every "period" microseconds, and |
45 | these "runtime" microseconds are available within "deadline" microseconds | |
3a3a58d4 | 46 | from the beginning of the period. In order to implement this behavior, |
712e5e34 DF |
47 | every time the task wakes up, the scheduler computes a "scheduling deadline" |
48 | consistent with the guarantee (using the CBS[2,3] algorithm). Tasks are then | |
49 | scheduled using EDF[1] on these scheduling deadlines (the task with the | |
b56bfc6c LA |
50 | earliest scheduling deadline is selected for execution). Notice that the |
51 | task actually receives "runtime" time units within "deadline" if a proper | |
52 | "admission control" strategy (see Section "4. Bandwidth management") is used | |
53 | (clearly, if the system is overloaded this guarantee cannot be respected). | |
712e5e34 | 54 | |
3aa2dbe2 | 55 | Summing up, the CBS[2,3] algorithm assigns scheduling deadlines to tasks so |
712e5e34 DF |
56 | that each task runs for at most its runtime every period, avoiding any |
57 | interference between different tasks (bandwidth isolation), while the EDF[1] | |
ad67dc31 LA |
58 | algorithm selects the task with the earliest scheduling deadline as the one |
59 | to be executed next. Thanks to this feature, tasks that do not strictly comply | |
60 | with the "traditional" real-time task model (see Section 3) can effectively | |
61 | use the new policy. | |
712e5e34 DF |
62 | |
63 | In more details, the CBS algorithm assigns scheduling deadlines to | |
64 | tasks in the following way: | |
65 | ||
3a3a58d4 | 66 | - Each SCHED_DEADLINE task is characterized by the "runtime", |
712e5e34 DF |
67 | "deadline", and "period" parameters; |
68 | ||
69 | - The state of the task is described by a "scheduling deadline", and | |
ad67dc31 | 70 | a "remaining runtime". These two parameters are initially set to 0; |
712e5e34 DF |
71 | |
72 | - When a SCHED_DEADLINE task wakes up (becomes ready for execution), | |
73 | the scheduler checks if | |
74 | ||
ad67dc31 LA |
75 | remaining runtime runtime |
76 | ---------------------------------- > --------- | |
77 | scheduling deadline - current time period | |
712e5e34 DF |
78 | |
79 | then, if the scheduling deadline is smaller than the current time, or | |
80 | this condition is verified, the scheduling deadline and the | |
3a3a58d4 | 81 | remaining runtime are re-initialized as |
712e5e34 DF |
82 | |
83 | scheduling deadline = current time + deadline | |
ad67dc31 | 84 | remaining runtime = runtime |
712e5e34 | 85 | |
ad67dc31 | 86 | otherwise, the scheduling deadline and the remaining runtime are |
712e5e34 DF |
87 | left unchanged; |
88 | ||
89 | - When a SCHED_DEADLINE task executes for an amount of time t, its | |
ad67dc31 | 90 | remaining runtime is decreased as |
712e5e34 | 91 | |
ad67dc31 | 92 | remaining runtime = remaining runtime - t |
712e5e34 DF |
93 | |
94 | (technically, the runtime is decreased at every tick, or when the | |
95 | task is descheduled / preempted); | |
96 | ||
ad67dc31 | 97 | - When the remaining runtime becomes less or equal than 0, the task is |
712e5e34 DF |
98 | said to be "throttled" (also known as "depleted" in real-time literature) |
99 | and cannot be scheduled until its scheduling deadline. The "replenishment | |
100 | time" for this task (see next item) is set to be equal to the current | |
101 | value of the scheduling deadline; | |
102 | ||
103 | - When the current time is equal to the replenishment time of a | |
ad67dc31 | 104 | throttled task, the scheduling deadline and the remaining runtime are |
712e5e34 DF |
105 | updated as |
106 | ||
107 | scheduling deadline = scheduling deadline + period | |
ad67dc31 | 108 | remaining runtime = remaining runtime + runtime |
712e5e34 DF |
109 | |
110 | ||
111 | 3. Scheduling Real-Time Tasks | |
112 | ============================= | |
113 | ||
114 | * BIG FAT WARNING ****************************************************** | |
115 | * | |
116 | * This section contains a (not-thorough) summary on classical deadline | |
117 | * scheduling theory, and how it applies to SCHED_DEADLINE. | |
118 | * The reader can "safely" skip to Section 4 if only interested in seeing | |
119 | * how the scheduling policy can be used. Anyway, we strongly recommend | |
120 | * to come back here and continue reading (once the urge for testing is | |
121 | * satisfied :P) to be sure of fully understanding all technical details. | |
122 | ************************************************************************ | |
123 | ||
124 | There are no limitations on what kind of task can exploit this new | |
125 | scheduling discipline, even if it must be said that it is particularly | |
126 | suited for periodic or sporadic real-time tasks that need guarantees on their | |
127 | timing behavior, e.g., multimedia, streaming, control applications, etc. | |
128 | ||
129 | A typical real-time task is composed of a repetition of computation phases | |
130 | (task instances, or jobs) which are activated on a periodic or sporadic | |
131 | fashion. | |
3a3a58d4 | 132 | Each job J_j (where J_j is the j^th job of the task) is characterized by an |
712e5e34 DF |
133 | arrival time r_j (the time when the job starts), an amount of computation |
134 | time c_j needed to finish the job, and a job absolute deadline d_j, which | |
135 | is the time within which the job should be finished. The maximum execution | |
c2a68493 | 136 | time max{c_j} is called "Worst Case Execution Time" (WCET) for the task. |
712e5e34 DF |
137 | A real-time task can be periodic with period P if r_{j+1} = r_j + P, or |
138 | sporadic with minimum inter-arrival time P is r_{j+1} >= r_j + P. Finally, | |
139 | d_j = r_j + D, where D is the task's relative deadline. | |
e0deda81 LA |
140 | Summing up, a real-time task can be described as |
141 | Task = (WCET, D, P) | |
142 | ||
3a3a58d4 | 143 | The utilization of a real-time task is defined as the ratio between its |
b56bfc6c LA |
144 | WCET and its period (or minimum inter-arrival time), and represents |
145 | the fraction of CPU time needed to execute the task. | |
146 | ||
c2a68493 | 147 | If the total utilization U=sum(WCET_i/P_i) is larger than M (with M equal |
b56bfc6c LA |
148 | to the number of CPUs), then the scheduler is unable to respect all the |
149 | deadlines. | |
3a3a58d4 | 150 | Note that total utilization is defined as the sum of the utilizations |
b56bfc6c LA |
151 | WCET_i/P_i over all the real-time tasks in the system. When considering |
152 | multiple real-time tasks, the parameters of the i-th task are indicated | |
153 | with the "_i" suffix. | |
3a3a58d4 | 154 | Moreover, if the total utilization is larger than M, then we risk starving |
b56bfc6c | 155 | non- real-time tasks by real-time tasks. |
3a3a58d4 | 156 | If, instead, the total utilization is smaller than M, then non real-time |
b56bfc6c LA |
157 | tasks will not be starved and the system might be able to respect all the |
158 | deadlines. | |
159 | As a matter of fact, in this case it is possible to provide an upper bound | |
160 | for tardiness (defined as the maximum between 0 and the difference | |
161 | between the finishing time of a job and its absolute deadline). | |
162 | More precisely, it can be proven that using a global EDF scheduler the | |
163 | maximum tardiness of each task is smaller or equal than | |
164 | ((M − 1) · WCET_max − WCET_min)/(M − (M − 2) · U_max) + WCET_max | |
c2a68493 | 165 | where WCET_max = max{WCET_i} is the maximum WCET, WCET_min=min{WCET_i} |
134136c4 LA |
166 | is the minimum WCET, and U_max = max{WCET_i/P_i} is the maximum |
167 | utilization[12]. | |
b56bfc6c LA |
168 | |
169 | If M=1 (uniprocessor system), or in case of partitioned scheduling (each | |
170 | real-time task is statically assigned to one and only one CPU), it is | |
171 | possible to formally check if all the deadlines are respected. | |
172 | If D_i = P_i for all tasks, then EDF is able to respect all the deadlines | |
3a3a58d4 | 173 | of all the tasks executing on a CPU if and only if the total utilization |
b56bfc6c LA |
174 | of the tasks running on such a CPU is smaller or equal than 1. |
175 | If D_i != P_i for some task, then it is possible to define the density of | |
48355c47 | 176 | a task as WCET_i/min{D_i,P_i}, and EDF is able to respect all the deadlines |
e0deda81 LA |
177 | of all the tasks running on a CPU if the sum of the densities of the tasks |
178 | running on such a CPU is smaller or equal than 1: | |
179 | sum(WCET_i / min{D_i, P_i}) <= 1 | |
180 | It is important to notice that this condition is only sufficient, and not | |
181 | necessary: there are task sets that are schedulable, but do not respect the | |
182 | condition. For example, consider the task set {Task_1,Task_2} composed by | |
183 | Task_1=(50ms,50ms,100ms) and Task_2=(10ms,100ms,100ms). | |
184 | EDF is clearly able to schedule the two tasks without missing any deadline | |
185 | (Task_1 is scheduled as soon as it is released, and finishes just in time | |
186 | to respect its deadline; Task_2 is scheduled immediately after Task_1, hence | |
187 | its response time cannot be larger than 50ms + 10ms = 60ms) even if | |
188 | 50 / min{50,100} + 10 / min{100, 100} = 50 / 50 + 10 / 100 = 1.1 | |
189 | Of course it is possible to test the exact schedulability of tasks with | |
190 | D_i != P_i (checking a condition that is both sufficient and necessary), | |
191 | but this cannot be done by comparing the total utilization or density with | |
192 | a constant. Instead, the so called "processor demand" approach can be used, | |
193 | computing the total amount of CPU time h(t) needed by all the tasks to | |
194 | respect all of their deadlines in a time interval of size t, and comparing | |
195 | such a time with the interval size t. If h(t) is smaller than t (that is, | |
196 | the amount of time needed by the tasks in a time interval of size t is | |
197 | smaller than the size of the interval) for all the possible values of t, then | |
198 | EDF is able to schedule the tasks respecting all of their deadlines. Since | |
199 | performing this check for all possible values of t is impossible, it has been | |
200 | proven[4,5,6] that it is sufficient to perform the test for values of t | |
201 | between 0 and a maximum value L. The cited papers contain all of the | |
202 | mathematical details and explain how to compute h(t) and L. | |
203 | In any case, this kind of analysis is too complex as well as too | |
204 | time-consuming to be performed on-line. Hence, as explained in Section | |
205 | 4 Linux uses an admission test based on the tasks' utilizations. | |
b56bfc6c LA |
206 | |
207 | On multiprocessor systems with global EDF scheduling (non partitioned | |
208 | systems), a sufficient test for schedulability can not be based on the | |
134136c4 LA |
209 | utilizations or densities: it can be shown that even if D_i = P_i task |
210 | sets with utilizations slightly larger than 1 can miss deadlines regardless | |
211 | of the number of CPUs. | |
212 | ||
213 | Consider a set {Task_1,...Task_{M+1}} of M+1 tasks on a system with M | |
214 | CPUs, with the first task Task_1=(P,P,P) having period, relative deadline | |
215 | and WCET equal to P. The remaining M tasks Task_i=(e,P-1,P-1) have an | |
216 | arbitrarily small worst case execution time (indicated as "e" here) and a | |
217 | period smaller than the one of the first task. Hence, if all the tasks | |
218 | activate at the same time t, global EDF schedules these M tasks first | |
219 | (because their absolute deadlines are equal to t + P - 1, hence they are | |
220 | smaller than the absolute deadline of Task_1, which is t + P). As a | |
221 | result, Task_1 can be scheduled only at time t + e, and will finish at | |
222 | time t + e + P, after its absolute deadline. The total utilization of the | |
223 | task set is U = M · e / (P - 1) + P / P = M · e / (P - 1) + 1, and for small | |
224 | values of e this can become very close to 1. This is known as "Dhall's | |
225 | effect"[7]. Note: the example in the original paper by Dhall has been | |
226 | slightly simplified here (for example, Dhall more correctly computed | |
227 | lim_{e->0}U). | |
228 | ||
229 | More complex schedulability tests for global EDF have been developed in | |
230 | real-time literature[8,9], but they are not based on a simple comparison | |
231 | between total utilization (or density) and a fixed constant. If all tasks | |
232 | have D_i = P_i, a sufficient schedulability condition can be expressed in | |
233 | a simple way: | |
234 | sum(WCET_i / P_i) <= M - (M - 1) · U_max | |
235 | where U_max = max{WCET_i / P_i}[10]. Notice that for U_max = 1, | |
236 | M - (M - 1) · U_max becomes M - M + 1 = 1 and this schedulability condition | |
237 | just confirms the Dhall's effect. A more complete survey of the literature | |
238 | about schedulability tests for multi-processor real-time scheduling can be | |
239 | found in [11]. | |
240 | ||
241 | As seen, enforcing that the total utilization is smaller than M does not | |
242 | guarantee that global EDF schedules the tasks without missing any deadline | |
243 | (in other words, global EDF is not an optimal scheduling algorithm). However, | |
244 | a total utilization smaller than M is enough to guarantee that non real-time | |
245 | tasks are not starved and that the tardiness of real-time tasks has an upper | |
246 | bound[12] (as previously noted). Different bounds on the maximum tardiness | |
247 | experienced by real-time tasks have been developed in various papers[13,14], | |
248 | but the theoretical result that is important for SCHED_DEADLINE is that if | |
249 | the total utilization is smaller or equal than M then the response times of | |
250 | the tasks are limited. | |
712e5e34 DF |
251 | |
252 | SCHED_DEADLINE can be used to schedule real-time tasks guaranteeing that | |
253 | the jobs' deadlines of a task are respected. In order to do this, a task | |
254 | must be scheduled by setting: | |
255 | ||
256 | - runtime >= WCET | |
257 | - deadline = D | |
258 | - period <= P | |
259 | ||
3aa2dbe2 | 260 | IOW, if runtime >= WCET and if period is <= P, then the scheduling deadlines |
712e5e34 DF |
261 | and the absolute deadlines (d_j) coincide, so a proper admission control |
262 | allows to respect the jobs' absolute deadlines for this task (this is what is | |
263 | called "hard schedulability property" and is an extension of Lemma 1 of [2]). | |
ad67dc31 LA |
264 | Notice that if runtime > deadline the admission control will surely reject |
265 | this task, as it is not possible to respect its temporal constraints. | |
712e5e34 DF |
266 | |
267 | References: | |
268 | 1 - C. L. Liu and J. W. Layland. Scheduling algorithms for multiprogram- | |
269 | ming in a hard-real-time environment. Journal of the Association for | |
270 | Computing Machinery, 20(1), 1973. | |
271 | 2 - L. Abeni , G. Buttazzo. Integrating Multimedia Applications in Hard | |
272 | Real-Time Systems. Proceedings of the 19th IEEE Real-time Systems | |
273 | Symposium, 1998. http://retis.sssup.it/~giorgio/paps/1998/rtss98-cbs.pdf | |
274 | 3 - L. Abeni. Server Mechanisms for Multimedia Applications. ReTiS Lab | |
ad67dc31 | 275 | Technical Report. http://disi.unitn.it/~abeni/tr-98-01.pdf |
e0deda81 LA |
276 | 4 - J. Y. Leung and M.L. Merril. A Note on Preemptive Scheduling of |
277 | Periodic, Real-Time Tasks. Information Processing Letters, vol. 11, | |
278 | no. 3, pp. 115-118, 1980. | |
279 | 5 - S. K. Baruah, A. K. Mok and L. E. Rosier. Preemptively Scheduling | |
280 | Hard-Real-Time Sporadic Tasks on One Processor. Proceedings of the | |
281 | 11th IEEE Real-time Systems Symposium, 1990. | |
282 | 6 - S. K. Baruah, L. E. Rosier and R. R. Howell. Algorithms and Complexity | |
283 | Concerning the Preemptive Scheduling of Periodic Real-Time tasks on | |
284 | One Processor. Real-Time Systems Journal, vol. 4, no. 2, pp 301-324, | |
285 | 1990. | |
134136c4 LA |
286 | 7 - S. J. Dhall and C. L. Liu. On a real-time scheduling problem. Operations |
287 | research, vol. 26, no. 1, pp 127-140, 1978. | |
288 | 8 - T. Baker. Multiprocessor EDF and Deadline Monotonic Schedulability | |
289 | Analysis. Proceedings of the 24th IEEE Real-Time Systems Symposium, 2003. | |
290 | 9 - T. Baker. An Analysis of EDF Schedulability on a Multiprocessor. | |
291 | IEEE Transactions on Parallel and Distributed Systems, vol. 16, no. 8, | |
292 | pp 760-768, 2005. | |
293 | 10 - J. Goossens, S. Funk and S. Baruah, Priority-Driven Scheduling of | |
294 | Periodic Task Systems on Multiprocessors. Real-Time Systems Journal, | |
295 | vol. 25, no. 2–3, pp. 187–205, 2003. | |
296 | 11 - R. Davis and A. Burns. A Survey of Hard Real-Time Scheduling for | |
297 | Multiprocessor Systems. ACM Computing Surveys, vol. 43, no. 4, 2011. | |
298 | http://www-users.cs.york.ac.uk/~robdavis/papers/MPSurveyv5.0.pdf | |
299 | 12 - U. C. Devi and J. H. Anderson. Tardiness Bounds under Global EDF | |
300 | Scheduling on a Multiprocessor. Real-Time Systems Journal, vol. 32, | |
301 | no. 2, pp 133-189, 2008. | |
302 | 13 - P. Valente and G. Lipari. An Upper Bound to the Lateness of Soft | |
303 | Real-Time Tasks Scheduled by EDF on Multiprocessors. Proceedings of | |
304 | the 26th IEEE Real-Time Systems Symposium, 2005. | |
305 | 14 - J. Erickson, U. Devi and S. Baruah. Improved tardiness bounds for | |
306 | Global EDF. Proceedings of the 22nd Euromicro Conference on | |
307 | Real-Time Systems, 2010. | |
308 | ||
712e5e34 DF |
309 | |
310 | 4. Bandwidth management | |
311 | ======================= | |
312 | ||
b56bfc6c LA |
313 | As previously mentioned, in order for -deadline scheduling to be |
314 | effective and useful (that is, to be able to provide "runtime" time units | |
315 | within "deadline"), it is important to have some method to keep the allocation | |
316 | of the available fractions of CPU time to the various tasks under control. | |
317 | This is usually called "admission control" and if it is not performed, then | |
318 | no guarantee can be given on the actual scheduling of the -deadline tasks. | |
319 | ||
320 | As already stated in Section 3, a necessary condition to be respected to | |
3a3a58d4 | 321 | correctly schedule a set of real-time tasks is that the total utilization |
b56bfc6c LA |
322 | is smaller than M. When talking about -deadline tasks, this requires that |
323 | the sum of the ratio between runtime and period for all tasks is smaller | |
3a3a58d4 | 324 | than M. Notice that the ratio runtime/period is equivalent to the utilization |
b56bfc6c LA |
325 | of a "traditional" real-time task, and is also often referred to as |
326 | "bandwidth". | |
327 | The interface used to control the CPU bandwidth that can be allocated | |
328 | to -deadline tasks is similar to the one already used for -rt | |
0d9ba8b0 JL |
329 | tasks with real-time group scheduling (a.k.a. RT-throttling - see |
330 | Documentation/scheduler/sched-rt-group.txt), and is based on readable/ | |
331 | writable control files located in procfs (for system wide settings). | |
332 | Notice that per-group settings (controlled through cgroupfs) are still not | |
333 | defined for -deadline tasks, because more discussion is needed in order to | |
334 | figure out how we want to manage SCHED_DEADLINE bandwidth at the task group | |
335 | level. | |
336 | ||
337 | A main difference between deadline bandwidth management and RT-throttling | |
712e5e34 | 338 | is that -deadline tasks have bandwidth on their own (while -rt ones don't!), |
0d9ba8b0 | 339 | and thus we don't need a higher level throttling mechanism to enforce the |
b56bfc6c LA |
340 | desired bandwidth. In other words, this means that interface parameters are |
341 | only used at admission control time (i.e., when the user calls | |
342 | sched_setattr()). Scheduling is then performed considering actual tasks' | |
343 | parameters, so that CPU bandwidth is allocated to SCHED_DEADLINE tasks | |
344 | respecting their needs in terms of granularity. Therefore, using this simple | |
345 | interface we can put a cap on total utilization of -deadline tasks (i.e., | |
346 | \Sum (runtime_i / period_i) < global_dl_utilization_cap). | |
712e5e34 DF |
347 | |
348 | 4.1 System wide settings | |
349 | ------------------------ | |
350 | ||
351 | The system wide settings are configured under the /proc virtual file system. | |
352 | ||
0d9ba8b0 | 353 | For now the -rt knobs are used for -deadline admission control and the |
3a3a58d4 | 354 | -deadline runtime is accounted against the -rt runtime. We realize that this |
0d9ba8b0 JL |
355 | isn't entirely desirable; however, it is better to have a small interface for |
356 | now, and be able to change it easily later. The ideal situation (see 5.) is to | |
357 | run -rt tasks from a -deadline server; in which case the -rt bandwidth is a | |
358 | direct subset of dl_bw. | |
712e5e34 DF |
359 | |
360 | This means that, for a root_domain comprising M CPUs, -deadline tasks | |
361 | can be created while the sum of their bandwidths stays below: | |
362 | ||
363 | M * (sched_rt_runtime_us / sched_rt_period_us) | |
364 | ||
365 | It is also possible to disable this bandwidth management logic, and | |
366 | be thus free of oversubscribing the system up to any arbitrary level. | |
367 | This is done by writing -1 in /proc/sys/kernel/sched_rt_runtime_us. | |
368 | ||
369 | ||
370 | 4.2 Task interface | |
371 | ------------------ | |
372 | ||
373 | Specifying a periodic/sporadic task that executes for a given amount of | |
374 | runtime at each instance, and that is scheduled according to the urgency of | |
375 | its own timing constraints needs, in general, a way of declaring: | |
376 | - a (maximum/typical) instance execution time, | |
377 | - a minimum interval between consecutive instances, | |
378 | - a time constraint by which each instance must be completed. | |
379 | ||
380 | Therefore: | |
381 | * a new struct sched_attr, containing all the necessary fields is | |
382 | provided; | |
383 | * the new scheduling related syscalls that manipulate it, i.e., | |
384 | sched_setattr() and sched_getattr() are implemented. | |
385 | ||
386 | ||
387 | 4.3 Default behavior | |
388 | --------------------- | |
389 | ||
390 | The default value for SCHED_DEADLINE bandwidth is to have rt_runtime equal to | |
391 | 950000. With rt_period equal to 1000000, by default, it means that -deadline | |
392 | tasks can use at most 95%, multiplied by the number of CPUs that compose the | |
393 | root_domain, for each root_domain. | |
b56bfc6c LA |
394 | This means that non -deadline tasks will receive at least 5% of the CPU time, |
395 | and that -deadline tasks will receive their runtime with a guaranteed | |
396 | worst-case delay respect to the "deadline" parameter. If "deadline" = "period" | |
397 | and the cpuset mechanism is used to implement partitioned scheduling (see | |
398 | Section 5), then this simple setting of the bandwidth management is able to | |
399 | deterministically guarantee that -deadline tasks will receive their runtime | |
400 | in a period. | |
401 | ||
402 | Finally, notice that in order not to jeopardize the admission control a | |
403 | -deadline task cannot fork. | |
712e5e34 DF |
404 | |
405 | 5. Tasks CPU affinity | |
406 | ===================== | |
407 | ||
408 | -deadline tasks cannot have an affinity mask smaller that the entire | |
409 | root_domain they are created on. However, affinities can be specified | |
410 | through the cpuset facility (Documentation/cgroups/cpusets.txt). | |
411 | ||
412 | 5.1 SCHED_DEADLINE and cpusets HOWTO | |
413 | ------------------------------------ | |
414 | ||
415 | An example of a simple configuration (pin a -deadline task to CPU0) | |
416 | follows (rt-app is used to create a -deadline task). | |
417 | ||
418 | mkdir /dev/cpuset | |
419 | mount -t cgroup -o cpuset cpuset /dev/cpuset | |
420 | cd /dev/cpuset | |
421 | mkdir cpu0 | |
422 | echo 0 > cpu0/cpuset.cpus | |
423 | echo 0 > cpu0/cpuset.mems | |
424 | echo 1 > cpuset.cpu_exclusive | |
425 | echo 0 > cpuset.sched_load_balance | |
426 | echo 1 > cpu0/cpuset.cpu_exclusive | |
427 | echo 1 > cpu0/cpuset.mem_exclusive | |
428 | echo $$ > cpu0/tasks | |
429 | rt-app -t 100000:10000:d:0 -D5 (it is now actually superfluous to specify | |
430 | task affinity) | |
431 | ||
432 | 6. Future plans | |
433 | =============== | |
434 | ||
435 | Still missing: | |
436 | ||
437 | - refinements to deadline inheritance, especially regarding the possibility | |
438 | of retaining bandwidth isolation among non-interacting tasks. This is | |
439 | being studied from both theoretical and practical points of view, and | |
440 | hopefully we should be able to produce some demonstrative code soon; | |
441 | - (c)group based bandwidth management, and maybe scheduling; | |
442 | - access control for non-root users (and related security concerns to | |
443 | address), which is the best way to allow unprivileged use of the mechanisms | |
444 | and how to prevent non-root users "cheat" the system? | |
445 | ||
446 | As already discussed, we are planning also to merge this work with the EDF | |
447 | throttling patches [https://lkml.org/lkml/2010/2/23/239] but we still are in | |
448 | the preliminary phases of the merge and we really seek feedback that would | |
449 | help us decide on the direction it should take. | |
f5801933 JL |
450 | |
451 | Appendix A. Test suite | |
452 | ====================== | |
453 | ||
454 | The SCHED_DEADLINE policy can be easily tested using two applications that | |
455 | are part of a wider Linux Scheduler validation suite. The suite is | |
456 | available as a GitHub repository: https://github.com/scheduler-tools. | |
457 | ||
458 | The first testing application is called rt-app and can be used to | |
459 | start multiple threads with specific parameters. rt-app supports | |
460 | SCHED_{OTHER,FIFO,RR,DEADLINE} scheduling policies and their related | |
461 | parameters (e.g., niceness, priority, runtime/deadline/period). rt-app | |
462 | is a valuable tool, as it can be used to synthetically recreate certain | |
463 | workloads (maybe mimicking real use-cases) and evaluate how the scheduler | |
464 | behaves under such workloads. In this way, results are easily reproducible. | |
465 | rt-app is available at: https://github.com/scheduler-tools/rt-app. | |
466 | ||
467 | Thread parameters can be specified from the command line, with something like | |
468 | this: | |
469 | ||
470 | # rt-app -t 100000:10000:d -t 150000:20000:f:10 -D5 | |
471 | ||
472 | The above creates 2 threads. The first one, scheduled by SCHED_DEADLINE, | |
473 | executes for 10ms every 100ms. The second one, scheduled at SCHED_FIFO | |
474 | priority 10, executes for 20ms every 150ms. The test will run for a total | |
475 | of 5 seconds. | |
476 | ||
477 | More interestingly, configurations can be described with a json file that | |
478 | can be passed as input to rt-app with something like this: | |
479 | ||
480 | # rt-app my_config.json | |
481 | ||
482 | The parameters that can be specified with the second method are a superset | |
483 | of the command line options. Please refer to rt-app documentation for more | |
484 | details (<rt-app-sources>/doc/*.json). | |
485 | ||
486 | The second testing application is a modification of schedtool, called | |
487 | schedtool-dl, which can be used to setup SCHED_DEADLINE parameters for a | |
488 | certain pid/application. schedtool-dl is available at: | |
489 | https://github.com/scheduler-tools/schedtool-dl.git. | |
490 | ||
491 | The usage is straightforward: | |
492 | ||
493 | # schedtool -E -t 10000000:100000000 -e ./my_cpuhog_app | |
494 | ||
495 | With this, my_cpuhog_app is put to run inside a SCHED_DEADLINE reservation | |
496 | of 10ms every 100ms (note that parameters are expressed in microseconds). | |
497 | You can also use schedtool to create a reservation for an already running | |
498 | application, given that you know its pid: | |
499 | ||
500 | # schedtool -E -t 10000000:100000000 my_app_pid | |
13924d2a JL |
501 | |
502 | Appendix B. Minimal main() | |
503 | ========================== | |
504 | ||
505 | We provide in what follows a simple (ugly) self-contained code snippet | |
506 | showing how SCHED_DEADLINE reservations can be created by a real-time | |
507 | application developer. | |
508 | ||
509 | #define _GNU_SOURCE | |
510 | #include <unistd.h> | |
511 | #include <stdio.h> | |
512 | #include <stdlib.h> | |
513 | #include <string.h> | |
514 | #include <time.h> | |
515 | #include <linux/unistd.h> | |
516 | #include <linux/kernel.h> | |
517 | #include <linux/types.h> | |
518 | #include <sys/syscall.h> | |
519 | #include <pthread.h> | |
520 | ||
521 | #define gettid() syscall(__NR_gettid) | |
522 | ||
523 | #define SCHED_DEADLINE 6 | |
524 | ||
525 | /* XXX use the proper syscall numbers */ | |
526 | #ifdef __x86_64__ | |
527 | #define __NR_sched_setattr 314 | |
528 | #define __NR_sched_getattr 315 | |
529 | #endif | |
530 | ||
531 | #ifdef __i386__ | |
532 | #define __NR_sched_setattr 351 | |
533 | #define __NR_sched_getattr 352 | |
534 | #endif | |
535 | ||
536 | #ifdef __arm__ | |
537 | #define __NR_sched_setattr 380 | |
538 | #define __NR_sched_getattr 381 | |
539 | #endif | |
540 | ||
541 | static volatile int done; | |
542 | ||
543 | struct sched_attr { | |
544 | __u32 size; | |
545 | ||
546 | __u32 sched_policy; | |
547 | __u64 sched_flags; | |
548 | ||
549 | /* SCHED_NORMAL, SCHED_BATCH */ | |
550 | __s32 sched_nice; | |
551 | ||
552 | /* SCHED_FIFO, SCHED_RR */ | |
553 | __u32 sched_priority; | |
554 | ||
555 | /* SCHED_DEADLINE (nsec) */ | |
556 | __u64 sched_runtime; | |
557 | __u64 sched_deadline; | |
558 | __u64 sched_period; | |
559 | }; | |
560 | ||
561 | int sched_setattr(pid_t pid, | |
562 | const struct sched_attr *attr, | |
563 | unsigned int flags) | |
564 | { | |
565 | return syscall(__NR_sched_setattr, pid, attr, flags); | |
566 | } | |
567 | ||
568 | int sched_getattr(pid_t pid, | |
569 | struct sched_attr *attr, | |
570 | unsigned int size, | |
571 | unsigned int flags) | |
572 | { | |
573 | return syscall(__NR_sched_getattr, pid, attr, size, flags); | |
574 | } | |
575 | ||
576 | void *run_deadline(void *data) | |
577 | { | |
578 | struct sched_attr attr; | |
579 | int x = 0; | |
580 | int ret; | |
581 | unsigned int flags = 0; | |
582 | ||
583 | printf("deadline thread started [%ld]\n", gettid()); | |
584 | ||
585 | attr.size = sizeof(attr); | |
586 | attr.sched_flags = 0; | |
587 | attr.sched_nice = 0; | |
588 | attr.sched_priority = 0; | |
589 | ||
590 | /* This creates a 10ms/30ms reservation */ | |
591 | attr.sched_policy = SCHED_DEADLINE; | |
592 | attr.sched_runtime = 10 * 1000 * 1000; | |
593 | attr.sched_period = attr.sched_deadline = 30 * 1000 * 1000; | |
594 | ||
595 | ret = sched_setattr(0, &attr, flags); | |
596 | if (ret < 0) { | |
597 | done = 0; | |
598 | perror("sched_setattr"); | |
599 | exit(-1); | |
600 | } | |
601 | ||
602 | while (!done) { | |
603 | x++; | |
604 | } | |
605 | ||
606 | printf("deadline thread dies [%ld]\n", gettid()); | |
607 | return NULL; | |
608 | } | |
609 | ||
610 | int main (int argc, char **argv) | |
611 | { | |
612 | pthread_t thread; | |
613 | ||
614 | printf("main thread [%ld]\n", gettid()); | |
615 | ||
616 | pthread_create(&thread, NULL, run_deadline, NULL); | |
617 | ||
618 | sleep(10); | |
619 | ||
620 | done = 1; | |
621 | pthread_join(thread, NULL); | |
622 | ||
623 | printf("main dies [%ld]\n", gettid()); | |
624 | return 0; | |
625 | } |