See Also: PerformanceCounter Members
The System.Diagnostics.PerformanceCounter component can be used for both reading existing predefined or custom counters and publishing (writing) performance data to custom counters.
In versions 1.0 and 1.1 of the .NET Framework, this class requires immediate callers to be fully trusted. Starting with the .NET Framework version 2.0, this class requires System.Diagnostics.PerformanceCounterPermission for specific actions. It is strongly recommended that System.Diagnostics.PerformanceCounterPermission not be granted to semi-trusted code. The ability to read and write performance counters allows code to perform actions such as enumerating executing processes and obtaining information about them.
Passing a System.Diagnostics.PerformanceCounter object to less-trusted code can create a security issue. Never pass performance counter objects, such as a System.Diagnostics.PerformanceCounterCategory or System.Diagnostics.PerformanceCounter, to less trusted code.
The predefined counters are too numerous to mention. You can get a list of the available pre-existing counters from the Windows Performance Monitor's tp://go.microsoft.com/fwlink/p/?LinkId=257854. To learn about the .NET Framework performance counters, see Performance Counters in the .NET Framework.
To read from a performance counter, create an instance of the System.Diagnostics.PerformanceCounter class, set the PerformanceCounter.CategoryName, PerformanceCounter.CounterName, and, optionally, the PerformanceCounter.InstanceName or PerformanceCounter.MachineName properties, and then call the PerformanceCounter.NextValue method to take a performance counter reading.
To publish performance counter data, create one or more custom counters using the PerformanceCounterCategory.Create(string, string, CounterCreationDataCollection) method, create an instance of the System.Diagnostics.PerformanceCounter class, set the PerformanceCounter.CategoryName, PerformanceCounter.CounterName and, optionally, PerformanceCounter.InstanceName or PerformanceCounter.MachineName properties, and then call the PerformanceCounter.IncrementBy(long), PerformanceCounter.Increment, or PerformanceCounter.Decrement methods, or set the PerformanceCounter.RawValue property to change the value of your custom counter.
The PerformanceCounter.Increment, PerformanceCounter.IncrementBy(long), and PerformanceCounter.Decrement methods use interlocks to update the counter value. This helps keep the counter value accurate in multithreaded or multiprocess scenarios, but also results in a performance penalty. If you do not need the accuracy that interlocked operations provide, you can update the PerformanceCounter.RawValue property directly for up to a 5 times performance improvement. However, in multithreaded scenarios, some updates to the counter value might be ignored, resulting in inaccurate data.
The counter is the mechanism by which performance data is collected. The registry stores the names of all the counters, each of which is related to a specific area of system functionality. Examples include a processor's busy time, memory usage, or the number of bytes received over a network connection.
Each counter is uniquely identified through its name and its location. In the same way that a file path includes a drive, a directory, one or more subdirectories, and a file name, counter information consists of four elements: the computer, the category, the category instance, and the counter name.
The counter information must include the category, or performance object, that the counter measures data for. A computer's categories include physical components, such as processors, disks, and memory. There are also system categories, such as processes and threads. Each category is related to a functional element within the computer and has a set of standard counters assigned to it. These objects are listed in the Performance object drop-down list of the Add Counters dialog box within the Windows 2000 System Monitor, and you must include them in the counter path. Performance data is grouped by the category to which is it related.
In certain cases, several copies of the same category can exist. For example, several processes and threads run simultaneously, and some computers contain more than one processor. The category copies are called category instances, and each instance has a set of standard counters assigned to it. If a category can have more than one instance, an instance specification must be included in the counter information.
To obtain performance data for counters that required an initial or previous value for performing the necessary calculation, call the PerformanceCounter.NextValue method twice and use the information returned as your application requires.
Performance counter categories installed with the dnprdnlong use separate shared memory, with each performance counter category having its own memory. You can specify the size of separate shared memory by creating a DWORD named FileMappingSize in the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<category name>\Performance. The FileMappingSize value is set to the shared memory size of the category. The default size is 131072 decimal. If the FileMappingSize value is not present, the fileMappingSize attribute value for the performanceCounters element specified in the Machine.config file is used, causing additional overhead for configuration file processing. You can realize a performance improvement for application startup by setting the file mapping size in the registry. For more information about the file mapping size, see <performanceCounters> Element.