31
CSE 522 Real-Time Scheduling (1) Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee [email protected] (480) 727-7507 1

CSE 522 Real-Time Scheduling (1)

  • Upload
    chace

  • View
    37

  • Download
    0

Embed Size (px)

DESCRIPTION

CSE 522 Real-Time Scheduling (1) . Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann -Hang Lee [email protected] (480) 727-7507. Event and Time-Driven Threads. Create a task with (name, priority, options, stacksize , main, …). initialization. - PowerPoint PPT Presentation

Citation preview

Page 1: CSE  522  Real-Time Scheduling (1)

1

CSE 522 Real-Time Scheduling (1)

Computer Science & Engineering DepartmentArizona State University

Tempe, AZ 85287

Dr. Yann-Hang [email protected](480) 727-7507

Page 2: CSE  522  Real-Time Scheduling (1)

2

Event and Time-Driven Threads

Create a task with (name, priority, options, stacksize, main, …)

initialization

external trigger?

Take actions andchange system

state

ISR: to set/clear events

start_time=time( )

Task initialization

computation

Sleep(period - ( time( ) -start_time) )

Page 3: CSE  522  Real-Time Scheduling (1)

3

Multiple Events in One Threadvoid compute(){

if (event1) then action1;if (event2) then action2;if (event3) then action3;

.}

or{

for (i=0, i<n, i++)if event[i] then action[i];

}

void compute(){

if (event1) then action1;else if (event2) then action2;else if (event3) then action3;

.}

or{

for (i=0, i<n, i++) {if event[i] then { action[i]; break; } }

}

Page 4: CSE  522  Real-Time Scheduling (1)

4

Real-time System Specification

Logical correctness requirements: The computation produces correct outputs. Models of computation to describe inputs and computations Additional requirements on resource, security, reliability, etc. Finite state machine

good for control logic and protocols, transition and activity

Data flow – modular computations that are triggered by the availability of input data.

Temporal correctness requirements: The computation produces outputs at the right time When the computation can get started and should be

completed

Page 5: CSE  522  Real-Time Scheduling (1)

Specification Patterns

Category Pattern Example

Duration(stimuli and responses)

minimum duration

The system has a minimum 'off' period of 120seconds before it reenters the cranking mode.

maximum duration

The system can only operate in engine cranking mode for no longer than 10 seconds at one time

Periodic bounded recurrence

The ABS controller checks for wheel skidding every 10 milliseconds.")

Real-time order

bounded response

The detection of and response to rapid deceleration must occur within 0.015 seconds.

bounded invariance

If Error 502 is received, then the braking systemis inhibited for 10 seconds.

(S. Konrad and B.H.C. Cheng “Real-time specification patterns”, ICSE 2005)

5

Page 6: CSE  522  Real-Time Scheduling (1)

6

RT Specification in FSM

Duration of staying in a state Periodic activity in a state Bounded response for each

transition Accumulated delay between

multiple transitions Hierarchical FSM

a state encloses a FSM enter a state activate a FSM

Concurrent FSM FSMs run in parallel (active

simultaneously)

up

down

idle open

Page 7: CSE  522  Real-Time Scheduling (1)

7

RT Embedded Systems: Terminology Some problem terms:

job, task, process, activity, action, procedure, event, time, deadline, latency, slack time, execution time, aperiodic, sporadic, jitter, priority

Job: unit of work that is scheduled and executed in the system control law computation, send a packet, read sensor data

Task: set of jobs Processor: CPU + Bus + I/O Time (instant) and duration (interval) Release time, completion time, deadline (absolute or

relative)

Page 8: CSE  522  Real-Time Scheduling (1)

8

Terminology

Hard deadline: late result is little or no value, or may lead to disaster need to validate (can you guarantee it?)

Soft deadline: late result may still be useful probability of missing deadlines 95% of telephone switch connects in 10 seconds

How serious is serious ? Tardiness:

min{ 0, deadline - completion time} Usefulness:

function of tardinesscompletion time

value

Page 9: CSE  522  Real-Time Scheduling (1)

9

Terminology: Temporal Parameters

Release time: fixed ( r ), jitter [ r-, r+ ], sporadic or aperiodic

Execution time: uncertainty from memory refresh, contention due to DMA,

cache misses, interrupts, OS overhead execution path variations

WCET: a “deterministic” parameter for the worst-case execution time a conservative measure an assumption to make scheduling and validation easier how can you measure the WCET of a job?

Page 10: CSE  522  Real-Time Scheduling (1)

10

Task Model

Periodic task Ti: (examples ??) constant (or bounded) period, pi: inter-release time between

two consecutive jobs phase i, utilization i = ei / pi, deadline (relative) Di

Aperiodic and sporadic: (examples ??) uncertain interarrival times but with a minimum separation aperiodic: with a soft or no deadline sporadic: with a hard deadline

i i+pi i+3pii+2pi

ei

Di

Page 11: CSE  522  Real-Time Scheduling (1)

11

waiting

executing

ready

blocked suspendeddispatched

wake-up

Task Functional Parameters Preemptivity: suspend the executing job and switch to the

