26
Windows 2000 Windows 2000 Scheduling Scheduling Computing Department, Computing Department, Lancaster University, UK Lancaster University, UK

Windows 2000 Scheduling Computing Department, Lancaster University, UK

Embed Size (px)

Citation preview

Windows 2000 SchedulingWindows 2000 Scheduling

Computing Department,Computing Department,

Lancaster University, UKLancaster University, UK

OverviewOverview

• GoalsGoals– SchedulingScheduling

•Scheduling conceptsScheduling concepts

•Thread priority levelsThread priority levels

•Multiprocessor supportMultiprocessor support

SchedulingScheduling

• Multiple threads may be ready to run…Multiple threads may be ready to run…– Who gets to use the CPU?Who gets to use the CPU?

• From Win32 point of view:From Win32 point of view:– Processes are given a priority class upon creationProcesses are given a priority class upon creation

• Idle, Below Normal, Normal, Above Normal, High, Idle, Below Normal, Normal, Above Normal, High, RealtimeRealtime

– Threads have a relative priority within the classThreads have a relative priority within the class• Idle, Lowest, Below_Normal, Normal, Above_Normal, Idle, Lowest, Below_Normal, Normal, Above_Normal,

Highest, Time_CriticalHighest, Time_Critical

Scheduling (2)Scheduling (2)

• To the Windows 2000 scheduler:To the Windows 2000 scheduler:– Concern is with threads not processesConcern is with threads not processes

• i.e. threads are scheduled, not processesi.e. threads are scheduled, not processes

– Threads have priorities 0 through 31Threads have priorities 0 through 3131

1615

10i

16 “real-time” levels

15 variable levels

Used by zero page thread

Used by idle thread(s)

Win32 versus Kernel Win32 versus Kernel PrioritiesPriorities

RealReal

TimeTime HighHigh

Above Above NormalNormal NormalNormal

Below Below NormalNormal IdleIdle

Time-criticalTime-critical 3131 1515 1515 1515 1515 1515HighestHighest 2626 1515 1212 1010 88 66

Above-normalAbove-normal 2525 1414 1111 99 77 55NormalNormal 2424 1313 1010 88 66 44

Below-NormalBelow-Normal 2323 1212 99 77 55 33LowestLowest 2222 1111 88 66 44 22

IdleIdle 1616 11 11 11 11 11

Win32 Process Classes

Win32ThreadPriorities

Manual Process Priority Manual Process Priority AdjustmentsAdjustments

Win32 Scheduling-related Win32 Scheduling-related APIsAPIs• Get/SetPriorityClassGet/SetPriorityClass • Get/SetThreadPriorityGet/SetThreadPriority – relative to its process – relative to its process

base prioritybase priority• Get/SetProcessAffinityMaskGet/SetProcessAffinityMask• SetThreadAffinityMask SetThreadAffinityMask – must be a subset of – must be a subset of

the process’s affinity maskthe process’s affinity mask• SetThreadIdealProcessor SetThreadIdealProcessor – Establishes a – Establishes a

“preferred” processor“preferred” processor• Get/SetProcessPriorityBoostGet/SetProcessPriorityBoost• Suspend/ResumeThreadSuspend/ResumeThread

Thread SchedulingThread Scheduling

• Strictly priority drivenStrictly priority driven– 32 queues (FIFO lists) of ready threads32 queues (FIFO lists) of ready threads

• One queue for each priority levelOne queue for each priority level• Queues are common to all CPUsQueues are common to all CPUs

– When thread becomes ready, it:When thread becomes ready, it:• either runs immediately, oreither runs immediately, or• is inserted at the tail end of the Ready queue for its is inserted at the tail end of the Ready queue for its

current prioritycurrent priority

– On a uniprocessor, highest priority Ready thread On a uniprocessor, highest priority Ready thread always runsalways runs

• Time-sliced, round-robin within a priority levelTime-sliced, round-robin within a priority level

Thread Scheduling Thread Scheduling Multiprocessor IssuesMultiprocessor Issues• On a multiprocessor, highest-priority On a multiprocessor, highest-priority n n

threads will always run (subject to Affinity, threads will always run (subject to Affinity, see later)see later)

• No attempt is made to share processors No attempt is made to share processors “fairly” among processes, only among “fairly” among processes, only among threadsthreads

• Tries to keep threads on the same CPUTries to keep threads on the same CPU

Scheduling ScenariosScheduling Scenarios

• PreemptionPreemption– A thread becomes ready at a higher A thread becomes ready at a higher

priority than the running threadpriority than the running thread•Lower-priority thread is preemptedLower-priority thread is preempted•Preempted thread goes to the head of its Preempted thread goes to the head of its

Ready queueReady queue

– Strictly event-drivenStrictly event-driven•Does not wait for the next clock tickDoes not wait for the next clock tick•No guaranteed execution period before No guaranteed execution period before

preemptionpreemption

Scheduling Scenarios (2)Scheduling Scenarios (2)

• Voluntary switchVoluntary switch– When the running thread gives up the CPUWhen the running thread gives up the CPU

• Waiting on a dispatcher objectWaiting on a dispatcher object• TerminationTermination• Explicit lowering of priorityExplicit lowering of priority

– Schedule the thread at the head of the next non-empty Schedule the thread at the head of the next non-empty Ready queueReady queue

• Running thread experiences quantum endRunning thread experiences quantum end– Priority is decremented unless at thread base priorityPriority is decremented unless at thread base priority– Thread goes to tail of Ready queue for its new priorityThread goes to tail of Ready queue for its new priority– May continue running if no equal or higher-priority threads May continue running if no equal or higher-priority threads

