1 | .. comment SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
2 | |
---|
3 | .. COMMENT: COPYRIGHT (c) 1988-2008. |
---|
4 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
5 | .. COMMENT: All rights reserved. |
---|
6 | |
---|
7 | Scheduling Concepts |
---|
8 | ******************* |
---|
9 | |
---|
10 | .. index:: scheduling |
---|
11 | .. index:: task scheduling |
---|
12 | |
---|
13 | Introduction |
---|
14 | ============ |
---|
15 | |
---|
16 | The concept of scheduling in real-time systems dictates the ability to provide |
---|
17 | immediate response to specific external events, particularly the necessity of |
---|
18 | scheduling tasks to run within a specified time limit after the occurrence of |
---|
19 | an event. For example, software embedded in life-support systems used to |
---|
20 | monitor hospital patients must take instant action if a change in the patient's |
---|
21 | status is detected. |
---|
22 | |
---|
23 | The component of RTEMS responsible for providing this capability is |
---|
24 | appropriately called the scheduler. The scheduler's sole purpose is to |
---|
25 | allocate the all important resource of processor time to the various tasks |
---|
26 | competing for attention. |
---|
27 | |
---|
28 | Scheduling Algorithms |
---|
29 | ===================== |
---|
30 | |
---|
31 | .. index:: scheduling algorithms |
---|
32 | |
---|
33 | RTEMS provides a plugin framework which allows it to support multiple |
---|
34 | scheduling algorithms. RTEMS now includes multiple scheduling algorithms in the |
---|
35 | SuperCore and the user can select which of these they wish to use in their |
---|
36 | application. In addition, the user can implement their own scheduling |
---|
37 | algorithm and configure RTEMS to use it. |
---|
38 | |
---|
39 | Supporting multiple scheduling algorithms gives the end user the option to |
---|
40 | select the algorithm which is most appropriate to their use case. Most |
---|
41 | real-time operating systems schedule tasks using a priority based algorithm, |
---|
42 | possibly with preemption control. The classic RTEMS scheduling algorithm which |
---|
43 | was the only algorithm available in RTEMS 4.10 and earlier, is a priority based |
---|
44 | scheduling algorithm. This scheduling algoritm is suitable for single core |
---|
45 | (e.g. non-SMP) systems and is now known as the *Deterministic Priority |
---|
46 | Scheduler*. Unless the user configures another scheduling algorithm, RTEMS |
---|
47 | will use this on single core systems. |
---|
48 | |
---|
49 | Priority Scheduling |
---|
50 | ------------------- |
---|
51 | .. index:: priority scheduling |
---|
52 | |
---|
53 | When using priority based scheduling, RTEMS allocates the processor using a |
---|
54 | priority-based, preemptive algorithm augmented to provide round-robin |
---|
55 | characteristics within individual priority groups. The goal of this algorithm |
---|
56 | is to guarantee that the task which is executing on the processor at any point |
---|
57 | in time is the one with the highest priority among all tasks in the ready |
---|
58 | state. |
---|
59 | |
---|
60 | When a task is added to the ready chain, it is placed behind all other tasks of |
---|
61 | the same priority. This rule provides a round-robin within priority group |
---|
62 | scheduling characteristic. This means that in a group of equal priority tasks, |
---|
63 | tasks will execute in the order they become ready or FIFO order. Even though |
---|
64 | there are ways to manipulate and adjust task priorities, the most important |
---|
65 | rule to remember is: |
---|
66 | |
---|
67 | .. note:: |
---|
68 | |
---|
69 | Priority based scheduling algorithms will always select the highest priority |
---|
70 | task that is ready to run when allocating the processor to a task. |
---|
71 | |
---|
72 | Priority scheduling is the most commonly used scheduling algorithm. It should |
---|
73 | be used by applications in which multiple tasks contend for CPU time or other |
---|
74 | resources and there is a need to ensure certain tasks are given priority over |
---|
75 | other tasks. |
---|
76 | |
---|
77 | There are a few common methods of accomplishing the mechanics of this |
---|
78 | algorithm. These ways involve a list or chain of tasks in the ready state. |
---|
79 | |
---|
80 | - The least efficient method is to randomly place tasks in the ready chain |
---|
81 | forcing the scheduler to scan the entire chain to determine which task |
---|
82 | receives the processor. |
---|
83 | |
---|
84 | - A more efficient method is to schedule the task by placing it in the proper |
---|
85 | place on the ready chain based on the designated scheduling criteria at the |
---|
86 | time it enters the ready state. Thus, when the processor is free, the first |
---|
87 | task on the ready chain is allocated the processor. |
---|
88 | |
---|
89 | - Another mechanism is to maintain a list of FIFOs per priority. When a task |
---|
90 | is readied, it is placed on the rear of the FIFO for its priority. This |
---|
91 | method is often used with a bitmap to assist in locating which FIFOs have |
---|
92 | ready tasks on them. |
---|
93 | |
---|
94 | RTEMS currently includes multiple priority based scheduling algorithms as well |
---|
95 | as other algorithms which incorporate deadline. Each algorithm is discussed in |
---|
96 | the following sections. |
---|
97 | |
---|
98 | Deterministic Priority Scheduler |
---|
99 | -------------------------------- |
---|
100 | |
---|
101 | This is the scheduler implementation which has always been in RTEMS. After the |
---|
102 | 4.10 release series, it was factored into pluggable scheduler selection. It |
---|
103 | schedules tasks using a priority based algorithm which takes into account |
---|
104 | preemption. It is implemented using an array of FIFOs with a FIFO per |
---|
105 | priority. It maintains a bitmap which is used to track which priorities have |
---|
106 | ready tasks. |
---|
107 | |
---|
108 | This algorithm is deterministic (e.g. predictable and fixed) in execution time. |
---|
109 | This comes at the cost of using slightly over three (3) kilobytes of RAM on a |
---|
110 | system configured to support 256 priority levels. |
---|
111 | |
---|
112 | This scheduler is only aware of a single core. |
---|
113 | |
---|
114 | Simple Priority Scheduler |
---|
115 | ------------------------- |
---|
116 | |
---|
117 | This scheduler implementation has the same behaviour as the Deterministic |
---|
118 | Priority Scheduler but uses only one linked list to manage all ready tasks. |
---|
119 | When a task is readied, a linear search of that linked list is performed to |
---|
120 | determine where to insert the newly readied task. |
---|
121 | |
---|
122 | This algorithm uses much less RAM than the Deterministic Priority Scheduler but |
---|
123 | is *O(n)* where *n* is the number of ready tasks. In a small system with a |
---|
124 | small number of tasks, this will not be a performance issue. Reducing RAM |
---|
125 | consumption is often critical in small systems which are incapable of |
---|
126 | supporting a large number of tasks. |
---|
127 | |
---|
128 | This scheduler is only aware of a single core. |
---|
129 | |
---|
130 | Simple SMP Priority Scheduler |
---|
131 | ----------------------------- |
---|
132 | |
---|
133 | This scheduler is based upon the Simple Priority Scheduler and is designed to |
---|
134 | have the same behaviour on a single core system. But this scheduler is capable |
---|
135 | of scheduling threads across multiple cores in an SMP system. When given a |
---|
136 | choice of replacing one of two threads at equal priority on different cores, |
---|
137 | this algorithm favors replacing threads which are preemptible and have executed |
---|
138 | the longest. |
---|
139 | |
---|
140 | This algorithm is non-deterministic. When scheduling, it must consider which |
---|
141 | tasks are to be executed on each core while avoiding superfluous task |
---|
142 | migrations. |
---|
143 | |
---|
144 | Earliest Deadline First Scheduler |
---|
145 | --------------------------------- |
---|
146 | .. index:: earliest deadline first scheduling |
---|
147 | |
---|
148 | This is an alternative scheduler in RTEMS for single core applications. The |
---|
149 | primary EDF advantage is high total CPU utilization (theoretically up to |
---|
150 | 100%). It assumes that tasks have priorities equal to deadlines. |
---|
151 | |
---|
152 | This EDF is initially preemptive, however, individual tasks may be declared |
---|
153 | not-preemptive. Deadlines are declared using only Rate Monotonic manager which |
---|
154 | goal is to handle periodic behavior. Period is always equal to deadline. All |
---|
155 | ready tasks reside in a single ready queue implemented using a red-black tree. |
---|
156 | |
---|
157 | This implementation of EDF schedules two different types of task priority types |
---|
158 | while each task may switch between the two types within its execution. If a |
---|
159 | task does have a deadline declared using the Rate Monotonic manager, the task |
---|
160 | is deadline-driven and its priority is equal to deadline. On the contrary if a |
---|
161 | task does not have any deadline or the deadline is cancelled using the Rate |
---|
162 | Monotonic manager, the task is considered a background task with priority equal |
---|
163 | to that assigned upon initialization in the same manner as for priority |
---|
164 | scheduler. Each background task is of a lower importance than each |
---|
165 | deadline-driven one and is scheduled when no deadline-driven task and no higher |
---|
166 | priority background task is ready to run. |
---|
167 | |
---|
168 | Every deadline-driven scheduling algorithm requires means for tasks to claim a |
---|
169 | deadline. The Rate Monotonic Manager is responsible for handling periodic |
---|
170 | execution. In RTEMS periods are equal to deadlines, thus if a task announces a |
---|
171 | period, it has to be finished until the end of this period. The call of |
---|
172 | ``rtems_rate_monotonic_period`` passes the scheduler the length of oncoming |
---|
173 | deadline. Moreover, the ``rtems_rate_monotonic_cancel`` and |
---|
174 | ``rtems_rate_monotonic_delete`` calls clear the deadlines assigned to the task. |
---|
175 | |
---|
176 | Constant Bandwidth Server Scheduling (CBS) |
---|
177 | ------------------------------------------ |
---|
178 | .. index:: constant bandwidth server scheduling |
---|
179 | |
---|
180 | This is an alternative scheduler in RTEMS for single core applications. The |
---|
181 | CBS is a budget aware extension of EDF scheduler. The main goal of this |
---|
182 | scheduler is to ensure temporal isolation of tasks meaning that a task's |
---|
183 | execution in terms of meeting deadlines must not be influenced by other tasks |
---|
184 | as if they were run on multiple independent processors. |
---|
185 | |
---|
186 | Each task can be assigned a server (current implementation supports only one |
---|
187 | task per server). The server is characterized by period (deadline) and |
---|
188 | computation time (budget). The ratio budget/period yields bandwidth, which is |
---|
189 | the fraction of CPU to be reserved by the scheduler for each subsequent period. |
---|
190 | |
---|
191 | The CBS is equipped with a set of rules applied to tasks attached to servers |
---|
192 | ensuring that deadline miss because of another task cannot occur. In case a |
---|
193 | task breaks one of the rules, its priority is pulled to background until the |
---|
194 | end of its period and then restored again. The rules are: |
---|
195 | |
---|
196 | - Task cannot exceed its registered budget, |
---|
197 | |
---|
198 | - Task cannot be unblocked when a ratio between remaining budget and remaining |
---|
199 | deadline is higher than declared bandwidth. |
---|
200 | |
---|
201 | The CBS provides an extensive API. Unlike EDF, the |
---|
202 | ``rtems_rate_monotonic_period`` does not declare a deadline because it is |
---|
203 | carried out using CBS API. This call only announces next period. |
---|
204 | |
---|
205 | Scheduling Modification Mechanisms |
---|
206 | ================================== |
---|
207 | |
---|
208 | .. index:: scheduling mechanisms |
---|
209 | |
---|
210 | RTEMS provides four mechanisms which allow the user to alter the task |
---|
211 | scheduling decisions: |
---|
212 | |
---|
213 | - user-selectable task priority level |
---|
214 | |
---|
215 | - task preemption control |
---|
216 | |
---|
217 | - task timeslicing control |
---|
218 | |
---|
219 | - manual round-robin selection |
---|
220 | |
---|
221 | Each of these methods provides a powerful capability to customize sets of tasks |
---|
222 | to satisfy the unique and particular requirements encountered in custom |
---|
223 | real-time applications. Although each mechanism operates independently, there |
---|
224 | is a precedence relationship which governs the effects of scheduling |
---|
225 | modifications. The evaluation order for scheduling characteristics is always |
---|
226 | priority, preemption mode, and timeslicing. When reading the descriptions of |
---|
227 | timeslicing and manual round-robin it is important to keep in mind that |
---|
228 | preemption (if enabled) of a task by higher priority tasks will occur as |
---|
229 | required, overriding the other factors presented in the description. |
---|
230 | |
---|
231 | Task Priority and Scheduling |
---|
232 | ---------------------------- |
---|
233 | .. index:: task priority |
---|
234 | |
---|
235 | The most significant task scheduling modification mechanism is the ability for |
---|
236 | the user to assign a priority level to each individual task when it is created |
---|
237 | and to alter a task's priority at run-time. RTEMS supports up to 255 priority |
---|
238 | levels. Level 255 is the lowest priority and level 1 is the highest. |
---|
239 | |
---|
240 | Preemption |
---|
241 | ---------- |
---|
242 | .. index:: preemption |
---|
243 | |
---|
244 | Another way the user can alter the basic scheduling algorithm is by |
---|
245 | manipulating the preemption mode flag (``RTEMS_PREEMPT_MASK``) of individual |
---|
246 | tasks. If preemption is disabled for a task (``RTEMS_NO_PREEMPT``), then the |
---|
247 | task will not relinquish control of the processor until it terminates, blocks, |
---|
248 | or re-enables preemption. Even tasks which become ready to run and possess |
---|
249 | higher priority levels will not be allowed to execute. Note that the |
---|
250 | preemption setting has no effect on the manner in which a task is scheduled. |
---|
251 | It only applies once a task has control of the processor. |
---|
252 | |
---|
253 | Timeslicing |
---|
254 | ----------- |
---|
255 | .. index:: timeslicing |
---|
256 | .. index:: round robin scheduling |
---|
257 | |
---|
258 | Timeslicing or round-robin scheduling is an additional method which can be used |
---|
259 | to alter the basic scheduling algorithm. Like preemption, timeslicing is |
---|
260 | specified on a task by task basis using the timeslicing mode flag |
---|
261 | (``RTEMS_TIMESLICE_MASK``). If timeslicing is enabled for a task |
---|
262 | (``RTEMS_TIMESLICE``), then RTEMS will limit the amount of time the task can |
---|
263 | execute before the processor is allocated to another task. Each tick of the |
---|
264 | real-time clock reduces the currently running task's timeslice. When the |
---|
265 | execution time equals the timeslice, RTEMS will dispatch another task of the |
---|
266 | same priority to execute. If there are no other tasks of the same priority |
---|
267 | ready to execute, then the current task is allocated an additional timeslice |
---|
268 | and continues to run. Remember that a higher priority task will preempt the |
---|
269 | task (unless preemption is disabled) as soon as it is ready to run, even if the |
---|
270 | task has not used up its entire timeslice. |
---|
271 | |
---|
272 | Manual Round-Robin |
---|
273 | ------------------ |
---|
274 | .. index:: manual round robin |
---|
275 | |
---|
276 | The final mechanism for altering the RTEMS scheduling algorithm is called |
---|
277 | manual round-robin. Manual round-robin is invoked by using |
---|
278 | the ``rtems_task_wake_after`` directive with a time interval of |
---|
279 | ``RTEMS_YIELD_PROCESSOR``. This allows a task to give up the processor and be |
---|
280 | immediately returned to the ready chain at the end of its priority group. If |
---|
281 | no other tasks of the same priority are ready to run, then the task does not |
---|
282 | lose control of the processor. |
---|
283 | |
---|
284 | Dispatching Tasks |
---|
285 | ================= |
---|
286 | .. index:: dispatching |
---|
287 | |
---|
288 | The dispatcher is the RTEMS component responsible for allocating the processor |
---|
289 | to a ready task. In order to allocate the processor to one task, it must be |
---|
290 | deallocated or retrieved from the task currently using it. This involves a |
---|
291 | concept called a context switch. To perform a context switch, the dispatcher |
---|
292 | saves the context of the current task and restores the context of the task |
---|
293 | which has been allocated to the processor. Saving and restoring a task's |
---|
294 | context is the storing/loading of all the essential information about a task to |
---|
295 | enable it to continue execution without any effects of the interruption. For |
---|
296 | example, the contents of a task's register set must be the same when it is |
---|
297 | given the processor as they were when it was taken away. All of the |
---|
298 | information that must be saved or restored for a context switch is located |
---|
299 | either in the TCB or on the task's stacks. |
---|
300 | |
---|
301 | Tasks that utilize a numeric coprocessor and are created with the |
---|
302 | ``RTEMS_FLOATING_POINT`` attribute require additional operations during a |
---|
303 | context switch. These additional operations are necessary to save and restore |
---|
304 | the floating point context of ``RTEMS_FLOATING_POINT`` tasks. To avoid |
---|
305 | unnecessary save and restore operations, the state of the numeric coprocessor |
---|
306 | is only saved when a ``RTEMS_FLOATING_POINT`` task is dispatched and that task |
---|
307 | was not the last task to utilize the coprocessor. |
---|
308 | |
---|
309 | Task State Transitions |
---|
310 | ====================== |
---|
311 | .. index:: task state transitions |
---|
312 | |
---|
313 | Tasks in an RTEMS system must always be in one of the five allowable task |
---|
314 | states. These states are: executing, ready, blocked, dormant, and |
---|
315 | non-existent. |
---|
316 | |
---|
317 | A task occupies the non-existent state before a ``rtems_task_create`` has been |
---|
318 | issued on its behalf. A task enters the non-existent state from any other |
---|
319 | state in the system when it is deleted with the ``rtems_task_delete`` |
---|
320 | directive. While a task occupies this state it does not have a TCB or a task |
---|
321 | ID assigned to it; therefore, no other tasks in the system may reference this |
---|
322 | task. |
---|
323 | |
---|
324 | When a task is created via the ``rtems_task_create`` directive it enters the |
---|
325 | dormant state. This state is not entered through any other means. Although |
---|
326 | the task exists in the system, it cannot actively compete for system resources. |
---|
327 | It will remain in the dormant state until it is started via the |
---|
328 | ``rtems_task_start`` directive, at which time it enters the ready state. The |
---|
329 | task is now permitted to be scheduled for the processor and to compete for |
---|
330 | other system resources. |
---|
331 | |
---|
332 | .. figure:: ../images/c_user/states.png |
---|
333 | :width: 70% |
---|
334 | :align: center |
---|
335 | :alt: Task State Transitions |
---|
336 | |
---|
337 | A task occupies the blocked state whenever it is unable to be scheduled to run. |
---|
338 | A running task may block itself or be blocked by other tasks in the system. |
---|
339 | The running task blocks itself through voluntary operations that cause the task |
---|
340 | to wait. The only way a task can block a task other than itself is with the |
---|
341 | ``rtems_task_suspend`` directive. A task enters the blocked state due to any |
---|
342 | of the following conditions: |
---|
343 | |
---|
344 | - A task issues a ``rtems_task_suspend`` directive which blocks either itself |
---|
345 | or another task in the system. |
---|
346 | |
---|
347 | - The running task issues a ``rtems_barrier_wait`` directive. |
---|
348 | |
---|
349 | - The running task issues a ``rtems_message_queue_receive`` directive with the |
---|
350 | wait option and the message queue is empty. |
---|
351 | |
---|
352 | - The running task issues an ``rtems_event_receive`` directive with the wait |
---|
353 | option and the currently pending events do not satisfy the request. |
---|
354 | |
---|
355 | - The running task issues a ``rtems_semaphore_obtain`` directive with the wait |
---|
356 | option and the requested semaphore is unavailable. |
---|
357 | |
---|
358 | - The running task issues a ``rtems_task_wake_after`` directive which blocks |
---|
359 | the task for the given time interval. If the time interval specified is |
---|
360 | zero, the task yields the processor and remains in the ready state. |
---|
361 | |
---|
362 | - The running task issues a ``rtems_task_wake_when`` directive which blocks the |
---|
363 | task until the requested date and time arrives. |
---|
364 | |
---|
365 | - The running task issues a ``rtems_rate_monotonic_period`` directive and must |
---|
366 | wait for the specified rate monotonic period to conclude. |
---|
367 | |
---|
368 | - The running task issues a ``rtems_region_get_segment`` directive with the |
---|
369 | wait option and there is not an available segment large enough to satisfy the |
---|
370 | task's request. |
---|
371 | |
---|
372 | A blocked task may also be suspended. Therefore, both the suspension and the |
---|
373 | blocking condition must be removed before the task becomes ready to run again. |
---|
374 | |
---|
375 | A task occupies the ready state when it is able to be scheduled to run, but |
---|
376 | currently does not have control of the processor. Tasks of the same or higher |
---|
377 | priority will yield the processor by either becoming blocked, completing their |
---|
378 | timeslice, or being deleted. All tasks with the same priority will execute in |
---|
379 | FIFO order. A task enters the ready state due to any of the following |
---|
380 | conditions: |
---|
381 | |
---|
382 | - A running task issues a ``rtems_task_resume`` directive for a task that is |
---|
383 | suspended and the task is not blocked waiting on any resource. |
---|
384 | |
---|
385 | - A running task issues a ``rtems_message_queue_send``, |
---|
386 | ``rtems_message_queue_broadcast``, or a ``rtems_message_queue_urgent`` |
---|
387 | directive which posts a message to the queue on which the blocked task is |
---|
388 | waiting. |
---|
389 | |
---|
390 | - A running task issues an ``rtems_event_send`` directive which sends an event |
---|
391 | condition to a task which is blocked waiting on that event condition. |
---|
392 | |
---|
393 | - A running task issues a ``rtems_semaphore_release`` directive which releases |
---|
394 | the semaphore on which the blocked task is waiting. |
---|
395 | |
---|
396 | - A timeout interval expires for a task which was blocked by a call to the |
---|
397 | ``rtems_task_wake_after`` directive. |
---|
398 | |
---|
399 | - A timeout period expires for a task which blocked by a call to the |
---|
400 | ``rtems_task_wake_when`` directive. |
---|
401 | |
---|
402 | - A running task issues a ``rtems_region_return_segment`` directive which |
---|
403 | releases a segment to the region on which the blocked task is waiting and a |
---|
404 | resulting segment is large enough to satisfy the task's request. |
---|
405 | |
---|
406 | - A rate monotonic period expires for a task which blocked by a call to the |
---|
407 | ``rtems_rate_monotonic_period`` directive. |
---|
408 | |
---|
409 | - A timeout interval expires for a task which was blocked waiting on a message, |
---|
410 | event, semaphore, or segment with a timeout specified. |
---|
411 | |
---|
412 | - A running task issues a directive which deletes a message queue, a semaphore, |
---|
413 | or a region on which the blocked task is waiting. |
---|
414 | |
---|
415 | - A running task issues a ``rtems_task_restart`` directive for the blocked |
---|
416 | task. |
---|
417 | |
---|
418 | - The running task, with its preemption mode enabled, may be made ready by |
---|
419 | issuing any of the directives that may unblock a task with a higher priority. |
---|
420 | This directive may be issued from the running task itself or from an ISR. A |
---|
421 | ready task occupies the executing state when it has control of the CPU. A |
---|
422 | task enters the executing state due to any of the following conditions: |
---|
423 | |
---|
424 | - The task is the highest priority ready task in the system. |
---|
425 | |
---|
426 | - The running task blocks and the task is next in the scheduling queue. The |
---|
427 | task may be of equal priority as in round-robin scheduling or the task may |
---|
428 | possess the highest priority of the remaining ready tasks. |
---|
429 | |
---|
430 | - The running task may reenable its preemption mode and a task exists in the |
---|
431 | ready queue that has a higher priority than the running task. |
---|
432 | |
---|
433 | - The running task lowers its own priority and another task is of higher |
---|
434 | priority as a result. |
---|
435 | |
---|
436 | - The running task raises the priority of a task above its own and the running |
---|
437 | task is in preemption mode. |
---|