Introduction to Performance Counter Using Windows under VC

xiaoxiao2021-03-06  17

This code has been updated, please go to http://hi.baidu.com/wlzqi to view.

/ * Vc Introduction windows performance counters under // * * Author: wlzqin * /

/ * Microsoft Windwos NT / 2000 provides a powerful API set to access numerous counters in system events and performance data. We can get the values ​​of the counter in real time or read counter data from a log file. The function can be powerful, and it is easy to use. Below I will simply talk about how to use Windows performance counters in VC. Ok, talk nonsense, we start:

We use a simple example to illustrate the method of use of performance counters. For example: How do we get the CPU usage of a process that is currently running? You will say: "This is not simple, there are many ways." Of course, I admit that this is not difficult, and there is a lot of ways. But which method is the easiest? Is the highest efficiency? I guess it probably uses performance counters. * /

// The basic step to use the performance counter is: // 1. Open the counter PDHOpenQuery; // 2. Assign space for the counter handle; // 3. Add the rejected counter coming in pdhaddcounter; // 4. Collect data PDHCOLLECTQUERYDATA / / 4. Get the value of the counter PDHGETFORMATTEDCOUNTERVALUE; // 5. Turn off the counter PDHCloseQuery.

// The following is the step // of the code // first step: // In the header file #include // In the implementation file #pragma Comment (LIB, "PDH.LIB")

// Step 2: Open the counter and assign space HQuery HQuery = null; PDH_STATUS PDHSTATUS; HCOUNTER * PCUNTERHANDLE = NULL; __TRY {// Open Counter PDHSTATUS = PDHopenQuery (0, 0, & hquery); if (PDHSTATUS = Error_Success) {

__leave;

}

PCOUNTERHANDLE = (hcounter *) GlobalAlloc (gptr, sizeof (hcounter)); if (PCOUNTERHANDLE == NULL) {

__leave;}} __ finally {if (AbnormalTerMination ()) {}}

// Step 3: Create a counter (assuming to get the QQ program CPU usage) PDH_FMT_COUNTERVALUE FMTVALUE; DWORD DWCTRTYPE; __TRY {PDHSTATUS = PDHADDCOUNTER (HQuery, _text ("// process (_text (" qq ") //% Processor Time "), 0, PCOUNTERHANDLE; if (PDHSTATUS! = Error_Success) {

__leave;} PDHSTATUS = PDHCOLLECTQUERYDATA (HQuery); if (PDHSTATUS! = Error_Success) {

__leave;

}

// Get the current counter value PDHSTATUS = PDHGETFORMATTEDCOUNTERVALUE (* PCOUNTERHANDLE, PDH_FMT_DOUBLE, & DWCTRTYPE, & FMTVALUE); if (PDHSTATUS! = Error_Success) {__LEAVE

} // fmtvalue.doublevalue is currently the CPU usage of the program at this moment (loop call to get real-time data)} __ finally {if (AbnormalTerMination ()) {}}

// Step 4: Close the counter

