[537] Schedulers Tyler Harter 9/10/14 Overview Review processes - - PowerPoint PPT Presentation

537 schedulers
SMART_READER_LITE
LIVE PREVIEW

[537] Schedulers Tyler Harter 9/10/14 Overview Review processes - - PowerPoint PPT Presentation

[537] Schedulers Tyler Harter 9/10/14 Overview Review processes Workloads, schedulers, and metrics (Chapter 7) A general purpose scheduler, MLFQ (Chapter 8) Lottery scheduling (Chapter 9) Review: Processes CPU Memory Process Creation


slide-1
SLIDE 1

[537] Schedulers

Tyler Harter 9/10/14

slide-2
SLIDE 2

Overview

Review processes Workloads, schedulers, and metrics (Chapter 7) A general purpose scheduler, MLFQ (Chapter 8) Lottery scheduling (Chapter 9)

slide-3
SLIDE 3

Review: Processes

slide-4
SLIDE 4

Process Creation

  • code

static data Program CPU Memory

slide-5
SLIDE 5

Process Creation

  • code

static data Program CPU Memory code static data heap

  • stack

Process

slide-6
SLIDE 6

State Transitions

Running Ready Blocked

Scheduled Descheduled I/O: initiate I/O: done

slide-7
SLIDE 7

Running Ready Blocked

Scheduled Descheduled I/O: initiate I/O: done

How to transition? (“mechanism”) When to transition? (“policy”)

slide-8
SLIDE 8

// Per-process state struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kern stack for this proc enum procstate state; // Process state volatile int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) };

slide-9
SLIDE 9

// Per-process state struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kern stack for this proc enum procstate state; // Process state volatile int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) };

slide-10
SLIDE 10

Process A
 …
 
 
 
 
 
 
 
 
 
 
 
 
 Operating System Hardware Program

slide-11
SLIDE 11


 
 timer interrupt
 save regs(A) to k-stack(A)
 move to kernel mode
 jump to trap handler
 
 
 
 
 Process A
 …
 
 
 
 
 
 
 
 
 
 
 
 Operating System Hardware Program

slide-12
SLIDE 12


 
 
 
 
 
 Handle the trap
 Call switch() routine
 save regs(A) to proc-struct(A)
 restore regs(B) from proc-struct(B)
 switch to k-stack
 return-from-trap (into B) 
 
 timer interrupt
 save regs(A) to k-stack(A)
 move to kernel mode
 jump to trap handler
 
 Process A
 …
 
 
 
 
 
 
 
 
 Operating System Hardware Program

slide-13
SLIDE 13


 
 timer interrupt
 save regs(A) to k-stack(A)
 move to kernel mode
 jump to trap handler
 
 
 
 
 
 
 restore regs(B) from k-stack(B)
 move to user mode
 jump to B’s IP Process A
 …
 
 
 
 
 
 
 
 
 
 
 
 
 Operating System Hardware Program 
 
 
 
 
 
 Handle the trap
 Call switch() routine
 save regs(A) to proc-struct(A)
 restore regs(B) from proc-struct(B)
 switch to k-stack
 return-from-trap (into B)

slide-14
SLIDE 14


 
 timer interrupt
 save regs(A) to k-stack(A)
 move to kernel mode
 jump to trap handler
 
 
 
 
 
 
 restore regs(B) from k-stack(B)
 move to user mode
 jump to B’s IP Process A
 …
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Process B
 … Operating System Hardware Program 
 
 
 
 
 
 Handle the trap
 Call switch() routine
 save regs(A) to proc-struct(A)
 restore regs(B) from proc-struct(B)
 switch to k-stack
 return-from-trap (into B)

slide-15
SLIDE 15

Basic Schedulers

slide-16
SLIDE 16

Vocabulary

Workload: set of job descriptions Scheduler: logic that decides when jobs run Metric: measurement of scheduling quality

slide-17
SLIDE 17

Vocabulary

Workload: set of job descriptions Scheduler: logic that decides when jobs run Metric: measurement of scheduling quality Scheduler “algebra”, given 2 variables, find the 3rd:

