CIS 4930/6930: Principles of Cyber-Physical Systems Chapter 11 - - PowerPoint PPT Presentation

cis 4930 6930 principles of cyber physical systems
SMART_READER_LITE
LIVE PREVIEW

CIS 4930/6930: Principles of Cyber-Physical Systems Chapter 11 - - PowerPoint PPT Presentation

CIS 4930/6930: Principles of Cyber-Physical Systems Chapter 11 Scheduling Hao Zheng Department of Computer Science and Engineering University of South Florida H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 1 / 44 Functions of an


slide-1
SLIDE 1

CIS 4930/6930: Principles of Cyber-Physical Systems

Chapter 11 Scheduling Hao Zheng

Department of Computer Science and Engineering University of South Florida

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 1 / 44

slide-2
SLIDE 2

Functions of an Operating System (or Microkernel)

  • Memory management
  • File system
  • Networking
  • Security
  • Input and output (interrupt handling)
  • Synchronization (semaphores and locks)
  • Scheduling of threads or processes
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 2 / 44

slide-3
SLIDE 3

Functions of an Operating System (or Microkernel)

  • Memory management
  • File system
  • Networking
  • Security
  • Input and output (interrupt handling)
  • Synchronization (semaphores and locks)
  • Scheduling of threads or processes
  • Creation and termination of threads
  • Timing of thread activations
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 2 / 44

slide-4
SLIDE 4

Real-time Systems

  • A real-time system includes timing constraints such as:
  • Physical-time deadlines by which a task must be completed.
  • Requirements that a task must occur no earlier than a particular

time.

  • A task must occur a set time after another task.
  • A task must occur periodically at some specified period.
  • Tasks may be dependent on one another or simply share a

processor.

  • All of these cases require a careful scheduling strategy. k6
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 3 / 44

slide-5
SLIDE 5

11.1 Basics of Scheduling: 11.1.1 Scheduling Decisions

  • A scheduling decision has three parts:
  • Assignment: which processor should execute the task.
  • Ordering: in what order each processor should execute its tasks.
  • Timing: the time at which each task executes.
  • Decisions may be at design time or run time.
  • A fully-static scheduler makes all decisions at design time (no

locks).

  • A static order scheduler defers timing to run time (may block
  • n lock).
  • A static assignment scheduler defers ordering and timing to

run time.

  • A fully-dynamic scheduler makes all decisions at run time.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 4 / 44

slide-6
SLIDE 6

11.1 Basics of Scheduling: 11.1.1 Scheduling Decisions

  • A non-preemptive scheduler dispatches when current thread

completes.

  • A preemptive scheduler may make a scheduling decision

during execution of a task

  • Upon a timer interrupt at a jiffy interval.
  • Upon an I/O interrupt.
  • When it attempts to acquire an unavailable lock, and resumed

when another task releases the lock.

  • When it releases a lock, if a higher priority thread requires the

lock.

  • When the current thread makes any OS call.
  • File system access
  • Network access
  • ...
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 5 / 44

slide-7
SLIDE 7

11.1.2 Task Models

  • The set of assumptions is called the task model of the

scheduler.

  • Assume a finite number of tasks that may or may not terminate.
  • Real-time systems often assume that tasks terminate.
  • Some schedulers can assume that
  • All tasks are known before scheduling begins, or
  • New tasks can arrive dynamically.
  • Some schedulers support scenarios where each task τ ∈ T

executes repeatedly, possibly forever, and possibly periodically.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 6 / 44

slide-8
SLIDE 8

11.1.2 Task Models

  • Distinction between a task and its executions:
  • When task τ ∈ T executes repeatedly, the task executions are

τ1, τ2, . . ..

  • A sporadic task repeats with irregular timing, but has a lower

bound on the time between executions.

  • If execution i must precede j, we write i < j (precedence

constraint).

  • A task may require preconditions to be satisfied before it is

enabled.

  • Availability of a lock may be a precondition for resumption of a

task.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 7 / 44

slide-9
SLIDE 9

