Windows system performance counters

Source: Internet
Author: User
System performance counters [Z]

From: http://hi.baidu.com/wlzqi/blog/item/d9782e73f4b4ba1c8701b092.html

The above address is the expert address. Please check it out if you have any questions.

1. performance counters:

System performance counters? Maybe a lot of friends have never used it! (Ugly) This feature is powerful. As its name suggests, it is something that can count Performance Indicators of Windows systems. In Windows 2000 and above, if it is installed by default, it will bring a performance counter program, you can see this program in "Control Panel-> Management-> performance.

Windows system performance counters can track hundreds of system performance indicators in real time. Right-click the system program's drawing interface and select "add counter" to view all the items that can be counted. I often see some new friends in the forum asking me how to implement the task manager and how to obtain the CPU usage of each process in real time. memory usage... so I think using the system performance counter should be the best choice. For more information, see the following section.

II. Specific implementation:

Related API introduction and example code:

1. pdhopenquery: Open the counter

Pdh_status pdhstatus;
Hquery phquery = NULL;
Hcounter pcounterhandle = NULL;
Pdhstatus = pdhopenquery (0, 0, & phquery );
If (pdhstatus! = Error_success) return false;
// Allocate counter handle Space
Pcounterhandle = (hcounter *) globalalloc (gptr, sizeof (hcounter ));
If (pcounterhandle = NULL) return false;

2. pdhclosequery: Close the counter
Pdhstatus = pdhclosequery (phquery );
If (pdhstatus! = Error_success) return false;

3. pdhenumobjects: Enumeration count item. This function has six parameters (see msdn for details)
Prototype:
Pdh_status pdhenumobjects (
Lpctstr szdatasource, // must be null
Lpctstr szmachinename, // machine name. If it is a local machine, it can be null.
Lptstr mszobjectlist, // receives the buffer for all available counting items
Lpdword pcchbufferlength, // buffer size (if it is 0, this value returns the required size)
DWORD dwdetaillevel, // Information Retrieval level
Bool brefresh // It is generally set to true.
);
Example:
Lptstr lpsobjectlistbuffer = NULL;
Lptstr lpsthisobject = NULL;
DWORD dwobjectlistsize = 0;
// Set the buffer size to 0 in the first step to obtain the required buffer size.
Pdhstatus = pdhenumobjects (

Null,
Lpcsmachinename,
Lpsobjectlistbuffer,
& Dwobjectlistsize,
Perf_detail_wizard,
True );
If (pdhstatus! = Error_success | pdhstatus! = Pdh_more_data) return false;
// After obtaining the buffer size, allocate the cache area memory
Lpsobjectlistbuffer = (lptstr) malloc (dwobjectlistsize + 1 );
If (lpsobjectlistbuffer = NULL) return false;
// The second step is to call the enumeration function to start enumeration counting items.
Pdhstatus = pdhenumobjects (

Null,
Lpcsmachinename,
Lpsobjectlistbuffer,
& Dwobjectlistsize,
Perf_detail_wizard,
True );
If (pdhstatus! = Error_success) return false;
// Save the buffer address
Lpsthisobject = lpsobjectlistbuffer;
// Print all available counting items
For (; * lpsthisobject! = 0; lpsthisobject + = (lstrlen (lpsthisobject) + 1 ))
{
Printf (lpsthisobject );
}
If (lpsobjectlistbuffer)
{
Free (lpsobjectlistbuffer );
Lpsobjectlistbuffer = NULL;
Dwobjectlistsize = 0;
}

4. pdhenumobjectitems: Enumeration counters and Instances
Prototype:
Pdh_status pdhenumobjectitems (

Lpctstr szdatasource, // must be null
Lpctstr szmachinename, // machine name. If it is a local machine, it can be null.
Lpctstr szobjectname, // count items (all available items can be obtained through the pdhenumobjects function)
Lptstr mszcounterlist, // counter Buffer
Lpdword pcchcounterlistlength, // counter buffer size
Lptstr mszinstancelist, // count instance Buffer
Lpdword pcchinstancelistlength, // count the instance buffer size
DWORD dwdetaillevel, // Information Retrieval level
DWORD dwflags // set to true
);
For example, the method of this function is the same as that of the previous function (pdhenumobjects). For details, see msdn or the test project code attached to this article.

5. pdhaddcounter: Add counter
When calculating the system information that you are interested in, you must first add the corresponding counters.
Prototype:
Pdh_status pdhaddcounter (
Pdh_hquery hquery, // handle opened for pdhopenquery
Lpctstr szfullcounterpath, // counter path (maximum length: pdh_max_counter_path)
Dword_ptr dwuserdata, // set it to 0
Pdh_hcounter * phcounter // counter handle Space (allocated after the pdhopenquery function in this article)
);
Example:
// The CPU usage of the obtained winlogon.exe process is used as an example.
// By enumerating and viewing the counting project description, you can know that the process project is a process-related project.
// By enumerating the counters and examples and viewing the instructions, we can know that the % processor time counter in the process project is about the CPU usage rate of the process.
// Finally, the Winlogon process is displayed in the counter example (indicating that the process is running)
Pdhstatus = pdhaddcounter (phquery, "processor time", 0, pcounterhandle );
If (pdhstatus! = Error_success) return false;
Tip: some counters do not have instances. For example, if you want to obtain the number of seconds that the system has run since it was started, the Count item is system, the counter is system up time, and the counter instance is null, the counter path is \ System \ system up time"

