Sunteți pe pagina 1din 48

Clock-driven scheduling

Also known as static or off-line scheduling

Michal Sojka

Czech Technical University in Prague,


Faculty of Electrical Engineering,
Department of Control Engineering

November 8, 2017

Some slides are derived from lectures by Steve Goddard and James H. Anderson

1 / 48
Classification of scheduling algorithms
(used in real-time systems)

Scheduling algorithms

Off-line scheduling On-line scheduling


(static, clock-driven) (dynamic)

Deadline-driven General purpose OS


Static-priority scheduling
scheduling scheduling
(VxWorks, SCHED_FIFO)
(EDF, …) (fair, interactive, …)

2 / 48
Table of contents

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

3 / 48
What Is Clock-Driven Scheduling

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

4 / 48
What Is Clock-Driven Scheduling

Motivation

Helicopter control system


Three tasks:
1 180× per second, total computation time 1 ms
Read sensor data
Compute the control laws of the inner yaw-control loop
Apply the control laws results to the actuators
Perform internal checks
2 90× per second, total computation time 3 ms:
Compute the control laws of the inner pitch-control loop
Compute the control laws of the inner roll- and collective-control loop
3 30× per second, total computation time 10 ms:
Compute the control lows of the outer loops

How to implement this control system?


Safety-critical application ⇒ KISS

5 / 48
What Is Clock-Driven Scheduling

Clock-driven (or static or off-line) scheduling


(Chapter 5 from Liu)

Decision are only made at a priory chosen time instants


It is sufficient to have a hardware timer (no need for OS)
Regularly spaced time instants are popular
Schedule is computed off-line and stored for use at run-time
All parameters of hard real-time jobs are fixed and known
Scheduling overhead during run time is minimal
Complexity of the scheduling algorithm is not important
Good (optimal) off-line schedules can be found
Disadvantage: no flexibility
Applicable only when we know all about the system in advance
Fixed set of tasks, fixed and known task parameters and resource
requirements
Met by many safety-critical applications
Easier to certify
6 / 48
What Is Clock-Driven Scheduling

Clock driven (static) scheduling


For this lecture, we will consider the following periodic model:
n periodic tasks τ1 , . . . , τn ; n is constant
We assume the “rest of world” model.
τi is specified with tuple (ϕi , Ti , Ci , Di ), where
ϕi is task phase,
Ti is task period,
Ci is execution time of τi and
Di is relative deadline.
We will shorten this as (Ti , Ci , Di ), if ϕi = 0 and as (Ti , Ci ) if
ϕ i = 0 ∧ T i = Di .
In addition, we consider aperiodic jobs released in arbitrary instants.
Later, we will look at sporadic tasks as well.
We are interested in scheduling for one processor.
Scheduling for multiple processors is covered in “Combinatorial
Optimization” course.
7 / 48
What Is Clock-Driven Scheduling

Schedule table

The scheduler will schedule periodic tasks according to a static schedule,


which is computed offline and stored in a table.
Every element of the table specifies decision time and scheduled task.
If no task is to be scheduled, the special symbol × (idle) is stored
there.
We will look at a simple algorithm how to construct the schedule
later.
Note: This algorithm need not be highly efficient (it runs off-line).
Aperiodic jobs can be scheduled at idle intervals.

8 / 48
What Is Clock-Driven Scheduling

Example

System of four tasks:


τ1 τ2 τ3 τ4
Period 4 5 20 20
Execution time 1 1.8 1 2

τ1 τ3 τ2 τ1 τ4 τ2 τ1 τ2 τ1 τ1 τ2 schedule repeats

0 2 4 6 8 10 12 14 16 18 20
The schedule table will look like this:
Time 0 1 2 3.8 4 5 6 8 9.8 10.8 ... 18
Task τ1 τ3 τ2 × τ1 × τ4 τ1 × τ2 ... τ2

9 / 48
What Is Clock-Driven Scheduling

Example of clock-driven scheduler implementation – basic


version
Scheduler – basic version without aperiodic tasks
/* H is hyperperiod, created by N “slices” (not necessarily of the same length).*/
Input: Schedule stored in table (tk , τ (tk )) for k = 0, 1, . . . , N − 1
Procedure SCHEDULER:
set next scheduling point i and table index k to 0;
set timer to time tk ;
Repeat forever
wait for timer expiration (for example interrupt – instruction hlt, or polling)
current task τ := τ (tk );
i := i + 1;
calculate index of next table entry as k := i mod N;
set timer to ⌊i/N⌋ · H + tk ;
We assume that τ always fin-
call function τ ;
ishes by next timer expiration.
End
End SCHEDULER

