07 Clock Driven Scheduling
07 Clock Driven Scheduling
OUTLINE
Notations and assumptions Practical Considerations Static, timer-driven scheduler - Handling frame overruns Cyclic scheduling - Mode changes - Job slices - General workloads Cyclic executive algorithm - Multiprocessor scheduling Improving the average response Constructing static schedules time of aperiodic jobs - Network-flow graph - Slack stealing Scheduling sporadic jobs - Acceptance test - Optimality of the EDF algorithm
Assumptions - n periodic tasks; n fixed per mode of operation; - each job Ji,j in Ti is released pi units of time after the previous job Ji,j-1; - Ji,k is ready for execution at ri,k; - aperiodic jobs may be released at unexpected times; 2 - no sporadic jobs (for the moment).
Static scheduling
When job parameters with hard deadlines are known in advance, a static schedule (containing all scheduling decision times) can be constructed off-line Example with the following 4 tasks: - T1=(4,1); T2=(5,1.8); T3=(20,1); T4=(20,2); - 1=0, D1=4; 2=0; D2=5; 3=0, D3=20; 4=0, D4=20; Lets verify their utilization factor (ui= ei / pi ): - u1= 0.25; u2=0.36; u3=0.05; u4=0.1; U = 0.76 - the 4 tasks dont require more processor time than available; Hyperperiod is defined as the lcm of all periods = 20; - Entire schedule consists of replicated segments of period 20.
3
Static schedules
- T1=(4,1); T2=(5,1.8); T3=(20,1); T4=(20,2); Hyperperiod = 20; - Different static schedules are possible:
T1
0
T2
T3 T1
4 5
T2
T1
8 10
T2
T1
12
T4
T1
15 16
T2
20
T2
0
T1
4 5
T4
T1
8
T2
10
T1
12
T2
T1
T3
T2
T1
20
15 16
T1 T3
0
T2
4
T1
5
T4
8
T2
T1
10 12
T2
T1
T1
15 16
T2
20
Processor time not used by periodic tasks (4.8 time units in H) may be used for aperiodic jobs: - The OS maintains a queue of aperiodic jobs: the aperiodic job at the head of the queue executes when processor is idle (in the last example, during time intervals: [3.8,4], [5,6], 4 [10.8,12], [14.8, 16], [19.8, 20]).
A clock-driven scheduler
input: (tk, T(tk)) for k=0,1,N-1 Task SCHEDULER set decision point i and table entry k to 0; set the timer to expire at tk; do forever: tk accept timer interrupt; if an aperiodic job is executing, preempt it current task T=T(tk); increment i by 1; sets timer to tk+1 prepares T(tk) next table entry k = i mod(N); suspends itself set the timer to expire at i/N H+tk if the current task T is I, execute a job from the aperiodic queue else let the task T execute; sleep; TABLE contains the static schedule: tk: decision time (is not periodic) end SCHEDULER
t0, T(t0) tk, T(tk) tJ, I TABLE
A CLOCK-DRIVEN SCHEDULER
1. f max(ei) (for 1 i n) (n tasks) each job may start and complete within one frame: no job is preempted 2. pi /f - pi /f = 0 (for at least one i) to keep the cyclic schedule short, f must divide the hyperperiod H; this is true if f divides at least one pi 3. 2f gcd(pi,f) Di (for 1 i n) to have at least one whole frame between the release time and the deadline of every job (so the job can be feasibly scheduled in that frame) (see next 2 slides for a demonstration of this third constraint)
6
to have at least one complete frame between the release time t and the deadline t + Di of a job; - in the special case (t = t ): it suffices to have f Di; - in the general case (t < t < t + f ): the jobs deadline (t + Di) should occure not earlier than t + 2f ; that is: 2f - Di (t- t) (f Di /2 + (t t)/2); - if this last constraint is satisfied, then, since t- t < f, it is also f Di ; so the condition for the special case (t = t ) is also satisfied; we may then consider t > t and choose f such that: 2f - Di (t- t) (for 1 i n)
7
we consider t > t and choose f such that 2f - Di (t- t) (for 1 i n) - t, t, f, pi, Di are all integers, - the first job of every task is released at the beginning of some frame (t = t for some frame); if we set the time = 0 at the beginning of this frame, then: - the beginning of the k-th frame is at: t = kf (k integer 0) - the release time of the job in k-th frame is at: t = jpi (j integer 0) - if G=gcd(pi, f), then we can write: f = mG and pi = nG (m and n integers > 0); and also: t- t = jnG - kmG = hG (h integer 0; h > 0 if t t ) since (t- t) G, the constraint (t- t) 2f - Di is met if G 2f - Di (if 2f - G Di ) as a result, there is at least one frame between the release time and the deadline of every job, if f satisfies the following constraint: 2f - gcd(pi,f) Di (for 1 i n)
8
f = 4: -T1 : 8 4 4 (OK) -T2 : 8 1 5 (NO) f = 4 is NOK f = 5: -T1 : 10 1 4 (NO) f = 5 is NOK f = 10: -T1 : 20 2 4 (NO) f = 10 is NOK f = 20: -T1 : 40 4 4 (NO) f = 20 is NOK
This is not the only solution: jobs could be fit into frames in other ways.
3. 2f gcd(pi,f) Di (for 1 i n) f = 3: -T1 : 6 3 14 (OK) - T2 : 6 1 26 (OK) - T3 : 6 1 22 (OK) - f = 3 is OK f = 4: -T1 : 8 1 14 (OK) - T2 : 8 4 26 (OK) - T3 : 8 2 22 (OK) - f = 4 is OK f = 5: -T1: 10 5 14 (OK) - T2: 10 5 26 (OK) - T3: 10 1 22 (OK) - f = 5 is OK
f = 10: - T1: 20 5 14 (NO) f =10 is NOK f = 11: - T1: 22 1 14 (NO) f = 11 is NOK f = 15: - T1: 30 15 14 (NO) f =15 is NOK f = 20: - T1: 40 5 14 (NO) f = 20 is NOK f = 22: - T1: 44 1 14 (NO) f = 22 is NOK the 3 constraints are met if f = 3 or f = 4 or f = 5 the most convenient choice is: f = 5
10
- T1 uses 1 time unit in each one of the 5 frames; - T2 uses 2 time units in 4 of the 5 frames; - this leaves 3 time units available in 1 frame and just 1 in the other 4.
12
13
14
Periodic-task server
Assumption: job slices in each scheduling block L(k) correspond to precisely defined code segments, such as procedures (job slicing has been performed by reviewing jobs design in order to meet frame size constraints); then the periodic-task server executes all the job slices in L(k) by calling them one after the other (no context switches take place).
Task PERIODIC-TASK SERVER: for each job-slice in L(k) do execute job-slice; enddo; end PERIODIC-TASK SERVER;
in a system where periodic job slices never overrun (no frame overrun) the periodic-task server is not needed: job slices may be executed directly by the cyclic executive.
15
Assume that the total amount of time allocated to all slices scheduled in frame k is: xk The slack time available in the frame is: initial_slack = f - xk After y units of slack are used by aperiodic jobs => slack = f - xk - y The cyclic executive can let aperiodic jobs execute as long as there is slack in the frame (as long as f - xk - y > 0).
16
19
d3
d2
d4
what if S2 was scheduled to use up the slack available in the time intervals (10,12), (15,16), and (19,20)? - S2 would complete earlier (at t =20), - but S3 would have been rejected! problem: early commit.
20
T3
...
TN
sporadic jobs aperiodic jobs
acceptance test reject EDF priority queue Aperiodic job queue
processor
22
d3
d2
d4
EDF algorithm is a good way to schedule accepted sporadic jobs: acceptance tests are done at the beginning of each frame (frame size f = 4 in the example); at the beginning of the frame, the scheduler inserts accepted sporadic jobs into a queue in non-decreasing order of their deadlines; whenever all the slices of periodic tasks scheduled in each frame are completed, the cyclic executive lets the sporadic jobs execute in EDF order.
23
Acceptance test
The acceptance test at the beginning of frame t for a sporadic job S(d,e) (with deadline d and execution time e) is performed in 2 steps: 1. compute (t,l ): the total slack time available in frames t to l before d; if (t,l ) < e, then S is rejected; 2. else if the acceptance of S will not cause any sporadic job already accepted to complete late, then S is accepted; otherwise S is rejected Sk(dk,ek) {S0,S1,...,Sns} - sporadic jobs already accepted for the first step of the acceptance test, the scheduler needs: - (i,h) - total (initial) amount of slack time in frames i to h, for i,h = 1, .., F; all values of these slack times (i,h), leftover by periodic jobs from any frame i to any frame h (i h) in the first (and in any) major cycle, can be pre-computed and stored in a slack table with (F + 1)*F/2 elements; - from (i,h) in the first major cycle, initial slack from frame i in any major cycle j to frame h in any major cycle j (> j) is computed when required: ((j 1)F + i, (j 1)F + h) = (i,F) + (1,h) + (j j 1)(1,F) (see example in next slide) 24
Initial slack from i-th frame of j-th major cycle to h-th frame of j-th major cycle: ((j - 1)F + i, (j - 1)F + h) = (i,F) + (1,h) + (j - j - 1)(1,F)
To compute (5,11): - frame 5 is in major cycle j = 1 (i = 5); - frame 11 is in major cycle j = 3 (h = 1): (5,11) = (5,5) + (1,1) + (3-1-1)*(1,5) = 1 + 0.5 +1*5.5 = 7
25
26
d1 d3 d2
CYCLIC EDF ALGORITHM Acceptance tests are performed at the beginning of frames:
d4 Time 4: - test of S1(17,4.5) - (2,4) = 4 < 4.5 => reject S1 Time 8: - test of S2(29,4) - c(3,7) = (3,5) + (1,2) = 4+1.5 = 5.5 > 4 => accepts S2 => 2 = 1.5 Time 12: test of S3(22,1.5) - d2 = 29 > d3 = 22 => c(4,5) = (4,5) 0 = 2 > 1.5 => first step OK - second step: 2 = 1.5 1.5 0 => accept S3 => 3 = 0.5; 2 = 0 Time 16: test of S4(44, 5.0) - d3 < d2 < d4; 2 = 2; 3 = 1; e2 - 2 = 4 2 = 2; e3 3 = 1.5 1 = 0.5; - c(5,11) =7- 2 - 0.5 = 4.5 < 5.0 => reject S4
28
30
Mode changes
During the mode change the system is reconfigured: - new periodic tasks are created; - some old periodic tasks stay, some are deleted; - pre-computed schedule table for the new mode is brought into memory; - the code of new tasks is brought into memory; - memory space for data is allocated; - execution can proceed. The work to reconfigure the system is a mode-change job; its deadline can be: - soft : aperiodic mode change job; - hard: sporadic mode change job.
31
32
Mode changes
Mode changer
task MODE_CHANGER (oldMode, newMode) fetch the deleteList of periodic tasks to be deleted; mark each periodic task in the deleteList; inform the cyclic executive that a mode change has commenced; fetch the newTaskList of periodic tasks to be executed in NewMode; allocate memory space for each task in newTaskList and create the tasks; fetch the newSchedule; perform acceptance test on each sporadic job in the system according to the newSchedule if every sporadic job in system can complete on time based on the newSchedule inform the cyclic executive to use the new Schedule; else, compute the latestCompletionTime of all sporadic jobs in system; inform the cyclic executive to use the newSchedule at max (latestCompletionTime, thresholdTime); end Mode_Changer
35
General workloads
Clock-driven approach is applicable also to other types of workload: - jobs not executed on a CPU (e. g. bus arbitrator), - jobs not characterized by the periodic task model. Whenever jobs parameters are known a priori a static schedule can be computed off-line (taking into account also other types of constraints, besides release times and deadlines, such as precedence and resource contention); The static schedule can be stored as a table and used by the same static scheduler used for periodic tasks.
36
Multiprocessor scheduling
Construct (offline) a global schedule which specifies on which processor and when each job executes; - global clock required. Sometimes a precomputed multiprocessor schedule can be derived from a precomputed uniprocessor schedule: - in the figures the system bus is the bottleneck: a feasible schedule for data transfer activities on the bus gives a feasible schedule of related jobs in the corresponding processors; - if clock drifts on processors are small, uniprocessor schedulers may be used In general searching for feasible multiproccesor schedule is more complex than in the uniprocessor case. 37
INF algorithm
The INF algorithm is performed in 2 steps: - step 1: find all the possible frame sizes of the system that meet the frame size constraints 2 and 3 (slide 6) but not necessarily constraint 1; - step 2: apply INF algorithm starting with the largest possible frame. Example (slide 11): - T1 = (4,1); T2 = (5,2,7) and T3 = (20,5) - frame sizes 2 and 4 meet constraints 2 and 3, but not 1. The INF algorithm iteratively tries to find a feasible cyclic schedule of the system for a possible frame size at a time, starting with the largest value.
39
Network-flow graph
The algorithm used in each iteration is based on the network-flow formulation of the preemptive scheduling problem. In this formulation: - we ignore the tasks to which the jobs belong; - we name the jobs in a major cycle of F frames as J1, J2, ..., JN; - the constraints are represented by graph. The graph contains the following vertices and edges: - Job vertex Ji, I = 1, ..., N - Frame vertex j, j = 1, ..., F - 2 additional vertices: source and sink. - Edges from job v. to frame v. (Ji,j ) if job Ji can be scheduled in frame j edge capacity = f (amount of time available in frame j ). - Edges from source to every job vertex Ji edge capacity = ei (amount of time required by job Ji ). - Edges from every frame vertex to the sink edge capacity = f (amount of time available in a frame). Edges are labeled with 2 numbers {(c),f }; c = capacity, f = flow.
40
41
04 48
F1 (0 4) F2 (4 8)
8 12 F3 (8 12)
Place jobs in frames following this rule (table above); then draw the network flow graph and apply INF algorithm (next slide).
4-8
8-12
(4),1
12-16
16-20
- T1 =(4,1); T2 =(5,2,7); T3 =(20,5) edge (Ji,j,k) from job vertex Ji,j to frame vertex k is drawn if job Ji,j is scheduleable in frame k; its flow gives the amount of time in frame k allocated to job Ji,j . INF algorithm: step 1: possible frame sizes 4, 2 step 2: try first with f = 4 (figure) - maximum flow is 18 = ei feasible schedule The flows of the feasible schedule indicate that T3 is to be partitioned in 3 slices and give their size. The time diagram with the job-slice schedule in each frame may now be drawn (the same as in slide 12).
43
job
feasible interval
schedulable in frame(s)
04 48 06
F1 (0 4) F2 (4 8) F1 (0 4)
8 12 F3 (8 12) 6 12 F3 (8 12)
F1
(4),4
J12
(3),3
J13
(3),3
(4),3
F2
(4),3
source
(1.5),1.5
J21
sink
(4),3 (1.5),1.5
J22
(4),1
F3
(4),4
04 48 06
How to slice jobs? additional constraint: - every job of the same task should be sliced in the same way: the cyclic executive (or the periodic-task server) must execute (call) every time the same sequence of code segments; - this suggest to slice each J1,i in 2 sub-jobs of equal execution time 1.5; - jobs of T1 leave 0.5 time units free in each frame: jobs of T2 (e2 = 1.5) must be sliced in 3 sub-jobs, scheduled in 3 frames; 46 - draw network flow graph and find maximum flow (next slide).
F1
(2),2
F2
(2),2
J12
(3),3
(2),0.5
F3
(2),2
J13
(2),1.5 (2),0.5
source
F4
(2),2
sink
J21
(1.5),1.5
F5
(2),2
(1.5),1.5
J22
(2),0.5
F6
(2),2
maximum flow is: 12 = ei feasible schedule jobs of T1 are sliced into 2 sub-jobs: J1,iA, J1,iB jobs of T2 are sliced into 3 sub-jobs: J2,iA, J2,iB , J2,iC the time diagram with the job-slice schedule in each frame may now be drawn (next slide).
47
F1
(2),2
F2
(2),2
J12
(3),3
(2),0.5
F3
(2),2
J13
(2),1.5 (2),0.5
source
F4
(2),2
sink
J21
(1.5),1.5
F5
(2),2
(1.5),1.5
J22
d1,1
(2),0.5
F6
d1,2
(2),2
d2,2
F1
J1,1A J2,1A
F2
J1,1B J2,1B
F3
J1,2A J2,1C
d2,1
F4
J1,2B J2,2A
F5
J1,3A J2,2B
F6
J1,3B J2,2B
d1,3
10
11
12 48
50