Inside the Windows nt scheduler, Part 1




Download 37.84 Kb.
Sana22.07.2021
Hajmi37.84 Kb.

In my July column, "Inside the Windows NT Scheduler, Part 1," I described the basics of threads, priorities, and uniprocessor scheduling on Windows NT 4.0. I presented definitions for threads and processes, and provided an overview of NT's thread priority scheme. This month, I'll finish my examination of NT scheduling by presenting the algorithms NT employs on multiprocessors.
The priority scheme and scheduling basics that I described last month apply to multiprocessor systems as well. However, in most cases on multiprocessor systems, the scheduler can choose among several CPUs to schedule a given thread. Deciding which CPU to pick so that NT uses the processors to their full potential is a complex problem, as you'll see.
In writing this article, I assumed that you are up to speed with the terminology I introduced last month. If you are not familiar with basic scheduling terms and concepts (e.g., you don't know what the Dispatcher Ready List is), I suggest you read last month's column before digging into this one.
NT and Multiprocessing

From the outset, Microsoft designed NT with multiprocessing in mind. The type of multiprocessing that NT supports is symmetric multiprocessing (SMP). In an SMP system, all the CPUs are identical, the operating system (NT) can run on any of the CPUs, and all the CPUs have equal access to main memory and peripheral devices. The opposite of an SMP system is a system in which the CPUs are different, the CPUs have private memory or devices, or the CPUs have priorities such that the operating system runs on only certain CPUs. Figures 1a and 1b (page 178) contrast an SMP system with an example of a non-SMP (an asymmetric multiprocessing--ASMP) system.


Figure1A depicts an SMP system. The CPUs are identical, all the CPUs have private Level 1 (L1) and Level 2 (L2) caches (the L1 cache usually resides on the CPU chip), and all the CPUs have equal access to main memory and the I/O subsystem. The NT kernel can run on any of the CPUs; it can even run simultaneously on several CPUs. Figure1B presents an example ASMP system with one master CPU. The master CPU, which runs the operating system and is the only CPU that can access the I/O subsystem, farms out work to the slave CPUs.
Microsoft designed NT's internal data structures and code to support up to 32 processors. However, NT Workstation enables a maximum of 2 processors, and NT Server enables a maximum of 4 processors, regardless of how many CPUs the system contains. Microsoft imposes these limitations with Registry values, which the NT kernel queries during processor initialization to determine the upper limit of CPUs to conFigure. This process involves two values that cross-reference each other, and the system monitors one value to prevent tampering. If a system has more than 4 processors, the vendor must license the OEM version of NT, which is nothing more than NT with modified setup files that initialize the Registry with values appropriate to the system during NT's installation. As NT enables processors, it assigns them numbers (starting with 0) to identify them internally.
To prevent corruption that might occur if multiple instances of the kernel modified the same data simultaneously, the NT kernel protects its shared data structures with locks. Typically, only one kernel instance (CPU) at a time can write to, or read from, core data structures. Before accessing a data structure, a CPU must acquire a lock that protects the data structure; after completing the access, the CPU must release the lock. If the lock is not available when the CPU requests it, the CPU waits.
An example of a core data structure that NT protects with a lock is the Dispatcher Ready List, which I described last month. Locking ensures that even though more than one processor might require a scheduling operation, the scheduling code will never be running on more than one CPU at a time. Other processors that require scheduling must wait until they can acquire the lock protecting the Dispatcher Ready List before executing the scheduling algorithms.
Although SMP systems contain more than one processor, the situations on SMP systems that require scheduling operations are the same as the situations that require scheduling operations on uniprocessor systems. You'll recall from last month that several situations invoke the scheduler. If a thread's turn on the CPU expires, if a thread blocks (i.e., waits for an event to occur), or if a thread yields its turn on a processor because the thread has terminated, the scheduler uses the FindReadyThread routine to find a new thread to execute on the CPU.
A thread that becomes ready to execute also invokes the scheduler. One example of a ready-to-execute thread is a thread that wakes up after waiting for an event to occur. Another example is a thread that NT pulls off a CPU because the thread's turn has ended--the thread is still eligible to execute, so the scheduler might move it to another CPU. The algorithm the scheduler executes in ready-to-execute situations is ReadyThread.
The multiprocessor FindReadyThread and ReadyThread functions have the same names as the functions that perform these tasks in the uniprocessor case. To make NT run as efficiently as possible on uniprocessors, Microsoft builds a special version of NT with most of the multiprocessor support code removed. One source code file contains both the uniprocessor and multiprocessor implementations of FindReadyThread and ReadyThread; the source code conditions the cores of the routines on the version of NT that's being compiled.
Affinity and Ideal Processors

Before launching into the details of FindReadyThread and ReadyThread, I want to discuss a few scheduling goals of the multiprocessor NT scheduler. The scheduler will always try to schedule a thread using soft affinity; that is, the scheduler attempts to assign the thread to the processor it last executed on.


NT implements soft affinity to take advantage of the possibility that some of the thread's data from the last time it ran on a processor is still in that processor's cache. In most cases, the next thread's data fills the cache, and the optimization isn't effective. When a new thread with higher priority than a running thread becomes ready to execute, the scheduler makes a decision based on a trade-off. The scheduler weighs the potential benefit of scheduling the new thread on the processor against the overhead that the pulled thread incurs when it moves to a new processor and loses its cached data.
A thread can also have hard affinity, which the application designer typically defines. (You can also use system utilities to set the hard affinity of all a process' threads.) The hard affinity of a thread is essentially a list of processors that the thread can execute on--the scheduler will never schedule a thread on a nonlisted processor.
Why would anyone want to use hard affinity? In some cases, you can improve overall performance if you distribute the threads of different applications (or even different threads of the same application) among different processors. For example, you can assign threads from Internet Information Server (IIS) to one processor and give SQL Server threads to all other processors. This assignment prevents IIS threads from competing for processors with the SQL Server threads, and the number of processors you assign to each application can reflect the relative priority of these applications with respect to the goals of the system you're managing. Divvying up processors using hard affinity is known as hard partitioning.
Because hard affinity is an all-or-nothing proposition, it can negatively affect performance in situations where the processors assigned to a particular application are busy and processors not in the hard-affinity list of the application's threads sit idle. NT 4.0 introduces a compromise: A programmer can assign an ideal processor to a thread.
The scheduler treats a thread's ideal processor much as it uses soft affinity: The scheduler tries to schedule a thread on its ideal CPU, but if that CPU is busy with a higher-priority thread, the scheduler looks at other processors in the thread's hard-affinity list. By using ideal processor settings, an application designer gives the scheduler hints about where threads need to run. Thus, the designer soft partitions the application.
Picking the Next Thread to Execute

Now that we're past some terminology, let's look at the FindReadyThread algorithm that NT uses to choose a new thread to execute on a particular CPU. FindReadyThread always executes on the CPU that's searching for the next thread to run. After acquiring the Dispatcher Ready List lock, FindReadyThread scans the Dispatcher Ready List for the highest-priority nonempty queue. FindReadyThread marks the first thread in the queue that has hard affinity for the CPU as the primary candidate for execution.


If the primary candidate has soft affinity for the CPU, if the CPU is the primary candidate's ideal processor, or if more than 20 milliseconds (on x86 systems) have elapsed since the primary candidate last executed, FindReadyThread chooses the primary candidate as the next thread to execute on the CPU. If the primary candidate doesn't satisfy any of these conditions, FindReadyThread looks down the queue for the first thread that does. If the algorithm finds an appropriate thread, the scheduler assigns that thread instead of the primary candidate; otherwise, the scheduler assigns the primary candidate to execute on the CPU. After the scheduler finishes its manipulation of the Dispatcher Ready List, it releases the list's lock.
As an example of FindReadyThread at work, consider the Dispatcher Ready List shown in Figure2. The scheduler must find a thread to run on CPU 1. The highest-priority queue with threads in it is the priority 10 queue. Thread 1 is the first thread in the queue, and it has hard affinity for CPU 1; thus, FindReadyThread marks Thread 1 as the primary candidate. However, Thread 1 does not satisfy any other condition to make it the immediate choice: Thread 1 didn't last run on CPU 1, its ideal processor is not CPU 1 (it doesn't even have an ideal processor), and it last ran 10 milliseconds (ms) ago. Therefore, FindReadyThread proceeds to scan the rest of the queue and comes across Thread 2, which also has hard affinity for CPU 1. Thread 2 satisfies the condition that it last ran on CPU 1, so Thread 2 is the scheduler's choice. Thus, the scheduler's soft-affinity goal causes it to favor Thread 2 over Thread 1.
Making a Thread Ready to Execute

When a thread becomes ready to execute, the scheduler must determine whether to place the thread in the Dispatcher Ready List or schedule it for immediate execution. On a uniprocessor system, this choice is straightforward: Given an executing thread and a thread to be scheduled, the scheduler sends the thread with the higher priority to the CPU and places the other thread in the Dispatcher Ready List.


On an SMP system, the decision is more complicated because the scheduler must consider multiple CPUs (unless the thread's hard affinity stipulates only one processor). ReadyThread, the algorithm responsible for this complex evaluation, uses soft affinity as its primary guide. After acquiring the Dispatcher Ready List lock, ReadyThread looks to see whether any idle processors (i.e., processors not executing instructions from an active thread) appear in the thread's hard-affinity list. If so, the scheduler's work is done; it signals the idle processor to execute the thread. (The scheduler picks the CPU it's running on if that CPU is a candidate; a CPU can be idle even though it's executing the scheduler because the scheduler executes in a nonthread-specific context).
If no processors are idle or the thread cannot execute on any thread, Ready-Thread looks at one other processor. If the thread has an ideal processor, the scheduler considers that CPU; otherwise, the scheduler examines the last processor that the thread executed on. If the thread has a priority that equals or exceeds the priority of the thread running on the CPU in question, the scheduler signals the CPU to stop executing its current thread and execute the new thread. In the case where the thread's priority is lower than the priority of the running thread, ReadyThread places the thread in the appropriate priority queue of the Dispatcher Ready List and releases the list's lock.
Unusual Consequences

If no processors are idle and the ready-to-execute thread has a lower priority than the thread running on the ready-to-execute thread's ideal or soft-affinity processor, ReadyThread can produce some unusual consequences because it doesn't evaluate other processors in the system. Consider the scenario in Figure3. A priority 9 thread (Thread 0) has just become ready to run, and even though it has a higher priority than the threads running on three of the four CPUs in the system, the scheduler places Thread 0 in the Dispatcher Ready List. Why? Thread 0's priority is lower than the priority of the thread running on the CPU that Thread 0 last executed on (Thread 1 on CPU 0 has priority 10). Thus, until FindReadyThread picks Thread 0 from the Dispatcher Ready List, the system's second highest-priority thread will wait for a processor.


ReadyThread can also potentially lead to needless movement (migration) of threads from one processor to another. Consider the variation of Figure3's scenario that Figure4 depicts. In this example, Thread 1 executing on CPU 0 has priority 8 instead of 10. ReadyThread will pull Thread 1 off CPU 0 in favor of Thread 0 because Thread 0 has a higher priority (9). Because Thread 1 is still ready to execute, NT calls ReadyThread for Thread 1. ReadyThread sees that Thread 1 has a lower priority than the thread executing on Thread 1's last processor (CPU 0); therefore, ReadyThread places Thread 1 in the Dispatcher Ready List.
If the next event that triggers FindReadyThread is the expiration of Thread 4's quantum, FindReadyThread pulls Thread 1 off the Dispatcher Ready List to execute on CPU 3 and puts Thread 4 on the list. At this point, Thread 1 has migrated from CPU 0 to CPU 3. Thread 4 might then migrate to CPU 1 or CPU 2 when one of them next invokes FindReadyThread.
NT could avoid unnecessary thread shuffling if ReadyThread considered only the system's lowest-priority executing thread in its scheduling decision, rather than using a soft-affinity strategy. Compare the thread movements diagrammed in Figure5a and Figure5b, in which threads with priorities 9, 8, and 7 are running on three CPUs. In the worst case for the soft-affinity strategy, a priority 10 thread that becomes ready-to-execute could set off all the shuffling shown in Figure5a. Alternatively, checking only the lowest-priority executing thread causes just the few movements shown in Figure5b.
The lowest-priority executing thread-scheduling strategy is simpler than the soft affinity-based ReadyThread algorithm and can eliminate many unnecessary thread migrations. The Solaris operating system uses this simpler algorithm, but Microsoft defends its use of soft affinity as a primary scheduling parameter by stating that SQL Server achieves higher throughput with this implementation.
Scalability Ramification

Microsoft bases NT's scheduling algorithms for SMP systems on the same principles it incorporates for NT's uniprocessor algorithms. However, efficiently using multiple processors complicates the scheduling problem.


Having more than one processor that the NT scheduler can assign a thread to leads to some interesting trade-offs in the ReadyThread algorithm--trade-offs that could potentially hinder NT's ability to effectively use extra processors that you add to a system (scalability). Because Microsoft has apparently decided that it will measure the scalability of NT by the performance of SQL Server, Microsoft has tuned NT's scheduling algorithms for this database application.

Assigning CPU time in a uniprocessor environment


Windows NT is a preemptive multithreading operating system. That is, NT lets several programs run simultaneously and switches among them often enough to create the illusion that each program is the only program running on the machine. Well, that's the idea anyway. How to smoothly share one CPU (or multiple CPUs) among many threads of control is a complicated problem. Solving this problem dynamically many times per second is the job of the NT scheduler. The NT scheduler must honor the relative priorities that the application's programmers designate for each thread and attempt to provide responsiveness to user-interactive threads.
In this first part of a two-part series about the algorithms NT's scheduler employs, I'll introduce basic information about the NT scheduler. (For an overview of how NT schedules applications to run, see Christa Anderson, "Foreground Application Handling in NT 4.0," June 1997.) You'll find out about the priority levels that NT assigns to threads, how Win32 programs specify priorities for their threads, the situations that invoke the scheduler, and the algorithms NT uses on uniprocessors in those situations. I'll wrap up with a discussion of some advanced features of the scheduler, including priority boosting and starvation prevention. Next month, I'll provide an in-depth tour of how the NT scheduler implements multiprocessor scheduling.
Threads and Priorities

The basic scheduling unit in NT is a thread. A thread is a point of control within a process. Processes consist of a virtual address space that includes executable instructions, a set of resources such as file handles, and one or more threads that execute within its address space. Typical applications consist of only one process, so program and process are often used synonymously. Most programs today are single-threaded, which means they run as one process with one thread. However, multithreaded programs are becoming more commonplace. An example of a multithreaded program is a program that lets a user sort a list, with an option to cancel. One thread in the program's process might perform the CPU-intensive sorting task while another thread in the process displays a how-to-cancel message to the user and waits for a response. The scheduler does not differentiate between threads of different processes. Instead, the scheduler examines the priorities of all the threads ready to run at a given instant to pick which thread to execute.


NT assigns each thread a priority number from 1 to 31, where higher numbers signal higher priorities. (NT uses priority 0 for the system idle thread, which executes when no other thread is able to.) NT reserves priorities 16 through 31 (realtime priorities) for use by time-critical operations. Only a user with Administrator privileges can direct the system to execute threads in this range. NT uses priorities 1 through 15 (dynamic priorities) for the program threads of typical applications (e.g., Notepad, Word, Lotus Notes).
The NT kernel provides functions that let you set a thread to any of the 31 priority levels, but the Win32 API is more indirect. In Win32, specifying a thread's priority is a two-step process. You must first set the priority class of the process; then, you can apply a relative priority to individual threads.
A process priority class is a priority level around which NT lets the process' threads execute. The Win32 API defines four priority classes: realtime, high, normal, and idle. These names correspond to priority levels 24, 13, 8, and 4. Setting a process priority class causes all the threads of that process to begin executing at priorities within ±2 of the class priority. This scheme is shown in Figure 1, page 168. New processes inherit the priority class of their parent. Process threads start at the priority level associated with their process' priority class.
The relative priorities that can change a thread's priority from its process class priority are highest, above normal, normal, below normal, and lowest. Highest adds 2 to the thread's priority, above normal adds 1, normal adds 0, below normal adds -1, and lowest adds -2. Figure 2, page 168, shows the relative priorities applied to the Normal priority class range.
The Win32 API includes two special-case priority modifiers: time-critical and idle. Time-critical moves a dynamic thread's priority to the top of the dynamic range (15), and idle moves it to the bottom (1). Similarly, time-critical and idle move realtime threads to the top (31) and bottom (16) of the realtime range.
Whose Turn Is It?

Threads must take turns running on the CPU so that one thread doesn't prevent other threads from performing work. One of the scheduler's jobs is to assign units of CPU time (quantums) to threads. A quantum is typically very short in duration, but threads receive quantums so frequently that the system appears to run smoothly--even when many threads are performing work. One difference between NT Server and NT Workstation is the length of a user thread's quantum. On most x86 systems running NT Server, a quantum is 120 milliseconds (ms). On x86 systems running NT Workstation, a quantum can be 20ms, 40ms, or 60ms, depending on your system settings and whether the thread is a background or foreground application thread (more on this topic later).


The scheduler must make a CPU scheduling decision every time one of three situations occurs:
* A thread's quantum on the CPU expires.
* A thread waits for an event to occur.
* A thread becomes ready to execute.
When a thread's quantum expires, the scheduler executes the FindReadyThread algorithm to decide whether another thread needs to take over the CPU. If a higher-priority thread is ready to execute, it replaces (or preempts) the thread that was running.
In many cases, threads perform processing and then wait for an event to occur. For example, a client/server application might have a server thread that performs processing when it receives client requests and then waits for more requests. A waiting (or blocked) thread gives up its quantum early, and the scheduler must execute the FindReadyThread algorithm to find a new thread to run.
When a new thread or a blocked thread becomes ready to execute (e.g., when the client/server application server thread receives another client request), the scheduler executes the ReadyThread algorithm. This algorithm determines whether the ready thread will take over the CPU immediately or be placed in an eligible-to-execute list.
FindReadyThread and ReadyThread are the key algorithms the NT scheduler uses to determine how threads take turns on the CPU. The uniprocessor versions of FindReadyThread and ReadyThread are straightforward algorithms. Let's examine how FindReadyThread and ReadyThread work.
FindReadyThread. FindReadyThread locates the highest-priority thread that's ready to execute. The scheduler keeps track of all ready-to-execute threads in the Dispatcher Ready List. The Dispatcher Ready List contains 31 entries, each of which corresponds to a priority level and a queue of threads assigned to that priority level. The FindReadyThread algorithm scans the Dispatcher Ready List and picks the front thread in the highest-priority nonempty queue. Figure 3 shows an example Dispatcher Ready List with three ready threads--two at priority 10 and one at priority 7. FindReadyThread directs the scheduler to choose the first thread in priority 10's queue as the next thread to run.
ReadyThread. ReadyThread is the algorithm that places threads in the Dispatcher Ready List. When ReadyThread receives a ready-to-execute thread, it checks to see whether the thread has a higher priority than the executing thread. If the new thread has a higher priority, it preempts the current thread and the current thread goes to the Dispatcher Ready List. Otherwise, ReadyThread places the ready-to-execute thread in the appropriate Dispatcher Ready List. At the front of the queue, ReadyThread places threads that the scheduler pulls off the CPU before they complete at least one quantum; all other threads (including blocked threads) go to the end of the queue.
Boosting and Decay

The picture I've presented so far is of a fairly static system: Threads execute at a priority level until a program changes their priorities or they exit. What actually happens is more dynamic: In a variety of situations, NT boosts (or increases) the priority of dynamic range threads. The most common boost occurs when an event happens that a blocked thread was waiting for. For example, a thread waiting for input from the keyboard increases six priority levels (a 6-point boost) when a keystroke wakes it up. Other increases include a 6-point boost for mouse events and a 1-point boost when a thread wakes up from a wait on a general event.


Boosting applies to only dynamic range threads. The system never changes the priority of a realtime thread--only a program can change a realtime priority. In addition, a boost never causes a thread's priority to move into the realtime range; priority level 15 is the upper limit for boosts. Event-related boosts are temporary because the boost decays over time. Each time a thread runs through an entire quantum, its boost decreases by 1 point. This decay continues until the thread reaches its programmed priority level (the priority it had before its first boost).
NT's boosting logic lets the system boost a thread repeatedly before its priority has decayed to its base priority. Thus, a priority 8 thread that receives keyboard input gets boosted to priority 14. If the thread completes a quantum, its priority decays to 13. If the thread waits for and receives another keyboard event, its priority gets boosted to the 15 limit.
Another type of boost NT Workstation applies is a foreground application boost, which you can control from the Performance tab of the System applet in Control Panel (shown in Screen 1). This type of boost affects quantum length, rather than priority. For the default Maximum setting, NT extends the quantums of foreground application threads to 60ms. If you position the slider in the middle, NT sets the quantums to 40ms. If you position the slider on None, the quantums are 20ms--the same as the quantums of background application threads.
Starvation Prevention

Left alone, the FindReadyThread and ReadyThread might prevent low-priority threads from getting a chance to execute. For example, a priority 4 thread running on a system with continuously running priority 8 threads would be starved for CPU time. However, NT provides a mechanism that gives low-priority threads a shot at the CPU. The NT Balance Set Manager is a system thread that wakes up every second or so to perform memory tuning. As a secondary responsibility, Balance Set Manager executes the ScanReadyQueues algorithm, which implements NT's anti-CPU starvation policy.


ScanReadyQueues scans the Dispatcher Ready List, working down the list from priority 31. It looks for threads that haven't executed in more than 3 seconds. When it finds one, ScanReadyQueues gives the thread a special anti-starvation boost, doubles its quantum, and calls ReadyThread with the thread as a parameter. The anti-starvation boost differs from other boosts: Instead of applying a relative priority increment, the anti-starvation boost slams the thread's priority to the top of the dynamic range. (On pre-Service Pack 2--SP2--systems, the anti-starvation boost was to priority 14; post-SP2 systems boost to priority 15). When a thread that receives an anti-starvation boost finishes its extended quantum (or the thread blocks), its priority returns to the pre-starvation boost level and its quantum returns to its usual length.
Next Month

Scheduling in a uniprocessor environment is relatively straightforward, but factors within a multiprocessor environment complicate how FindReadyThread and ReadyThread work. For example, NT lets applications define threads to execute on only certain CPUs, and NT tries to keep threads running on the same CPU for performance benefits. Next month, I'll describe the multiprocessor implementations of FindReadyThread and ReadyThread. These algorithms are complex--so complex that you might argue that a better way must exist for scheduling in a multiprocessor environment. Stay tuned.
Download 37.84 Kb.




Download 37.84 Kb.

Bosh sahifa
Aloqalar

    Bosh sahifa



Inside the Windows nt scheduler, Part 1

Download 37.84 Kb.