Lecture 11: CPU Scheduling
Lecture 11: CPU Scheduling
Zhong Shao
Dept. of Computer Science
Yale University
CPU scheduler
1
10/1/19
Main points
Example
2
10/1/19
Definitions
◆ Task/Job
– User request: e.g., mouse click, web request, shell command, …
◆ Latency/response time
– How long does a task take to complete?
◆ Throughput
– How many tasks can be done per unit of time?
◆ Overhead
– How much extra work is done by the scheduler?
◆ Fairness
– How equal is the performance received by different users?
◆ Predictability
– How consistent is the performance over time?
More definitions
◆ Workload
– Set of tasks for system to perform
◆ Preemptive scheduler
– If we can take resources away from a running task
◆ Work-conserving
– Resource is used whenever there is a task to run
– For non-preemptive schedulers, work-conserving is not always
better
◆ Scheduling algorithm
– takes a workload as input
– decides which tasks to do first
– Performance metric (throughput, latency) as output
– Only preemptive, work-conserving schedulers to be considered
3
10/1/19
◆ Example: memcached
– Facebook cache of friend lists, …
4
10/1/19
FIFO scheduling
◆ Example: Process Burst Time
P1 24
P2 3
P3 3
P1 P2 P3
0 24 27 30
◆ Waitingtime for P1 = 0; P2 = 24; P3 = 27
◆ Average waiting time: (0 + 24 + 27)/3 = 17
P2 P3 P1
0 3 6 30
◆ FIFO Pros: simple; Cons: short jobs get stuck behind long jobs
5
10/1/19
◆ SJF (non-preemptive)
P1 P3 P2 P4
0 3 7 8 12 16
◆ Average waiting time = (0 + 6 + 3 + 7)/4 = 4
6
10/1/19
◆ SJF (preemptive)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
(2)
(3)
(4)
(5)
Tasks SJF
(1)
(2)
(3)
(4)
(5)
Time
7
10/1/19
8
10/1/19
Round Robin
Tasks Round Robin (1 ms time slice)
(1) Rest of Task 1
(2)
(3)
(4)
(5)
(2)
(3)
(4)
(5)
Time
9
10/1/19
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
◆ Typically,
higher average turnaround than
SJF, but better response.
RR vs. FIFO
◆ RR
– job1: 991s, job2: 992s, ... , job10: 1000s
◆ FIFO
– job 1: 100s, job2: 200s, ... , job10: 1000s
◆ Comparisons
– RR is much worse for jobs about the same length
– RR is better for short jobs
10
10/1/19
(2)
(3)
(4)
(5)
(2)
(3)
(4)
(5)
Time
Mixed workload
Tasks
I/O Bound
CPU Bound
CPU Bound
Time
11
10/1/19
Max-Min Fairness
◆ Goals:
– Responsiveness
– Low overhead
– Starvation freedom
– Some tasks are high/low priority
– Fairness (among equal priority tasks)
12
10/1/19
MFQ
MFQ
New or I/O
1 10 Bound Task
Time Slice
2 20 Expiration
3 40
4 80
13
10/1/19
14
10/1/19
Multiprocessor scheduling
15
10/1/19
16
10/1/19
17
10/1/19
Tail latency
Local Computation
Barrier
Time
Communication
Barrier
Local Computation
p1.4 p1.2
p2.1
p2.3 p1.3
Time
p3.4
p3.1 p2.2
p2.4
18
10/1/19
Gang scheduling
Perfectly Parallel
(Inverse Response Time)
Performance
Diminishing Returns
Limited Parallelism
Number of Processors
19
10/1/19
Space sharing
Process 1 Process 2
Queueing theory
20
10/1/19
Queueing model
Arrivals Departures
(Throughput)
Queue Server
Definitions
21
10/1/19
Little’s law
N=X*R
Question
22
10/1/19
Question
◆ From example:
X = 100 task/sec
R = 50 ms/task
S = 5 ms/task
W = 45 ms/task
Q = 4.5 tasks
Queueing
23
10/1/19
Throughput (X)
μ
λ<μ λ>μ Max throughput
no queuing growing queues
R=S R undefined
S
μ μ
Arrival Rate (λ) Arrival Rate (λ)
λ<μ λ>μ
queuing growing queues
depends on R undefined
burstiness
Response Time (R)
bursty arrivals
μ
Arrivals Per Second (λ)
24
10/1/19
◆ What is average?
– Gaussian: Arrivals are spread out, around a mean value
– Exponential: arrivals are memoryless
– Heavy-tailed: arrivals are bursty
Exponential distribution
Probability of x
Exponential Distribution
-λx
f(x) = λe
25
10/1/19
Exponential distribution
λ λ λ λ λ
0 1 2 3 4 ...
μ μ μ μ μ
100 S
80 S
Response Time R
60 S
40 S
R = S/(1-U)
20 S
0
0 0.2 0.4 0.6 0.8 1.0
Utilization U
26
10/1/19
Question
◆ Exponential arrivals
– Variance in R = S/(1-U)^2
27
10/1/19
◆ Response time =
Sum over all i
Service time for resource i /
(1 – Utilization of resource i)
◆ Implication
– If you fix one bottleneck, the next highest utilized resource
will limit performance
Overload management
28
10/1/19
29