Monitoring Performance

For years, Windows NT included the Performance Monitor tool, commonly called Perfmon. This tool also exists in Windows 2000, and is handy for monitoring all manner of resources on the computer. Most common is the monitoring of the processor's busy time, but the Performance Monitor can include dozens of categories, such as Processor and FTP Server (if the FTP service was installed).

Windows includes a number of performance counters, which monitor these various aspects. The performance counters monitor what Microsoft calls performance objects, which fall into two categories. Performance objects can be physical objects, such as memory or processors; or they can be system objects, such as threads, processes, or compilations. It is these performance objects that are divided into various categories, and then you have the counters within those categories.

.NET includes a PerformanceCounter class in the System.Diagnostics namespace. This class allows you to connect to the performance counters on a computer and retrieve values in your code. The PerformanceCounter class also allows you to create your own custom counters, as you will see later in this chapter.

Accessing Performance Monitors

The PerformanceCounter class allows you to easily access performance counters on NT or Windows 2000 machines. You can use the PerformanceCounter easily by using the Server Explorer.

Create a new Windows application project and name it PerfTest. Open the Server Explorer window by hovering over the Server Explorer button next to the Toolbox button. You'll want to expand the Servers node, and then the server in which you are interested. In Figure 12.1, you can see that the server is named culaptop in this example. Next, expand the Performance Counter's node, and you'll see a list of the categories available for this server. The actual categories will vary depending on what you have installed on the server you are exploring.

Figure 12.1. The Server Explorer allows you to easily add performance counters to your application.


Expand the Processor node to see what counters are available. You'll see such counters as % Interrupt Time, % User Time, and DPC Rate. If you try to add one of the counters to the form by dragging it over, however, you'll see that it doesn't work. Instead, you'll have to expand the counter in which you are interested, and you'll see more choices. These choices are called instances, and are available to allow you to access the counter for multiple objects. For example, a computer could have more than one processor, so you'd need a way to be able to access each processor separately. There's also a _Total instance that will give you a total for all the processors. You can see two instances in Figure 12.2 for the % Processor Time counter. The zero represents the one processor on the server, and the _Total represents all the processors. On single-processor systems, of course, these are the same.

Figure 12.2. The instances of the % Processor Time counter.


Make sure that you have the instances showing for the % Processor Time counter, and click on _Total and drag it onto your form. When you do this, a new control is added to the component tray, with the name PerformanceCounter1. If you click on the control once and examine the properties, you'll see that the properties match what you'd expect given the terminology. The CategoryName is Processor, the CounterName is % Processor Time, and the Instance is _Total. The MachineName property lists the name of the server on which you are monitoring the counter.

Now, go to the Toolbox and open the Windows Form tab. Add a Label and a Timer to the form. Click once on the Timer1 control, which will appear in the component tray. Change the Interval property to 1000 and make sure that Enabled is set to True.

Now, go into the code for the form and add the following code:

Private Sub Timer1_Tick(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Timer1.Tick
   Label1.Text = PerformanceCounter1.NextValue.ToString
End Sub

This simple code takes the NextValue of the PerformanceCounter1 object and places it in the label each second. This way, you can monitor the CPU utilization of your system. Go ahead and start the application and watch it run for a few moments. Perform some actions that will tax the processor, such as starting up large applications or performing a search for files on the computer.

Understanding the Performance Counters

Looking at the percentage utilization is fairly easy to understand. However, if you look at the categories and counters available, not all are intuitive. If there are counters on which you want more information, you can find descriptions for them by starting the Performance Monitor application. You can start it in Windows 2000 by selecting the Administrative Tools menu and then selecting Performance. Or, you can simply open a command prompt, and type perfmon, and press the Enter key.

After you start the Performance Monitor application, you click on the plus sign button to add counters. This opens the Add Counter dialog box, which has a drop-down list box labeled Performance object. This box shows the available categories. If you choose System as the performance object, the available counters will appear in the list box on the left. The list box on the right will show the available instances, although there are none in the case of the System category.

Select the Context switches/sec counter. If you aren't sure what this means, click the Explain button and a new window appears beneath the Performance Monitor, as shown in Figure 12.3. This window will stay open until you explicitly close it, and it will stay in sync as you switch categories or counters. This is a handy way to find out more information about some of the counters, if the names themselves are confusing.

Figure 12.3. Clicking the Explain button in the Performance Monitor gives you a description of the various counters.


A better way to get a description of a particular performance counter is available in VS .NET, however. Choose the performance counter in which you are interested using the Server Explorer, right-click on the category of the counter, and choose View Category. This opens the Performance Counter Builder dialog box in read-only mode. This dialog box, shown in Figure 12.4, displays the category, the category description, all the counters, and the data type and description for each counter. This is the same dialog you will use in a few moments to create your own custom performance counters.

Figure 12.4. The Performance Counter Builder dialog box displaying the information for a particular category.


Instantaneous and Calculated Values

You can retrieve two types of values from counters: instantaneous and calculated. All values are samples taken from the actual object. However, instantaneous values are just that: They are a moment in time snapshot. A good example of an instantaneous value is a count; for example, the number of open file handles or the count of threads for a particular process.

Calculated values, on the other hand, require that at least two samples be present. The counter you are viewing for the processor utilization is actually a calculated value, based on the last two samples taken. This is why, when you first run the program, your very first value is zero. Most counters that include a time component are calculated, such as the Context Switches/sec you read about in the previous section.

When you use the PerformanceCounter class, two methods appear to be very similar: NextSample and NextValue. NextSample actually returns a CounterSample structure. This CounterSample contains the raw (uncalculated) value for the performance counter. This is no different from making a call to the RawValue property of the PerformanceCounter class the first time. On subsequent calls, however, you could also use the NextSample's Calculate method to calculate over the last two values, usually in a case in which you want an average to help smooth out wildly fluctuating values.

NextValue, on the other hand, returns a calculated value only. This is similar to using the NextSample.Calculate method, except that it is done for you. The return value of the NextValue method is a single-precision floating-point number, not a structure as with the NextSample method.

Adding Performance Counters Through Code

Adding performance counters to your application via the Server Explorer is fairly easy to do. However, you can also access the performance counters programmatically. The PerformanceCounter class is in System.Diagnostics, and using it allows you access to the counters just as you had with the Server Explorer.

To do the previous example using code, you can delete the PerformanceCounter1 control, and change your code to the following, making sure you change the MachineName property to the name of a machine to which you have access:

Dim pcProc As New PerformanceCounter()

Private Sub Form1_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles MyBase.Load
   pcProc.CategoryName = "Processor"
   pcProc.CounterName = "% Processor Time"
   pcProc.InstanceName = "_Total"
   pcProc.MachineName = "culaptop"
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Timer1.Tick
   Label1.Text = pcProc.NextValue.ToString
End Sub

Here, you programmatically create a PerformanceCounter object called pcProc. This is created outside any procedure to make it module-level in scope.

In the Form_Load sub, you set all the necessary information: the category, counter, instance, and server names are all set in the Form_Load sub. Then, just as in previous examples, the value is displayed in the label when the timer fires.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.116.50