UPM
Tiempo real
Juan Antonio de la Puente
DIT/UPM
Transparencias basadas en el captulo 12 del libro de A. Burns y A. Wellings Real-Time Systems and Programming Languuages, 3 edicin (2001)
Tiempo real
Objetivo
Comprender el papel del tiempo en el diseo y realizacin de
sistemas de tiempo real
Contenido:
Tiempo real -
Necesidades
Tiempo real -
Tiempo universal
TU UT (Universal Time)
tiempo solar medio en el meridiano 0 (Greenwich)
definido en 1884 (entonces se llamaba GMT)
Tiempo real -
Tiempo de efemrides
Ao trpico
Tiempo real -
Tiempo atmico
Tiempo real -
0,0000
-5,0000
UT-TAI (s)
-15,0000
UTC-TA I (s)
-10,0000
-25,0000
-20,0000
-30,0000
-35,0000
1960
1965
1970
1975
1980
1985
1990
1995
2000
2005
Ao
Tiempo real -
El calendario
Calendario juliano
1 ao = 365,25 das
un da adicional (ao bisiesto) cada 4 aos
Tiempo real -
Referencias de tiempo
Tiempo real -
Relojes
Caractersticas dinmicas
Granularidad
Exactitud
Estabilidad
variacin o movimiento
Tiempo real -
(t ) (t )
t t
1+
Relojes - 10
Relojes en Ada
Ada.Real_Time
Tiempo real -
Relojes en Ada- 11
Intervalos de tiempo
Tiempo real -
Relojes en Ada- 12
Ada.Calendar (1)
package Ada.Calendar is
type Time is private;
subtype
subtype
subtype
subtype
Year_Number
Month_Number
Day_Number
Day_Duration
is
is
is
is
Integer
Integer
Integer
Duration
range
range
range
range
1901..2099;
1..12;
1..31;
0.0..86_400.0;
Year
(Date
Month (Date
Day
(Date
Seconds(Date
:
:
:
:
procedure Split(Date
Year
Month
Day
Seconds
Tiempo real -
Time)
Time)
Time)
Time)
return
return
return
return
:
:
:
:
:
Time;
Year_Number;
Month_Number;
Day_Number;
Day_Duration);
in
out
out
out
out
Year_Number;
Month_Number;
Day_Number;
Day_Duration;
Relojes en Ada- 13
Ada.Calendar (2)
function Time_Of
(Year
: Year_Number;
Month
: Month_Number;
Day
: Day_Number;
Seconds : Day_Duration := 0.0)
return
Time;
function "+"
function "+"
function "-"
function "-"
Duration;
function
function
function
function
(Left
(Left
(Left
(Left
"<"
"<="
">"
">="
:
:
:
:
(Left,
(Left,
(Left,
(Left,
Time;
Duration;
Time;
Time;
Right
Right
Right
Right
:
:
:
:
Right
Right
Right
Right
Time)
Time)
Time)
Time)
:
:
:
:
Duration)
Time)
Duration)
Time)
return
return
return
return
return Time;
return Time;
return Time;
return
Boolean;
Boolean;
Boolean;
Boolean;
Time_Error : exception;
end Ada.Calendar;
Tiempo real -
Relojes en Ada- 14
Comentarios a Ada.Calendar
El reloj se supone
Tiempo real -
Relojes en Ada- 15
Ejemplo
declare
use Ada.Calendar;
Start, Finish
: Time;
Interval
: Duration;
begin
Start := Clock;
-- instrucciones cuya duracin se mide
Finish := Clock;
Interval := Finsih - Start;
end;
Tiempo real -
Relojes en Ada- 16
Ada.Real_Time (1)
package Ada.Real_Time is
type Time is
Time_First :
Time_Last :
Time_Unit :
private;
constant Time;
constant Time;
constant := -- real number;
Time_Span;
Time_Span;
Time_Span;
Time_Span;
"+"
"+"
"-"
"-"
(Left
(Left
(Left
(Left
function
function
function
function
"<"
"<="
">"
">="
(Left,
(Left,
(Left,
(Left,
Tiempo real -
:
:
:
:
Time;
Time_Span;
Time;
Time;
Right
Right
Right
Right
:
:
:
:
Right
Right
Right
Right
Time)
Time)
Time)
Time)
:
:
:
:
return
return
return
return
Boolean;
Boolean;
Boolean;
Boolean;
Relojes en Ada- 17
Ada.Real_Time (2)
function
function
function
function
function
function
function
function
function
function
function
function
"<"
"<="
">"
">="
(Left,
(Left,
(Left,
(Left,
Right
Right
Right
Right
:
:
:
:
Time_Span)
Time_Span)
Time_Span)
Time_Span)
return
return
return
return
Time_Span;
Time_Span;
Time_Span;
Time_Span;
Time_Span;
Integer;
Time_Span;
Time_Span;
Boolean;
Boolean;
Boolean;
Boolean;
return Duration;
return Time_Span;
return Time_Span;
return Time_Span;
return Time_Span;
Tiempo real -
Relojes en Ada- 18
Ada.Real_Time (3)
Time;
out Seconds_Count;
out Time_Span);
: Seconds_Count; TS : Time_Span)
end Ada.Real_Time;
Tiempo real -
Relojes en Ada- 19
Relojes en Ada- 20
Tiempo real -
Relojes en Ada- 21
Ejemplo
declare
use Ada.Real_Time;
Start, Finish : Time;
Frame
: Time_Span := Milliseconds(10);
begin
Start := Clock;
-- instrucciones
Finish := Clock;
if Finish - Start > Frame then
raise Time_Error; -- excepcin definida por el programador
end if;
end;
Tiempo real -
Relojes en Ada- 22
declare
T0, T1, T2
: Ada.Real_Time.Time;
Execution_Time : Ada.Real_Time.Time_Span;
N : constant Positive := ...;
begin
T0 = Ada.Real_Time.Clock;
for i in 1 .. N loop
-- secuencia de instrucciones
end loop;
T1 = Ada.Real_Time.Clock;
for i in 1 .. N loop
null;
end loop;
T2 = Ada.Real_Time.Clock;
Execution_Time = ((T1 - T0) - (T2 - T1))/N;
end;
Tiempo real -
Relojes en Ada- 23
Relojes en Java
Tiempo real -
Relojes en Java -
24
Tiempo real -
Relojes en Java -
25
Relojes
public abstract class Clock
{
public Clock();
public static Clock getRealtimeClock();
public abstract RelativeTime getResolution();
public AbsoluteTime getTime();
public abstract void getTime(AbsoluteTime time);
public abstract void setResolution(RelativeTime resolution);
}
Tiempo real -
Relojes en Java -
26
Ejemplo
Tiempo real -
Relojes en Ada- 27
Temporizadores
public abstract class Timer extends AsyncEvent
{
protected Timer(HighResolutionTime t, Clock c,
AsyncEventHandler handler);
...
public void reschedule(HighResolutionTime time);
public void start();
}
public class OneShotTimer extends Timer {...}
public class PeriodicTimer extends Timer {...}
Tiempo real -
28
Relojes en C / POSIX
Tiempo real -
29
Reloj-calendario
Tiempo real -
30
/* segundos */
/* nanosegundos */
Tiempo real -
31
Leer la hora:
#include <time.h>
int clock_gettime (clockid_t clockid,
struct timespec *tp);
Poner en hora
#include <time.h>
int clock_settime (clockid_t clockid,
const struct timespec *tp);
#include <time.h>
int clock_getres (clockid_t clockid,
struct timespec *res);
Tiempo real -
32
ndice
Tiempo real -
33
Retardos
un cierto tiempo
Hay dos tipos
Tiempo real -
34
Tiempo real -
35
La funcin
unsigned int sleep (unsigned int seconds);
Tiempo real -
36
La instruccin
delay until expresin;
Tiempo real -
37
La instruccin
delay until T;
Tiempo real -
38
Ejecucin de un retardo
el retardo
termina
aqu
instruccin
de
retardo
retardo especificado
Tiempo real -
error de interrupcin
granularidad inhibida preparado
ejecucin
39
ndice
Tiempo real -
40
Tiempo real -
41
Ejemplo
task Controller is
entry Call (T : Temperature);
end Controller;
task body Controller is
-- declaraciones
begin
loop
accept Call (T : Temperature) do
New_Temp := T;
end Call;
-- otras acciones
end loop;
end Controller;
Tiempo real -
42
Aceptacin temporizada
Tiempo real -
43
Llamada temporizada
Tiempo real -
44
Llamada condicional
Tiempo real -
45
select
P.E(...); -- P es un objeto protegido
or
delay 0.5;
end select;
Tiempo real -
46
Acciones temporizadas
Tiempo real -
47
Tiempo real -
48
La funcin
#include <pthread.h>
int pthread_cond_timedwait
(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec *abstime);
Tiempo real -
49
Tiempo real -
50
Temporizadores en POSIX
timer_t
El tiempo de espera se especifica mediante un valor de
tipo itimerspec:
struct itimerspec {
struct timespec it_interval; /* perodo */
struct timespec it_value} /* expiracin */
Tiempo real -
51
Creacin y destruccin de
temporizadores
La funcin
#include <signal.h>
#include <time.h>
int timer_create (clockid_t clock_id,
struct sigevent *evp,
timer_t *timerid);
Tiempo real -
52
Armar un temporizador
Se usa la funcin
int timer_settime (timer_t timerid,
int flags,
const struct itimerspec *value,
struct itimerspec *ovalue);
Tiempo real -
53
ndice
Tiempo real -
54
Requisitos temporales
Hay dos formas de enfocar este tema:
Mtodos formales:
Especificar las propiedades temporales con un modelo formal
Validar la especificacin
Comprobar que la implementacin satisface las propiedades
temporales
Mtodos analticos:
Tiempo real -
55
Atributos temporales
suceso de
activacin
tiempo de respuesta R
latencia
c1
tr
Tiempo real -
c2
tiempo de cmputo C = c1 + c2
tf
56
Parmetros temporales
Parmetros
Activacin
Peridica
Aperidica
Espordica
Tiempo real -
D
L
Jmin
Jmax
C
Plazo de respuesta (R D)
Tiempo lmite (tf L)
Latencia mnima
Latencia mxima
Tiempo de cmputo mximo
T
O
Perodo
Fase
Irregular, a rfagas, estocstica
Separacin mnima
57
procesos
Generalmente se trata de
Tiempo real -
58
Criticidad
Una tarea de tiempo real puede ser
Crtica (hard) : No se puede admitir que se sobrepase el
plazo de respuesta especificado ni una sola vez
Acrtica (soft) : Es admisible que se sobrepase el plazo
ocasionalmente
Firme (firm) : El plazo no es crtico, pero una respuesta
tarda no sirve para nada
Interactiva : No se especifican plazos de respuesta, sino
tiempos de respuesta medios
Tiempo real -
59
Tiempo real -
60
instante
inicial
delay until 0 delay until 100
40ms
60ms
75ms
t
0
Tiempo real -
60
100
140
200 225
300
61
Tiempo real -
62
Tiempo real -
63
Tiempo real -
64
Activation : Event;
task body Sporadic is
begin
-- iniciacin
loop
Activation.Wait;
-- accin espordica
end loop;
end Sporadic;
Tiempo real -
65
use Ada.Real_Time.Time;
...
protected type Event is
procedure Signal;
entry
Wait (Event_Time : out Time);
private
Occurred
: Boolean := False;
Occurrence_Time : Time;
end Event;
Tiempo real -
66
Tiempo real -
67
Activation : Event;
task body Sporadic is
Separation : Time_Span;
Next_Time, Activation_Time : Time;
begin
-- iniciacin
loop
Activation.Wait (Activation_Time);
Next_Time := Activation_Time + Separation;
-- accin espordica
delay until Next_Time;
end loop;
end Sporadic;
Tiempo real -
68
Tiempo real -
69
Tiempo real -
70
Tiempo real -
/* end periodic */
71
main () {
pthread_t periodic_id;
pthread_attr_t periodic_attr;
sigset_t signals;
int t_signal;
t_signal = ...; /* la misma de antes! */
sigemptyset (&signals);
sigaddset (&signals, t_signal;
pthread_sigmask (SIG_BLOCK, &signals, NULL);
pthread_attr_init (&periodic_attr);
pthread_create (&periodic_id, &periodic_attr,
periodic, NULL);
pthread_join (&periodic_id);
}
Tiempo real -
72
ndice
Tiempo real -
73
Fallos temporales
Una tarea puede incumplir su plazo por varias razones,
por ejemplo:
El tiempo de cmputo no est bien calculado
El anlisis de tiempos de respuesta no es realista
Las herramientas de anlisis contienen errores
No se cumplen las hiptesis de diseo
(por ejemplo, separacin mnima entre eventos)
En estos casos hay que detectar los fallos
Si el sistema es crtico, debe recuperarse
Tiempo real -
74
Tiempo real -
75
Recuperacin de fallos
Tiempo real -
76
Resumen
Tiempo real -
77