10 / 48
What Is Clock-Driven Scheduling

Example of clock-driven scheduler


Scheduler – called from interrupt, handles aperiodic tasks
/* H is hyperperiod, created by N “slices” (not necessarily of the same length).*/
Input: Schedule stored in table (tk , τ (tk )) pro k = 0, 1, . . . , N − 1
Initialization:
set next scheduling point i and table index k to 0;
setup an interrupt handler
set timer to tk ;
In timer interrupt do:
if aperiodic job is running, preempt it;
current task τ := τ (tk );
i := i + 1;
calculate index of next table entry as k := i mod N;
set timer to ⌊i/N⌋ · H + tk ;
if current task τ is ×, then
activate a job from aperiodic job queue;
otherwise
Here we also assume task τ
activate job of task τ ;
to finish by the next interrupt.
return from interrupt

11 / 48
Frame-based scheduling

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

12 / 48
Frame-based scheduling

Frame-based scheduling
Problems
Big number of tasks ⇒ big schedule table
Embedded systems have limited size of memory
Reprogramming the timer might be slow

Idea
Divide time to constant-size frames
Combine multiple jobs into a single frame
Scheduling decisions are made only at frame boundaries

Consequences:
There is no preemption within each frame.
Each job must fit into the frame
In addition to schedule calculation, we should check for various error
conditions such as task overrun.
Let f denote the frame size. How to select f?
13 / 48
Frame-based scheduling

Frame size constraints

1 We want big enough frames to fit every job without preempting it.
This gives us
f ≥ max (Ci ). (1)
i=1,...,n

2 In order to have the table small, f should divide H. Since


H = lcm(T1 , . . . , Tn ), f divides Ti for at least one task τi :
⌊ ⌋
Ti Ti
= . (2)
f f

Let F = Hf (F is integer). Each interval H is called major cycle and


each interval f minor cycle.
Each major cycle is composed from F minor cycles.

14 / 48
Frame-based scheduling

Frame size constrains (cont.)


3 We want the frame size to be sufficiently small so that there is at least one
whole frame between task release time and deadline. The reasons for this
are:
Our scheduler can only start jobs at frame boundaries. Jobs release in
between have to wait for the boundary.
Similarly, we want the scheduler to check whether each jobs complete by its
deadline. If there is no frame boundary before the deadline, we cannot
prevent overruns. For example, if the deadline falls into frame k + 2, overrun
can be checked only at the beginning of frame k + 2.
frame k frame k + 1 frame k + 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

t t+f t + 2f t + 3f
t′ t′ + Di t′ + Ti
(release time of job τi )

Thus:
2f − gcd(Ti , f) ≤ Di . (3)
See Liu 5.3.2 for explanation.
15 / 48
Frame-based scheduling

Example

Let’s have a system from the last example with four tasks:
τ1 = (4, 1), τ2 = (5, 1.8), τ3 = (20, 1), τ4 = (20, 2).
From the first constraint (1): f ≥ 2.
Hyperperiod is 20 so second constraint (2), tells us that f can be one
of 2, 4, 5, 10 a 20.
Third constraint (3) satisfies only f = 2.
Possible cyclic schedule:

τ1 τ3 τ2 τ1 τ4 τ2 τ1 τ2 τ1 τ1 τ2 schedule repeats

0 2 4 6 8 10 12 14 16 18 20

16 / 48
Frame-based scheduling

Split jobs

What to do if frame size constrains cannot be met?


Example: Consider τ = {(4, 1), (5, 2, 7), (20, 5)}. By first constraint
(1) f ≥ 5, but by third constraint (3) f ≤ 4!
Solution: Split task (20, 5) into subtasks (20, 1), (20, 3) and (20, 1).
Then, f = 4 works. Here is a schedule:

τ1 τ2 τ31 τ1 τ32 τ1 τ2 τ1 τ2 τ1 τ2 τ33 schedule repeats

0 4 8 12 16 20

17 / 48
Frame-based scheduling

Summary of design decisions

Three design decisions:


1 choosing a frame size,
2 partitioning jobs into slices, and
3 placing slices in frames.
In general, these decisions cannot be made independently.
See next slides for an algorithm that makes these decisions for us.

18 / 48
Schedule Construction with Network Flow Algorithm

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

19 / 48
Schedule Construction with Network Flow Algorithm

Network Flow Algorithm


Algorithm for finding a static schedule

Initialization Find all possible frame sizes by constraints (2) a (3):


⌊ ⌋
Ti
f − Tf i = 0 2f − gcd(Ti , f) ≤ Di

