Version 1.4 of ais/ai-00307.txt

Unformatted version of ais/ai-00307.txt version 1.4
Other versions for file ais/ai-00307.txt

!standard 9.6 (00)          03-06-10 AI95-00307/03
!standard D.9 (00)
!class amendment 02-08-28
!status work item 02-08-28
!status received 02-08-28
!priority High
!difficulty Hard
!subject Execution-Time Clocks
Modern real-time scheduling policies require that applications have the ability to measure execution time of tasks, and to detect execution time overruns. Two mechanisms were proposed to achieve these requirements. One was based on a library-level package, and the other one was integrated into the Ada 95 language by creating a new Time type that could be used in the languages time constructs such as the delay until statement or various forms of the select statement. Prototype implementations exist for both.
After discussion by the ARG it was decided that the package mechanism was preferable, and a few changes were suggested. This document describes the package mechanism only, and includes the changes requested by the ARG. A discussion of these changes can be found at the end of the "Discussion" section.
Real-time analysis techniques are always based on the assumption that the application developer can accurately measure the worst-case execution time (WCET) of each task. This measurement is always very difficult, because, with effects like cache misses, pipelined and superscalar processor architectures, etc., the execution time is highly unpredictable. There are models that allow calculation of WCET's for some architectures, but they are generally very complex and not widely available for all architectures.
In hard real-time systems it is essential to monitor the execution times of all tasks and detect situations in which the estimated WCET is exceeded. This detection was usually available in systems scheduled with cyclic executives, because the periodic nature of its cycle allowed checking that all initiated work had been completed at each cycle. In event-driven concurrent systems the same capability should be available, and this can be accomplished with execution time clocks and timers.
Moreover, many flexible real-time scheduling algorithms require the capability to measure execution time and be able to perform scheduling actions when a certain amount of execution time has been consumed (for example, sporadic servers in fixed priority systems, or the constant bandwidth server in EDF-scheduled systems). Support for execution time clocks and timers would ease implementation of such flexible scheduling algorithms in Ada 95.
Ada 95 does not have any support for execution time monitoring and overrun detection. If we want Ada 95 to continue to be the best language in the environment of real-time systems, we need to include this essential functionality into the language. Java is looking to support this feature.
(See wording.)
Add new section D.14.
D.14 Execution Time
This clause specifies an execution-time clock package.
Static Semantics
The following language-defined library package exists:
with Ada.Task_Identification; package Ada.Real_Time.Execution_Time is
type CPU_Time is private; CPU_Time_First : constant CPU_Time; CPU_Time_Last : constant CPU_Time; CPU_Time_Unit : constant := implementation-defined-real-number; CPU_Tick : constant Time_Span;
function Clock (T : Ada.Task_Identification.Task_ID := Ada.Task_Identification.Current_Task) return CPU_Time;
function "+" (Left : CPU_Time; Right : Time_Span) return CPU_Time; function "+" (Left : Time_Span; Right : CPU_Time) return CPU_Time; function "-" (Left : CPU_Time; Right : Time_Span) return CPU_Time; function "-" (Left : CPU_Time; Right : CPU_Time) return Time_Span;
function "<" (Left, Right : CPU_Time) return Boolean; function "<=" (Left, Right : CPU_Time) return Boolean; function ">" (Left, Right : CPU_Time) return Boolean; function ">=" (Left, Right : CPU_Time) return Boolean;
procedure Split (T : CPU_Time; SC : out Seconds_Count; TS : out Time_Span);
function Time_Of (SC : Seconds_Count; TS : Time_Span) return CPU_Time;
protected type Timer (T : access Ada.Task_Identification.Task_ID) is procedure Arm (Interval : Time_Span); procedure Arm (Abs_Time : CPU_Time); procedure Disarm; entry Timer_Expired; function Timer_Has_Expired return Boolean; function Time_Remaining return Time_Span; private ... -- not specified by the language end Timer;
Timer_Error : exception; -- may be raised by Timer.Timer_Expired, Timer.Time_Has_Expired, -- and Timer.Time_Remaining
Timer_Resource_Error : exception; -- may be raised on the declaration of a Timer or calls to -- either Arm
private ... -- not specified by the language end Ada.Real_Time.Execution_Time;
In this Annex, execution time or cpu time of a given task is defined as the time spent by the system executing that task, including the time spent executing run-time or system services on behalf of it. The mechanism used to measure execution time is implementation defined. It is implementation defined to whom will be charged the execution time that is consumed by interrupt handlers and run-time services on behalf of the system.
The type CPU_Time represents the execution time of a task. The set of the type CPU_Time corresponds one-to-one with an implementation-defined range of mathematical integers.
The CPU_Time value I represents the half-open execution-time interval that starts with I*CPU_Time_Unit and is limited by (I+1)*CPU_Time_Unit, where CPU_Time_Unit is an implementation-defined real number. For each task, the execution time value is set to zero at some unspecified point between the task creation and the start of the task's activation.
CPU_Time_First and CPU_Time_Last are the smallest and largest values of the Time type, respectively.
Dynamic Semantics
CPU_Time_Unit is the smallest amount of execution time representable by the CPU_Time type; it is expressed in seconds. A CPU clock tick is an execution time interval during which the clock value (as observed by calling the Clock function) remains constant. CPU_Tick is the average length of such intervals.
The effects of the operators on Time and Time_Span are as for the operators defined for integer values.
The function Clock returns the amount of execution time of the task associated with the execution-time clock of the task represented by T.
The effects of the Split and Time_Of operations are defined as follows, treating values of type CPU_Time, Time_Span, and Seconds_Count as mathematical integers. The effect of Split (T, SC, TS) is to set SC and TS to values such that TCPU_Time_Unit = SC1.0 + TSCPU_Time_Unit, and 0.0 <= TSCPU_Time_Unit < 1.0. The value returned by Time_Of(SC,TS) is the execution-time value T such that T*CPU_Time_Unit=SC*1.0 + TS*CPU_Time_Unit.
Package Execution_Time contains a protected object type called Timer, which represents a software object that is capable of detecting execution time overruns. Each timer is attached to a specific execution time clock, which is the clock of the task specified by the access discriminant T. This protected object type has visible operations for the application tasks to arm or disarm a timer, and to determine whether a timer has expired or not (Timer_Has_Expired). In addition, Timer has an entry (Timer_Expired) that can be used by application tasks to block until an execution time budget is exhausted, or as an event that triggers the abortion of the instructions of a select statement with an abortable part.
When a Timer is created, at a time no later than return from the first call of an Arm procedure, the resources required to operate a CPU-time timer based on the associated execution-time clock will be allocated and initialized. If this operation would exceed the limit of the maximum number of timers in the system, the Timer_Resource_Error exception is raised. The timer is initialized in the disarmed state.
The Timer.Arm protected procedure that takes a Time_Span parameter loads the associated timer with the relative value specified by Interval and sets it to the armed state. In this state the timer counts execution time and, when the CPU clock associated with the timer measures the passage of Interval, it is said to have expired. If the timer was already armed, it is rearmed.
The Timer.Arm protected procedure that takes a CPU_Time parameter loads the associated timer with the absolute value specified by Abs_Time and sets it to the armed state. In this state the timer counts execution time and, when the CPU clock associated with the timer reaches the value Abs_Time, it is said to have expired. If the value of Abs_Time had already been reached by the clock at the time of the call, the timer is set to the armed state and is said to have expired. If the timer was already armed, it is rearmed.
The Timer.Disarm protected procedure sets the timer to the disarmed state. In this state no timer expirations occur.
The Timer.Timer_Expired protected entry suspends the calling task if the timer is in the armed state but has not yet expired. The entry is allowed to complete when the timer is in the armed state and has expired. If the timer is in the disarmed state, the Timer_Error exception is raised.
The Timer.Time_Has_Expired protected function returns True if the timer is in the armed state and has expired, and returns False if the timer is in the armed state but has not yet expired. If the timer is in the disarmed state, the Timer_Error exception is raised.
The Timer.Time_Remaining protected function returns, when the timer is in the armed state, the CPU time interval that remains until the timer will expire, or a value representing zero if the timer has expired. If the timer is in the disarmed state, the Timer_Error exception is raised.
The Timer_Error exception is raised by Timer.Timer_Expired, Timer.Timer_Has_Expired, or Timer.Time_Remaining if an attempt is made to use a timer that is in the disarmed state.
Implementation Requirements
When an object of type Timer is finalized, the system resources user by the timer must be deallocated.
The range of CPU_Time values shall be sufficient to uniquely represent the range of execution times from the task start-up to 50 years of execution time later. CPU_Tick shall be no greater than 1 millisecond.
Documentation Requirements
The implementation shall document the values of CPU_Time_First, CPU_Time_Last, CPU_Time_Unit, and CPU_Tick.
The implementation shall document the properties of the underlying mechanism used to measure execution times, such as the range of values supported and any relevant aspects of the underlying hardware or operating system facilities used.
The implementation shall document the following metrics:
- An upper bound on the execution-time duration of a clock
tick. This is a value D such that if t1 and t2 are any execution times of a given task such that t1<t2 and Clock[t1]=Clock[t2] then t2-t1 <= D.
- An upper bound on the size of a clock jump. A clock jump is the
difference between two successive distinct values of an execution -time clock (as observed by calling the Clock function with the same Task_Id)
- An upper bound on the execution time of a call to the Clock
function, in processor clock cycles.
- Upper bounds on the execution times of the operators of the type
CPU_Time, in processor clock cycles.
Implementation Permissions
Implementations targeted to machines with word size smaller than 32 bits need not support the full range and granularity of the CPU_Time type.
Implementation Advice
When appropriate, implementations should provide configuration mechanisms to change the value of CPU_Tick.
Changes made from previous version include renaming two of the components of Timer, making it implementation defined to whom system resources are charged, changing time when clock is set to zero to be between task creation and start of activation, the need to deallocate resources at finalization is moved to implementation requirement, exception raised when resources not available is Timer_Resource_Error (and this can be raised by first call of Arm).
Need for execution time monitoring functionality ------------------------------------------------
Traditional real time systems were built (and still are) using cyclic executive schedulers. In these systems, if a particular task or routine exceeded its budgeted execution time, the system could detect the situation. Basically, whenever the minor cycle interrupt came in, it could check whether the current action had completed or not. If not, that meant an overrun. Unfortunately, in some concurrent real-time systems built with priority-based on-line schedulers there is no equivalent method to detect and handle execution time overruns. This is the case for systems built using the Ada tasking model and the associated Real-Time Annex.
In addition, many flexible real-time scheduling algorithms require the capability to measure execution time and be able to perform scheduling actions when a certain amount of execution time has been consumed (for example, sporadic servers in fixed priority systems, or the constant bandwidth server in EDF-scheduled systems). Support for execution time clocks and timers is essential to be able to implement such flexible scheduling algorithms.
In recognition of all these application requirements, the Real-Time extensions to POSIX have recently incorporated support for them. Real-Time POSIX supports execution time clocks and timers that allow an application to monitor the consumption of execution time by its tasks, and to set limits for this consumption. The next revision of the Ada language should support this functionality in order for the language to continue to be the best for programming real-time applications.
It could be argued that given that the POSIX standard already defines execution time clocks and timers it would not be necessary to have the same functionality in the Ada language standard, because the POSIX services can be accessed through the appropriate bindings. This is true for Ada platforms built on top of POSIX OS implementations, but not for bare-machine implementations, like the ones used in embedded systems, avionics, etc. For portability purposes it is necessary to have this functionality supported in a homogeneous way for all the implementations that choose to support it.
The package solution --------------------
One important requirement of any execution-time functionality that may be incorporated into the Ada language is that it is implementable on top of the POSIX execution-time clocks and timers. Two different ways of providing the execution-time monitoring functionality were proposed, that satisfied the above requirement. The ARG chose the package solution because it does not require changes to the compiler.
The package proposal defines a new package that includes all the operations required to access the execution-time monitoring functionality. Some of these operations belong to a protected object type that represents execution-time timers. This approach does not require modification of the compiler, nor of the run-time system (provided that the POSIX execution-time functionality is available), but its execution time type is not integrated in the Ada language as a Time type.
Prototype implementations -------------------------
Prototype implementations were developed for both of the initial proposals, using the MaRTE operating system that provides a POSIX.13 interface and includes execution-time clocks and timers.
The implementation of the proposal with the execution-time functionality integrated into the language requires small modifications to the compiler and to the run-time, which are described in document [1]. The modifications were implemented in a short period of time and should not represent a large effort to current compiler implementors.
The implementation of the package-based proposal is very simple, because it does not require modifications to the compiler nor to the runtime system.
Implementations on bare machines or systems without POSIX execution-time clocks and timers would be a bit more complex because the underlaying execution-time monitoring functionality would have to be implemented in the scheduler. Document [2] describes one such implementation and it can be seen that it is relatively simple, and that it does not introduce any significant overhead into the scheduler.
The overheads are quite similar in both implementations, and are small.
Conclusion ----------
The IRTAW group feels that there is a strong need to have the execution-time functionality in Ada.
The IRTAW group is not opposed to having this new functionality defined as optional in the Real-Time Systems Annex.
Discussion of changes requested by the ARG ------------------------------------------
Clock_Id -------- A value of the Clock_Id type was used in the first proposal to identify each execution-time clock. The ARG questioned its need and requested that a Task_Id be used for identifying the execution-time clock of a given task. The origin of the Clock_Id type in the original proposal was the POSIX API, but it is true that it is not necessary in the Ada implementation. In a run-time implementation the (internal) clock id would be part of the task control block, and fully accessible. In a library-level implementation the (internal) clock_id could be obtained via a task attribute, using package Ada.Task_Attributes. Although in this case there is some performance penalty due to the task attribute, it is implementable.
Consequently the Clock_Id has been removed from this proposal, although it is requested that the performance issue is discussed by the ARG.
Time_Of -------
The ARG pointed out that Time_Of should return a CPU_Time. This was a typo in the previous proposal; it is now fixed.
The ARG also suggests that Time_Of does not take a Task_Id parameter. The reason for having it was that an implementation could choose to have the Task_Id as part of an Execution_Time value, in case we wished to disallow operations between CPU_Time values of different tasks. So if CPU_Time values of different tasks are not comparable and we create an exception for disallowing operations among them, we should keep the Task_Id parameter; if not, we can delete it. In the present proposal it has been deleted.
Initial value of the clock ---------------------------
The initial proposal left the initial value of the execution time clock of each task unspecified. The ARG requested that the value be forced to start at zero. Although in some implementations this requirement may imply setting the CPU-Time clock, there is value in being able to determine a task's absolute execution time, and therefore the proposal has been changed as requested.
Because the execution time value may be set to zero during the task's activation, the requirement is that the value is set to zero at some unspecified point between the task creation and the end of the task's activation.
Initialize and Finalize -----------------------
The initial proposal had two protected operations inside the Timer: Initialize and Finalize. Initialize took a parameter that identified the execution time clock to be used. The ARG proposed suppressing these operations, and moving the parameter to an access discriminant of the protected type or a parameter of the Arm operation, with a preference for the later solution.
The Initialize and Finalize operations can be removed but the implication is that in a library implementation of the package on top of the POSIX execution-time services, the protected type will have to include a component derived from Ada.Finalize.Controlled to ensure that appropriate initialization and finalization are made. I have no experience with using controlled types, so I cannot comment on whether this is a good idea or not. While the issue is discussed, the Initialize and Finalize operations have been removed from the interface.
Given that it is forecasted that most implementations of package Ada.Real_Time.Execution_Time will be made on top of the POSIX execution-time services, if the Initialize operation is removed, the parameter that identifies the clock should be an access discriminant, and not a parameter to Arm. The reason is that in the POSIX API Execution-Time timers are system objects that require allocation and deallocation of resources, and therefore it is not reasonable to allocate these resources each time a Timer is being used. The common use is to allocate the resources once, and then use the timer many times. It is also necessary to take into account that an application may want to establish more than one timer per task, and therefore the resources cannot be preallocated: for example, a system may be implementing a sporadic server for a given task using a CPU-Time Timer, but that task itself may use another timer to finish some particular any-time algorithm that it may be executing.
In summary, the Initialize and Finalize operations have been deleted (with a request that the ARG discusses the issue of using a controlled type), and the identification of the task is made as an access discriminant.
Relative and absolute Arm -------------------------
The initial proposal only had a relative ARM operation. The ARG suggested adding also an absolute version. The current proposal includes the suggested operation.
Example 1: Stopped task -----------------------
The following is an example of a periodic task that limits its own execution time to some predefined amount called WCET (worst-case execution time). If an execution time overrun is detected, the task aborts the remainder of its execution, until the next period.
with Ada.Real_Time, Ada.Real_Time.Execution_Time, Ada.Task_Identification; use Ada; use type Ada.Real_Time.Time;
task body Periodic_Stopped is My_Id : aliased Task_Identification.Task_Id:= Periodic_Stopped'Identity; The_Timer : Ada.Real_Time.Execution_Time.Timer(My_Id'access); Next_Start : Real_Time.Time:=Real_Time.Clock; WCET : constant Duration:=1.0E-3; Period : constant Duration:=1.0E-2; begin loop The_Timer.Arm(Real_Time.To_Time_Span(WCET)); select The_Timer.Timer_Expired; -- Execution-time overrun detection Handle_the_error; then abort Do_useful_work; end select; The_Timer.Disarm; Next_Start := Next_Start + Real_Time.To_Time_Span(Period); delay until Next_Start; end loop; end Periodic_Stopped;
Example 2: Lowered task -----------------------
In this example, when an execution time overrun is detected the priority of the task is lowered to allow other lower priority tasks to continue meeting their timing requirements.
A simple implementation of this scheme uses two nested tasks: the Worker task and the Supervisor task. The Supervisor task sleeps until the execution time of the Worker task reaches the instant of the priority change. When this happens the supervisor lowers (or increases) the priority of the worker task. If the Worker task completes the work before the instant of the priority change then it aborts the Supervisor task.
with Ada.Real_Time,Ada.Real_Time.Execution_Time, Ada.Dynamic_Priorities, System, Ada.Task_Identification; use Ada;
task body Worker is task Supervisor is pragma Priority(System.Priority'Last); end Supervisor;
task body Supervisor is WCET : constant Duration:=1.0E-3; My_Id : aliased Task_Identification.Task_Id:=Supervisor'Identity; The_Timer : Ada.Real_Time.Execution_Time.Timer(My_Id'access); Low_Prio : System.Priority:=System.Priority'First; begin The_Timer.Arm(Real_Time.To_Time_Span(WCET)); The_Timer.Timer_Expired; -- wait for timer overrun Dynamic_Priorities.Set_Priority(Low_Prio,Worker'Identity); end Supervisor; begin Do_useful_work; abort Supervisor; end Worker;

