source: rtems/doc/user/schedule.t @ 29e6637e

4.115
Last change on this file since 29e6637e was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

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