We will ignore constraint (1)

f ≥ max (Ci )
i=1,...,n

this can force us to split tasks to into subtasks.


Iterate For each frame size f construct network flow graph and run
max-flow algorithm. If the algorithm finds a flow, we have a
schedule.

20 / 48
Schedule Construction with Network Flow Algorithm

Network-flow graph

Denote all jobs in the major cycle of F frames as J1 , J2 , . . . , JN .


Nodes:
N nodes representing jobs, denoted as J1 , J2 , . . . , JN .
F nodes representing frames, denoted as F1 , F2 , . . . , FF .
Source a sink.
Edges:
(Ji , Fj ) with capacity f, if Ji can be scheduled in frame Fj , i.e. frame Fj
satisfies release-time and deadline constraints of Ji .
∀i : (source, Ji ) with capacity Ci .
∀j : (Jj , sink) with capacity f.

21 / 48
Schedule Construction with Network Flow Algorithm

Network-flow graph example

Jobs Frames
..
.
Ji f Fx

Ci f f
Fy
f
source sink
Ck f f
Jk f Fz f
..
.
FF

22 / 48
Schedule Construction with Network Flow Algorithm

Finding the schedule


Clearly, maximal flow is at most equal to i=1,...,N Ci . This is the
duration of jobs that need to be scheduler during major cycle.

If the flow found by the maximum-flow algorithm is i=1,...,N Ci , it
corresponds to feasible schedule.
If a job is “scheduled” in multiple frames, it has to be split to
sub-jobs.

23 / 48
Schedule Construction with Network Flow Algorithm

Example graph and maximum flow


Jobs Frame
..
.
Ji h/f Fx

Ci /Ci h/f
(Ci − h)/f
Fy (C + Ck − h)/f
i
source sink
Ck /f 0/f
Ck /Ck Jk 0/f Fz 0/f
..
This flow tells us to split J1 into two subtasks.
.
One with execution time h, which is scheduled in FF
frame Fx and second with execution time (Ci −h),
which is scheduled in frame Fy . Jk stays as one
jobs as is scheduled in frame Fy .

24 / 48
Schedule Construction with Network Flow Algorithm

Example

Example: Consider τ = {(4, 1), (5, 2, 7), (20, 5)}. Constraints (2) and
(3) give us f = 2 a 4!
We select value 4 for the first max-flow algorithm iteration.
Maximum flow 18, which equals to the total execution time of all jobs
in the hyperperiod.
Flow of edges represent a schedule of the tasks:
schedule
τ1 τ2 τ31 τ1 τ32 τ1 τ2 τ1 τ2 τ1 τ2 τ33 repeats

0 4 8 12 16 20

25 / 48
Schedule Construction with Network Flow Algorithm

Example – graph
Jobs Frames

J11
1
J12 F1
(τ1 )
1
(τ1 ) J13
2
F2
(τ1 ) 1 4
J14
(τ1 ) 4
J15 1 F3
source (τ1 ) 3 sink
2
(τ2 ) 1 3
J21
(τ2 ) 4
1 2 F4
(τ2 ) J22 3

(τ2 ) 0
J23 2 F5
0
(τ3 )
J24 1

J31

26 / 48
Schedule Construction with Network Flow Algorithm

When tasks are not independent

Jobs with precedence constraints are easy to take into account.


Precedence constraints of “Ji precedes Jk ” can be easily satisfied by
setting release time ri at or before release time rj and setting deadline
di at or before dk .
If jobs Ji a Jk are scheduled in the wrong order, we can switch them in
the schedule.
Restriction of preemption (critical sections) is more ambitious.
Manual modification of the found schedule to remove preemptions in
non-preemptive regions.
Unfortunately, there is no efficient optimal algorithm for this – the
problem is NP-hard, even on one processor.

27 / 48
Cyclic Executive

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

28 / 48
Cyclic Executive

Cyclic Executive
Fancy name for a scheduler that executes the frame-based schedule.

Pseudo-code for Cyclic Executive (with interrupts)

Input: Stored schedule: L(k) for k = 0, 1, . . . , F − 1;


Initialization
actual time t:=0; actual frame k:=0;
set timer interrupt period to f
end
In timer interrupt do
current_block := L(k);
t := t + 1; k := t mod F;
if the last job is not completed (overrun), take appropriate action (error);
Setup execution of jobs in current_block
(e.g. by modifying the return address on the stack)
end

29 / 48
Cyclic Executive

Cyclic Executive (with OS)


Pseudo-code for Cyclic Executive (with OS)

