blob: a029891a82288d29c7c8f81fb6fc2acb999274b6 [file] [log] [blame]
Dario Faggioli712e5e32014-01-27 12:20:15 +01001 Deadline Task Scheduling
2 ------------------------
3
4CONTENTS
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
18
19
200. WARNING
21==========
22
23 Fiddling with these settings can result in an unpredictable or even unstable
24 system behavior. As for -rt (group) scheduling, it is assumed that root users
25 know what they're doing.
26
27
281. Overview
29===========
30
31 The SCHED_DEADLINE policy contained inside the sched_dl scheduling class is
32 basically an implementation of the Earliest Deadline First (EDF) scheduling
33 algorithm, augmented with a mechanism (called Constant Bandwidth Server, CBS)
34 that makes it possible to isolate the behavior of tasks between each other.
35
36
372. Scheduling algorithm
38==================
39
40 SCHED_DEADLINE uses three parameters, named "runtime", "period", and
41 "deadline" to schedule tasks. A SCHED_DEADLINE task is guaranteed to receive
42 "runtime" microseconds of execution time every "period" microseconds, and
43 these "runtime" microseconds are available within "deadline" microseconds
44 from the beginning of the period. In order to implement this behaviour,
45 every time the task wakes up, the scheduler computes a "scheduling deadline"
46 consistent with the guarantee (using the CBS[2,3] algorithm). Tasks are then
47 scheduled using EDF[1] on these scheduling deadlines (the task with the
Luca Abeniad67dc32014-09-09 10:57:12 +010048 earliest scheduling deadline is selected for execution). Notice that this
Dario Faggioli712e5e32014-01-27 12:20:15 +010049 guaranteed is respected if a proper "admission control" strategy (see Section
50 "4. Bandwidth management") is used.
51
52 Summing up, the CBS[2,3] algorithms assigns scheduling deadlines to tasks so
53 that each task runs for at most its runtime every period, avoiding any
54 interference between different tasks (bandwidth isolation), while the EDF[1]
Luca Abeniad67dc32014-09-09 10:57:12 +010055 algorithm selects the task with the earliest scheduling deadline as the one
56 to be executed next. Thanks to this feature, tasks that do not strictly comply
57 with the "traditional" real-time task model (see Section 3) can effectively
58 use the new policy.
Dario Faggioli712e5e32014-01-27 12:20:15 +010059
60 In more details, the CBS algorithm assigns scheduling deadlines to
61 tasks in the following way:
62
63 - Each SCHED_DEADLINE task is characterised by the "runtime",
64 "deadline", and "period" parameters;
65
66 - The state of the task is described by a "scheduling deadline", and
Luca Abeniad67dc32014-09-09 10:57:12 +010067 a "remaining runtime". These two parameters are initially set to 0;
Dario Faggioli712e5e32014-01-27 12:20:15 +010068
69 - When a SCHED_DEADLINE task wakes up (becomes ready for execution),
70 the scheduler checks if
71
Luca Abeniad67dc32014-09-09 10:57:12 +010072 remaining runtime runtime
73 ---------------------------------- > ---------
74 scheduling deadline - current time period
Dario Faggioli712e5e32014-01-27 12:20:15 +010075
76 then, if the scheduling deadline is smaller than the current time, or
77 this condition is verified, the scheduling deadline and the
Luca Abeniad67dc32014-09-09 10:57:12 +010078 remaining runtime are re-initialised as
Dario Faggioli712e5e32014-01-27 12:20:15 +010079
80 scheduling deadline = current time + deadline
Luca Abeniad67dc32014-09-09 10:57:12 +010081 remaining runtime = runtime
Dario Faggioli712e5e32014-01-27 12:20:15 +010082
Luca Abeniad67dc32014-09-09 10:57:12 +010083 otherwise, the scheduling deadline and the remaining runtime are
Dario Faggioli712e5e32014-01-27 12:20:15 +010084 left unchanged;
85
86 - When a SCHED_DEADLINE task executes for an amount of time t, its
Luca Abeniad67dc32014-09-09 10:57:12 +010087 remaining runtime is decreased as
Dario Faggioli712e5e32014-01-27 12:20:15 +010088
Luca Abeniad67dc32014-09-09 10:57:12 +010089 remaining runtime = remaining runtime - t
Dario Faggioli712e5e32014-01-27 12:20:15 +010090
91 (technically, the runtime is decreased at every tick, or when the
92 task is descheduled / preempted);
93
Luca Abeniad67dc32014-09-09 10:57:12 +010094 - When the remaining runtime becomes less or equal than 0, the task is
Dario Faggioli712e5e32014-01-27 12:20:15 +010095 said to be "throttled" (also known as "depleted" in real-time literature)
96 and cannot be scheduled until its scheduling deadline. The "replenishment
97 time" for this task (see next item) is set to be equal to the current
98 value of the scheduling deadline;
99
100 - When the current time is equal to the replenishment time of a
Luca Abeniad67dc32014-09-09 10:57:12 +0100101 throttled task, the scheduling deadline and the remaining runtime are
Dario Faggioli712e5e32014-01-27 12:20:15 +0100102 updated as
103
104 scheduling deadline = scheduling deadline + period
Luca Abeniad67dc32014-09-09 10:57:12 +0100105 remaining runtime = remaining runtime + runtime
Dario Faggioli712e5e32014-01-27 12:20:15 +0100106
107
1083. Scheduling Real-Time Tasks
109=============================
110
111 * BIG FAT WARNING ******************************************************
112 *
113 * This section contains a (not-thorough) summary on classical deadline
114 * scheduling theory, and how it applies to SCHED_DEADLINE.
115 * The reader can "safely" skip to Section 4 if only interested in seeing
116 * how the scheduling policy can be used. Anyway, we strongly recommend
117 * to come back here and continue reading (once the urge for testing is
118 * satisfied :P) to be sure of fully understanding all technical details.
119 ************************************************************************
120
121 There are no limitations on what kind of task can exploit this new
122 scheduling discipline, even if it must be said that it is particularly
123 suited for periodic or sporadic real-time tasks that need guarantees on their
124 timing behavior, e.g., multimedia, streaming, control applications, etc.
125
126 A typical real-time task is composed of a repetition of computation phases
127 (task instances, or jobs) which are activated on a periodic or sporadic
128 fashion.
129 Each job J_j (where J_j is the j^th job of the task) is characterised by an
130 arrival time r_j (the time when the job starts), an amount of computation
131 time c_j needed to finish the job, and a job absolute deadline d_j, which
132 is the time within which the job should be finished. The maximum execution
133 time max_j{c_j} is called "Worst Case Execution Time" (WCET) for the task.
134 A real-time task can be periodic with period P if r_{j+1} = r_j + P, or
135 sporadic with minimum inter-arrival time P is r_{j+1} >= r_j + P. Finally,
136 d_j = r_j + D, where D is the task's relative deadline.
137
138 SCHED_DEADLINE can be used to schedule real-time tasks guaranteeing that
139 the jobs' deadlines of a task are respected. In order to do this, a task
140 must be scheduled by setting:
141
142 - runtime >= WCET
143 - deadline = D
144 - period <= P
145
146 IOW, if runtime >= WCET and if period is >= P, then the scheduling deadlines
147 and the absolute deadlines (d_j) coincide, so a proper admission control
148 allows to respect the jobs' absolute deadlines for this task (this is what is
149 called "hard schedulability property" and is an extension of Lemma 1 of [2]).
Luca Abeniad67dc32014-09-09 10:57:12 +0100150 Notice that if runtime > deadline the admission control will surely reject
151 this task, as it is not possible to respect its temporal constraints.
Dario Faggioli712e5e32014-01-27 12:20:15 +0100152
153 References:
154 1 - C. L. Liu and J. W. Layland. Scheduling algorithms for multiprogram-
155 ming in a hard-real-time environment. Journal of the Association for
156 Computing Machinery, 20(1), 1973.
157 2 - L. Abeni , G. Buttazzo. Integrating Multimedia Applications in Hard
158 Real-Time Systems. Proceedings of the 19th IEEE Real-time Systems
159 Symposium, 1998. http://retis.sssup.it/~giorgio/paps/1998/rtss98-cbs.pdf
160 3 - L. Abeni. Server Mechanisms for Multimedia Applications. ReTiS Lab
Luca Abeniad67dc32014-09-09 10:57:12 +0100161 Technical Report. http://disi.unitn.it/~abeni/tr-98-01.pdf
Dario Faggioli712e5e32014-01-27 12:20:15 +0100162
1634. Bandwidth management
164=======================
165
166 In order for the -deadline scheduling to be effective and useful, it is
167 important to have some method to keep the allocation of the available CPU
168 bandwidth to the tasks under control.
169 This is usually called "admission control" and if it is not performed at all,
170 no guarantee can be given on the actual scheduling of the -deadline tasks.
171
172 Since when RT-throttling has been introduced each task group has a bandwidth
173 associated, calculated as a certain amount of runtime over a period.
174 Moreover, to make it possible to manipulate such bandwidth, readable/writable
175 controls have been added to both procfs (for system wide settings) and cgroupfs
176 (for per-group settings).
177 Therefore, the same interface is being used for controlling the bandwidth
178 distrubution to -deadline tasks.
179
180 However, more discussion is needed in order to figure out how we want to manage
181 SCHED_DEADLINE bandwidth at the task group level. Therefore, SCHED_DEADLINE
182 uses (for now) a less sophisticated, but actually very sensible, mechanism to
183 ensure that a certain utilization cap is not overcome per each root_domain.
184
185 Another main difference between deadline bandwidth management and RT-throttling
186 is that -deadline tasks have bandwidth on their own (while -rt ones don't!),
187 and thus we don't need an higher level throttling mechanism to enforce the
188 desired bandwidth.
189
1904.1 System wide settings
191------------------------
192
193 The system wide settings are configured under the /proc virtual file system.
194
195 For now the -rt knobs are used for dl admission control and the -deadline
196 runtime is accounted against the -rt runtime. We realise that this isn't
197 entirely desirable; however, it is better to have a small interface for now,
198 and be able to change it easily later. The ideal situation (see 5.) is to run
199 -rt tasks from a -deadline server; in which case the -rt bandwidth is a direct
200 subset of dl_bw.
201
202 This means that, for a root_domain comprising M CPUs, -deadline tasks
203 can be created while the sum of their bandwidths stays below:
204
205 M * (sched_rt_runtime_us / sched_rt_period_us)
206
207 It is also possible to disable this bandwidth management logic, and
208 be thus free of oversubscribing the system up to any arbitrary level.
209 This is done by writing -1 in /proc/sys/kernel/sched_rt_runtime_us.
210
211
2124.2 Task interface
213------------------
214
215 Specifying a periodic/sporadic task that executes for a given amount of
216 runtime at each instance, and that is scheduled according to the urgency of
217 its own timing constraints needs, in general, a way of declaring:
218 - a (maximum/typical) instance execution time,
219 - a minimum interval between consecutive instances,
220 - a time constraint by which each instance must be completed.
221
222 Therefore:
223 * a new struct sched_attr, containing all the necessary fields is
224 provided;
225 * the new scheduling related syscalls that manipulate it, i.e.,
226 sched_setattr() and sched_getattr() are implemented.
227
228
2294.3 Default behavior
230---------------------
231
232 The default value for SCHED_DEADLINE bandwidth is to have rt_runtime equal to
233 950000. With rt_period equal to 1000000, by default, it means that -deadline
234 tasks can use at most 95%, multiplied by the number of CPUs that compose the
235 root_domain, for each root_domain.
236
237 A -deadline task cannot fork.
238
2395. Tasks CPU affinity
240=====================
241
242 -deadline tasks cannot have an affinity mask smaller that the entire
243 root_domain they are created on. However, affinities can be specified
244 through the cpuset facility (Documentation/cgroups/cpusets.txt).
245
2465.1 SCHED_DEADLINE and cpusets HOWTO
247------------------------------------
248
249 An example of a simple configuration (pin a -deadline task to CPU0)
250 follows (rt-app is used to create a -deadline task).
251
252 mkdir /dev/cpuset
253 mount -t cgroup -o cpuset cpuset /dev/cpuset
254 cd /dev/cpuset
255 mkdir cpu0
256 echo 0 > cpu0/cpuset.cpus
257 echo 0 > cpu0/cpuset.mems
258 echo 1 > cpuset.cpu_exclusive
259 echo 0 > cpuset.sched_load_balance
260 echo 1 > cpu0/cpuset.cpu_exclusive
261 echo 1 > cpu0/cpuset.mem_exclusive
262 echo $$ > cpu0/tasks
263 rt-app -t 100000:10000:d:0 -D5 (it is now actually superfluous to specify
264 task affinity)
265
2666. Future plans
267===============
268
269 Still missing:
270
271 - refinements to deadline inheritance, especially regarding the possibility
272 of retaining bandwidth isolation among non-interacting tasks. This is
273 being studied from both theoretical and practical points of view, and
274 hopefully we should be able to produce some demonstrative code soon;
275 - (c)group based bandwidth management, and maybe scheduling;
276 - access control for non-root users (and related security concerns to
277 address), which is the best way to allow unprivileged use of the mechanisms
278 and how to prevent non-root users "cheat" the system?
279
280 As already discussed, we are planning also to merge this work with the EDF
281 throttling patches [https://lkml.org/lkml/2010/2/23/239] but we still are in
282 the preliminary phases of the merge and we really seek feedback that would
283 help us decide on the direction it should take.