source: rtems-docs/c-user/rate_monotonic_manager.rst @ 39773ce

5
Last change on this file since 39773ce was c8b6745, checked in by Joel Sherrill <joel@…>, on 03/10/17 at 21:26:35

c-user/rate_monotonic_manager.rst: Use Latex math for utilization formulas

  • Property mode set to 100644
File size: 41.6 KB
Line 
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: COPYRIGHT (c) 2017 Kuan-Hsun Chen.
6.. COMMENT: All rights reserved.
7
8Rate Monotonic Manager
9**********************
10
11.. index:: rate mononitonic tasks
12.. index:: periodic tasks
13
14Introduction
15============
16
17The rate monotonic manager provides facilities to implement tasks which execute
18in a periodic fashion.  Critically, it also gathers information about the
19execution of those periods and can provide important statistics to the user
20which can be used to analyze and tune the application.  The directives provided
21by the rate monotonic manager are:
22
23- rtems_rate_monotonic_create_ - Create a rate monotonic period
24
25- rtems_rate_monotonic_ident_ - Get ID of a period
26
27- rtems_rate_monotonic_cancel_ - Cancel a period
28
29- rtems_rate_monotonic_delete_ - Delete a rate monotonic period
30
31- rtems_rate_monotonic_period_ - Conclude current/Start next period
32
33- rtems_rate_monotonic_get_status_ - Obtain status from a period
34
35- rtems_rate_monotonic_get_statistics_ - Obtain statistics from a period
36
37- rtems_rate_monotonic_reset_statistics_ - Reset statistics for a period
38
39- rtems_rate_monotonic_reset_all_statistics_ - Reset statistics for all periods
40
41- rtems_rate_monotonic_report_statistics_ - Print period statistics report
42
43Background
44==========
45
46The rate monotonic manager provides facilities to manage the execution of
47periodic tasks.  This manager was designed to support application designers who
48utilize the Rate Monotonic Scheduling Algorithm (RMS) to ensure that their
49periodic tasks will meet their deadlines, even under transient overload
50conditions.  Although designed for hard real-time systems, the services
51provided by the rate monotonic manager may be used by any application which
52requires periodic tasks.
53
54Rate Monotonic Manager Required Support
55---------------------------------------
56
57A clock tick is required to support the functionality provided by this manager.
58
59Period Statistics
60-----------------
61
62This manager maintains a set of statistics on each period object.  These
63statistics are reset implictly at period creation time and may be reset or
64obtained at any time by the application.  The following is a list of the
65information kept:
66
67``owner``
68  is the id of the thread that owns this period.
69
70``count``
71  is the total number of periods executed.
72
73``missed_count``
74  is the number of periods that were missed.
75
76``min_cpu_time``
77  is the minimum amount of CPU execution time consumed on any execution of the
78  periodic loop.
79
80``max_cpu_time``
81  is the maximum amount of CPU execution time consumed on any execution of the
82  periodic loop.
83
84``total_cpu_time``
85  is the total amount of CPU execution time consumed by executions of the
86  periodic loop.
87
88``min_wall_time``
89  is the minimum amount of wall time that passed on any execution of the
90  periodic loop.
91
92``max_wall_time``
93  is the maximum amount of wall time that passed on any execution of the
94  periodic loop.
95
96``total_wall_time``
97  is the total amount of wall time that passed during executions of the
98  periodic loop.
99
100Each period is divided into two consecutive phases.  The period starts with the
101active phase of the task and is followed by the inactive phase of the task.  In
102the inactive phase the task is blocked and waits for the start of the next
103period.  The inactive phase is skipped in case of a period miss.  The wall time
104includes the time during the active phase of the task on which the task is not
105executing on a processor.  The task is either blocked (for example it waits for
106a resource) or a higher priority tasks executes, thus preventing it from
107executing.  In case the wall time exceeds the period time, then this is a
108period miss.  The gap between the wall time and the period time is the margin
109between a period miss or success.
110
111The period statistics information is inexpensive to maintain and can provide
112very useful insights into the execution characteristics of a periodic task
113loop.  But it is just information.  The period statistics reported must be
114analyzed by the user in terms of what the applications is.  For example, in an
115application where priorities are assigned by the Rate Monotonic Algorithm, it
116would be very undesirable for high priority (i.e. frequency) tasks to miss
117their period.  Similarly, in nearly any application, if a task were supposed to
118execute its periodic loop every 10 milliseconds and it averaged 11
119milliseconds, then application requirements are not being met.
120
121The information reported can be used to determine the "hot spots" in the
122application.  Given a period's id, the user can determine the length of that
123period.  From that information and the CPU usage, the user can calculate the
124percentage of CPU time consumed by that periodic task.  For example, a task
125executing for 20 milliseconds every 200 milliseconds is consuming 10 percent of
126the processor's execution time.  This is usually enough to make it a good
127candidate for optimization.
128
129However, execution time alone is not enough to gauge the value of optimizing a
130particular task.  It is more important to optimize a task executing 2
131millisecond every 10 milliseconds (20 percent of the CPU) than one executing 10
132milliseconds every 100 (10 percent of the CPU).  As a general rule of thumb,
133the higher frequency at which a task executes, the more important it is to
134optimize that task.
135
136Periodicity Definitions
137----------------------------------
138.. index:: periodic task, definition
139
140A periodic task is one which must be executed at a regular interval.  The
141interval between successive iterations of the task is referred to as its
142period.  Periodic tasks can be characterized by the length of their period and
143execution time.  The period and execution time of a task can be used to
144determine the processor utilization for that task.  Processor utilization is
145the percentage of processor time used and can be calculated on a per-task or
146system-wide basis.  Typically, the task's worst-case execution time will be
147less than its period.  For example, a periodic task's requirements may state
148that it should execute for 10 milliseconds every 100 milliseconds.  Although
149the execution time may be the average, worst, or best case, the worst-case
150execution time is more appropriate for use when analyzing system behavior under
151transient overload conditions... index:: aperiodic task, definition
152
153In contrast, an aperiodic task executes at irregular intervals and has only a
154soft deadline.  In other words, the deadlines for aperiodic tasks are not
155rigid, but adequate response times are desirable.  For example, an aperiodic
156task may process user input from a terminal.
157
158.. index:: sporadic task, definition
159
160Finally, a sporadic task is an aperiodic task with a hard deadline and minimum
161interarrival time.  The minimum interarrival time is the minimum period of time
162which exists between successive iterations of the task.  For example, a
163sporadic task could be used to process the pressing of a fire button on a
164joystick.  The mechanical action of the fire button ensures a minimum time
165period between successive activations, but the missile must be launched by a
166hard deadline.
167
168Rate Monotonic Scheduling Algorithm
169-----------------------------------
170.. index:: Rate Monotonic Scheduling Algorithm, definition
171.. index:: RMS Algorithm, definition
172
173The Rate Monotonic Scheduling Algorithm (RMS) is important to real-time systems
174designers because it allows one to sufficiently guarantee that a set of tasks
175is schedulable (see :cite:`Liu:1973:Scheduling`, :cite:`Lehoczky:1989:RM`,
176:cite:`Sha:1990:Ada`, :cite:`Burns:1991:Review`).
177
178A set of tasks is said to be schedulable if all of the tasks can meet their
179deadlines.  RMS provides a set of rules which can be used to perform
180a guaranteed schedulability analysis for a task set.  This analysis determines
181whether a task set is schedulable under worst-case conditions and emphasizes
182the predictability of the system's behavior.  It has been proven that:
183
184.. sidebar:: *RMS*
185
186  RMS is an optimal fixed-priority algorithm for scheduling independent,
187  preemptible, periodic tasks on a single processor.
188
189RMS is optimal in the sense that if a set of tasks can be scheduled by any
190fixed-priority algorithm, then RMS will be able to schedule that task set.
191RMS bases it schedulability analysis on the processor utilization level below
192which all deadlines can be met.
193
194RMS calls for the static assignment of task priorities based upon their period.
195The shorter a task's period, the higher its priority.  For example, a task with
196a 1 millisecond period has higher priority than a task with a 100 millisecond
197period.  If two tasks have the same period, then RMS does not distinguish
198between the tasks.  However, RTEMS specifies that when given tasks of equal
199priority, the task which has been ready longest will execute first.  RMS's
200priority assignment scheme does not provide one with exact numeric values for
201task priorities.  For example, consider the following task set and priority
202assignments:
203
204+--------------------+---------------------+---------------------+
205| Task               | Period              | Priority            |
206|                    | (in milliseconds)   |                     |
207+====================+=====================+=====================+
208|         1          |         100         |         Low         |
209+--------------------+---------------------+---------------------+
210|         2          |          50         |       Medium        |
211+--------------------+---------------------+---------------------+
212|         3          |          50         |       Medium        |
213+--------------------+---------------------+---------------------+
214|         4          |          25         |        High         |
215+--------------------+---------------------+---------------------+
216
217RMS only calls for task 1 to have the lowest priority, task 4 to have the
218highest priority, and tasks 2 and 3 to have an equal priority between that of
219tasks 1 and 4.  The actual RTEMS priorities assigned to the tasks must only
220adhere to those guidelines.
221
222Many applications have tasks with both hard and soft deadlines.  The tasks with
223hard deadlines are typically referred to as the critical task set, with the
224soft deadline tasks being the non-critical task set.  The critical task set can
225be scheduled using RMS, with the non-critical tasks not executing under
226transient overload, by simply assigning priorities such that the lowest
227priority critical task (i.e. longest period) has a higher priority than the
228highest priority non-critical task.  Although RMS may be used to assign
229priorities to the non-critical tasks, it is not necessary.  In this instance,
230schedulability is only guaranteed for the critical task set.
231
232Schedulability Analysis
233-----------------------
234
235.. index:: RMS schedulability analysis
236
237RMS allows application designers to ensure that tasks can meet all deadlines under fixed-priority assignment,
238even under transient overload, without knowing exactly when any given task will
239execute by applying proven schedulability analysis rules.
240
241Assumptions
242^^^^^^^^^^^
243
244The schedulability analysis rules for RMS were developed based on the following
245assumptions:
246
247- The requests for all tasks for which hard deadlines exist are periodic, with
248  a constant interval between requests.
249
250- Each task must complete before the next request for it occurs.
251
252- The tasks are independent in that a task does not depend on the initiation or
253  completion of requests for other tasks.
254
255- The execution time for each task without preemption or interruption is
256  constant and does not vary.
257
258- Any non-periodic tasks in the system are special.  These tasks displace
259  periodic tasks while executing and do not have hard, critical deadlines.
260
261Once the basic schedulability analysis is understood, some of the above
262assumptions can be relaxed and the side-effects accounted for.
263
264Processor Utilization Rule
265^^^^^^^^^^^^^^^^^^^^^^^^^^
266.. index:: RMS Processor Utilization Rule
267
268The Processor Utilization Rule requires that processor utilization be
269calculated based upon the period and execution time of each task.
270The fraction of processor time spent executing task index is ``Time(i)
271/ Period(i)``.  The processor utilization can be calculated as follows
272where n is the number of tasks in the set being analyzed:
273
274.. math::
275
276    Utilization = \sum_{i=1}^{n} Time_i/Period_i
277
278To ensure schedulability even under transient overload, the processor
279utilization must adhere to the following rule:
280
281.. math::
282
283    maximumUtilization = n * (2^{\frac{1}{n}} - 1)
284
285As the number of tasks increases, the above formula approaches ln(2) for a
286worst-case utilization factor of approximately 0.693.  Many tasks sets can be
287scheduled with a greater utilization factor.  In fact, the average processor
288utilization threshold for a randomly generated task set is approximately 0.88.
289See more detail in :cite:`Liu:1973:Scheduling`.
290
291Processor Utilization Rule Example
292^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
293
294This example illustrates the application of the Processor Utilization Rule to
295an application with three critical periodic tasks.  The following table details
296the RMS priority, period, execution time, and processor utilization for each
297task:
298
299+------------+----------+--------+-----------+-------------+
300| Task       | RMS      | Period | Execution | Processor   |
301|            | Priority |        | Time      | Utilization |
302+============+==========+========+===========+=============+
303|     1      |   High   |  100   |    15     |    0.15     |
304+------------+----------+--------+-----------+-------------+
305|     2      |  Medium  |  200   |    50     |    0.25     |
306+------------+----------+--------+-----------+-------------+
307|     3      |   Low    |  300   |   100     |    0.33     |
308+------------+----------+--------+-----------+-------------+
309
310The total processor utilization for this task set is 0.73 which is below the
311upper bound of 3 * (2**(1/3) - 1), or 0.779, imposed by the Processor
312Utilization Rule.  Therefore, this task set is guaranteed to be schedulable
313using RMS.
314
315First Deadline Rule
316^^^^^^^^^^^^^^^^^^^
317.. index:: RMS First Deadline Rule
318
319If a given set of tasks do exceed the processor utilization upper limit imposed
320by the Processor Utilization Rule, they can still be guaranteed to meet all
321their deadlines by application of the First Deadline Rule.  This rule can be
322stated as follows:
323
324For a given set of independent periodic tasks, if each task meets its first
325deadline when all tasks are started at the same time, then the deadlines will
326always be met for any combination of start times.
327
328A key point with this rule is that ALL periodic tasks are assumed to start at
329the exact same instant in time.  Although this assumption may seem to be
330invalid, RTEMS makes it quite easy to ensure.  By having a non-preemptible user
331initialization task, all application tasks, regardless of priority, can be
332created and started before the initialization deletes itself.  This technique
333ensures that all tasks begin to compete for execution time at the same instant
334- when the user initialization task deletes itself.
335See more detail in :cite:`Lehoczky:1989:RM`.
336
337First Deadline Rule Example
338^^^^^^^^^^^^^^^^^^^^^^^^^^^
339
340The First Deadline Rule can ensure schedulability even when the Processor
341Utilization Rule fails.  The example below is a modification of the Processor
342Utilization Rule example where task execution time has been increased from 15
343to 25 units.  The following table details the RMS priority, period, execution
344time, and processor utilization for each task:
345
346+------------+----------+--------+-----------+-------------+
347| Task       | RMS      | Period | Execution | Processor   |
348|            | Priority |        | Time      | Utilization |
349+============+==========+========+===========+=============+
350|     1      |   High   |  100   |    25     |    0.25     |
351+------------+----------+--------+-----------+-------------+
352|     2      |  Medium  |  200   |    50     |    0.25     |
353+------------+----------+--------+-----------+-------------+
354|     3      |   Low    |  300   |   100     |    0.33     |
355+------------+----------+--------+-----------+-------------+
356
357The total processor utilization for the modified task set is 0.83 which is
358above the upper bound of 3 * (2**(1/3) - 1), or 0.779, imposed by the Processor
359Utilization Rule.  Therefore, this task set is not guaranteed to be schedulable
360using RMS.  However, the First Deadline Rule can guarantee the schedulability
361of this task set.  This rule calls for one to examine each occurrence of
362deadline until either all tasks have met their deadline or one task failed to
363meet its first deadline.  The following table details the time of each deadline
364occurrence, the maximum number of times each task may have run, the total
365execution time, and whether all the deadlines have been met:
366
367+----------+------+------+------+----------------------+---------------+
368| Deadline | Task | Task | Task | Total                | All Deadlines |
369| Time     | 1    | 2    | 3    | Execution Time       | Met?          |
370+==========+======+======+======+======================+===============+
371|   100    |  1   |  1   |  1   |  25 + 50 + 100 = 175 |      NO       |
372+----------+------+------+------+----------------------+---------------+
373|   200    |  2   |  1   |  1   |  50 + 50 + 100 = 200 |     YES       |
374+----------+------+------+------+----------------------+---------------+
375
376The key to this analysis is to recognize when each task will execute.  For
377example at time 100, task 1 must have met its first deadline, but tasks 2 and 3
378may also have begun execution.  In this example, at time 100 tasks 1 and 2 have
379completed execution and thus have met their first deadline.  Tasks 1 and 2 have
380used (25 + 50) = 75 time units, leaving (100 - 75) = 25 time units for task 3
381to begin.  Because task 3 takes 100 ticks to execute, it will not have
382completed execution at time 100.  Thus at time 100, all of the tasks except
383task 3 have met their first deadline.
384
385At time 200, task 1 must have met its second deadline and task 2 its first
386deadline.  As a result, of the first 200 time units, task 1 uses (2 * 25) = 50
387and task 2 uses 50, leaving (200 - 100) time units for task 3.  Task 3 requires
388100 time units to execute, thus it will have completed execution at time 200.
389Thus, all of the tasks have met their first deadlines at time 200, and the task
390set is schedulable using the First Deadline Rule.
391
392Relaxation of Assumptions
393^^^^^^^^^^^^^^^^^^^^^^^^^
394
395The assumptions used to develop the RMS schedulability rules are uncommon in
396most real-time systems.  For example, it was assumed that tasks have constant
397unvarying execution time.  It is possible to relax this assumption, simply by
398using the worst-case execution time of each task.
399
400Another assumption is that the tasks are independent.  This means that the
401tasks do not wait for one another or contend for resources.  This assumption
402can be relaxed by accounting for the amount of time a task spends waiting to
403acquire resources.  Similarly, each task's execution time must account for any
404I/O performed and any RTEMS directive calls.
405
406In addition, the assumptions did not account for the time spent executing
407interrupt service routines.  This can be accounted for by including all the
408processor utilization by interrupt service routines in the utilization
409calculation.  Similarly, one should also account for the impact of delays in
410accessing local memory caused by direct memory access and other processors
411accessing local dual-ported memory.
412
413The assumption that nonperiodic tasks are used only for initialization or
414failure-recovery can be relaxed by placing all periodic tasks in the critical
415task set.  This task set can be scheduled and analyzed using RMS.  All
416nonperiodic tasks are placed in the non-critical task set.  Although the
417critical task set can be guaranteed to execute even under transient overload,
418the non-critical task set is not guaranteed to execute.
419
420In conclusion, the application designer must be fully cognizant of the system
421and its run-time behavior when performing schedulability analysis for a system
422using RMS.  Every hardware and software factor which impacts the execution time
423of each task must be accounted for in the schedulability analysis.
424
425Operations
426==========
427
428Creating a Rate Monotonic Period
429--------------------------------
430
431The ``rtems_rate_monotonic_create`` directive creates a rate monotonic period
432which is to be used by the calling task to delineate a period.  RTEMS allocates
433a Period Control Block (PCB) from the PCB free list.  This data structure is
434used by RTEMS to manage the newly created rate monotonic period.  RTEMS returns
435a unique period ID to the application which is used by other rate monotonic
436manager directives to access this rate monotonic period.
437
438Manipulating a Period
439---------------------
440
441The ``rtems_rate_monotonic_period`` directive is used to establish and maintain
442periodic execution utilizing a previously created rate monotonic period.  Once
443initiated by the ``rtems_rate_monotonic_period`` directive, the period is said
444to run until it either expires or is reinitiated.  The state of the rate
445monotonic period results in one of the following scenarios:
446
447- If the rate monotonic period is running, the calling task will be blocked for
448  the remainder of the outstanding period and, upon completion of that period,
449  the period will be reinitiated with the specified period.
450
451- If the rate monotonic period is not currently running and has not expired, it
452  is initiated with a length of period ticks and the calling task returns
453  immediately.
454
455- If the rate monotonic period has expired before the task invokes the
456  ``rtems_rate_monotonic_period`` directive, the postponed job will be released
457  until there is no more postponed jobs. The calling task returns immediately
458  with a timeout error status. In the watchdog routine, the period will still
459  be updated periodically and track the count of the postponed jobs :cite:`Chen:2016:Overrun`.
460  Please note, the count of the postponed jobs is only saturated until 0xffffffff.
461
462Obtaining the Status of a Period
463--------------------------------
464
465If the ``rtems_rate_monotonic_period`` directive is invoked with a period of
466``RTEMS_PERIOD_STATUS`` ticks, the current state of the specified rate
467monotonic period will be returned.  The following table details the
468relationship between the period's status and the directive status code returned
469by the ``rtems_rate_monotonic_period`` directive:
470
471.. list-table::
472 :class: rtems-table
473
474 * - ``RTEMS_SUCCESSFUL``
475   - period is running
476 * - ``RTEMS_TIMEOUT``
477   - period has expired
478 * - ``RTEMS_NOT_DEFINED``
479   - period has never been initiated
480
481Obtaining the status of a rate monotonic period does not alter the state or
482length of that period.
483
484Canceling a Period
485------------------
486
487The ``rtems_rate_monotonic_cancel`` directive is used to stop the period
488maintained by the specified rate monotonic period.  The period is stopped and
489the rate monotonic period can be reinitiated using the
490``rtems_rate_monotonic_period`` directive.
491
492Deleting a Rate Monotonic Period
493--------------------------------
494
495The ``rtems_rate_monotonic_delete`` directive is used to delete a rate
496monotonic period.  If the period is running and has not expired, the period is
497automatically canceled.  The rate monotonic period's control block is returned
498to the PCB free list when it is deleted.  A rate monotonic period can be
499deleted by a task other than the task which created the period.
500
501Examples
502--------
503
504The following sections illustrate common uses of rate monotonic periods to
505construct periodic tasks.
506
507Simple Periodic Task
508--------------------
509
510This example consists of a single periodic task which, after initialization,
511executes every 100 clock ticks.
512
513.. code-block:: c
514    :linenos:
515
516    rtems_task Periodic_task(rtems_task_argument arg)
517    {
518        rtems_name        name;
519        rtems_id          period;
520        rtems_status_code status;
521        name = rtems_build_name( 'P', 'E', 'R', 'D' );
522        status = rtems_rate_monotonic_create( name, &period );
523        if ( status != RTEMS_STATUS_SUCCESSFUL ) {
524            printf( "rtems_monotonic_create failed with status of %d.\n", rc );
525            exit( 1 );
526        }
527        while ( 1 ) {
528            if ( rtems_rate_monotonic_period( period, 100 ) == RTEMS_TIMEOUT )
529                break;
530            /* Perform some periodic actions */
531        }
532        /* missed period so delete period and SELF */
533        status = rtems_rate_monotonic_delete( period );
534        if ( status != RTEMS_STATUS_SUCCESSFUL ) {
535            printf( "rtems_rate_monotonic_delete failed with status of %d.\n", status );
536            exit( 1 );
537        }
538        status = rtems_task_delete( SELF );    /* should not return */
539        printf( "rtems_task_delete returned with status of %d.\n", status );
540        exit( 1 );
541    }
542
543The above task creates a rate monotonic period as part of its initialization.
544The first time the loop is executed, the ``rtems_rate_monotonic_period``
545directive will initiate the period for 100 ticks and return immediately.
546Subsequent invocations of the ``rtems_rate_monotonic_period`` directive will
547result in the task blocking for the remainder of the 100 tick period.  If, for
548any reason, the body of the loop takes more than 100 ticks to execute, the
549``rtems_rate_monotonic_period`` directive will return the ``RTEMS_TIMEOUT``
550status. If the above task misses its deadline, it will delete the rate
551monotonic period and itself.
552
553Task with Multiple Periods
554--------------------------
555
556This example consists of a single periodic task which, after initialization,
557performs two sets of actions every 100 clock ticks.  The first set of actions
558is performed in the first forty clock ticks of every 100 clock ticks, while the
559second set of actions is performed between the fortieth and seventieth clock
560ticks.  The last thirty clock ticks are not used by this task.
561
562.. code-block:: c
563    :linenos:
564
565    rtems_task Periodic_task(rtems_task_argument arg)
566    {
567        rtems_name        name_1, name_2;
568        rtems_id          period_1, period_2;
569        rtems_status_code status;
570        name_1 = rtems_build_name( 'P', 'E', 'R', '1' );
571        name_2 = rtems_build_name( 'P', 'E', 'R', '2' );
572        (void ) rtems_rate_monotonic_create( name_1, &period_1 );
573        (void ) rtems_rate_monotonic_create( name_2, &period_2 );
574        while ( 1 ) {
575            if ( rtems_rate_monotonic_period( period_1, 100 ) == TIMEOUT )
576                break;
577            if ( rtems_rate_monotonic_period( period_2, 40 ) == TIMEOUT )
578            break;
579            /*
580             *  Perform first set of actions between clock
581             *  ticks 0 and 39 of every 100 ticks.
582             */
583            if ( rtems_rate_monotonic_period( period_2, 30 ) == TIMEOUT )
584                break;
585            /*
586             *  Perform second set of actions between clock 40 and 69
587             *  of every 100 ticks.  THEN ...
588             *
589             *  Check to make sure we didn't miss the period_2 period.
590             */
591            if ( rtems_rate_monotonic_period( period_2, STATUS ) == TIMEOUT )
592                break;
593            (void) rtems_rate_monotonic_cancel( period_2 );
594        }
595        /* missed period so delete period and SELF */
596        (void ) rtems_rate_monotonic_delete( period_1 );
597        (void ) rtems_rate_monotonic_delete( period_2 );
598        (void ) task_delete( SELF );
599    }
600
601The above task creates two rate monotonic periods as part of its
602initialization.  The first time the loop is executed, the
603``rtems_rate_monotonic_period`` directive will initiate the period_1 period for
604100 ticks and return immediately.  Subsequent invocations of the
605``rtems_rate_monotonic_period`` directive for period_1 will result in the task
606blocking for the remainder of the 100 tick period.  The period_2 period is used
607to control the execution time of the two sets of actions within each 100 tick
608period established by period_1.  The ``rtems_rate_monotonic_cancel( period_2
609)`` call is performed to ensure that the period_2 period does not expire while
610the task is blocked on the period_1 period.  If this cancel operation were not
611performed, every time the ``rtems_rate_monotonic_period( period_2, 40 )`` call
612is executed, except for the initial one, a directive status of
613``RTEMS_TIMEOUT`` is returned.  It is important to note that every time this
614call is made, the period_2 period will be initiated immediately and the task
615will not block.
616
617If, for any reason, the task misses any deadline, the
618``rtems_rate_monotonic_period`` directive will return the ``RTEMS_TIMEOUT``
619directive status. If the above task misses its deadline, it will delete the
620rate monotonic periods and itself.
621
622Directives
623==========
624
625This section details the rate monotonic manager's directives.  A subsection is
626dedicated to each of this manager's directives and describes the calling
627sequence, related constants, usage, and status codes.
628
629.. raw:: latex
630
631   \clearpage
632
633.. _rtems_rate_monotonic_create:
634
635RATE_MONOTONIC_CREATE - Create a rate monotonic period
636------------------------------------------------------
637.. index:: create a period
638.. index:: rtems_rate_monotonic_create
639
640CALLING SEQUENCE:
641    .. code-block:: c
642
643        rtems_status_code rtems_rate_monotonic_create(
644            rtems_name  name,
645            rtems_id   *id
646        );
647
648DIRECTIVE STATUS CODES:
649    .. list-table::
650     :class: rtems-table
651
652     * - ``RTEMS_SUCCESSFUL``
653       - rate monotonic period created successfully
654     * - ``RTEMS_INVALID_NAME``
655       - invalid period name
656     * - ``RTEMS_TOO_MANY``
657       - too many periods created
658
659DESCRIPTION:
660    This directive creates a rate monotonic period.  The assigned rate
661    monotonic id is returned in id.  This id is used to access the period with
662    other rate monotonic manager directives.  For control and maintenance of
663    the rate monotonic period, RTEMS allocates a PCB from the local PCB free
664    pool and initializes it.
665
666NOTES:
667    This directive will not cause the calling task to be preempted.
668
669.. raw:: latex
670
671   \clearpage
672
673.. _rtems_rate_monotonic_ident:
674
675RATE_MONOTONIC_IDENT - Get ID of a period
676-----------------------------------------
677.. index:: get ID of a period
678.. index:: obtain ID of a period
679.. index:: rtems_rate_monotonic_ident
680
681CALLING SEQUENCE:
682    .. code-block:: c
683
684        rtems_status_code rtems_rate_monotonic_ident(
685            rtems_name  name,
686            rtems_id   *id
687        );
688
689DIRECTIVE STATUS CODES:
690    .. list-table::
691     :class: rtems-table
692
693     * - ``RTEMS_SUCCESSFUL``
694       - period identified successfully
695     * - ``RTEMS_INVALID_NAME``
696       - period name not found
697
698DESCRIPTION:
699    This directive obtains the period id associated with the period name to be
700    acquired.  If the period name is not unique, then the period id will match
701    one of the periods with that name.  However, this period id is not
702    guaranteed to correspond to the desired period.  The period id is used to
703    access this period in other rate monotonic manager directives.
704
705NOTES:
706    This directive will not cause the running task to be preempted.
707
708.. raw:: latex
709
710   \clearpage
711
712.. _rtems_rate_monotonic_cancel:
713
714RATE_MONOTONIC_CANCEL - Cancel a period
715---------------------------------------
716.. index:: cancel a period
717.. index:: rtems_rate_monotonic_cancel
718
719CALLING SEQUENCE:
720    .. code-block:: c
721
722        rtems_status_code rtems_rate_monotonic_cancel(
723            rtems_id id
724        );
725
726DIRECTIVE STATUS CODES:
727    .. list-table::
728     :class: rtems-table
729
730     * - ``RTEMS_SUCCESSFUL``
731       - period canceled successfully
732     * - ``RTEMS_INVALID_ID``
733       - invalid rate monotonic period id
734     * - ``RTEMS_NOT_OWNER_OF_RESOURCE``
735       - rate monotonic period not created by calling task
736
737DESCRIPTION:
738
739    This directive cancels the rate monotonic period id.  This period will be
740    reinitiated by the next invocation of ``rtems_rate_monotonic_period``
741    with id.
742
743NOTES:
744    This directive will not cause the running task to be preempted.
745
746    The rate monotonic period specified by id must have been created by the
747    calling task.
748
749.. raw:: latex
750
751   \clearpage
752
753.. _rtems_rate_monotonic_delete:
754.. index:: rtems_rate_monotonic_delete
755
756RATE_MONOTONIC_DELETE - Delete a rate monotonic period
757------------------------------------------------------
758.. index:: delete a period
759
760CALLING SEQUENCE:
761    .. code-block:: c
762
763        rtems_status_code rtems_rate_monotonic_delete(
764            rtems_id id
765        );
766
767DIRECTIVE STATUS CODES:
768    .. list-table::
769     :class: rtems-table
770
771     * - ``RTEMS_SUCCESSFUL``
772       - period deleted successfully
773     * - ``RTEMS_INVALID_ID``
774       - invalid rate monotonic period id
775
776DESCRIPTION:
777
778    This directive deletes the rate monotonic period specified by id.  If the
779    period is running, it is automatically canceled.  The PCB for the deleted
780    period is reclaimed by RTEMS.
781
782NOTES:
783    This directive will not cause the running task to be preempted.
784
785    A rate monotonic period can be deleted by a task other than the task which
786    created the period.
787
788.. raw:: latex
789
790   \clearpage
791
792.. _rtems_rate_monotonic_period:
793
794RATE_MONOTONIC_PERIOD - Conclude current/Start next period
795----------------------------------------------------------
796.. index:: conclude current period
797.. index:: start current period
798.. index:: period initiation
799.. index:: rtems_rate_monotonic_period
800
801CALLING SEQUENCE:
802    .. code-block:: c
803
804        rtems_status_code rtems_rate_monotonic_period(
805            rtems_id       id,
806            rtems_interval length
807        );
808
809DIRECTIVE STATUS CODES:
810    .. list-table::
811     :class: rtems-table
812
813     * - ``RTEMS_SUCCESSFUL``
814       - period initiated successfully
815     * - ``RTEMS_INVALID_ID``
816       - invalid rate monotonic period id
817     * - ``RTEMS_NOT_OWNER_OF_RESOURCE``
818       - period not created by calling task
819     * - ``RTEMS_NOT_DEFINED``
820       - period has never been initiated (only possible when period is set to PERIOD_STATUS)
821     * - ``RTEMS_TIMEOUT``
822       - period has expired
823
824DESCRIPTION:
825    This directive initiates the rate monotonic period id with a length of
826    period ticks.  If id is running, then the calling task will block for the
827    remainder of the period before reinitiating the period with the specified
828    period.  If id was not running (either expired or never initiated), the
829    period is immediately initiated and the directive returns immediately.
830    If id has expired its period, the postponed job will be released immediately
831    and the following calls of this directive will release postponed
832    jobs until there is no more deadline miss.
833
834    If invoked with a period of ``RTEMS_PERIOD_STATUS`` ticks, the current
835    state of id will be returned.  The directive status indicates the current
836    state of the period.  This does not alter the state or period of the
837    period.
838
839NOTES:
840    This directive will not cause the running task to be preempted.
841
842.. raw:: latex
843
844   \clearpage
845
846.. _rtems_rate_monotonic_get_status:
847
848RATE_MONOTONIC_GET_STATUS - Obtain status from a period
849-------------------------------------------------------
850.. index:: get status of period
851.. index:: obtain status of period
852.. index:: rtems_rate_monotonic_get_status
853
854CALLING SEQUENCE:
855    .. code-block:: c
856
857        rtems_status_code rtems_rate_monotonic_get_status(
858            rtems_id                            id,
859            rtems_rate_monotonic_period_status *status
860        );
861
862DIRECTIVE STATUS CODES:
863    .. list-table::
864     :class: rtems-table
865
866     * - ``RTEMS_SUCCESSFUL``
867       - period initiated successfully
868     * - ``RTEMS_INVALID_ID``
869       - invalid rate monotonic period id
870     * - ``RTEMS_INVALID_ADDRESS``
871       - invalid address of status
872
873*DESCRIPTION:
874    This directive returns status information associated with the rate
875    monotonic period id in the following data structure:
876
877    .. index:: rtems_rate_monotonic_period_status
878
879    .. code-block:: c
880
881        typedef struct {
882            rtems_id                              owner;
883            rtems_rate_monotonic_period_states    state;
884            rtems_rate_monotonic_period_time_t    since_last_period;
885            rtems_thread_cpu_usage_t              executed_since_last_period;
886            uint32_t                              postponed_jobs_count;
887        }  rtems_rate_monotonic_period_status;
888
889    .. COMMENT: RATE_MONOTONIC_INACTIVE does not have RTEMS in front of it.
890
891    A configure time option can be used to select whether the time information
892    is given in ticks or seconds and nanoseconds.  The default is seconds and
893    nanoseconds.  If the period's state is ``RATE_MONOTONIC_INACTIVE``, both
894    time values will be set to 0.  Otherwise, both time values will contain
895    time information since the last invocation of the
896    ``rtems_rate_monotonic_period`` directive.  More specifically, the
897    since_last_period value contains the elapsed time which has occurred
898    since the last invocation of the ``rtems_rate_monotonic_period`` directive
899    and the ``executed_since_last_period`` contains how much processor
900    time the owning task has consumed since the invocation of the
901    ``rtems_rate_monotonic_period`` directive. In addition, the
902    postponed_jobs_count value contains the count of jobs which are not released yet.
903
904NOTES:
905    This directive will not cause the running task to be preempted.
906
907.. raw:: latex
908
909   \clearpage
910
911.. _rtems_rate_monotonic_get_statistics:
912
913RATE_MONOTONIC_GET_STATISTICS - Obtain statistics from a period
914---------------------------------------------------------------
915.. index:: get statistics of period
916.. index:: obtain statistics of period
917.. index:: rtems_rate_monotonic_get_statistics
918
919CALLING SEQUENCE:
920    .. code-block:: c
921
922        rtems_status_code rtems_rate_monotonic_get_statistics(
923            rtems_id                                id,
924            rtems_rate_monotonic_period_statistics *statistics
925        );
926
927DIRECTIVE STATUS CODES:
928    .. list-table::
929     :class: rtems-table
930
931     * - ``RTEMS_SUCCESSFUL``
932       - period initiated successfully
933     * - ``RTEMS_INVALID_ID``
934       - invalid rate monotonic period id
935     * - ``RTEMS_INVALID_ADDRESS``
936       - invalid address of statistics
937
938DESCRIPTION:
939    This directive returns statistics information associated with the rate
940    monotonic period id in the following data structure:
941
942    .. index:: rtems_rate_monotonic_period_statistics
943
944    .. code-block:: c
945
946        typedef struct {
947            uint32_t     count;
948            uint32_t     missed_count;
949            #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
950              struct timespec min_cpu_time;
951              struct timespec max_cpu_time;
952              struct timespec total_cpu_time;
953            #else
954              uint32_t  min_cpu_time;
955              uint32_t  max_cpu_time;
956              uint32_t  total_cpu_time;
957            #endif
958            #ifdef RTEMS_ENABLE_NANOSECOND_RATE_MONOTONIC_STATISTICS
959              struct timespec min_wall_time;
960              struct timespec max_wall_time;
961              struct timespec total_wall_time;
962            #else
963             uint32_t  min_wall_time;
964             uint32_t  max_wall_time;
965             uint32_t  total_wall_time;
966            #endif
967        }  rtems_rate_monotonic_period_statistics;
968
969    This directive returns the current statistics information for the period
970    instance assocaited with ``id``.  The information returned is indicated by
971    the structure above.
972
973NOTES:
974    This directive will not cause the running task to be preempted.
975
976.. raw:: latex
977
978   \clearpage
979
980.. _rtems_rate_monotonic_reset_statistics:
981
982RATE_MONOTONIC_RESET_STATISTICS - Reset statistics for a period
983---------------------------------------------------------------
984.. index:: reset statistics of period
985.. index:: rtems_rate_monotonic_reset_statistics
986
987CALLING SEQUENCE:
988    .. code-block:: c
989
990        rtems_status_code rtems_rate_monotonic_reset_statistics(
991            rtems_id  id
992        );
993
994DIRECTIVE STATUS CODES:
995    .. list-table::
996     :class: rtems-table
997
998     * - ``RTEMS_SUCCESSFUL``
999       - period initiated successfully
1000     * - ``RTEMS_INVALID_ID``
1001       - invalid rate monotonic period id
1002
1003DESCRIPTION:
1004    This directive resets the statistics information associated with this rate
1005    monotonic period instance.
1006
1007NOTES:
1008    This directive will not cause the running task to be preempted.
1009
1010.. raw:: latex
1011
1012   \clearpage
1013
1014.. _rtems_rate_monotonic_reset_all_statistics:
1015
1016RATE_MONOTONIC_RESET_ALL_STATISTICS - Reset statistics for all periods
1017----------------------------------------------------------------------
1018.. index:: reset statistics of all periods
1019.. index:: rtems_rate_monotonic_reset_all_statistics
1020
1021CALLING SEQUENCE:
1022    .. code-block:: c
1023
1024        void rtems_rate_monotonic_reset_all_statistics(void);
1025
1026DIRECTIVE STATUS CODES:
1027    NONE
1028
1029DESCRIPTION:
1030    This directive resets the statistics information associated with all rate
1031    monotonic period instances.
1032
1033NOTES:
1034    This directive will not cause the running task to be preempted.
1035
1036.. raw:: latex
1037
1038   \clearpage
1039
1040.. _rtems_rate_monotonic_report_statistics:
1041
1042RATE_MONOTONIC_REPORT_STATISTICS - Print period statistics report
1043-----------------------------------------------------------------
1044.. index:: print period statistics report
1045.. index:: period statistics report
1046.. index:: rtems_rate_monotonic_report_statistics
1047
1048CALLING SEQUENCE:
1049    .. code-block:: c
1050
1051        void rtems_rate_monotonic_report_statistics(void);
1052
1053DIRECTIVE STATUS CODES:
1054    NONE
1055
1056DESCRIPTION:
1057    This directive prints a report on all active periods which have executed at
1058    least one period. The following is an example of the output generated by
1059    this directive.
1060
1061    .. index:: rtems_rate_monotonic_period_statistics
1062
1063    .. code-block:: c
1064
1065        ID      OWNER   PERIODS  MISSED    CPU TIME    WALL TIME
1066        MIN/MAX/AVG  MIN/MAX/AVG
1067        0x42010001  TA1       502     0       0/1/0.99    0/0/0.00
1068        0x42010002  TA2       502     0       0/1/0.99    0/0/0.00
1069        0x42010003  TA3       501     0       0/1/0.99    0/0/0.00
1070        0x42010004  TA4       501     0       0/1/0.99    0/0/0.00
1071        0x42010005  TA5        10     0       0/1/0.90    0/0/0.00
1072
1073NOTES:
1074    This directive will not cause the running task to be preempted.
Note: See TracBrowser for help on using the repository browser.