Partilhar via


Introduction to Monitoring Performance Thresholds 

The mechanism by which Windows collects performance data on various system resources is the performance counter. Windows contains a pre-defined set of performance counters with which you can interact; some of these are found on all Windows 2000 computers and some are custom to specific applications and are found only on certain computers. Each counter is related to a specific area of system functionality. Examples include counters that monitor a processor's busy time, memory usage, or the number of bytes received over a network connection. You use an instance of the PerformanceCounter component to connect directly to existing performance counters and interact with their data in dynamic ways.

A performance counter monitors the behavior of performance objects on a computer. These include physical components, such as processors, disks, and memory, and system objects, such as processes and threads. In Visual Studio and the .NET Framework SDK, these performance objects are shown as categories; system counters related to the same performance object are grouped into categories that indicate their common focus. When you create an instance of the PerformanceCounter component, you first indicate the category with which the component will interact and then choose a counter to interact with from within that category.

For example, a category of counters on Windows is the Memory category. System counters within this category track data such as bytes available and bytes cached. To deal with bytes cached in your application, you create an instance of the PerformanceCounter component and connect it to the Memory category, then pick the appropriate counter in that category (Cached Bytes, in this case).

In some situations, categories are further subdivided into instances. Instances track data about multiple occurrences of the object that a category relates to. It is important to note that instances apply to the category as whole, rather than to individual counters. All counters within a category have each instance defined for the category. For example, the Process category contains instances called "Idle" and "System." Each counter within the Process category specifies data in these two ways, showing information about either idle processes or system processes. The structure of the category and counters looks like this:


Categories, Instances, and Counters

Visual Basic Categories Instances

As you can see, the Process category contains two instances, so both counters in the category also contain instances of Idle and System. For more information on instances, see How to: Add and Remove Performance Counter Instances.

Although your system makes available many more categories of counters, the default categories you'll probably interact with most frequently include the Cache, Memory, Objects, PhysicalDisk, Processor, Server, System, and Thread categories.

Performance Counter Data

Performance counters record values about various parts of the system. These values are not stored as entries, but are persisted for as long as a handle remains open for the given category in memory. The process of retrieving data from a performance counter is called sampling. When you sample, you either retrieve the immediate value of a counter or a calculated value.

Depending on how a counter is defined, its value might be the most recent aspect of resource utilization, also called the instantaneous value, or it might be the average of the last two measurements over the period of time between samples. For example, when you retrieve a value from the Process category's Thread Count counter, you retrieve the number of threads for a particular process as of the last time this was measured. This is an instantaneous value. However, if you retrieve the Memory category's Pages/Sec counter, you retrieve a rate per second based on the average number of memory pages retrieved during the last two samples.

Resource usage can vary dramatically based on the work being done at various times of day. Because of this, performance counters that show usage ratios over an interval are a more informative measurement than averages of instantaneous counter values. Averages can include data for service startup or other events that might cause the numbers to go far out of range for a brief period, thereby skewing results.

The PerformanceCounter component provides facilities for the most common Windows performance monitoring requirement, namely, connecting to an existing counter on the server and reading and writing values to it. Additional functionality, such as complex data modeling, is available directly through Windows Performance Monitor. For example, you can use Performance Monitor to chart the data a counter contains, run reports on the data, set alerts, and save data to a log.

Working with Performance Counters in .NET Applications

Using the PerformanceCounter component, you can connect to the existing counters installed on the servers to which you have access, or you can create your own custom performance counters. You can read data from any counter, and you can write to your own custom counters.

When you connect to an existing performance counter, you do so by specifying the computer on which the counter exists, the category for the counter, and the name of the counter itself. Additionally, you have the option of specifying the instance of the counter you want to use, if the counter contains more than one instance. You can then read any and all data from the counter. You can also enumerate the existing categories, counters, and instances on the computer by using code, or you can use Server Explorer to see a list of existing counters on the computer.

When you create a new counter, you must specify a completely new category for your local computer. You cannot add a new counter to an existing category or create new counters on a remote computer. However, you can write data to and read data from any custom counters you create.

You can expand the Performance Counters node in Server Explorer to see a list of the counters that exist on that server (by category) and the instances by which they are classified. You can also drag a specific performance counter or instance from Server Explorer to your Component Designer in Microsoft Visual Studio to create an instance of the PerformanceCounter component that automatically has its properties set to point to the selected counter.

If you are using a PerformanceCounter component in an ASP.NET application, the default settings of the ASPNET user account restrict access to performance counters. The ASPNET user account, by default, can write to but not read from performance counters, and it cannot create new categories. You can use impersonation with the ASPNET account to allow creation of new categories. The impersonation identity must have sufficient privileges to create categories. If your application needs performance counters that can be specified before deployment, they can be created by the deployment project. For more information, see ASP.NET Web Application Security.

NoteNote

You can create a new counter category in Server Explorer by right-clicking the Performance Counters node and choosing the appropriate menu command. You can also edit an existing category by right-clicking its specific node.

For more information, see Server Explorer/Database Explorer.

Restrictions

In this release, you may have to restart the Performance Monitor (Perfmon.exe) that is installed with Windows 2000 when you create custom performance counters before you can see the custom counter in that application.

The PerformanceCounter class is not fully supported on Microsoft Windows NT version 4.0. You can read from the system counters, but you cannot create, write to, or delete custom counters.

See Also

Concepts

Performance Counter Data Analysis
Performance Counter Programming Architecture
Performance Counter Lifetime
Performance Counter Types