WINDOWS 1002000 PROFESSIONAL RESOURCE KIT [Electronic resources]

Chris Aschauer

نسخه متنی -صفحه : 335/ 281
نمايش فراداده

Advanced Topic: Changing Thread Priority to Improve Performance

After observing the threads that use the greatest amount of processor time, monitor the dispatch states of the threads. This tells you which threads are running and which threads are ready. Most important, monitoring thread states on your system can help you identify which threads are piling up in the queue and which threads are actively running at various times.

The ThreadThread State counter provided by System Monitor reports the current execution state (also known as dispatch state) of a thread. System Monitor reports thread state as a numeric value from 0 through 7, corresponding to whether the thread is ready, running, terminated, and so on.

Table 29.3 lists the typical thread states.

Table 29.3 Typical Thread States

Thread State Description Comments
0 Initialized
1 Ready The thread is prepared to run on the next available processor.
2 Running
3 Standby The thread is about to use the processor.
4 Terminated
5 Waiting The thread is not ready to run, typically because another operation (for example, involving I/O) must finish before the thread can run.
6 Transition The thread is not ready to run because it is waiting for a resource (such as code being paged in from disk).
7 Unknown The thread is in an unknown state.

To determine which threads are contending for the processor, track the states of all threads in the system by using System Monitor. Figure 29.15 shows a histogram. The vertical maximum for the chart is set to 10 to make it easier to see the values; an alternative for easier viewing is to display the thread-state values in a report view.

Figure 29.15 Display of Thread States

Notice that the preceding figure plots a thread of the MMC process with the steady value of 2 for running. This is the thread of the System Monitor snap-in that is collecting the data that you are monitoring. As long as System Monitor is running, one of its threads shows as running. Other threads' state values alternate between 1 for ready and 5 for waiting.

Plotting thread-state data in a chart rather than a histogram might make it easier to view the switching of thread states. Note in Figure 29.16 how a process's thread moves from the waiting state (plotted on the chart at 5) to the ready state (plotted on the chart at 1).

Figure 29.16 Changing Thread States

To find out how long each thread remains in a particular state, define log settings with a tab-separated (TSV) or comma-separated (CSV) file format and include Thread Thread State. When the log is completed, import the file into Microsoft® Excel. A sample log is shown in Figure 29.17.

Figure 29.17 Sample Log Output Viewed in Microsoft Excel

By looking at log output, you can get an idea of the length of time that a thread remains in a state by determining the number of seconds that elapsed until the thread's state changed. However, it is important to note that, because sampling omits some data, you might not see all the state changes that occur.

In addition, the ThreadThread Wait State counter and Perfmon4.exe on the Windows 2000 Resource Kit companion CD give you information about why a thread is in a waiting state.

The value reported for Thread Wait Reason is a code. The Counters Help file on the Windows 2000 Resource Kit companion CD provides descriptions for these codes.

Examining and Adjusting Thread Priority

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 designed to optimize the scheduling of threads, you have some control over this behavior because you can 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.

Priority Class and Priority

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:

Idle. Screen savers and other processes that periodically update the display typically use the Idle class.

Normal. The default priority class for a process is Normal.

High. Processes that run in the High priority class receive the majority of processor time.

Real Time. Many kernel-mode system processes, such as those that manage mouse and keyboard input and other device operations, run in the Real Time priority class.

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 29.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 29.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

Thread Scheduling

The scheduling routines of the operating system checks for the highest-priority thread that is in a ready state and runs it without interruption during a quantum. A quantum, also known as a time slice, is 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 with the same priority are scheduled to run in a round-robin fashion, and the operating system switches among those threads in order, allowing them to run until the quantum expires.

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.

Foreground Process Scheduling

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 runs more often, and it runs longer before being preempted. By default, Windows 2000 Professional defines short, variable time slices for applications and gives a foreground application a priority boost. On the other hand, Windows 2000 Server has longer, fixed time slices 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 29.19 for an illustration.

Automatic Priority Boost

The operating system automatically boosts a thread's priority until it is high enough for a 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.

Determining and Tuning Priority

If a system has a high rate of CPU use, it is generally best to add processing power by upgrading to a faster processor or to add a processor for symmetric multiprocessing (SMP). However, if you find that a thread is consistently unable to get processor time, you can adjust its priority to allow it to run temporarily. Adjusting thread priority is not recommended as a long-term solution but is a useful illustration of the effect of thread priority on thread activity. Although this section explains how to elevate a priority to allow a process to run, you can also lower the priority of a process if you want it to run in the background while nothing else is running.

Windows 2000 and the Windows 2000 Support Tools include several utilities for monitoring the base priority of processes and threads and the dynamic priority of threads. They include:

    In Windows 2000: System Monitor and Task Manager.

    In Support Tools, which is on the Windows 2000 operating system CD: Pviewer.

CAUTION

Changing priorities might destabilize the system. Increasing the priority of a process might prevent other processes, including system services, from running. Decreasing the priority of a process might prevent it from running, not merely force it to run less frequently. In addition, lowering priority does not necessarily reduce the amount of processor time a thread receives; this happens only if it is no longer the highest-priority thread.

System Monitor

System Monitor lets you watch and record-but not change-the base and dynamic priorities of threads and processes. System Monitor has priority counters on the Process and Thread objects:

    Process Priority Base

    Thread Priority Base

    Thread Priority Current