Task Execution Times

  • i
  • ei

ri si fi di i

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 8 / 44

slide-10
SLIDE 10

Deadlines and Priority

  • Hard real-time scheduling has hard deadlines which are real

physical constraints that are an error when missed.

  • Soft real-time scheduling has desired deadlines which are not

errors when missed.

  • A priority-based scheduler assumes each task is assigned a

number (priority) and chooses the enabled task with the highest priority.

  • A fixed priority remains constant while a dynamic priority can

change.

  • A non-preemptive priority-based scheduler only uses the

priorities to choose the next task, but never interrupts a task that is executing.

  • A preemptive priority-based scheduler can change to a

higher priority task at any time.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 9 / 44

slide-11
SLIDE 11

11.1.3 Comparing Schedulers

  • A schedule is feasible if it meets all deadlines (fi ≤ di).
  • A scheduler that produces feasible schedules whenever possible

is optimal with respect to feasibility.

  • Schedulers also judged based on utilization (the percentage of

time the processor is executing tasks).

  • Optimal w.r.t feasibility schedulers deliver feasible schedules

whenever the utilization is ≤ 100%.

  • Maximum lateness is another criterion:

Lmax = max

i∈T (fi − di)

  • Total completion time is also important:

M = max

i∈T fi − min i∈T ri

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 10 / 44

slide-12
SLIDE 12

11.1.4 Implementation of a Scheduler

  • Thread data structure:
  • Copy of all state (machine registers).
  • Address at which to resume executing the thread.
  • Status of the thread (e.g. blocked on mutex).
  • Priority, worst case execution time, and other info to assist the

scheduler.

  • Operating System:
  • Set up periodic timer interrupts.
  • Create default thread data structures.
  • Dispatch a thread.
  • Timer interrupt service routine:
  • Setup next timer interrupt.
  • Dispatch a thread.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 11 / 44

slide-13
SLIDE 13

Dispatching a Thread

1 Disable interrupts. 2 Save state (registers) including the return address on the stack. 3 Save the stack pointer into the current thread data structure. 4 Determine which thread should execute (scheduling). 5 If the same one, enable interrupts and return. 6 Restore the stack pointer for the new thread. 7 Copy thread state into machine registers. 8 Replace program counter on the stack for the new thread. 9 Enable interrupts. 10 Return.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 12 / 44

slide-14
SLIDE 14

11.2 Rate Monotonic (RM) Scheduling

  • Assume n tasks (i.e., T = {τ1, τ2, . . . τn}) invoked periodically

where each task must complete in each period, pi.

  • Rate monotonic schedule gives higher priority to a task with

smaller period, and it is optimal with respect to feasibility.

  • Note: important assumption is that context switch time is

negligible. Liu and Leland, “Scheduling algorithms for multiprogramming in a hard-real-time environment,” J. ACM, 20(1), 1973.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 13 / 44

slide-15
SLIDE 15

Example: Two Periodic Tasks

e2 p2

e1

p1 τ1,1 τ1,2 τ2,2 τ2,1 τ1,7 τ1,6 τ1,5 τ1,4 τ1,3 τ1 τ2

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 14 / 44

slide-16
SLIDE 16

Example: Two Periodic Tasks

e2 p2

e1

p1 τ1,1 τ1,2 τ2,2 τ2,1 τ1,7 τ1,6 τ1,5 τ1,4 τ1,3 τ1 τ2 Is a non-preemptive schedule feasible?

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 14 / 44

slide-17
SLIDE 17

Example: Two Periodic Tasks

e2 p2

e1

p1 τ1,1 τ1,2 τ2,2 τ2,1 τ1,7 τ1,6 τ1,5 τ1,4 τ1,3 τ1 τ2 Is a non-preemptive schedule feasible? No!

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 14 / 44

slide-18
SLIDE 18

Example: Two Periodic Tasks

e2 p2

e1

p1 τ1,1 τ1,2 τ2,2 τ2,1 τ1,7 τ1,6 τ1,5 τ1,4 τ1,3 τ1 τ2 How about a preemptive schedule with higher priority for red task?

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 14 / 44

