PerfMon Sample: Demonstrates How to Monitor System Performance Using Performance Counters
The PerfMon sample is an introduction to performance counters. It demonstrates how to monitor local or remote system performance. It shows how to dynamically create timers, context menus, and graphic displays. It also demonstrates the following activities:
- Including new C# classes derived from other components.
- Dynamically adding and removing event handlers and components.
- Using the TreeView control, TreeNode class, and Timer component.
- Using the PerformanceCounterCategory and PerformanceCounter components.
For more information on performance counters, see PerformanceCounter Class.
Note This sample is functional only on Windows NT 4.0 and Windows 2000 and later.
Building and Running the Sample
To build and run this sample
- Open the solution PerfMon.sln.
- From the Build menu, click Build Solution.
- From the Debug menu, click Start Without Debugging.
The main application window will appear.
The following figure shows the performance monitor program running. Refer to the figure, as needed, while you go through the various performance monitoring procedures.
The Display Panel Properties
Each graphic display in the figure shows the following:
- The top of the display shows the name of the counter and the name of the instance separated by "***". If the category does not contain an instance name (for example, the ICMP category), the "***" is followed by a blank space.
- The bottom of the display shows the current counter value. This value may change periodically (the default is every second).
- The right side of the display shows a red bar. The height of the bar depends on the last value monitored on the counter.
- The graph plotted in the display shows the performance history for that counter.
To load the performance counters of a system
This application is an MDI application. It starts with four windows that can be used to monitor four computers. You can open as many windows as you want by clicking the New PerfMon command on the Options menu. In the following procedures, only one window is used. Therefore, you can close all of the windows except one.
- In the Machine Name field, type the name of the system you want to monitor. If you do not type anything, the local machine will be monitored.
- Click the Load Counters button (or press ALT+L). The application will load PerformanceCounterCategory, InstanceName, and PerformanceCounter components on the specified machine. The Load Counters button will be disabled until all of the counters on the system have been loaded.
The tree view in the left pane will contain all of the performance categories and the performance counters available. Each leaf in the tree represents a performance counter. You can monitor the completion progress by watching the progress indicator on the bottom of the application window. After the tree has been completed, the Load Counters button will be enabled again.Note To monitor a specific machine, you need administrator rights to the machine; otherwise, the PerformanceCounterCategory components will not be able to load the categories (security protection).
To monitor the performance of a system
- Load the performance counters by performing the preceding steps.
- Select one of the counters by expanding the corresponding category node. For example, to watch the performance of the processor, expand the Processor node, and then select one of the Processor instances (for example, "_Total" or "0"). Expand the instance node as well to see the counters.
- On the leaf level, the counters are represented by check boxes. Select one of the counters, for example, %Processor Time. A tab, which has the name Processor, will appear in the right pane, and a graphic display of the selected counter will appear on the tab page.
- To remove a specific graphic display from the tab page, clear the check box for the corresponding counter. When all of the counters in a specific page are cleared, the corresponding tab disappears.
To manage the displays properties
Note Each time you change any of the display properties, click the Refresh Display button to apply the changes. The changes will apply for current or future displays.
- To change the line thickness, select the thickness you want from the Line Thickness box.
- To change the number of rows/display, select the number you want from the Rows box. For example, selecting one row makes the display occupy the full height of the tab page.
- To change the number of columns/display, select the number you want from the Columns box. For example, selecting one column makes the display occupy the full width of the tab page.
Note If the number of graphic displays exceeds the visible area of the tab page, scroll bars will be created automatically.
- To change the time interval, select the number of seconds from the Frequency box.
- To change the number of points to display in the graph, select a number from the Density box.
To merge two or more displays
- Drag the display you want to move and drop it on the destination display. Notice that the background of the destination display becomes black when it is selected and is ready to receive the moved display. Both displays will merge, and two different graphic colors will appear (red and green in this case). For example, if you drop "%Processor Time *** _total" on "%User Time *** _total", the green color will represent the first and the red color will represent the second. The data shown in the display's top, bottom, and right side will only represent the "%Processor Time *** _total" counter.
- To switch from one graph to another after a display merge, right-click the display and select the name of the graph from the shortcut menu. In the preceding example, if you want to change the display data from "% Processor Time *** _total" to "%User Time *** _total", right-click the display and select "%User Time *** _total" from the shortcut menu. The display will show the data of the selected counter, and the bar color on the right side will change to red (or the color representing the selected counter).
- If you have more than two merged counters, each counter will be represented by a different color. To select the data view for any one of the counters, perform the preceding step.
To separate two or more merged displays
- Locate the counter's check box of the display you want to separate.
- Clear the counter's check box. The corresponding display will be removed.
- Select the counter's check box again. A new display will appear on the tab page.
To use menu options
- To stop the graphics plotting:
- Click the Option menu (or press ALT+O).
- Click Stop Graphics.
The graphics will freeze and no more lines will be plotted.
- To restart the graphics plotting:
- Click the Option menu.
- Click Start Graphics.
The displays will continue plotting from the last position.
- To exit the application:
- Click the Option menu.
- Click Exit.
Classes and Keywords
This sample demonstrates the following classes:
System.Windows.Forms.Form, System.Windows.Forms.TabControl, System.Windows.Forms.TabPage, System.Windows.Forms.Panel, System.Windows.Forms.ProgressBar, System.Windows.Forms.TreeView, System.Windows.Forms.TreeNode, System.Windows.Forms.MouseButtons, System.Windows.Forms.ContextMenu, System.Windows.Forms.MenuItem, System.Collection.HashTable, System.Drawing.Color, System.Drawing.Graphics, System.Diagnostics.PerformanceCounterCategory, System.Diagnostics.PerformanceCounter
This sample demonstrates the following keywords:
PerformanceCounterCategory, PerformanceCounter, Graphics, GraphicTab, HashTable, DisplayPanel, ContextMenu, MenuItem, Draw, Color, TreeView, TreeNode, ProgressBar, TabControl, TabPage