are Ready – ie. it “gets” the new quantumare Ready – ie. it “gets” the new quantum

Quantum DetailsQuantum Details

• Standard quantum is two clock ticksStandard quantum is two clock ticks– 12 on NT Server12 on NT Server

• If normal-priority process owns the foreground If normal-priority process owns the foreground window, its threads may be given a longer window, its threads may be given a longer quantumquantum

• Windows 2000 has the ability Windows 2000 has the ability to choose Workstation to choose Workstation quantums on Server quantums on Server (and vice versa)(and vice versa)

Thread Scheduling StatesThread Scheduling States

• InitInit (0) – Thread is “under construction” (0) – Thread is “under construction”• ReadyReady (1) – thread eligible to be scheduled to run (1) – thread eligible to be scheduled to run• RunningRunning (2) (2)• StandbyStandby (3) – thread is selected to run on CPU (3) – thread is selected to run on CPU• TerminateTerminate (4) – thread has executed its last code, (4) – thread has executed its last code,

but must wait till all references to it are closedbut must wait till all references to it are closed• WaitingWaiting (5) – wait for one or multiple objects, after (5) – wait for one or multiple objects, after

a voluntary switcha voluntary switch• TransitionTransition (6) – thread was in a wait entered from (6) – thread was in a wait entered from

user mode for 12 seconds or more, system was user mode for 12 seconds or more, system was short of physical memory?short of physical memory?

Watching the SchedulerWatching the Scheduler

• Demo…Demo…• CPU Stress and Performance Monitor CPU Stress and Performance Monitor

– both available in Windows 2000 – both available in Windows 2000 Resource KitResource Kit

• Trying to show:Trying to show:– Priority of current threadPriority of current thread– State of current threadState of current thread

Priority AdjustmentsPriority Adjustments

• Threads in “dynamic” classes can have priority Threads in “dynamic” classes can have priority adjustments applied to them (Boost or Decay)adjustments applied to them (Boost or Decay)– Idle, Below Normal, Normal, Above Normal and High Idle, Below Normal, Normal, Above Normal and High – Carried out upon wait completionCarried out upon wait completion– Used to avoid CPU starvation (see later slide)Used to avoid CPU starvation (see later slide)

• No automatic adjustments in “real-time” classNo automatic adjustments in “real-time” class– Priority 16 and abovePriority 16 and above– Scheduling is therefore “predictable” with respect to ther Scheduling is therefore “predictable” with respect to ther

“real-time” threads“real-time” threads• Note though that this does not mean that there are absolute Note though that this does not mean that there are absolute

latency guaranteeslatency guarantees

Priority BoostingPriority Boosting

• Priority boost takes place after a waitPriority boost takes place after a wait– Occurs when a wait (usually I/O) is resolvedOccurs when a wait (usually I/O) is resolved

• Slow devices / long waits = big boostSlow devices / long waits = big boost• Fast devices / short waits = small boostFast devices / short waits = small boost

– Boost is applied to thread’s base priorityBoost is applied to thread’s base priority• Does not go over priority 15Does not go over priority 15

– Keeps I/O devices busyKeeps I/O devices busy

• After boost:After boost:– Priority decays one level, followed by another Priority decays one level, followed by another

quantum, continuing till at base priorityquantum, continuing till at base priority

CPU StarvationCPU Starvation

• Balance Set Manager looks for “starved” Balance Set Manager looks for “starved” threadsthreads– BSM is a thread running at priority 16, waking up BSM is a thread running at priority 16, waking up

once per secondonce per second– Looks at threads that have been Ready for 4 Looks at threads that have been Ready for 4

seconds or moreseconds or more– Boosts up to 10 Ready threads per passBoosts up to 10 Ready threads per pass

• Special boost appliedSpecial boost applied– Priority 15Priority 15– Quantum is doubledQuantum is doubled

• Does not apply in real-time rangeDoes not apply in real-time range

Multiprocessor SupportMultiprocessor Support

• By default, threads can run on any By default, threads can run on any available processoravailable processor

• Soft affinity (introduced in NT 4.0)Soft affinity (introduced in NT 4.0)– Every thread has an “ideal processor”Every thread has an “ideal processor”– When thread becomes ready:When thread becomes ready:

• if “ideal” is idle, it runs thereif “ideal” is idle, it runs there

• else, if previous processor is idle, it runs thereelse, if previous processor is idle, it runs there

• else, may look at next thread, to run on its ideal else, may look at next thread, to run on its ideal processorprocessor

Multiprocessor Support (2)Multiprocessor Support (2)

• Hard affinityHard affinity– Restricts thread to a subset of the Restricts thread to a subset of the

available CPUsavailable CPUs– Can lead to:Can lead to:

•threads getting less CPU time that they threads getting less CPU time that they normally wouldnormally would

•Other scheduling anomaliesOther scheduling anomalies

– Only rarely appropriateOnly rarely appropriate

Hard Affinity Example (1)Hard Affinity Example (1)Single processor, fully Single processor, fully loadedloaded

Hard Affinity Example (2)Hard Affinity Example (2)Set affinity menuSet affinity menu

Hard Affinity Example (3)Hard Affinity Example (3)Affinity settingAffinity setting

Hard Affinity Example (4)Hard Affinity Example (4)Hard affinity – one CPU Hard affinity – one CPU loadedloaded

Hard Affinity Example (5)Hard Affinity Example (5)Affinity effectsAffinity effects

Hard Affinity Example (6)Hard Affinity Example (6)Adding CPU time using Adding CPU time using PerfmonPerfmon

Hard Affinity Example (7)Hard Affinity Example (7)Perfmon affinity effectsPerfmon affinity effects