Download - LEC5 CPU Scheduling
1/31/171
CSE3320OperatingSystemsCPUScheduling
Jia RaoDepartmentofComputerScience and Engineering
http://ranger.uta.edu/~jrao
WhatisCPUScheduling?
1/31/172
• Thefive-stateprocessmodel
ready running
waiting
new terminated
schedulerdispatch
interrupt
I/OoreventwaitI/Ooreventcompletion
admit exit
CPUschedulingSelectsfromamongtheprocesses/threadsthatarereadytoexecute,andallocatestheCPUtoit
WhyCPUScheduling?
1/31/173
• Insupportofmultiprogrammingo uniprocessorsystems
} Time-sharingprocessor
o multiprocessorsystems} Efficientlydistributingtasks
o Real-timesystems} Reliablyguaranteeingdeadlines
• Itis(maybe)themostimportantpartinaOSo WhysomeOSseemstobefasterthanothers?o WhyIdonotseeperformanceimprovementwhenupgradingtoa16-corecomputer?
InthisLecture
1/31/174
• Outlineo BasicsofCPUscheduling
} Schedulingpolicies} Evaluationcriteria} Examples} Apracticalpolicy
o Challengesonemerginghardwareandapplications} Manycore,NUMA,asymmetricprocessors} Datacenter,accurateresourceprovisioning
o Acloselookatthestate-of-art} TheLinuxCFSscheduler
CPUScheduling
1/31/175
• CPUschedulingmaytakeplaceato Clockinterruptso I/Ocompletiono I/Ointerruptso Termination
• Nonpreemptiveo Schedulingonlywhencurrentprocessterminatesorgivesupcontrol
• Preemptiveo Processescanbeforcedtogiveupcontrol
processorpick_next_task()readyqueue
preemption
exit/blocked
preemptive
nonpreemptive
SchedulingGoals
1/31/176
SchedulingGoals:A DifferentPointofView
1/31/177
• Userorientedàminimizeo Responsetime(waittime):thetimethatthefirstresponseisreceived(interactivity)
o Turnaroundtime:thetimethatthetaskfinisheso Predictability:variationsindifferentruns
• Systemorientedàmaximizeo Throughput:#oftasksthatfinishpertimeunito Utilization:thepercentageoftimetheCPUisbusyo Fairness:avoidstarvation
ProcessBehaviors
1/31/178
• Bursts of CPU usage alternate with periods of I/O waito a CPU-bound/CPU-intensive processo an I/O bound / I/O intensive process
Ø I/O is when a process enters the blocked state waiting for an external device to complete its work
SchedulingPolicies
1/31/179
• BatchSystemso First-ComeFirst-Serveo ShortestJobFirsto ShortestRemainingTimeNext
• InteractiveSystemso Round-Robino PrioritySchedulingo MultipleQueueso ShortestProcessNexto GuaranteedSchedulingo LotteryScheduling
• Real-timeSystemso RateMonotonicSchedulingo EarliestDeadlineFirstScheduling
MoreonSchedulingPolicy
1/31/17*TheactualmajorschedulefunctioninLinux10
• Determinethenextreadytasktoruno Howwedesignpick_next_task()
• Basicpolicieso First-Come,First-Served(FCFS)o Shortest-Job-First(SJF)o RoundRobin(RR)o Priorityscheduling
processorpick_next_task()*readyqueue
preemption
exit/blocked
First-Come,First-Serve(FCFS)
1/31/1711
• CPUschedulesthetaskthatarrivedearliest,non-preemptiveProcess ArrivalTime BurstTime
P1 0 8P2 1 4P3 2 9P4 3 5
260 8 12 21P1 P2 P3 P4
Averageturnaroundtime=((8-0)+(12-1)+(21-2)+(26-3))/4=15.25Averageresponsetime=(0+(8-1)+(12-2)+(21-3))/4=8.75
260 5 14 18P1P2P3P4
Averageturnaroundtime=((5-0)+(14-1)+(18-2)+(26-3))/4=14.25Averageresponsetime=(0+(5-1)+(14-2)+(18-3))/4=7.75
ShortestJobFirst(SJF)
1/31/1712
• CPUschedulesthetaskwiththeshortestremainingtimeProcess ArrivalTime BurstTime
P1 0 8P2 1 4P3 2 9P4 3 5
Averageturnaroundtime=((8-0)+(12-1)+(26-2)+(17-3))/4=14.25Averageresponsetime=(0+(8-1)+(17-2)+(12-3))/4=7.75
260 8 12 17P1 P2 P3P4nonpreemptive
260 5 10 17Averageturnaroundtime=((17-0)+(5-1)+(10-3)+(26-2))/4=13Averageresponsetime=(0+(1-1)+(5-3)+(17-2))/4=4.25
preemptive P1P2 P3P4P11
RoundRobin(RR)
1/31/1713
• LikeFCFS,butwithlimitedtimeslices,preemptiveProcess ArrivalTime BurstTime
P1 0 8P2 1 4P3 2 9P4 3 5
250 12 16 24Averageturnaroundtime=((20-0)+(8-1)+(26-2)+(25-3))/4=18.25Averageresponsetime=(0+(4-1)+(8-2)+(12-3))/4=4.5
q=4 P1 P2 P3 P4 P1 P3 P3P44 8 20 26
260 9 14 19Averageturnaroundtime=((22-0)+(9-1)+(26-2)+(19-3))/4=17.5Averageresponsetime=(0+(5-1)+(9-2)+(14-3))/4=5.5
q=55
P4P1 P2 P3 P1 P322
PriorityScheduling
1/31/1714
• CPUschedulesthehighestpriorityfirst,FCFSwithinthesamepriority Process Priority BurstTime
P1 3 8P2 1 4P3 4 9P4 2 5
P1P2 P3P4Process Priority BurstTime
P1 2 8P2 4 4P3 1 9P4 3 5
P1 P2P3 P4
Putittogether
1/31/1715
Turnaroundtime Responsetime
FCFS 15.25 8.75
SJF-preemptive 13 4.25
RR(q=5) 17.5 5.5
Priorityscheduling N/A N/A
Throughput Responsetime Starvation
FCFS TBD TBD No
SJF-preemptive High Good Yes
RR Can below Good No
Priorityscheduling Canbehigh Canbegood Can remove
MultilevelFeedbackQueue
MultilevelFeedbackQueue
1/31/1716
RR,q=4
RR,q=8
RR,q=16
Highpriority/shorttask
Lowpriority/longtask
pick_next_task()
WindowsXP,MacOSX,Linux2.6.22andbefore
Anti-StarvationAswaittimeincreases,
ataskgraduallymovesup
Real-timeScheduling
1/31/1717
Schedulable real-time system• Given
o m periodic eventso event i occurs within period Pi and requires Ci seconds
• Then the load can only be handled if
• Example: a soft real-time system with three periodic events, with periods of 100, 200, and 500 ms, respectively. If these events require 50, 30, and 100 ms of CPU time per event, respective, the system is schedulableo Process/context switching overhead is often an issue though!o Given the example, what would be the maximum CPU burst for a 4th
job with a period of 500 ms ?
11
mi
i i
CP=
£å
Misc.
1/31/1718
• I/Otaskso Identify:runsfew,sleepsaloto Consideredasshorttasko Highpriority
• Theoreticalanalysiso Assumetaskdistributiono Queuingmodel
RR,q=4
RR,q=8
RR,q=16
Kerneltasks
I/Otasks
computationtasks
ChallengesonEmergingHardwareandApplications
1/31/1719
• MultiprocessoràMany core
processorpick_next_task()readyqueue
processor
…
Multiprocessor=morepowerfulprocessor
pick_next_task()willbethebottleneck
processorpick_next_task()readyqueue
processor
…readyqueue
…
pick_next_task()
LoadbalancingCacheaffinity
Problem:Self-scheduling,globalcontrolisdifficult
Linux2.4.x
Linux2.6.x
ChallengesonEmergingHardwareandApplications(cont’)
1/31/1720
• NUMA,Asymmetricprocessorso OLD:CPUtimeà usefulworko NEW:calibratedCPUtimeàusefulwork
• Datacenter,accurateresourceprovisioningo Proportionalfairsharingà P1 : P2 =1:2
RR,q=4
RR,q=8
RR,q=16
pick_next_task()
q=?Whentomovetask?Heuristic-based
Fine-grainedAdaptiveq
ACloseLookattheState-of-Art
1/31/1721
• LinuxCompletelyFairScheduler(CFS)o Separatereadyqueueperprocessoro Red-blacktreebasedreadyqueueo Proportionalfairsharing
pick_next_task()Removetheleftmosttask;Runthetask;Everyticksupdatevruntime;vruntime =vruntime +runtime;ifvruntime >curr_leftmost’sPreemptcurr task;Putitbacktothesortedtree;Runcurr_leftmost;
vruntime
Fine-grainAdaptiveq
Priority:lowpriorityTask’svruntime runs
FasterNostarvation
Summary
1/31/1722
• Thebasicschedulingpoliciesareimportanto MultilevelFeedbackQueue=RR+SJF+Priorityo CFS=RR+SJF+Priority+smartdatastructure
• Additionalreadingso Gotohttp://lxr.linux.no/linux+v2.6.24/kernel/o Read/kernel/sched.c,/kernel/sched_fair.c,/include/linux/sched.h(startingfromtheschedule(void)function)
o Seehowthevruntime isactuallyupdatedo Documentation:http://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt
o Anotherinterestingscheduler:BFS} http://ck.kolivas.org/patches/bfs/bfs-faq.txt