Figure 29.18 is a chart of the base priorities of several processes. The Idle process (the white line at the bottom of the chart) runs at a priority of Idle (0), so it never interrupts another process.

Figure 29.18 Processes and Their Base Priorities

Figure 29.19 is a chart of the dynamic priority of the single thread in the Paintbrush utility (Mspaint.exe) as it changes in response to user actions. The base priority of the thread is 8 (Foreground Normal). During this period of foreground use, the dynamic priority of the thread is 12, but drops to 8 when other processes need to run.

Figure 29.19 Processes Showing Base and Current Priorities

Task Manager

Task Manager presents and lets you change the base priority of a process, but it does not monitor threads. Base priorities that you change with Task Manager are effective only as long as the process runs. For more information, see Task Manager Help or see "Overview of Performance Monitoring" in this book.

IMPORTANT

To make this change, your user account must have permission to increase scheduling priority. By default, only user accounts in the Power User groups in Windows 2000 Professional have this permission.

To change the base priority of a process

    In Task Manager, click the Processes tab.

    Right-click a process name and its menu appears.

    Click Set Priority, and then click a new base priority.

CAUTION

Problems can arise if multiple processes are running at the High priority class level. Avoid setting more than one process to run at this level.

The change in priority is effective at the next Task Manager update; you need not restart the process.

Process Viewer

Process Viewer (Pviewer.exe), one of the Support Tools on the Windows 2000 operating system CD, lets you monitor process and thread priority and change the base priority class of a process. For information about installing and using the Windows 2000 Support Tools and Support Tools Help, see the file Sreadme.doc in the SupportTools folder of the Windows 2000 operating system CD.

Start Command

When you begin processes from a command prompt by using the start command, you can specify a base priority for the processes for that run. To see all of the start options, type start /? at the command prompt.

You can change the priority class of a process to Real Time by using start /realtime at the command prompt or by using Task Manager. Note that changing the priority from /normal to /high or to /realtime can severely degrade the performance of other tasks.

CAUTION

Setting a processor-bound application to Real Time priority could cause the computer to stop responding.

Windows 2000 Configuration and Process Priority

Windows 2000 Professional is configured by default to assign variable, short time slices (quanta) to applications and to boost applications in the foreground. In contrast, Windows 2000 Server is configured to assign long, fixed quanta without any foreground boost to support the efficiency of background services. You can simulate the behavior of one type of operating system when the other type of system is installed-that is, simulate Windows 2000 Server behavior on a Windows 2000 Professional installation, or vice versa. Figure 29.20 shows the interface that you use for changing application response on an installation of Windows 2000 Professional.

To access the Performance Options dialog box

    In Control Panel, double-click System.

    Click the Advanced tab, and then click Performance Options.

    Figure 29.20 Performance Options Dialog Box in System Properties

It is easiest to see a change in thread priority as a result of resetting Performance Options if you shut down and then restart the computer. Changes in quantum type and length are so minute that they are often undetectable to the user.

Testing Priority Changes

The previous sections described changing thread priority so that, under bottleneck conditions, threads can run more efficiently. Unfortunately, when processor capacity is already stretched to its limit, boosting priorities of blocked threads might not eliminate or reduce processor bottlenecks. In such cases, it is best to add processor capacity.

Figure 29.21 shows threads of different priorities contending for processor time. It demonstrates the changing distribution of processor time among processes of different priorities as demand for processor time increases. (This test was conducted using the utility CpuStress.)

Figure 29.21 CPU Time Allocation to Threads Based on Priorities

This chart shows two threads of the same process running on a single-processor computer. Notice the values for processor time for the _Total instance and for threads 1 and 2.

Figure 29.22 shows two threads of the same process running on a single-processor computer. Figures 29.23, 29.24, and 29.25 provide more detail about how processor usage by threads of CpuStress changed in relation to each thread's priority.

In Figure 29.22, the two threads running CpuStress start out at the same low level of activity and run at the same priority-Normal.

Figure 29.22 Comparison of Threads at Normal Priority

Then, if you increase the priority of Thread 1 to Above Normal, and increase its activity level to moderate, you should notice a slight drop in CPU time for Thread 2, as shown in Figure 29.23.

Figure 29.23 Comparison of Threads at Normal and Above Normal Priority

Resetting priority for both threads to Normal while running at a higher rate of activity causes each process to consume a large share of processor time, as shown in Figure 29.24. Total processor usage is consistently and extremely high.

Figure 29.24 Comparison of Normal-Priority Threads Under Slightly Different Loads

Finally, raising the priority level of Thread 1 to Above Normal while maintaining its heightened level of activity results in a much greater allocation of processor time to the higher-priority thread and a dramatic drop in processor time for Thread 2, as shown in Figure 29.25.

Figure 29.25 Normal Threads Under Substantially Different Loads

These results demonstrate that when the processor has extra capacity, increasing the priority of one thread has little effect on the processor time allotted to each of the competing threads. However, when the processor is at its busiest, increasing the priority of one of the threads, even by one priority level, causes the higher-priority thread to get the vast majority of processor time.

In fact, when all processor time is consumed, Thread 2 might not have been scheduled at all were it not for priority boosts. Windows 2000 uses priority boosts to give processor time to lower-priority ready threads that would not otherwise be able to run. This is especially useful when a thread in low priority is waiting for an I/O operation.