f(W, S) = M

slide-18
SLIDE 18

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-19
SLIDE 19

Scheduling Basics

Metrics:
 turnaround_time
 response_time
 Schedulers:
 FIFO
 SJF
 STCF
 RR Workloads:
 arrival_time
 run_time

slide-20
SLIDE 20

Example: workload, scheduler, metric

JOB arrival_time (s) run_time (s) A 0.0001 10 B 0.0002 10 C 0.0003 10

FIFO: First In, First Out (run jobs in arrival_time order)

What is our turnaround?: completion_time - arrival_time

slide-21
SLIDE 21

Example: workload, scheduler, metric

JOB arrival_time (s) run_time (s) A ~0 10 B ~0 10 C ~0 10

FIFO: First In, First Out (run jobs in arrival_time order)

What is our turnaround?: completion_time - arrival_time

slide-22
SLIDE 22

Event Trace

Time Event

A arrives B arrives C arrives run A 10 complete A 10 run B

  • 20

complete B 20 run C 30 complete C

slide-23
SLIDE 23

Trace Visualization

A B C 20 40 60 80

slide-24
SLIDE 24

Trace Visualization

A B C 20 40 60 80 What is the average turnaround time? (Q1)

  • Def: turnaround_time = completion_time - arrival_time

[A,B,C arrive]

slide-25
SLIDE 25

Trace Visualization

A B C 20 40 60 80 What is the average turnaround time? (Q1)

  • Def: turnaround_time = completion_time - arrival_time

[A,B,C arrive]

slide-26
SLIDE 26

Trace Visualization

20 40 60 80 What is the average turnaround time? (Q1)

  • Def: turnaround_time = completion_time - arrival_time

A: 10s B: 20s C: 30s

slide-27
SLIDE 27

Trace Visualization

20 40 60 80 What is the average turnaround time? (Q1)

  • (10 + 20 + 30) / 3 = 20s

A: 10s B: 20s C: 30s

slide-28
SLIDE 28

Scheduling Basics

Metrics:
 turnaround_time
 response_time
 Schedulers:
 FIFO
 SJF
 STCF
 RR Workloads:
 arrival_time
 run_time

slide-29
SLIDE 29

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-30
SLIDE 30

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-31
SLIDE 31

“Solve” for W

f(W, S) = M

Workload: ? Scheduler: FIFO Metric: turnaround is high

slide-32
SLIDE 32

Example: Big First Job

JOB arrival_time (s) run_time (s) A ~0 60 B ~0 10 C ~0 10

What is the average turnaround time? (Q2)

slide-33
SLIDE 33

Example: Big First Job

JOB arrival_time (s) run_time (s) A ~0 60 B ~0 10 C ~0 10

What is the average turnaround time? (Q2)

slide-34
SLIDE 34

A C B

20 40 60 80 Average turnaround time: 70s

A: 60s B: 70s C: 80s

Example: Big First Job

slide-35
SLIDE 35

Convoy Effect

slide-36
SLIDE 36

Passing the Tractor

New scheduler: SJF (Shortest Job First) Policy: when deciding what job to run next,
 choose the one with smallest run_time

slide-37
SLIDE 37

Example: Shortest Job First

JOB arrival_time (s) run_time (s) A ~0 60 B ~0 10 C ~0 10

What is the average turnaround time with SJF? (Q3)

slide-38
SLIDE 38

Example: Shortest Job First

JOB arrival_time (s) run_time (s) A ~0 60 B ~0 10 C ~0 10

What is the average turnaround time with SJF? (Q3)

slide-39
SLIDE 39

Q3 Answer

A C B

20 40 60 80

A: 80s B: 10s C: 20s

What is the average turnaround time with SJF? (Q3)

  • (80 + 10 + 20) / 3 = ~36.7s
slide-40
SLIDE 40

Scheduling Basics

Metrics:
 turnaround_time
 response_time
 Schedulers:
 FIFO
 SJF
 STCF
 RR Workloads:
 arrival_time
 run_time