PDHSTATUS = PDHCLOSEQUERY (HQuery); if (PDHSTATUS == Error_Success) {// Turning success} else {// Off failed}

/ * Is it very simple! The PDHADDCOUNTER function is an add counter in the above example, and its second parameter is the counter address, we can replace other to get other count data. (For details, please query MSDN) Windows performance counter can get hundreds of system count information, almost all information related to counts can be obtained. It must be asked here to ask: "What information can I get? What mean?", What mean? ", We continue to look down. The above is said, to get other technical information only need to change the counter address (that is the second parameter "// process ((" QQ ") //% processor time" in the PDHADDCUNTER function ", each counter address contains three Part (Counter Object Process, Counter, Counter QQ), we just know what counter objects in your system, each counter object contains which counters, what counter instances have each counter, follow these The call format can get all count information you want. Microsoft provides us with easy access to counter objects, counters, instance information - sticks. Need to take the following API: 1. PennumObjects (null, // [in] data source, NT4.0 must be null szmachinename, // [in] machine name. Local machine NULL must be NULL SZMACHINENAME, / / ​​[in] machine name. SzObjectListBuffer, // [OUT] Receives buffers of the counter list, if the counter list is 0, then the item is empty & dwobjectListSize, // [in / out] set or receive the counter list length dwdetailLevel, // Get information level // perf_detail_novice primary level // perf_detail_advance Advanced Level (including Proseclas) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contains all levels) true); 2. Crew fresher and counter instance PdhenumObjectItems (null, // [in] data source, NT4.0 must be null szmachinename, // [in] machine name.

The local machine is a null pctCounter, // [in] counter name szcountristbuffer, // [out] to receive buffers of the counter list, if the counter list is 0, the item is empty & dwcounterlistsize, // [in / out] setting Or receive the counter list length szinstancelistbuffer, // [out] receive buffers of the instance list, if the counter list is 0, then the item is empty & dwinstanceListSize, // [in / out] set or receive the instance list length Dwdetaillevel, / / Property level // perf_detail_novice primary level // perf_detail_advance Advanced Level (including primary) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contain all levels) 0); // Last parameter system For more details, please refer to the basic step of the MSDN * /// Crew plug-in counter object is: // 1. Get the list size of the counter object // 2. Assign buffers for the counter list // 3. Start the stick

// The following is a program implementation: // First step: Get the counter object list size LPTSTSTSTSTSZOBJECTLISTBUFFER = 0; DWORD DWObjectListSize = 0; lptstr szthisobject = null; __ try {// First call this function to get a list of reception performance counters Buffer size PDHSTATUS = PDHENUMOBJECTS (null, // [in] data source, NT4.0 must be null null, // [in] machine name. Local machine is null szobjectlistbuffer, // [out] buffer listing buffer list If the counter list length is 0, the item is an empty & dwobjectListSize, // [IN / OUT] setting or receive the counter list length perf_detail_wizard, // Get information level // perf_detail_novice primary level // perf_detail_advance advanced level (including primary ) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contain all levels) true); if (pdhstatus! = Error_success) {__leave;} // Depending on the resulting buffer size assignment counter object list buffer memory szObjectListBuffer = (LPTSTR) malloc ((dwObjectListSize * sizeof (TCHAR))); if (szObjectListBuffer == NULL) {__leave;} // this function gets the second call counter object pdhStatus = PdhEnumObjects (NULL, // [IN ] Data source, NT4.0 must be null null, // [in] machine name. The local machine is null szobjectlistbuffer, // [out] receives the buffer of the counter list. If the counter list is 0, the item is empty & dwobje CTLISTSIZE, / / ​​[IN / OUT] Set or receive counter list length perf_detail_wizard, // Get information level // perf_detail_novice primary level // perf_detail_advance advanced level (including primary) // perf_detail_expert expert level (including primary and advanced) // Perf_detail_wizard system level (contains all levels) true);

if (pdhStatus = ERROR_SUCCESS!) {__leave;} szThisObject = szObjectListBuffer; // start lifting rod for (; * szThisObject = 0;! szThisObject = (lstrlen (szThisObject) 1)) {// each cycle is szThisObject rod Raised counter object}} __ finally {if (AbnormalterMination ()) {// If the IF (SzObjectListBuffer! = Null) {free (szObjectListBuffer; szobjectlistbuffer = null;}} else {// If success}} // Finally Don't forget free

/ * With the counter to get the counter object, you can continue the counter and counter instances, methods, and the basic interest in the object, and interested friends can do it yourself. I will not repeat it. I am talking about how to know the description information of the counter (but Chinese!), That is, what mean each counter represents? What is it used? To know that each counter description information needs to be used to use the PDHGetCounterInfo function (all in the API started at the beginning of the PDH). * /

// Basic steps are as follows: // 1. Formatting a counter address (string) / * Here you need to explain: There are many counters without instance. There is a slightly different form of examples and formatting without instances. For example: (have an example) Get the byte speed transmitted to the disk when the current write operation: you need to use the "PhysicalDisk" counter object, the "Disk Write Bytes / Sec" counter under the counter object, and the counter instance (in The instance of my machine is on the main hard disk is "0 C: D: E: F:"), then obtain the counter address I transmit to the byte speed on the main hard drive is: "// PhysicalDisk (" 0 C: D: E: f: ") // disk write bytes / sec". (No case) Get the time (in seconds) of the computer after the last startup: The "System Up Time" counter under the "System" counter object, the cover counter object, no instance, then this address is "// System // System Up Time". // 2. Create a counter PDHADDCUNTER // 3. Assign buffer // 4. Get Description Information * /

// The following is the program implementation:

__Try {// Create a counter PDHSTATUS = PDHADDCUNTER ("// system // system up time"), 0, pcounterHandle; if (pdhstatus! = error_success) {__leave;} // Assignment Receive Description Information buffer region DWORD dwCounterBuff; BYTE byCounterBuff [sizeof (PDH_COUNTER_INFO) sizeof (TCHAR) * 2048]; dwCounterBuff = sizeof (byCounterBuff); // Get the description information pdhStatus = PdhGetCounterInfo (* pCounterHandle, TRUE, & dwCounterBuff, (PPDH_COUNTER_INFO) byCounterBuff); if (pdhStatus = ERROR_SUCCESS!) {__leave;} PDH_COUNTER_INFO pdhCounterInfo = * (PPDH_COUNTER_INFO) byCounterBuff; // configuration information PDH_COUNTER_INFO see MSDN // PDH_COUNTER_INFO structure contains a lot of information about the counter, wherein the counter szExplainText description / / pdhcounterinfo.szExplainText} __ finally {if ()) {// If there is a failure} else {// If success}} / * here, the brief introduction to the performance counter is completed. In front of it, a big string is mainly to consider friends who have just contacted VCs. If this article can help me, I will be lucky. Slightly VC programming experience is certainly in this article, I hope to see the majority of beginners (including me), please don't attack me. I hope some friends have more valuable comments, and the ax is positive. Last description: 1. This article uses Unicode encoding, where some macros are used, and if you want to use it directly, you can select Unicode encoding in the compiler, and add #include (omitted) in the header file. Changes can be run under ANSI encoding) 2. The __TRY {} __final or {} is only structured by this article, can not be. 3. Friends who need this example of this article and source code please go to my homepage to download www.clock.5888.com 4. Performance counters can only be used in 2000 / XP system (2003 did not tried) 5. This class code compile environment Windows 2000 VC.NET 6. Welcome to reprint, please indicate the author and source of the article. * /

转载请注明原文地址:https://www.9cbs.com/read-41244.html

New Post(0)