slide-19
SLIDE 19

Example: Two Periodic Tasks

e2 p2 p1

  • +
  • τ1

τ2

How about a preemptive schedule with higher priority for red task? Yes!

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 14 / 44

slide-20
SLIDE 20

Worst Case Response Time

τ1 τ2 τ1 τ2 τ1 τ2 τ1 τ2

  • 2
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 15 / 44

slide-21
SLIDE 21

Non-RM Schedule Feasible

e2 p2

e1

p1 τ1 τ2 Condition for feasibility: e1 + e2 ≤ p1

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 16 / 44

slide-22
SLIDE 22

RM Schedule Also Feasible

e2 p2

e1

p1 τ1 τ2 e1 + e2 ≤ p1 − → feasible schedule

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 17 / 44

slide-23
SLIDE 23

Comments

  • Proof can be extended to an arbitrary number of tasks.
  • Proof only gives optimality w.r.t. feasibility, not other optimality

criteria.

  • Practical implementation:
  • Timer interrupt at greatest common divisor of the periods.
  • Multiple timers.
  • Note RM schedulers do not always achieve 100 percent

utilization.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 18 / 44

slide-24
SLIDE 24

11.3 Earliest Deadline First

Jackson’s earliest due date (EDD) Algorithm (1955)

  • Given n independent one-time tasks with deadlines d1, . . . , dn,

schedule them to minimize the maximum lateness, defined as Lmax = max

1≤i≤n{fi − di}

where fi is the finishing time of task i. Note that this is negative iff all deadlines are met.

  • Earliest Due Date (EDD) algorithm: Execute them in order of

non-decreasing deadlines.

  • Sort tasks such that d1 ≤ d2 ≤ . . . ≤ dn.
  • Then, executes τ1, τ2, . . . , τn.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 19 / 44

slide-25
SLIDE 25

11.3 Earliest Deadline First: EDD

  • Note that this does not require preemption.
  • Minimizes the maximum lateness as compared to all other

possible orderings.

  • Does not support arrival of tasks, thus no periodic or repeating

tasks.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 20 / 44

slide-26
SLIDE 26

11.3 Earliest Deadline First

  • Horn’s earliest deadline first (EDF) Algorithm (1974) extends

EDD to allow tasks to arrive at any time. Given a set of n independent tasks with arbitrary ar- rival times, any algorithm that at any instant executes the task with the earliest absolute deadline among all arrived tasks is optimal w.r.t. minimizing the maxi- mum lateness.

  • EDF requires the scheduler to always execute the task with the

earliest deadline among all arrived tasks.

  • EDF has a dynamic priority making it more difficult to

implement.

  • A repeated task may be assigned with different priority at

different time.

  • EDF can be applied to periodic tasks as well as aperiodic tasks

by making deadline be the end of the period.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 21 / 44

slide-27
SLIDE 27

Comparison of EDF and RMS

  • Favoring RMS:
  • Scheduling decisions are simpler (fixed vs. dynamic priorities

required by EDF).

  • EDF scheduler must maintain a list of ready tasks that is sorted

by priority.

  • Favoring EDF:
  • Since EDF is optimal w.r.t. maximum lateness, it is also optimal

w.r.t. feasibility while RMS is only optimal w.r.t. feasibility.

  • EDF can achieve full utilization where RMS fails to do that.
  • EDF results in fewer preemptions in practice, and hence less
  • verhead for context switching.
  • Deadlines can be different from the period.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 22 / 44

slide-28
SLIDE 28

11.3.1 EDF with Precedences

1 d1= 2 d2= 5 d3= 4 d6= 6 d5= 5 d4= 3 6 4 2 3 2 4 5 6 EDF 1 2 4 3 5 6 LDF 1 2 4 3 5 6 EDF*

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 23 / 44

slide-29
SLIDE 29

Latest Deadline First (Lawler, 1973)

  • Latest deadline first (LDF) builds a schedule backwards.
  • Given a precedence graph, starting from the leaf nodes,