slide-41
SLIDE 41

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-42
SLIDE 42

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-43
SLIDE 43

Shortest Job First (Arrival Time)

JOB arrival_time (s) run_time (s) A ~0 60 B ~10 10 C ~10 10

What is the average turnaround time with SJF?

slide-44
SLIDE 44

Stuck Behind a Tractor Again

A C B

20 40 60 80 What is the average turnaround time?

  • (Q4)

[B,C arrive]

slide-45
SLIDE 45

Stuck Behind a Tractor Again

A C B

20 40 60 80 What is the average turnaround time?

  • (Q4)

[B,C arrive]

slide-46
SLIDE 46

Stuck Behind a Tractor Again

A C B

20 40 60 80 What is the average turnaround time?

  • (60 + 60 + 70) / 3 = 63.3s

A: 60s B: 60s C: 70s

slide-47
SLIDE 47

A Preemptive Scheduler

Prev schedulers: FIFO and SJF are non-preemptive New scheduler: STCF (Shortest Time-to-Completion First) Policy: switch jobs so we always run the one that
 will complete the quickest

slide-48
SLIDE 48

SJF

A C B

20 40 60 80 Average turnaround time: 70s

[B,C arrive]

slide-49
SLIDE 49

STCF

A C B

20 40 60 80 Average turnaround time: (Q4)

[B,C arrive]

A

slide-50
SLIDE 50

STCF

A C B

20 40 60 80 Average turnaround time: (Q4)

[B,C arrive]

A

slide-51
SLIDE 51

STCF

A C B

20 40 60 80 Average turnaround time: 36.6

A

A: 80s B: 10s C: 20s

slide-52
SLIDE 52

Scheduling Basics

Metrics:
 turnaround_time
 response_time
 Schedulers:
 FIFO
 SJF
 STCF
 RR Workloads:
 arrival_time
 run_time

slide-53
SLIDE 53

Break!

slide-54
SLIDE 54

Administrative Stuff

P1 due on 9/16 (6 days left!) Office hours: today in CS 7373, 2-3pm Reading: chapters 3-6 (last time) and 7-11 (today) Discussion tomorrow: fork/exec, C review

slide-55
SLIDE 55

Scheduling Basics

Metrics:
 turnaround_time
 response_time
 Schedulers:
 FIFO
 SJF
 STCF
 RR Workloads:
 arrival_time
 run_time

slide-56
SLIDE 56

Response Time

Sometimes we care about when a job starts instead of when it finishes. Why? response_time = first_run_time - arrival_time

slide-57
SLIDE 57

Response vs. Turnaround

A

20 40 60 80

B’s turnaround: 20s

B

[B arrives]

B’s response: 10s

slide-58
SLIDE 58

Round-Robin Scheduler

Prev schedulers: FIFO, SJF, and STCF have poor
 response time New scheduler: RR (Round Robin) Policy: alternate between ready processes every
 fixed-length slice

slide-59
SLIDE 59

FIFO vs. RR (Q5) — which is each?

5 10 15 20 5 10 15 20 A B C ABC… Avg Response Time? Q5 Avg Response Time? Q5

slide-60
SLIDE 60

FIFO vs. RR (Q5) — which is each?

5 10 15 20 5 10 15 20 A B C ABC… Avg Response Time? Q5 Avg Response Time? Q5

slide-61
SLIDE 61

5 10 15 20 5 10 15 20 A B C ABC… Avg Response Time? (0+1+2)/3 = 1 Avg Response Time? (0+5+10)/3 = 5

FIFO vs. RR (Q5) — which is each?

slide-62
SLIDE 62

Scheduling Basics

Metrics:
 turnaround_time
 response_time
 Schedulers:
 FIFO
 SJF
 STCF
 RR Workloads:
 arrival_time
 run_time

slide-63
SLIDE 63

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-64
SLIDE 64

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-65
SLIDE 65

Not I/O Aware

A

20 40 60 80 Disk:

A B

CPU:

A A A

slide-66
SLIDE 66

