I. Overview
Performance monitoring is a system function provided by Windows NT. Windows NT has always integrated a performance monitoring tool that provides information about the current operating status of the operating system and provides hundreds of performance counters for various objects. A performance object is a Monitored object. Typical examples include Processor, Process, Memory, TCP/UDP/IP/ICMP, and PhysicalDisk. Counters generally provide performance-related information about operating systems, applications, services, and drivers to analyze system bottlenecks and diagnose and Optimize System and Application Performance. The Performance counter mechanism allows applications and operating system components to Monitor Performance applications, such as Performance Monitor, and report Performance-related statistics. PerfMon.exe allows you to view performance objects, performance counters, and object instances. You can add counters to view related descriptions.
In fact, you can write programs to access all Windows performance counters. In Windows, the Registry is a mechanism for accessing performance counters. The property information does not exist in the registration table. It cannot be viewed in the Registration Table editor regedit.exe, but can be accessed through the registry function. The registry key is used to obtain the data provided from the performance data provider. Open the special key named HKEY_PERFORMANCE_DATA and use the RegQueryValueEx function to query the values under the key to directly access the Registry performance counter information. Of course, you can also use the Performance Data Helper (PDH, Performance Data Helper) API (Pdh. dll) to access the Performance counter information.
Performance Logs and alarms are a management performance monitoring tool released as part of the Windows operating system. It relies on performance counters released by various Windows Components, subsystems, and applications, allowing you to track resource usage and draw them graphically for time. You can use Performance Logs and Alerts to monitor standard Performance counters (for example, memory usage or processor usage ), alternatively, you can define your own custom counters to monitor application-specific activities.
Ii. HKEY_PERFORMANCE_DATA Data Organization
The header of the performance data is a PERF_DATA_BLOCK structure (1), which describes the overall information of the system and performance data. You can obtain the structure data from the Global key value query. PERF_DATA_BLOCK defines all the performance object types (PERF_OBJECT_TYPE) in the system. The Offset of the next performance object type is described in the header of each object type.
Figure 1 Figure 2
There are two types of performance objects: one is a single instance object and the other is a multi-instance object. Figure 2 and Figure 3 respectively describe the data organization methods of these two performance objects. Each object data block contains a PERF_OBJECT_TYPE structure that describes the object's performance data. Followed by the PERF_COUNTER_DEFINITION structure list, which describes all the counter definitions of performance objects. For a single instance object, the counter definition list is followed by a PERF_COUNTER_BLOCK structure, followed by counter data. Each PERF_COUNTER_DEFINITION structure defines the offset between the counter data and the PERF_COUNTER_BLOCK, so you can easily obtain all the counter values. For multi-instance performance objects, the PERF_COUNTER_DEFINITION structure list is followed by a group of instance information data blocks, each representing an object instance. Each instance information data block consists of a PERF_INSTANCE_DEFINITION struct, Instance name, and a PERF_COUNTER_BLOCK struct. The following is the counter value data, which is the same as the single instance object.
Figure 3
Iii. Performance Counter Architecture
The performance counter adopts the client server structure, and the performance data is stored in shared memory. The performance counter is customized in the application and called to count when the application calls the performance counter. The architecture is as follows:
Performance counters can obtain real-time metric data for application performance, without the need to record logs for additional processing. In addition, the performance counters can be viewed remotely and managed using the management tool (MOM. The loss is also very small.
IV. Implementation of. NET performance counters
There is an assembly under. NET Framework.
(C: WINDOWSMicrosoft. NETFrameworkv2.0.50727perfcounter. dll) implements the performance counters function, including the installation/uninstallation of performance counters and reading and writing of values.
The namespace System. Diagnostic provides a set of performance counter classes, including PerformanceCounterCategory, PerformanceCounter, PerformanceCounterInstaller, and CouterSample.
In. NET, performance counters have excellent abstract encapsulation, which is much easier to implement than C/C ++. In applications, we often only focus on Data measurements of some request types, such as the following:
Requests Started
Requests Executing
Requests Completed
Requests Failed
Request Execution Time
Requests/Hour
Requests/5 Min
Requests/Min
Requests/Sec
However, the installation, uninstallation, counting, and reading of these performance counters still require a large amount of code, which simplifies programming interfaces through encapsulation. Services implemented using WCF can be further encapsulated based on the extendibility of WCF.
. Net Best Practice 3: Use performance counters to collect performance data
Http://blogs.msdn.com/ B /ralarcon/archive/2012/10/03/web-app-performance-counters-compilation.aspx
Monitor your SQL Server