other one should a job (or a portion of job) be preemptable context switch: save the current process status (PC, registers, etc.)

and initiate a ready job transmit a UDP package, write a block of data to disk, a busy waiting

loop Preemptivity of resources: concurrent use of resources or

critical section lock, semaphore, disable interrupts

How can a context switch be triggered? Assume you want to preempt an

executing job -- why a higher priority job arrives run out the time quantum

Page 12: CSE  522  Real-Time Scheduling (1)

12

Task Scheduling Schedule: to determine which job is assigned to a

processor at any time valid schedule: satisfies constraints (release time, WCET,

precedence, etc.) feasible schedule: meet job deadlines

Need an algorithm to generate a schedule optimal scheduling algorithm: always find a feasible schedule

if and only if a feasible schedule exists Scheduler or dispatcher: the mechanism to

implement a schedule

Page 13: CSE  522  Real-Time Scheduling (1)

13

Clock-driven a schedule determines (off-line) which job to be executed at each

instant static or cyclic predicable and deterministic scheduler: invoked by a timer multiple tables for different operation modes

Task Scheduling

p1= 6, e1= 3, d1= 6p2= 8, e2= 3, d2= 8 Major cycle = lcm (6,8) = 24

Page 14: CSE  522  Real-Time Scheduling (1)

14

Weighted Round-robin interleave job executions allocate a time slice to each job in the FIFO queue time slice may vary while sharing the processor good for pipelined jobs

Task Scheduling

p1= 6, e1= 3, d1= 6p2= 8, e2= 3, d2= 8 Major cycle = lcm (6,8) = 24

Page 15: CSE  522  Real-Time Scheduling (1)

15

Priority-driven The highest-priority job gets to run until completion or blocked processor never idle if jobs are waiting (work conserving) preemptive or nonpreemptive priority assignment can be static or dynamic scheduler just look at the priority queue for waiting jobs (list schedule)

Task Scheduling

p1= 6, e1= 3, d1= 6p2= 8, e2= 3, d2= 8 Major cycle = lcm (6,8) = 24

Page 16: CSE  522  Real-Time Scheduling (1)

16

dk di

JkJi

rk ( rk )

dk di

JkJk Ji

(non-EDF)

(EDF)

Earliest-deadline First (EDF) Schedule Priority preemptive scheduling

a job with earliest (absolute) deadline has the highest priority does not require the knowledge of execution time

Optimal if single processor, no resource contention, preemption why it is optimal: assume a feasible schedule

Page 17: CSE  522  Real-Time Scheduling (1)

17

Least Slack Time (LST) Schedule

Priority preemptive scheduling based on slack (laxity) time ( di - ei

* ) schedule instants: when jobs are released or completed. optimal for preemptive single processor schedule

J1

J2

J3

Slacktime

LST

Page 18: CSE  522  Real-Time Scheduling (1)

18

Non-preemptive or multiple processors scheduling anomaly --- the schedule fails after we reduce job

execution times

Non-optimality of EDF

T1

T2

T3

D1 D2 D3

Missed deadline

idle

( all jobs meet their deadline under EDF after increasing e1 )

Page 19: CSE  522  Real-Time Scheduling (1)

19

Predicable System With variant job execution times, do we know when a task is

started or completed? If the start time and completion time are predicable, then we

can determine whether a schedule is feasible or not Two extreme condition:

maximal schedule: all jobs take their maximal execution times minimal schedule: all jobs take their minimal execution times

A job is predicable iff its start time and complete time are predicable: s- ( Ji ) s ( Ji ) s+ ( Ji ) f - ( Ji ) f ( Ji ) f+ ( Ji )

The execution of every job in a set of independent, preemptive jobs with fixed release time is predictable when scheduled in a priority-driven manner on one processor (proved by induction)

Page 20: CSE  522  Real-Time Scheduling (1)

20

On-line vs. Off-line Scheduling Off-line scheduling: the schedule is computed off-line and is

based on the knowledge of the release times and execution times of all jobs. For deterministic systems: with fixed set of functions and job

characteristics does not vary or vary only slightly. On-line scheduling: a scheduler makes each scheduling

decision without knowledge about the jobs that will be released in the future. there is no optimal on-line schedule if jobs are non-preemptive when a job is released, the system can serve it or wait for the future jobs

r1 r2 D2D1

r1 r3 D1 and D3

( should wait for J2 )

( should begin J1 )J1 J3

J2 J1

Page 21: CSE  522  Real-Time Scheduling (1)

21

Clock-Driven Scheduling Assumption:

system must be deterministic (with few aperiodic or sporadic jobs) periodic tasks ( I , pi, ei, Di )

Cyclic schedule: ( tk, T(tk) ) -- at instance tk, run task set T(tk) or idle

Scheduler: Need a major cycle A table with entries for all tk in major cycle Timer interrupts at tk.

An example: T1 =(4,1), T2 = (5,1.8), T3 = (20,1), T4 = (20,2) hyperperiod (major cycle) = 20

T1 T3 T2 T1 T4 T2 T1 T2 T1 T1 T2