I/O Aware (Overlap)

A

20 40 60 80 Disk:

A B

CPU:

A A A B B

slide-67
SLIDE 67

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known
slide-68
SLIDE 68

Workload Assumptions

  • 1. Each job runs for the same amount of time
  • 2. All jobs arrive at the same time
  • 3. All jobs only use the CPU (no I/O)
  • 4. The run-time of each job is known


(need smarter, fancier scheduler)

slide-69
SLIDE 69

MLFQ

slide-70
SLIDE 70

MLFQ (Multi-Level Feedback Queue)

Goal: general-purpose scheduling Must support two job types with distinct goals


  • “interactive” programs care about response time

  • “batch” programs care about turnaround time

Approach: multiple levels of round-robin

slide-71
SLIDE 71

Priorities

Rule 1: If priority(A) > Priority(B), A runs
 Rule 2: If priority(A) == Priority(B), A & B run in RR

A B C

Q3 Q2 Q1 Q0

D

slide-72
SLIDE 72

Rule 1: If priority(A) > Priority(B), A runs
 Rule 2: If priority(A) == Priority(B), A & B run in RR

A B C

Q3 Q2 Q1 Q0

D

How to know process type to set priority? Approach 1: nice
 Approach 2: history

Priorities

slide-73
SLIDE 73

Rule 1: If priority(A) > Priority(B), A runs
 Rule 2: If priority(A) == Priority(B), A & B run in RR

A B C

Q3 Q2 Q1 Q0

D

How to know process type to set priority? Approach 1: nice
 Approach 2: history

Priorities

slide-74
SLIDE 74

History

Processes alternate between I/O and CPU work Consider each CPU session it’s own “job” Guess what a job will be like based on past jobs from the same process

slide-75
SLIDE 75

More MLFQ Rules

Rule 1: If priority(A) > Priority(B), A runs
 Rule 2: If priority(A) == Priority(B), A & B run in RR More rules:
 Rule 3: Processes start at top priority
 Rule 4: If job uses whole slice, demote process


slide-76
SLIDE 76

5 10 15 20

One Long Job (Example)

Q3 Q2 Q1 Q0

slide-77
SLIDE 77

120 140 160 180 200

An Interactive Process Joins

Q3 Q2 Q1 Q0

slide-78
SLIDE 78

120 140 160 180 200

Improvements

Q3 Q2 Q1 Q0

What are problems?

slide-79
SLIDE 79

120 140 160 180 200

Improvements

Q3 Q2 Q1 Q0

What are problems?


  • unforgiving

  • gaming the system

  • hard to tune

(read OSTEP)

slide-80
SLIDE 80

Lottery

slide-81
SLIDE 81

Lottery Scheduling

Goal: proportional share Approach:


  • give processes lottery tickets

  • whoever wins runs

  • higher priority => more tickets
slide-82
SLIDE 82

Random Algorithms

Discuss:
 disadvantages?
 advantages?


slide-83
SLIDE 83

Lottery Code

int counter = 0;
 int winner = getrandom(0, totaltickets);
 node_t *current = head;
 while(current) {
 counter += current->tickets;
 if (counter > winner)
 break;
 current = current->next;
 }
 // current is the winner

slide-84
SLIDE 84

int counter = 0;
 int winner = getrandom(0, totaltickets);
 node_t *current = head;
 while(current) {
 counter += current->tickets;
 if (counter > winner)
 break;
 current = current->next;
 }
 // current gets to run

Job A
 (1) Job B
 (1)

head

Job C
 (100) Job D
 (200) Job E
 (100)

null

Who runs if winner is:
 50 (Q6)
 350 (Q7)
 (Q8)

slide-85
SLIDE 85

Other Lottery Ideas

Ticket Transfers Ticket Currencies Ticket Inflation (read more in OSTEP)

slide-86
SLIDE 86

Summary

Understand your goals (metrics) and workload, then design your scheduler around that. General purpose schedulers need to support processes with different types of goals. Random algorithms are often simple to implement, and avoid corner cases.