6. pdhcollectquerydata: Prepare to obtain the current data
Example:
Pdhstatus = pdhcollectquerydata (phquery );
If (pdhstatus! = Error_success) return false;

7. pdhgetformattedcountervalue: Get Data
Example:
Pdhstatus = pdhgetformattedcountervalue (pcounterhandle, pdh_fmt_double,
& Dwctrtype, & fmtvalue );
If (pdhstatus! = Error_success) return false;
// Pdh_fmt_double indicates that double type data is returned. Of course, int and other types of data can be returned. Please check msdn
// Obtain the data of the next time point
Pdhstatus = pdhcollectquerydata (phquery );
If (pdhstatus! = Error_success) return false;
Tip: pcounterhandle is the handle obtained by pdhaddcounter. Different count values can be obtained by different pcounterhandle values.

8. pdhremovecounter: Remove counter
If you do not want to obtain a counter value, you should remove it to save resources.
Example:
If (pdhremovecounter (pdhcouner )! = Error_success) return false;
This function parameter is the counter handle.

So far, if you use counters to track system information in real time, it has been explained. If you still have any questions, check msdn or
Email: or QQ: 8573980 contact.

3.. Finally, we will introduce the related API pdhgetcounterinfo. This API has nothing to do with using counters. However, it helps you understand the path of the counters you are interested in. It can get the description of each counter Project (Chinese !)
Example:
Take obtaining the number of seconds that the system has run since its startup as an example.
Pdh_counter_info pdhcounterinfo;
DWORD dwcounterbuffsize;
// Add counter
Pdhstatus = pdhaddcounter (phquery, "up time", 0, pcounterhandle );
If (pdhstatus! = Error_success) return false;
// Obtain the buffer size
Pdhstatus = pdhgetcounterinfo (* pcounterhandle, true, & dwcounterbuffsize, null );
If (pdhstatus! = Error_success | pdhstatus! = Pdh_more_data) return false;
// Set the buffer
Byte * bycounterbuff = (byte *) malloc (dwcounterbuffsize );
// Obtain information
Pdhstatus = pdhgetcounterinfo (* pcounterhandle, true, & dwcounterbuffsize, (ppdh_counter_info) bycounterbuff );
If (pdhstatus! = Error_success) return false;
Pdhcounterinfo = * (ppdh_counter_info) bycounterbuff;
// Print the information
Printf (pdhcounterinfo-> szexplaintext );

The printed information is similar to: "system up time indicates the time (in seconds) that the computer has run since its last startup ). This Count value shows the difference between the start time and the current time. "

I hope this article will be helpful and have a good time.

Some key definitions of a PDH found elsewhere

# Define pdh_cstatus_valid_data 0x0
# Define pdh_cstatus_new_data 0x1
# Define pdh_cstatus_no_machine 0x800007d0
# Define pdh_cstatus_no_instance 0x800007d1
# Define pdh_more_data 0x800007d2
# Define pdh_cstatus_item_not_validated 0x800007d3
# Define pdh_retry 0x800007d4
# Define pdh_no_data 0x800007d5
# Define pdh_calc_negative_denominator 0x800007d6
# Define pdh_calc_negative_timebase 0x800007d7
# Define pdh_calc_negative_value 0x800007d8
# Define pdh_dialog_cancelled 0x800007d9
# Define pdh_cstatus_no_object 0xc1_bb8
# Define pdh_cstatus_no_counter 0xc1_bb9
# Define pdh_cstatus_invalid_data 0xc1_bba
# Define pdh_memory_allocation_failure 0xc1_bbb
# Define pdh_invalid_handle 0xc0000bbc
# Define pdh_invalid_argument 0xc1_bbd
# Define pdh_function_not_found 0xc1_bbe
# Define pdh_cstatus_no_countername 0xc1_bbf
# Define pdh_cstatus_bad_countername 0xc1_bc0
# Define pdh_invalid_buffer 0xc1_bc1
# Define pdh_insufficient_buffer 0xc1_bc2
# Define pdh_cannot_connect_machine 0xc1_bc3
# Define pdh_invalid_path 0xc0000bc4
# Define pdh_invalid_instance 0xc0000bc5
# Define pdh_invalid_data 0xc1_bc6
# Define pdh_no_dialog_data 0xc1_bc7
# Define pdh_cannot_read_name_strings 0xc1_bc8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.