Input: Stored schedule: L(k) for k = 0, 1, . . . , F − 1;


Aperiodic job queue
Task CYCLIC_EXECUTIVE (highest priority OS task)
current time t:=0; actual frame k:=0;
repeat forever
wait until t · f;
current_block := L(k);
t := t + 1; k := t mod F;
if the last job is not completed (overrun), take appropriate action (error);
release all the jobs in the current_block;
wait until the jobs complete (or use a scheduling server);
while aperiodic job queue is not empty do
wake up the job at the head of the aperiodic job queue;
wait until the jobs complete (or use a scheduling server);
remove the aperiodic job from queue;
end
end
End CYCLIC_EXECUTIVE

30 / 48
Cyclic Executive

Cyclic Executive implementation – Summary

Main program loop that calls functions (tasks) at appropriate time


Cannot prevent task overruns
Cannot preempt execution of aperiodic jobs.
In timer interrupt or with an OS
Task overruns can be detected and handled appropriately
Aperiodic jobs can be preempted

31 / 48
Improving response times of non-periodic jobs

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

32 / 48
Improving response times of non-periodic jobs → Aperiodic jobs – slack stealing

Improving response times of aperiodic jobs

Intuitively, it makes sense to give hard real-time jobs higher priority


than aperiodic jobs.
However, this may lengthen the response time of an aperiodic job.
hard aper. aper. hard
hard deadline is still met, but aperi-
odic job completes sooner

Note that there is no point in completing a hard real-time job early,


as long as it finishes by its deadline.

33 / 48
Improving response times of non-periodic jobs → Aperiodic jobs – slack stealing

Slack Stealing – Example


(CZ: Kradení rezervy)

schedule
hard real- τ1 τ2 τ1 τ3 τ1 τ4 τ2 τ1 τ1 τ3 repeats
time jobs
0 4 8 12 16 20
1.5 0.5 2
aperiodic
jobs
0 4 8 12 16 20

without slack τ1 τ2 τ1 τ3 τ1 τ4 τ2 τ1 τ1 τ3
stealing
0 4 8 12 16 20

with slack τ1 τ2 τ1 τ3 τ1 τ4 τ2 τ1 τ1 τ3
stealing
0 4 8 12 16 20
34 / 48
Improving response times of non-periodic jobs → Aperiodic jobs – slack stealing

Slack stealing
(CZ: Kradení rezervy)

Let the total amount of time allocated to all the jobs scheduled in
frame k be xk .
Definition: slack available at the beginning of frame k is f − xk .
Scheduler change :
If aperiodic job queue is not empty, run aperiodic jobs in every frame
with non-zero slack.

35 / 48
Improving response times of non-periodic jobs → Aperiodic jobs – slack stealing

Slack stealing implementation

Precomputed slack table – entries depend only on static values


To maintain the value of remaining slack use interval timer.
Start the timer when running the aperiodic job. If the timer expires, a
periodic job has to be executed.
Problem: Older operating systems do not support high-resolution
timers. In VxWorks, this can be partially solved by sysClkRateSet().

36 / 48
Improving response times of non-periodic jobs → Sporadic jobs

Scheduling sporadic jobs

Sporadic jobs arrive at arbitrary times.


They have hard deadlines.
Implies we cannot hope to schedule every sporadic job.
When a sporadic job arrives, the scheduler performs an admission test
(acceptance test) to see if the job can be completed by its deadline.
We must ensure that a new sporadic job does not cause a
previously-accepted sporadic job to miss its deadline.
We assume sporadic jobs are prioritized on an earliest-deadline-first
(EDF) basis.

37 / 48
Improving response times of non-periodic jobs → Sporadic jobs

Example

Frame size is 4, gray rectangles are periodic tasks


S1 – S4 are sporadic tasks with parameters (Di , Ci )

38 / 48
Improving response times of non-periodic jobs → Sporadic jobs

Example (continued)
S1 is released in time 3.
Must be scheduled in frames 2, 3 and 4.
Acceptance test – at the beginning of frame 2:
Slack time is 4 which is less that execution time – job is rejected.
S2 is released in time 5.
Must be scheduled in frames 3 through 7.
Acceptance test – at the beginning of frame 3:
Slack time is 5.5 – job is accepted.
First part (2 units) executes in current frame.
S3 is released in time 11.
Must be scheduled in frames 4 and 5.
S3 runs ahead of S2 .
Acceptance test – at the beginning of frame 4:
Slack time is 2 (enough for S3 and the part of S2 ) – job is accepted.
First part (1 unit) S3 executes in current frame, followed by second part of S2 .
S4 is released in time 14.
Acceptance test – at beginning of frame 5:
Slack time is 4.5 (accounted for slack committed by S2 and S3 ) – job is rejected.
Remaining portion of S3 completes in current frame, followed by the part of S2 .
Remaining potions of S2 execute in the next two frames.

