Analyzing Processor Activity |
Examining thread context switching and thread state gives you information about when threads in a bottleneck are being scheduled to run by the operating system and when threads are being held in the queue prior to running. Although the operating system is inherently designed to optimize the scheduling of threads, you have some control over this behavior, to adjust for situations in which scheduling behavior on your system is unsatisfactory. This section describes how you can determine a thread's scheduling priority and how you can adjust thread priority to reduce bottlenecks and allow blocked threads to run.
Important
To ensure optimum performance on production systems, Microsoft recommends that you adjust priorities of processes first in a test environment. In addition, you should make these adjustments only if you have an in-depth understanding of priority settings and their effect on other processes and the operating system.
Under the preemptive multitasking strategy built into Windows 2000, threads and processes are assigned a priority for scheduling purposes. A thread's priority determines the order in which it is scheduled to run on the processor.
A thread's priority is based on the priority class of its parent process. The four process priority classes are:
Each process's priority class sets a range of priority values (between 1 and 31, where 1 is lowest and 31 is highest), and the threads of that process have a priority value that is within that range. (Priority 0 is reserved for system use.) If the priority class is Real Time (priorities 16 through 31), the thread's priority cannot change while the thread is running. If you have at least one priority 31 thread running, other threads cannot run.
On the other hand, threads running in all other priority classes are variable, meaning that the thread's priority can change while the thread is running. For threads in the Normal or High priority classes (priorities 1 through 15), the thread's priority can be raised or lowered by up to a value of 2 but cannot fall below its original, program-defined base priority. When the base priority is adjusted to optimize scheduling, the resulting value is called the thread's dynamic priority.
Table 7.4 associates each process priority class with relative thread priorities, ranked from highest priority to lowest. Notice that the highest priority class is Real Time and the lowest is Idle.
Table 7.4 Process Priority Classes With Relative Thread Priorities
Process priority classes | ||||
---|---|---|---|---|
Thread priorities | Real time | High | Normal | Idle |
Time critical | 31 | 15 | 15 | 15 |
Highest | 26 | 15 | 10 | 6 |
Above normal | 25 | 14 | 9 | 5 |
Normal | 24 | 13 | 8 | 4 |
Below normal | 23 | 12 | 7 | 3 |
Lowest | 22 | 11 | 6 | 2 |
Idle | 16 | 1 | 1 | 1 |
The scheduling routines of the operating system run the highest-priority ready thread uninterrupted during a quantum (also known as a time slice—the maximum amount of time a thread can run before the system checks for another ready thread of the same priority to run). If a higher-priority thread becomes ready during the quantum, the lower-priority thread is interrupted and the higher-priority thread is run. Otherwise, threads running at the same priority are scheduled in a round-robin fashion, switching among threads in order, and run until the quantum expires.
In general, Windows 2000 always runs the highest-priority ready thread. However, there are optimization strategies built into the operating system to address situations in which the default scheduling methods would cause problems. The following sections describe these strategies.
The scheduler runs a foreground process at a higher priority, which means it tends to get more time slices than background processes. In addition, the scheduler ensures that those time slices are longer than the ones allocated to background processes. As a result, the foreground process is much more responsive than other processes because it is scheduled more often, and when it is scheduled it continues running longer before being preempted. By default, Windows 2000 Professional defines short, variable quanta for applications and gives a foreground application a priority boost. On the other hand, Windows 2000 Server has longer, fixed quanta with no priority boost for foreground applications, allowing background services to run more efficiently. To see foreground process scheduling in action, monitor the processor time for a process and move its window to the bottom of the stack. Note that the time value allocated to that process falls immediately. Then move the process to the top of the stack and note that the processor time value rises immediately. See Figure 7.18 for an illustration.
The operating system automatically boosts the thread's priority enough for the low-priority thread to complete its operation and release the resource. After raising a thread's dynamic priority, the scheduler reduces that priority by one level each time the thread completes a time slice (quantum), until the thread drops back to its base priority.
For information about how you can change thread priority, see "Determining and Tuning Priority" later in this chapter.