Page 22: CSE  522  Real-Time Scheduling (1)

22

0 10 20 30 40 50 60

Major and Minor Cycle Model Time is divided into equal-sized frame

minor cycle = length of frame Major cycle = length of schedule = k * minor_cycle

An example: A=(10,4) B=(20,6) C=(30,5) major cycle=60, minor cycle=10 scheduling string AB_AC_AB_AC_AB_A_

Jobs must be done within a minor cycle limit timing error to one frame suspend and resume as background, continue, or abort if overrun

Page 23: CSE  522  Real-Time Scheduling (1)

23

Determination of Minor Cycle (frame)

Major cycle: H = LCM ( pi ) Frame: f devides H Tasks can be done within one minor cycle --- f ei

There is at least one minor cycle between release time and deadline assume a frame starts at t task arrives at t’ t with a deadline D to have time for execution in the second frame, we need t + 2f t’ + D since t’ - t gcd ( f, pi ), we have a sufficient condition 2f - gcd ( f, pi )

Di

t t+f t+2ft’ t’+D

Page 24: CSE  522  Real-Time Scheduling (1)

24

Examples of Major/Minor Cyclic Scheduling

Three tasks (15, 1, 14) (20, 2, 26) and (22,3) H = 660 , f 3, and f = 3, 4, 5, 10, or 11 f can not too big since 2f - gcd(….) 14 f can be 3, 4, and 5

Three tasks (4, 1) (5, 2, 7) and (20, 5) H=20, f 5, but f 4 job slices: divide the 3rd task to (20, 1) (20, 3) and (20, 1) f can be 4

T1 T2 T3,,1 T1 T3,2 T1 T2 T3,3 T1 T2 T1 T2

Page 25: CSE  522  Real-Time Scheduling (1)

25

Examples of Major/Minor Cyclic Scheduling

Two tasks (100, 20) and (75, 15) choose f=25

run 1st task in high frequency (period =75)

run 2nd task in higher frequency (period =50)

harmonic set of periods

Page 26: CSE  522  Real-Time Scheduling (1)

26

An Example A1 must be done at least every 10ms, and takes 1ms A2 must be completed with 5ms when E occurs and

takes 2 ms E must be detected by polling and is detectable for at

least 0.5 ms

E would not occur twice within 50 ms polling of E takes 0 overhead

0.5ms

Page 27: CSE  522  Real-Time Scheduling (1)

27

Major/Minor Cyclic Scheduling There should be a periodic polling action for E Assume a timer of 0.5ms to activate polling operation and no polling

overhead Should be an interval of 2ms to execute A2 for an arbitrary 5ms interval

May detect E in the first frame and execute A2 in the second frame period=2.5ms

A2 takes 2ms if E, otherwise is 0 WCET=2ms Should be an interval of 1ms to execute A1 for an arbitrary 10ms

interval Period= 10ms, WCET= 1ms Since 2ms + 1ms > 2.5ms, we will divide A1 into two parts of 0.5ms

A2 A1_1 A2 A1_2 A2 A2 A2 A1_1 A2 A1_2 A2 A2

Page 28: CSE  522  Real-Time Scheduling (1)

28

Algorithm to Find Major/Minor Cyclic Schedule

Assume tasks can be sliced (or preemptable) choose f that divides H and 2f - gcd ( f, pi ) Di

For each f, construct a network flow diagram a source and a sink a vertex for each job Ji,j(task instance) and an edge from the

source with a capacity ei

a vertex for each frame fk and an edge from each frame to the sink with a capacity f

if Ji,j can be scheduled in fk, add an edge between the vertices with a capacity f

Find the maximal flow from the source to the sink feasible schedule if the maximal flow equals to the total

execution time

Page 29: CSE  522  Real-Time Scheduling (1)

29

source sink

J1,1

f1

Example Three tasks (4, 1) (5, 2, 7) and (20, 5)

H=20, f = 2 or 4

Page 30: CSE  522  Real-Time Scheduling (1)

30

Aperiodic Tasks A periodic server follows the cyclic schedule A aperiodic server looks at the aperiodic task queue

runs at the background Slack stealing

slack time: how much each periodic task can be delayed Assume all tasks must be completed before the end of their

frames and aperiodic tasks are not preemptable at frame k, ek is allocated to periodic tasks slack time: s= f - ek at the beginning of frame k, find a aperiodic task j with an execution time

ej that is less than s try to run the other aperiodic task with a slack time: s=s - ej

Do slack stealing at the beginning of each frame and then examine the queue when idle

Page 31: CSE  522  Real-Time Scheduling (1)

31

Summary of Cyclic Schedule Pros

simple, table-driven, easy to validate (knows what is doing at any moment)

fit well for harmonic periods and small system variations static schedule deterministic, static resource allocation, no preemption small jitter no scheduling anomalies

Cons difficult to change (need to re-schedule all tasks) fixed released times for the set of tasks difficult to deal with different temporal dependencies schedule algorithm may get complex (NP-hard) doesn’t support aperiodic and sporadic tasks efficiently