1 inside the windows nt scheduler, part 1 assigning cpu time in a uniprocessor environment by mark...

26
1 Inside the Windows NT Scheduler, Part 1 Assigning CPU time in a uniprocessor environment by Mark Russinovich Windows NT Magazine - July 1997 Inside the Windows NT Scheduler, Part 2 Multiprocessor scheduling decisions can affect NT’s scalability by Mark Russinovich Windows NT Magazine - August 1997 Presented by: Jack McCrorey

Post on 22-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

1

Inside the Windows NT Scheduler, Part 1Assigning CPU time in a uniprocessor environment

by Mark RussinovichWindows NT Magazine - July 1997

Inside the Windows NT Scheduler, Part 2Multiprocessor scheduling decisions can affect NT’s scalability

by Mark RussinovichWindows NT Magazine - August 1997

Presented by:

Jack McCrorey

2

Main Topics

• Windows NT Scheduler on Uniprocessor system

• Windows NT Scheduler on Multiprocessor systems

• Options, Tradeoffs, and techniques

• Unusual Consequences

• Final thoughts

3

Windows NT Scheduling

• Basic scheduling unit is a Thread

• A thread is an executable entity that runs in the address space of a process

• Priority based scheduling

• Preemptive operating system

• No shortest job first, no quotas

4

Priority Levels

• Priorities are assigned to Threads

• Priorities are from 0 to 31– 31 is the highest; 0 is system idle thread– Realtime priorities: 16 - 31– Dynamic priorities: 1 - 15

5

Priority Assignment

• NT kernel uses 31 priority levels

• Users specify a priority class:– realtime (24) , high (13), normal (8) and idle (4)

– and a relative priority:– highest (+2), above normal (+1), normal (0), below

normal (-1), and lowest (-2)

– to establish the Starting priority

• Threads also have a current priority

6

Priorities

7

Quantum

• Determines how long a Thread runs once selected

• Varies based on:• NT Workstation or NT Server

• Intel or Alpha hardware

• Foreground/Background application threads

– NOTE: NT 4.0 increases quantum for foreground threads while NT 3.5 increased priorities

8

Thread States

Waiting

Running

Ready

New

Dispatch

Interrupt

I/O Wait

I/O Complete

Exit

Slide “borrowed” from CS502 Class Slides - Mark Claypool

9

Scheduling Decision

1 A running thread’s quantum expires

2 A running thread waits for an event to occur

3 A thread becomes ready to execute

• FindReadyThread

• FindReadyThread

• ReadyThread

10

Dispatcher Ready List

• Keeps track of all Ready-to-execute threads

• 31 entries for each priority level

• queue of threads assigned to each level

DispatcherReady List

11

10

9

8

7

Ready Threads

11

FindReadyThread

• Locates the highest priority thread that is ready to execute

• Scans dispatcher ready list

• Picks front thread in highest priority nonempty queue

– How is this different from SelectProcessToRun (last weeks code sample)? How is it the same?

12

ReadyThread

• Places threads in Dispatcher Ready List

If (ready-thread-priority > executing-thread-priority)

Preempt executing-thread

Put executing-thread back in DRL at front of queue

(this thread has some remaining quantum to run)

Else

Put ready-thread in DRL at back of queue

13

Boosting and Decay

• Priority of Dynamic range threads are boosted in some situations– Usually when an event happens that a blocked

thread is waiting on– Realtime priorities are not boosted– Boosts never exceed priority 15

• Boost decreases by one for each quantum

• Decays to starting priority (no lower)

14

Starvation Prevention

• Low priority threads may never execute

• NT has an “anti-CPU starvation policy” for threads that have not executed for 3 seconds– boosts priority to 15– doubles quantum– calls ReadyThread

• Decay is swift not gradual after this boost

15

Multiprocessor Systems

• Scheduler can choose among several CPUs

• NT supports symmetric multiprocessing

• OS kernel can run on any CPU– Simultaneously on several CPUs!!

• CPUs have own cache

• Equal access to main memory and I/O

16

SMP

17

Protecting Data

• NT kernel protects shared data structures with locks

• The DRL is a core data structure that NT protects with a lock

• If lock is not available, the CPU waits

• Scheduler will never run simultaneously

18

SMP Scheduling

• Situations requiring scheduling are the same

• One new Ready-to-execute situation– One CPU pulls thread off before quantum is

exhausted– Thread is still eligible to execute (on another

CPU)– Call ReadyThread

19

Affinity and Ideal Processor

• Soft Affinity - Scheduler tries to run thread on CPU it last executed on

• Hard Affinity - thread is assigned a list of CPUs that in can run on

• Ideal Processor - every thread is assigned an ideal CPU

• Scheduler tries to run thread on Ideal CPU

• Preferred choice from Hard affinity list

20

FindReadyThread -SMPAcquire DRL Lock

Find highest priority non-empty queue

Mark first thread with Hard Affinity = CPU as Primary candidate

If Primary-Soft-affinity = CPU OR Ideal-Processor = CPU OR Time-since-last-execute > 20 ms

Choose Primary as next thread to execute

Else

Look down queue for 1st thread that satisfies above

If new-thread is found

Choose it

Else

Choose Primary

Release DRL Lock

21

Skipping a thread

22

ReadyThread - SMP

Acquire DRL lock

If Idle-CPU = ready-thread-Hard-Affinity-List

signal Idle-CPU to execute thread

Else

Examine executing-thread-priority on ready-thread-IdealCPU

If ready-thread-priority > execute-thread-priority

signal CPU to stop current and execute this thread

Else

place ready-thread on DRL

Release DRL lock

23

High priority thread must wait

24

Thread Migration

25

Alternative Approach

Soft - Affinity Priority Based

26

Final Thoughts

• Microsoft claims high SQL Srv. throughput

• Author claims NT scheduling tuned for this

• Theoretical problem shown

• No measurements, proofs, scenarios

• What is a “good” scheduler?– Fair, Efficient, Responsive, Turnaround Time, and

Throughput