choose a node with no other dependent nodes and with the latest deadline to be scheduled last, and work backwards.

  • LDF is optimal in the sense that it minimizes the maximum

lateness.

  • It does not require preemption while EDF does.
  • However, it requires that all tasks be available and their

precedences known before any task is executed.

  • Does not support arrival of tasks.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 24 / 44

slide-30
SLIDE 30

LDF with Precedences

1 d1= 2 d2= 5 d3= 4 d6= 6 d5= 5 d4= 3 6 4 2 3 2 4 5 6 EDF 1 2 4 3 5 6 LDF 1 2 4 3 5 6 EDF*

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 25 / 44

slide-31
SLIDE 31

EDF with Precedences (Chetto et al., 1990)

  • With a preemptive scheduler, EDF can be modified to account

for precedences and to allow tasks to arrive at arbitrary times.

  • It adjusts the deadlines and arrival times according to the

precedences.

  • For a task τi ∈ T, modify its deadline as follows:

d′

i

= min(di, min

j∈D(i)(d′ j − ej))

where D(i) ⊂ T are the tasks that immediately depend on i in the precedence graph.

  • EDF with precedences (EDF*) is optimal in the sense of

minimizing the maximum lateness.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 26 / 44

slide-32
SLIDE 32

EDF with Precedences

1 d1= 2 d2= 5 d3= 4 d6= 6 d5= 5 d4= 3 6 4 2 3 2 4 5 6 EDF 1 2 4 3 5 6 LDF 1 2 4 3 5 6 EDF*

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 27 / 44

slide-33
SLIDE 33

11.4 Scheduling and Mutual Exclusion

  • Fixed priority scheduler always executes the enabled tasks with

highest priority.

  • When threads access shared resources, they need to use mutexes

to ensure data integrity.

  • Mutexes can also complicate scheduling.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 28 / 44

slide-34
SLIDE 34

Mars Pathfinder

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 29 / 44

slide-35
SLIDE 35

Priority Inversion: A Hazard with Mutexes

2 4 6 8 10 task 1 task 2 task 3 acquire lock preempt block preempt release done task 1 blocked

Task 1 has highest priority, task 3 lowest. Task 3 acquires a lock on a shared object, entering a critical section. It gets preempted by task 1, which then tries to acquire the lock and blocks. Task 2 preempts task 3 at time 4, keeping the higher priority task 1 blocked for an unbounded amount of time. In effect, the priorities of tasks 1 and 2 get inverted, since task 2 can keep task 1 waiting arbitrarily long.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 30 / 44

slide-36
SLIDE 36

11.4.2 Priority Inheritance Protocol (PIP)

  • When a task blocks attempting to acquire a lock, the task

holding the lock inherits the priority of the blocked task.

2 4 6 8 10 task 1 task 2 task 3 acquire lock preempt block release done task 1 blocked at priority of 1 done task 2 preempted

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 31 / 44

slide-37
SLIDE 37

11.4.3 Priority Ceiling Protocol

Priorities can be used to remove certain kinds of deadlocks.

2 4 6 task 1 task 2 acquire lock a preempt block on a acquire lock b a b block on b a

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 32 / 44

slide-38
SLIDE 38

11.4.3 Priority Ceiling Protocol (PCP)

  • Every lock or semaphore is assigned a priority ceiling equal to the

priority of the highest-priority task that can lock it.

  • Can one automatically compute the priority ceiling?
  • A task τ can acquire a lock only if the task’s priority is strictly

higher than the priority ceilings of all locks currently held by

  • ther tasks.
  • Intuition: task τ will not later try to acquire these locks held by
  • ther tasks.
  • Locks that are not held by any task don’t affect the task.
  • This prevents deadlocks.
  • There are extensions supporting dynamic priorities and dynamic

creations of locks.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 33 / 44

slide-39
SLIDE 39

11.4.3 Priority Ceiling Protocol (PCP)

2 4 6 task 1 task 2 lock a preempt prevented from locking b by priority ceiling protocol a b a b unlock b, then a a

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 34 / 44

