source: rtems-docs/c_user/scheduling_concepts.rst @ f916fca

4.115
Last change on this file since f916fca was d389819, checked in by Amar Takhar <amar@…>, on 01/18/16 at 05:37:40

Convert all Unicode to ASCII(128)

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