From: Alan Burns
Sent: Monday, August 5, 2002  8:28 AM

I attach the AI from Michael Harbour on CPU clocks.
I'm not sure if this issue actually had an AI number.

For the real-time folks this is the second most important
issue (after Ravenscar).

You can see that the AI contains two possible approches, a
new timer type or a new library package. It would be useful
at the next ARG to have a general discusion about the approach
to be take.

[Editor's note: The attachment is draft 1 of this AI. The draft credited
authors of Michael Gonzalez Harbour, Javier Miranda, and Mario Aldea
on behalf of IRTAW11].


Additional references for the original proposal:

 [1] A Proposal to Integrate the POSIX Execution-Time Clocks into Ada
     95. Technical Report, June, 2002, By J. Miranda Gonzalez and
     M. Gonzalez Harbour.

 [2] Implementing and using Execution Time Clocks in Ada Hard
     Real-Time Applications. By Gonzalez Harbour M., Aldea Rivas M.,
     Gutierrez Garcia J.J., Palencia Gutierrez J.C. International
     Conference on Reliable Software Technologies, Ada-Europe'98,
     Uppsala, Sweden, in Lecture Notes on Computer Science No. 1411,
     Junio, 1998, ISBN:3-540-64563-5, pp. 91,101.

 [3] Extending Ada's Real-Time Systems Annex with the POSIX Scheduling
     Services. By: Mario Aldea Rivas and Michael Gonzalez
     Harbour. IRTAW-2000, Las Navas, Avila, Spain.


Questions? Ask the ACAA Technical Agent