04 Uniprocessor
04 Uniprocessor
Uniprocessor Scheduling
Prof. Seyed Majid Zahedi
https://ece.uwaterloo.ca/~smzahedi
Outline
• History
• Definitions
• Response time, throughput, scheduling policy, …
• In 2016, it was shown that bugs in Linux kernel scheduler could cause up
to 138x slowdown in some workloads with proportional energy waist [2]
[1] L. Torvalds. The Linux Kernel Mailing List. http://tech-insider.org/linux/research/2001/1215.html, Feb. 2001.
[2] Lozi, Jean-Pierre, et al. "The Linux scheduler: a decade of wasted cores." Proceedings of the Eleventh European Conference on Computer Systems. 2016.
Definitions
• Work-conserving scheduler: CPUs won’t be left idle if there are ready tasks to run
• For non-preemptive schedulers, work-conserving is not always better (why?)
• These may not hold in all systems, but they simplify the problem
• Maximize throughput
• Maximize tasks per time unit (e.g., tasks per second)
• Related to response time, but not identical
• Minimizing response time could lead to more context switching which will than
hurt throughput (more on this later)
• Two parts to maximizing throughput
• Minimize overhead (e.g., context-switching)
• Efficient use of resources (e.g., CPU, disk, memory, etc.)
Fairness
T1 T2 T3
0 24 27 30
FCFS Scheduling (cont.)
• Example continued
T1 T2 T3
0 24 27 30
T2 T3 T1
0 3 6 30
• Wait time for T1 is 6, for T2 is 0, and for T3 is 3
• Average wait time is (6 + 0 + 3)/3 = 3
• Average response time is (3 + 6 + 30)/3 = 13
• Average wait time is much better (before it was 17)
• Average response time is better (before it was 27)
• Pros and cons of FCFS
• + Simple
• − Short tasks get stuck behind long ones
Convoy Effect
• With FCFS, convoys of small tasks tend to build up when a large one is running
Scheduled Task
Time
Scheduling queue
Arrivals
Round Robin (RR) Scheduling
Photo: Pinterest.ca
Example: RR with Time Quantum of 20
T1 T2 T3 T4 T1 T3 T4 T1 T3 T3
T1 T2
0 1 2
• Average response time = (1 + 2)/2 = 1.5
Increase Response Time
0 2
FCFS T1 T2 … T9 T10
0 100 200 800 900 1000
RR … … … … …
0 10 20 980 990 1000
991 999
• Completion times
Task # FCFS RR
1 100 991
2 200 992
… … …
9 900 999
10 1000 1000
FCFS vs. RR (cont.)
Wait Time 8
10
20
Worst FCFS
Best FCFS 85 8 153 32 69½
1
5
Response 8
Time
10
20
Worst FCFS
Earlier Example: RR vs. FCFS,
Effect of Different Time Quanta (cont.)
Worst
T3 (68) T1 (53) T4 (24) T2 (8)
FCFS
0 68 121 145 153
Quantum T1 T2 T3 T5 Average
Best FCFS 32 0 85 8 31¼
1
5
Wait Time 8
10
20
Worst FCFS 68 145 0 121 83½
Best FCFS 85 8 153 32 69½
1
5
Response 8
Time
10
20
Worst FCFS 121 153 68 145 121¾
Earlier Example: RR vs. FCFS,
Effect of Different Time Quanta (cont.)
P1 P2 P3 P4 P1 P3 P4 P1 P3 P4 P1 P3 P1 P3 P1 P3 P1 P3 P3 P3
0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 133 141149 153
Quantum T1 T2 T3 T5 Average
Best FCFS 32 0 85 8 31¼
1 84 22 85 57 62
5 82 20 85 58 61¼
Wait Time 8 80 8 85 56 57¼
10 82 10 85 68 61¼
20 72 20 85 88 66¼
Worst FCFS 68 145 0 121 83½
Best FCFS 85 8 153 32 69½
1 137 30 153 81 100½
5 135 28 153 82 99½
Response 8 133 16 153 80 95½
Time
10 135 18 153 92 99½
20 125 28 153 112 104½
Worst FCFS 121 153 68 145 121¾
Shortest Task First (SJF) Scheduling
• Keep order of tasks the same, but run the shorter task first
A and B : C:
I/O
I/O
I/O
SRTF
I/O Utilization:
~90%
CPU A A A A A A A A A A A A A A A A
I/O
Problems with SRTF
• Starvation: large tasks may never run if short ones keep coming
• Overhead: short tasks preempt long ones ⇒ too many context switches
• Unfair: large tasks are penalized, there is high variance in response time
• Bottom line, we can’t really know how long tasks will take
• However, we can use SRTF as yardstick for measuring other policies
• Optimal, so we can’t do any better
• Execution plan
• Always execute highest-priority runnable tasks to completion
• Each queue can be threaded in RR with some time-quantum
• Notice any problems?
• Starvation
• Lower-priority tasks may never run because of higher-priority tasks
• Priority inversion
• Low-priority task delays high-priority task by holding resources needed by high-
priority task (more on this later)
Fairness
Long-Running compute
tasks are demoted to
lower priority queues
• Define unfairness for two tasks with the same burst time as
• Unfairness = finish time of first one / finish time of last one
• As function of burst time
Stride Scheduling
• Runs thread with lowest pass and adds its stride to its pass
• Low-stride threads (lots of tickets) run more often
• Thread with twice the tickets gets to run twice as often
• Some messiness of counter wrap-around, new threads, …
Max-min Fair (MMF) Scheduling
t/N
T1 T2 T3
MMF Scheduling (cont.)
T1 T3
T2
• What if we want to give more to some and less to others (proportional share)?
• Key Idea: assign weight wi to each thread i
• MMF uses single time quantum for all threads
Target latency
Q=
N
• Weighted MMF uses different time quanta for different threads
wi ⇥ Target latency
Qi = PN
j=1 wj
• E.g., with 20ms target latency, 1ms minimum granularity, and 2 threads: A with
weight 1 and B with weight 4
• Time quantum for A is 4 ms
• Time quantum for B is 16 ms
Weighted MMF Scheduling (cont.)
• Also track threads’ virtual runtime rather than their true wall-clock runtime
• Higher weight: virtual runtime increases more slowly
• Lower weight: virtual runtime increases more quickly
• Linux completely fair scheduler deploys very similar ideas
• Ready queue is implemented as red-black tree, in which threads are sorted in increasing
order of their virtual runtime
16 1 1
B B A
4
A
How to Evaluate Scheduling Algorithms?
• Deterministic modeling
• Pick workload and compute performance of each algorithm
• Queueing models
• Mathematical approach for handling stochastic workloads
(more on this later)
• Simulations/Implementations
• Build system which allows actual algorithms to be run against actual
data – most flexible/general
Starvation and Sample Bias
globaldigitalcitizen.org
Acknowledgment