39 / 48
Improving response times of non-periodic jobs → Sporadic jobs

Admission test
Let σ(i, k) be initial slack in frames i through až k, where
1 ≤ i ≤ k ≤ F. This depends only on periodic tasks and their
parameters are known in advance.
Assume the admission test is executed at the beginning of frame t for
a just arrived sporadic task S with deadline D and execution time C.
Further assume that D happens in frame l + 1, i.e. S must be finished
before end of frame l.
The current total amount of slack time σc (t, l) in frames t through l
can be computed as

σc (t, l) = σ(t, l) − Ck − ξ k
Dk ≤D

We sum over earlier accepted sporadic jobs with equal or earlier


deadline.
ξk denotes the execution time of Sk that has been completed at the
beginning of frame t.
40 / 48
Improving response times of non-periodic jobs → Sporadic jobs

Admission test (cont.)


Admission test algorithm

Input: current frame t, new sporadic job Sn = (Cn , Dn ),


precomputed slack table σ,
set of already accepted jobs {Sk |Sk = (Ck , Dk , ξk , σk )}

if σc (t, l) ≤ C then reject Sn


else
store σn := σc (t, l) − Cn as slack time of job Sn ;
foreach earlier accepted sporadic job Sk with Dk > Dn :
if σk − C < 0 then reject Sn ; return; end
end
Insert Sn = (Cn , Dn , 0, σn ) into {Sk }
end

41 / 48
Improving response times of non-periodic jobs → Sporadic jobs

Execution of sporadic jobs

Accepted sporadic jobs are executed in the same manner as aperiodic


jobs in the original algorithm without slack stealing.
Difference: Aperiodic job queue is FIFO ordered, whereas sporadic
job queue is EDF ordered.
Aperiodic jobs run only if sporadic job queue is empty.
As before, slack stealing could be used when executing aperiodic jobs
(in which case, some aperiodic jobs could execute when the sporadic
job queue is not empty).

42 / 48
Miscellaneous

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

43 / 48
Miscellaneous

Practical considerations

Handling frame overruns


Main issue: Should offending job be completed or aborted? It
depends on the application.
Mode changes
Changing the set of running tasks (schedule table).
May be implemented as aperiodic or sporadic job that makes the
change. If sporadic, it can be rejected!

44 / 48
Miscellaneous

Pseudocode for “mode changer”

Task Mode changer


Fetch the list of tasks to be deleted;
Mark each periodic task included in this list;
Inform cyclic executive about mode change start;
Fetch the newTaskList of periodic tasks to be executed in newMode;
Allocate memory for each task from newTaskList and crate each of these task;
Read new schedule;
Perform admission test for each sporadic job in the system according to the new schedule
If all sporadic jobs are accepted in the new schedule then
Inform cyclic executive to use the new schedule;
else
Calculate maximum completion time among all accepted sporadic jobs. (latestCompletionTime)
Inform cyclic executive to use the new schedule in time latestCompletionTime);
end
end Mode changed

45 / 48
Summary

Outline

1 What Is Clock-Driven Scheduling

2 Frame-based scheduling

3 Schedule Construction with Network Flow Algorithm

4 Cyclic Executive

5 Improving response times of non-periodic jobs


Aperiodic jobs – slack stealing
Sporadic jobs

6 Miscellaneous

7 Summary

46 / 48
Summary

Cyclic executives: pros and cons

Main advantage: Cyclic executives are very simple – a single table is all
what is needed, no complex run queues etc.
For example, there is no need for concurrency control and task
synchronization (e.g. semaphores). In the simplest case, there are no
tasks and processes and everything is just a function call.
Can be validated, tested and certified with very high confidence.
Certain anomalies will not occur.
For these reasons, cyclic executives are predominant approach in
many safety critical applications (e.g in airplanes, trains, etc.)

47 / 48
Summary

Pros and cons (continued)

Disadvantages of cyclic executives:


Very “fragile”: Any change, no matter how trivial, requires a new
schedule table to be computed!
Slicing one function into several smaller can be error-prone.
Release times of all jobs must be fixed, i.e. “real-world” sporadic
tasks are difficult to support.
Temporal parameters must be essentially multiples of f.
F can be very high.
All combinations of period tasks that may execute together must a
priori be analyzed.

48 / 48

S-ar putea să vă placă și