sched-deadline.rst 37 KB

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