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