Embedded Systems Programming: Ver Onica Gaspes
Embedded Systems Programming: Ver Onica Gaspes
Verónica Gaspes
www2.hh.se/staff/vero
Real Time
Constrained by time
Do something before a certain point in time.
Difficult
There is a limit to how fast a processor can work . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Real Time
Constrained by time
Do something before a certain point in time.
Difficult
There is a limit to how fast a processor can work . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Real Time
Constrained by time
Do something before a certain point in time.
Difficult
There is a limit to how fast a processor can work . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Real Time
Constrained by time
Do something before a certain point in time.
Difficult
There is a limit to how fast a processor can work . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Execution speed
Execution time . . .
the time from program start to program stop
Execution speed
Execution time . . .
the time from program start to program stop
Execution speed
Execution time . . .
the time from program start to program stop
Execution speed
Execution time . . .
the time from program start to program stop
Execution speed
Execution time . . .
the time from program start to program stop
Obtaining WCET
By meassurement
Deal with data dependencies by By analysis
testing the program on every Deal with data dependencies
possible combination of input using semantic information and
data. conservative approximations.
Obtaining WCET
By meassurement
Deal with data dependencies by By analysis
testing the program on every Deal with data dependencies
possible combination of input using semantic information and
data. conservative approximations.
Obtaining WCET
By meassurement
Deal with data dependencies by By analysis
testing the program on every Deal with data dependencies
possible combination of input using semantic information and
data. conservative approximations.
Obtaining WCET
By meassurement
Deal with data dependencies by By analysis
testing the program on every Deal with data dependencies
possible combination of input using semantic information and
data. conservative approximations.
Obtaining WCET
By meassurement
Deal with data dependencies by By analysis
testing the program on every Deal with data dependencies
possible combination of input using semantic information and
data. conservative approximations.
Obtaining WCET
By meassurement
Deal with data dependencies by By analysis
testing the program on every Deal with data dependencies
possible combination of input using semantic information and
data. conservative approximations.
WCET by meassurements
How likely is it that it generates data that finds the worst case?
Constrained by time Deadlines Priorities Deadlines in TinyTimber
WCET by meassurements
WCET by meassurements
A conservative approximation
Example
Each turn takes 300 ms and so
for(i=1;i<=10;i++){ WCET = 10*300 ms!
if(E)
// do something
Assume the worst, err on the safe
// that takes 300ms
side!
else
// do something Using semantic information
// that takes 5ms
Suppose E is i<3. The test is true
}
at most 2 turns, WCET is
2*300+8*5 = 640ms!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
A conservative approximation
Example
Each turn takes 300 ms and so
for(i=1;i<=10;i++){ WCET = 10*300 ms!
if(E)
// do something
Assume the worst, err on the safe
// that takes 300ms
side!
else
// do something Using semantic information
// that takes 5ms
Suppose E is i<3. The test is true
}
at most 2 turns, WCET is
2*300+8*5 = 640ms!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
A conservative approximation
Example
Each turn takes 300 ms and so
for(i=1;i<=10;i++){ WCET = 10*300 ms!
if(E)
// do something
Assume the worst, err on the safe
// that takes 300ms
side!
else
// do something Using semantic information
// that takes 5ms
Suppose E is i<3. The test is true
}
at most 2 turns, WCET is
2*300+8*5 = 640ms!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
A conservative approximation
Example
Each turn takes 300 ms and so
for(i=1;i<=10;i++){ WCET = 10*300 ms!
if(E)
// do something
Assume the worst, err on the safe
// that takes 300ms
side!
else
// do something Using semantic information
// that takes 5ms
Suppose E is i<3. The test is true
}
at most 2 turns, WCET is
2*300+8*5 = 640ms!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Obtaining WCET
Testing Analysis
is likely to find the typical can always find a safe WCET
execution times, but finding the approximation but comming close
worst case is much harder. to the real WCET is much harder
In this course
We will assume that for any sequential program fragment a safe
WCET can be obtained either by meassurement or by analysis or
both!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Obtaining WCET
Testing Analysis
is likely to find the typical can always find a safe WCET
execution times, but finding the approximation but comming close
worst case is much harder. to the real WCET is much harder
In this course
We will assume that for any sequential program fragment a safe
WCET can be obtained either by meassurement or by analysis or
both!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Obtaining WCET
Testing Analysis
is likely to find the typical can always find a safe WCET
execution times, but finding the approximation but comming close
worst case is much harder. to the real WCET is much harder
In this course
We will assume that for any sequential program fragment a safe
WCET can be obtained either by meassurement or by analysis or
both!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Obtaining WCET
Testing Analysis
is likely to find the typical can always find a safe WCET
execution times, but finding the approximation but comming close
worst case is much harder. to the real WCET is much harder
In this course
We will assume that for any sequential program fragment a safe
WCET can be obtained either by meassurement or by analysis or
both!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Obtaining WCET
Testing Analysis
is likely to find the typical can always find a safe WCET
execution times, but finding the approximation but comming close
worst case is much harder. to the real WCET is much harder
In this course
We will assume that for any sequential program fragment a safe
WCET can be obtained either by meassurement or by analysis or
both!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Scheduling
Interleaving
Moreover, if tasks can be split into arbitrarily small fragments,
there are infinitely many ways of running the fragments of even
just 2 tasks!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Scheduling
Interleaving
Moreover, if tasks can be split into arbitrarily small fragments,
there are infinitely many ways of running the fragments of even
just 2 tasks!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Scheduling
Interleaving
Moreover, if tasks can be split into arbitrarily small fragments,
there are infinitely many ways of running the fragments of even
just 2 tasks!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Scheduling
Interleaving
Moreover, if tasks can be split into arbitrarily small fragments,
there are infinitely many ways of running the fragments of even
just 2 tasks!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Scheduling
Interleaving
Moreover, if tasks can be split into arbitrarily small fragments,
there are infinitely many ways of running the fragments of even
just 2 tasks!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Scheduling
The schedule
is a major factor
in real-time
behaviour of
concurrent tasks!
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Three issues
Deadlines
How do we express the real-time constraints a program must meet?
Priority scheduling!
Schedulability analysis
How do we tell whether scheduling is impossible? Ahead of time or
only when it is too late? (next lecture)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Three issues
Deadlines
How do we express the real-time constraints a program must meet?
Priority scheduling!
Schedulability analysis
How do we tell whether scheduling is impossible? Ahead of time or
only when it is too late? (next lecture)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Three issues
Deadlines
How do we express the real-time constraints a program must meet?
Priority scheduling!
Schedulability analysis
How do we tell whether scheduling is impossible? Ahead of time or
only when it is too late? (next lecture)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Three issues
Deadlines
How do we express the real-time constraints a program must meet?
Priority scheduling!
Schedulability analysis
How do we tell whether scheduling is impossible? Ahead of time or
only when it is too late? (next lecture)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Deadlines
Deadlines
Deadlines
Deadlines
Deadlines
Deadlines
Meeting a deadline
Generate some specific response
before the specified time
Signal level must reach
10mV before . . .
Letter must be post-stamped
no later than . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Deadlines
Meeting a deadline
Generate some specific response
before the specified time
Signal level must reach
10mV before . . .
Letter must be post-stamped
no later than . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Deadlines
Meeting a deadline
Generate some specific response
before the specified time
Signal level must reach
10mV before . . .
Letter must be post-stamped
no later than . . .
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Example 1
When a SIG PIN CHANGE Example 2
interrupt occurs, react within When a timer signals that a
15ms from the time of the future baseline is due, react within
interrupt (i.e. the newly defined 200ms from the new baseline
baseline)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Example 1
When a SIG PIN CHANGE Example 2
interrupt occurs, react within When a timer signals that a
15ms from the time of the future baseline is due, react within
interrupt (i.e. the newly defined 200ms from the new baseline
baseline)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Example 1
When a SIG PIN CHANGE Example 2
interrupt occurs, react within When a timer signals that a
15ms from the time of the future baseline is due, react within
interrupt (i.e. the newly defined 200ms from the new baseline
baseline)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Example 1
When a SIG PIN CHANGE Example 2
interrupt occurs, react within When a timer signals that a
15ms from the time of the future baseline is due, react within
interrupt (i.e. the newly defined 200ms from the new baseline
baseline)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Begin execution?
Does that mean completing the first assembler instruction? Is that
observable?
Begin execution?
Does that mean completing the first assembler instruction? Is that
observable?
Begin execution?
Does that mean completing the first assembler instruction? Is that
observable?
Begin execution?
Does that mean completing the first assembler instruction? Is that
observable?
Begin execution?
Does that mean completing the first assembler instruction? Is that
observable?
Conclusion
All instructions should be completed before the deadline for all
messages of a chain-reaction.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Conclusion
All instructions should be completed before the deadline for all
messages of a chain-reaction.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Conclusion
All instructions should be completed before the deadline for all
messages of a chain-reaction.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Conclusion
All instructions should be completed before the deadline for all
messages of a chain-reaction.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Timely reaction
Baseline Deadline
"start after" "finish before"
Original event
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
Original event
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Timely reaction
Baseline Deadline
"start after" "finish before"
A
SYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
A
SYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
A
SYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Timely reaction
Baseline Deadline
"start after" "finish before"
A
ASYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
A
ASYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
A
ASYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Priorities
Priority scheduler
Always run the task with the highest priority! (tasks with the same
prio are sorted according to some secondary scheme, e.g. FIFO)
A task can only run after all tasks considered more important have
terminated or are blocked.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Priorities
Priority scheduler
Always run the task with the highest priority! (tasks with the same
prio are sorted according to some secondary scheme, e.g. FIFO)
A task can only run after all tasks considered more important have
terminated or are blocked.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Priorities
Priority scheduler
Always run the task with the highest priority! (tasks with the same
prio are sorted according to some secondary scheme, e.g. FIFO)
A task can only run after all tasks considered more important have
terminated or are blocked.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Priorities
Priority scheduler
Always run the task with the highest priority! (tasks with the same
prio are sorted according to some secondary scheme, e.g. FIFO)
A task can only run after all tasks considered more important have
terminated or are blocked.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Terminology
Terminology
Terminology
Terminology
Preemptivness
A system where the scheduler is run only when a task calls the
kernel (or terminate) is non-preemptive.
A system where it also runs as the result of interrupts is called
preemptive.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Terminology
Preemptivness
A system where the scheduler is run only when a task calls the
kernel (or terminate) is non-preemptive.
A system where it also runs as the result of interrupts is called
preemptive.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Terminology
Preemptivness
A system where the scheduler is run only when a task calls the
kernel (or terminate) is non-preemptive.
A system where it also runs as the result of interrupts is called
preemptive.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
in OS
Supported by real-time operating systems like QNX, VxWorks,
RTLinux, Lynx and standards like POSIX (pthreads)
in Languages
The basis of real-time languages like Ada and Real-time Java
This course
Preemptive scheduling (dispatch might be called within
interrupt handlers).
Static as well as dynamic priorities.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
in OS
Supported by real-time operating systems like QNX, VxWorks,
RTLinux, Lynx and standards like POSIX (pthreads)
in Languages
The basis of real-time languages like Ada and Real-time Java
This course
Preemptive scheduling (dispatch might be called within
interrupt handlers).
Static as well as dynamic priorities.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
in OS
Supported by real-time operating systems like QNX, VxWorks,
RTLinux, Lynx and standards like POSIX (pthreads)
in Languages
The basis of real-time languages like Ada and Real-time Java
This course
Preemptive scheduling (dispatch might be called within
interrupt handlers).
Static as well as dynamic priorities.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
in OS
Supported by real-time operating systems like QNX, VxWorks,
RTLinux, Lynx and standards like POSIX (pthreads)
in Languages
The basis of real-time languages like Ada and Real-time Java
This course
Preemptive scheduling (dispatch might be called within
interrupt handlers).
Static as well as dynamic priorities.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
What happens?
What happens?
Using priorities
Any cycles not used by this task are offered to the second but
highest priority task.
Using priorities
Any cycles not used by this task are offered to the second but
highest priority task.
Using priorities
Any cycles not used by this task are offered to the second but
highest priority task.
Using priorities
Any cycles not used by this task are offered to the second but
highest priority task.
Using priorities
Using priorities
Using priorities
Using priorities
Timely reaction
Baseline Deadline
"start after" "finish before"
Original event
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
Original event
Late reaction
Baseline Deadline
"start after" "finish before"
Original event
Late reaction
Baseline Deadline
"start after" "finish before"
Original event
Late reaction
Baseline Deadline
"start after" "finish before"
Original event
Timely reaction
Baseline Deadline
"start after" "finish before"
A
ASYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
A
ASYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Late reaction
Baseline Deadline
"start after" "finish before"
A
ASYNC(&B,meth,arg)
Original event
B
same same
baseline deadline
Adjusted deadlines
Baseline Deadline
"start after" "finish before"
A
BEFORE(dl,&B,meth,arg)
B
same
baseline dl
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
Baseline Deadline
"start after" "finish before"
A
BEFORE(dl,&B,meth,arg)
B
same
baseline dl
Constrained by time Deadlines Priorities Deadlines in TinyTimber
baseline deadline
"start after" "finish before"
A
AFTER(bl,&B,meth,arg)
bl
Constrained by time Deadlines Priorities Deadlines in TinyTimber
baseline deadline
"start after" "finish before"
A
AFTER(bl,&B,meth,arg)
bl
MAX(now, current−>baseline+bl)
Constrained by time Deadlines Priorities Deadlines in TinyTimber
baseline deadline
"start after" "finish before"
A
AFTERBEFORE(bl,dl,&B,meth,arg)
B
bl dl
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Late reaction
baseline deadline
"start after" "finish before"
A
AFTERBEFORE(bl,dl,&B,meth,arg)
B
bl dl
Constrained by time Deadlines Priorities Deadlines in TinyTimber
timestamp
deadline = infinity!
top
level
object
Interrupt signal
Note
Interrupt handlers are scheduled by the CPU hardware, i.e. they
will run as fast as possible without regard to any deadline.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
timestamp
deadline = infinity!
top
level
object
Interrupt signal
Note
Interrupt handlers are scheduled by the CPU hardware, i.e. they
will run as fast as possible without regard to any deadline.
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Expressing deadlines
In TinyTimber.h
#define BEFORE(dl, to, meth, arg) \
AFTERBEFORE(0, dl, to, meth, arg);
Expressing deadlines
In TinyTimber.h
#define BEFORE(dl, to, meth, arg) \
AFTERBEFORE(0, dl, to, meth, arg);
In the application
Using BEFORE, we can both define the deadline for a chain of
reactions to an external interrupt, and fork off a new chain of
reactions with its own deadline at any point.
Core question
What will be the preferred relation between deadlines and
priorities?
Constrained by time Deadlines Priorities Deadlines in TinyTimber
In the application
Using BEFORE, we can both define the deadline for a chain of
reactions to an external interrupt, and fork off a new chain of
reactions with its own deadline at any point.
Core question
What will be the preferred relation between deadlines and
priorities?
Constrained by time Deadlines Priorities Deadlines in TinyTimber
In the application
Using BEFORE, we can both define the deadline for a chain of
reactions to an external interrupt, and fork off a new chain of
reactions with its own deadline at any point.
Core question
What will be the preferred relation between deadlines and
priorities?
Constrained by time Deadlines Priorities Deadlines in TinyTimber
In the application
Using BEFORE, we can both define the deadline for a chain of
reactions to an external interrupt, and fork off a new chain of
reactions with its own deadline at any point.
Core question
What will be the preferred relation between deadlines and
priorities?
Constrained by time Deadlines Priorities Deadlines in TinyTimber
Priority assignment
Question
How do we set thread/message priority for the purpose of meeting
deadlines?
:-(
In neither case a method exists that is both predictable and
generally applicable to all programs!
Priority assignment
Question
How do we set thread/message priority for the purpose of meeting
deadlines?
:-(
In neither case a method exists that is both predictable and
generally applicable to all programs!
Priority assignment
Question
How do we set thread/message priority for the purpose of meeting
deadlines?
:-(
In neither case a method exists that is both predictable and
generally applicable to all programs!
Priority assignment
Question
How do we set thread/message priority for the purpose of meeting
deadlines?
:-(
In neither case a method exists that is both predictable and
generally applicable to all programs!
Priority assignment
Question
How do we set thread/message priority for the purpose of meeting
deadlines?
:-(
In neither case a method exists that is both predictable and
generally applicable to all programs!