slide-40
SLIDE 40

11.5 Multiprocessor Scheduling

  • Scheduling a fixed finite set of tasks with precedence on a finite

number of processors with goal to minimize execution time is NP-hard.

  • Hu level scheduling algorithm assigns priority to task τ based
  • n the level.
  • Level is the greatest sum of execution times of tasks on a path

in the precedence graph from τ to another task with no dependents.

  • It is a critical path method that is not optimal, but

approximates an optimal solution for most graphs.

  • List scheduler assigns tasks to processors based on priorities.
  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 35 / 44

slide-41
SLIDE 41

Precedence Example Revisited

1 d1= 2 d2= 5 d3= 4 d6= 6 d5= 5 d4= 3 6 4 2 3 2 4 5 6 EDF 1 2 4 3 5 6 LDF 1 2 4 3 5 6 EDF*

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 36 / 44

slide-42
SLIDE 42

A Two Processor Parallel Schedule

1 4 2 3 5 6 Processor A: 2 4 Processor B:

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 37 / 44

slide-43
SLIDE 43

11.5.1 Scheduling Anomalies and Brittleness

  • All thread scheduling algorithms are brittle (i.e., small changes

can have big, unexpected consequences).

  • Let us consider a schedule for a multiprocessor (or multicore).
  • Theorem (Richard Graham, 1976):

If a task set with fixed priorities, execution times, and precedence constraints is scheduled according to prior- ities on a fixed number of processors, then increasing the number of processors, reducing execution times,

  • r weakening precedence constraints can increase the

schedule length.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 38 / 44

slide-44
SLIDE 44

Richard’s Anomalies

4 8 12 16 proc1 proc2 proc3 2 6 10 14 3 2 1 4 9 5 7 8 6 time

e1 = 3 e2 = 2 e3 = 2 e4 = 2 e9 = 9 e8 = 4 e7 = 4 e6 = 4 e5 = 4

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 39 / 44

slide-45
SLIDE 45

Richard’s Anomalies: Smaller Computation Time

e1 = 3 e2 = 2 e3 = 2 e4 = 2 e9 = 9 e8 = 4 e7 = 4 e6 = 4 e5 = 4

4 8 12 16 proc1 proc2 proc3 2 6 10 14 3 2 1 4 9 5 7 8 6 time

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 40 / 44

slide-46
SLIDE 46

Richard’s Anomalies: More Processors

e1 = 3 e2 = 2 e3 = 2 e4 = 2 e9 = 9 e8 = 4 e7 = 4 e6 = 4 e5 = 4 4 8 12 16 proc1 proc2 proc3 2 6 10 14 3 2 1 4 9 5 7 8 6 time proc4

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 41 / 44

slide-47
SLIDE 47

Richard’s Anomalies: Less Precedences

Suppose precedences between task 4 and tasks 7 and 8 are removed.

e1 = 3 e2 = 2 e3 = 2 e4 = 2 e9 = 9 e8 = 4 e7 = 4 e6 = 4 e5 = 4 4 8 12 16 proc1 proc2 proc3 2 6 10 14 3 2 1 4 9 5 7 8 6 time

Remove precedences between task 4 and tasks 7 and 8.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 42 / 44

slide-48
SLIDE 48

Richard’s Anomalies with Mutexes

4 8 12 proc1 proc2 2 6 10 3 1 4 5 time 2 proc1 proc2 3 1 4 5 2 4 8 12 2 6 10 time

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 43 / 44

slide-49
SLIDE 49

Concluding Remarks

  • Scheduling is inherently difficult
  • SW execution time hardly to predict accurately.
  • The actual dynamic behavior can be quite different.
  • Timing behavior under all known task scheduling strategies is

brittle.

  • Small changes can have big (and unexpected) consequences.
  • Unfortunately, since execution times are so hard to predict, such

brittleness can result in unexpected system failures.

  • H. Zheng (CSE USF)

CIS 4930/6930: Principles of CPS 44 / 44