Professional Documents
Culture Documents
Phelma 2A - SEOC Real-Time Systems: Constraints and Implementation Wh. (At - y - en - O)
Phelma 2A - SEOC Real-Time Systems: Constraints and Implementation Wh. (At - y - en - O)
Phelma 2A - SEOC Real-Time Systems: Constraints and Implementation Wh. (At - y - en - O)
(at|y|en|o)
Phelma 2A - SEOC
Real-Time Systems: What: Correct-by-construction implementations of (hard) real-time systems.
When: Sem 4: after the course on operating systems and concurrent programming;
Constraints and Implementation in parallel with the course on real-time operating systems; before the use of those
techniques in 3A courses.
Florence Maraninchi Why: because getting correct real-time programs is hopeless without dedicated
www-verimag.imag.fr/˜maraninx methods.
Who: F. Maraninchi, has worked on the design and validation of embedded critical
Verimag/Grenoble INP - Ensimag systems and the HW/SW interface (with applications in consumer electronics,
avionic and automotive systems, medical systems, ...).
2023-2024 – 7th Edition
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 1 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 2 / 345
The scientific background for these two activities includes: formal models for time and
concurrency. This course focuses on high level formalisms and programming languages
for real-time systems.
1
https://www.ansys.com/fr-fr/products/embedded-software/ansys-scade-suite
2
http://fr.wikipedia.org/wiki/Joseph Sifakis
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 3 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 4 / 345
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 5 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 6 / 345
Outline
1 “Real-Time” and the 3 Categories of Computer Systems
1 “Real-Time” and the 3 Categories of Computer Systems
2 Reactive Systems and the Connection to inputs/outputs
2 Reactive Systems and the Connection to inputs/outputs
3 Logical vs Physical Time
3 Logical vs Physical Time
4 Logical Concurrency vs Implementation Parallelism
4 Logical Concurrency vs Implementation Parallelism
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 7 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 8 / 345
“Real-Time” and the 3 Categories of Computer Systems “Real-Time” and the 3 Categories of Computer Systems
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 9 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 10 / 345
“Real-Time” and the 3 Categories of Computer Systems “Real-Time” and the 3 Categories of Computer Systems
The environment
AIR
Heater
loop-based behavior (does not necessarily terminate), where inputs come all the time
(human actions on buttons, mouse, keyboard) and outputs are produced all the time The same as interactive systems, but the speed of the interaction is driven by the
also (changes of the interface, effects on the underlying computer system). (physical) environment. The computer system should be sufficiently fast in order not to
miss relevant evolutions of the environment.
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 11 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 12 / 345
“Real-Time” and the 3 Categories of Computer Systems “Real-Time” and the 3 Categories of Computer Systems
“Real-Time” and the 3 Categories of Computer Systems “Real-Time” and the 3 Categories of Computer Systems
t Time
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 15 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 16 / 345
“Real-Time” and the 3 Categories of Computer Systems I/O
Quizz 1
1 “Real-Time” and the 3 Categories of Computer Systems
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 17 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 18 / 345
I/O I/O
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 19 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 20 / 345
I/O I/O
Imposing Regular Timing in the Single-Loop Code Imposing Regular Timing in the Single-Loop Code:
FreeRTOS Solution (for inputs)
(Used for static scheduling examples, see slide 61)
init m; TickType_t tLast;
while (true) { — period 1p tLast = xTaskGetTickCount(); // current absolute time
T = what_time_is_it(); — ET(write) exact while (1) {
// one reaction, and then wait
read (i); execution time of write read (i); compute o and update m; write (o);
compute o and update m; assumed to be constant. vTaskDelayUntil( &tLast, FUNCTION_PERIOD );
}
T’ = what_time_is_it(); — what time is it also
wait (1p - (T’-T) executes in constant
FUNCTION_PERIOD
- ET(write)) ; (negligible?) time.
write (o); time
}
tLast wait tLast wait tLast
I/O I/O
Arduino example
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 23 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 24 / 345
I/O I/O
I/O I/O
The DS18B20 Temperature Sensor and the OneWire Protocol Example: Typical Use of the DS18B20 Temperature Sensor
http://playground.arduino.cc/Learning/OneWire
#include <OneWire.h> // One Wire protocol
OneWire ds(DS18B20_pin);
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 27 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 28 / 345
I/O I/O
Dealing with a Temperature Input (1) Dealing with a Temperature Input (2)
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 29 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 30 / 345
I/O I/O
4 x 3 Faulty
physical connections
4 identical physical devices
each of them made of 2 wires
(4 x 3 values, deg/s) −−> 4 x 3 x 2 inputs
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 31 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 32 / 345
I/O I/O
Internal Structure of the Voter (roll value) Several Sensors for One Combined Input
Implements f (xa1 , xb1 , xa2 , xb2 , xa3 , xb3 , xa4 , xb4 ),
depending on the number of faults. Airbus Helicopters Example
Accelerometers
First, each “channel computes: if xak = xbk then xak else ERROR Phase sensor
if three failures happened (together) “recently” then main
take a safe value rotor rm , rt :
rotation speed w.r.t.
else if the 4 channels are ok then main or tail rotor
take the olympic average of the 4 values
tail rotor
else if 3 channels only are ok then
take the median value of the three relevant values rm = 2 rt = 4
else if 2 channels only are ok then
and
take the average of the two relevant values sampling freq.
I/O I/O
Sample the main rotor phase sensor and the accelerometers at the same frequency 2 Reactive Systems and the Connection to inputs/outputs
Sampling Regularity in the single-loop code
Gather samples from the accelerometer on an object that rotates rm times faster I/O Timing and Protocol/Scheduling Problems
than the main rotor Fault-Tolerance, Multiple Sensors
Compute various indicators on the set of samples that corresponds to one Summary of I/O Aspects and General On-Board Organizations
revolution of the monitored object.
+ the acquisition card fills a buffer of tuples (phase, accel), which is then sent to the
computer in packets of sufficient size for the bus.
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 35 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 36 / 345
I/O I/O
Summary of I/O Aspects Decoupling I/O Management and Computation via Buffers
used in quizz slide 49.
Ensuring sampling regularity if done in the “functional” code: ok for simple Inputs Outputs
programs on a microcontroller directly connected to the I/O; otherwise need to from sensors to actuators
reactive real-time system
decouple. i0
i1 o0
Some inputs come from equipments for which a “protocol” has to be respected
ADC
DAC
i2 o1
(e.g., the temperature sensor on the Arduino); imposes scheduling problems in the
i3 o2
functional code.
In complex systems, several sensors need to be sampled synchronously (Airbus H SW
SW
example), and there is an acquisition card that fills a buffer HW
Fault tolerance implies the use of several sensors, the voting principle being done
before providing a clean input to the functional code The input acquisition card ensures sampling regularity; buffers increase the end-2-end latency;
constraint for the computer: compute fast enough.
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 37 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 38 / 345
Synchronous Circuits
Difference between the discrete-time idea represented by timing diagrams, and the actual
1 “Real-Time” and the 3 Categories of Computer Systems
physical time involved by propagation in electronic circuits (from inputs to outputs).
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 39 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 40 / 345
Logical vs Physical Time Logical vs Physical Time
penser-modeliser-et-maitriser-le-calcul-informatique/parallelisme-flot-de-donnees-le-langage-synchrone-lustre
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 41 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 42 / 345
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 43 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 44 / 345
There’s no reason why one element of the design (logical concurrency) should be
In the computation of the formula: mapped onto exactly one element of the implementation.
i.e., exactly one subproblem per computer/core/thread.
(x 2 + (a + 2b + c 4 ))
x3 + z3
Think in terms of Logical Concurrency, rely on compilers and other automatic tools to
what are the “independent” parts? (things that can be done by two persons, get one or several parallel implementations.
independently, before their results are merged)?
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 45 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 46 / 345
From Logical Concurrency to Implementation Parallelism From Logical Concurrency to Implementation Parallelism
Logical Concurrency
Implementation Parallelism
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 49 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 50 / 345
Outline
and Its Scheduling Possibilities 7 Data Dependencies between Tasks - Same Frequencies
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 51 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 52 / 345
Specification
5 2 Independent Tasks, Same Frequencies
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 53 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 54 / 345
Single-loop codes for A alone, or B alone Single-loop code for A (similar for B)
(See FreeRTOS solution for regular input sampling, slide 22).
Timing Diagrams
#define A_PERIOD ... #define B_PERIOD ... ai0 ai1 ai2 ai3
TickType_t tLast; TickType_t tLast;
initA(); initB();
time
tLast = xTaskGetTickCount(); tLast = xTaskGetTickCount(); A A A A
while (1) { while (1) {
read (ai); read (bi); ao0 ao1 ao2 ao3
ao = A (ai); bo = B (bi);
write (ao); write (bo); A PERIOD
vTaskDelayUntil vTaskDelayUntil
(&tLast, A_PERIOD); (&tLast, B_PERIOD);
} }
Constraint (if A alone on a single-core): WCET(A)+δ R+δ W < A PERIOD = 1s
— How to choose A PERIOD, B PERIOD? where δ R (resp. δ W ) is the (assumed constant) execution time of the Read (resp.
— Constraint on WCET(A), WCET(B) (if A, B executed alone)? Write).
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 55 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 56 / 345
2 Independent Tasks, Same Frequencies 2 Independent Tasks, Same Frequencies
Scheduling A and B on the same core Scheduling A and B on the same core - Timing
ai0,bi0 ai1,bi1 ai2,bi2
#define A_PERIOD ...
time
#define B_PERIOD A_PERIOD A B A B A B
TickType_t tLast;
initA(); initB(); ao0,bo0 ao1,bo1 ao2,bo2
tLast = xTaskGetTickCount();
while (1) { A PERIOD
read (ai); read (bi);
ao = A(ai); bo = B (bi); B PERIOD
write(ao); write (bo);
vTaskDelayUntil
(&tLast, B_PERIOD); WCET(A)+WCET(B)+2 × δ R + 2 × δ W < A PERIOD = B PERIOD
}
Sometimes you know by studying the functionality that A and B don’t produce their
worst-case in the same pass in the loop... can help relax the constraint
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 57 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 58 / 345
Specification
5 2 Independent Tasks, Same Frequencies
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 59 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 60 / 345
Single-loop codes for A alone or B alone Scheduling A and B on the same core - Solution (a)
#define A_PERIOD 1s
TickType_t tLast;
(See FreeRTOS solution for regular sampling, slide 22). initA(); initB();
half = 1;
#define A_PERIOD 1s #define B_PERIOD 2s tLast = xTaskGetTickCount();
TickType_t tLast; TickType_t tLast; while (1) {
initA(); initB(); read (ai); if (half) read (bi);
tLast = xTaskGetTickCount(); tLast = xTaskGetTickCount(); ao = A(ai); if (half) bo = B (bi);
while (1) { while (1) { write(ao); if (half) write (bo);
read (ai); read (bi); half = !half;
ao = A (ai); bo = B (bi); vTaskDelayUntil
write (ao); write (bo); (&tLast, A_PERIOD);
vTaskDelayUntil vTaskDelayUntil }
(&tLast, A_PERIOD); (&tLast, B_PERIOD);
} }
WCET(A)+WCET(B)+2 × δ R + 2 × δ W < A PERIOD
In each A period, we execute A alone, or A and B.
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 61 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 62 / 345
Scheduling A and B on the same core - Solution (b) Scheduling A and B on the same core - Solution (c)
Requires that the B function be split into two “equal timing” parts B first, B second.
#define A_PERIOD 1s
TickType_t tLast; Use dynamic scheduling to ensure proper splitting of B.
initA(); initB();
int half = 1;
tLast = xTaskGetTickCount(); See course on real-time operating systems for general methods.
while (1) { And later in this course, specific methods.
read (ai); if (half) read (bi);
ao = A(ai); if (half) B_first (bi); else bo=B_second();
write(ao); if (!half) write (bo);
half = !half;
vTaskDelayUntil But keep in mind that these manual splitting methods are still in use, in order to avoid
(&tLast, A_PERIOD);
} uncertainties due to the use of a dynamic scheduler.
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 65 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 66 / 345
Data Dependencies between Tasks - Same Frequencies Data Dependencies between Tasks - Frequencies 1 and 2
High-level programming languages allow to reason on the functionality and logical timing
of the problem independently of the actual execution time on a particular architecture.
F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 67 / 345 F. Maraninchi (Ensimag) SEOC 2A 2023-2024 – 7th Edition 68 / 345
Communication-by-sampling means:
— In the first case, drop one sample out of two
— In the second case, use the same sample twice