sched-deadline.txt 28 KB

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