The performance monitor displays the built-in Windows performance counters in real time or by viewing historical data. You can drag and drop or create a custom data collector set to add performance counters to performance monitors. This feature allows you to visually view multiple chart views of performance log data. You can create a custom view in Performance Monitor, which can be exported as a data collector set for use with performance and logging functions.
This article describes how to read data from performance monitor through APIS.
I. API introduction:
1. pdhopenquery: obtain the data query handle of the Performance Monitor;
2. pdhaddcounter: Add a counter;
3. pdhcollectquerydata: query performance monitor data;
4. pdhgetformattedcountervalue: obtains the Specified Counter value;
Ii. instance code:
1. the PDH header file in the SDK:
# Include <PDH. h>
2. Import Lib:
PDH. Lib
3. Code:
Void ctestdlg: testpdh ()
{
Cstring strinfo = "system performance: \ r \ n ";
Cstring strtemp = "";
Hquery;
Hcounter hccommittotal, hccommitlimit;
Hcounter hckernelpaged, hckernelnonpaged;
Hcounter hcsyshandlecount, hcprocesses, hcthreads;
Pdh_status lstatus = pdhopenquery (null, null, & hquery );
If (lstatus! = Error_success)
{
Return;
}
Pdhaddcounter (hquery, _ T ("\ Memory \ committed bytes"), null, & hccommittotal );
Pdhaddcounter (hquery, _ T ("\ Memory \ commit limit"), null, & hccommitlimit );
Pdhaddcounter (hquery, _ T ("\ Memory \ pool paged bytes"), null, & hckernelpaged );
Pdhaddcounter (hquery, _ T ("\ Memory \ pool nonpaged bytes"), null, & hckernelnonpaged );
Pdhaddcounter (hquery, _ T ("\ process (_ total) \ handle count"), null, & hcsyshandlecount );
Pdhaddcounter (hquery, _ T ("\ System \ processes"), null, & hcprocesses );
Pdhaddcounter (hquery, _ T ("\ System \ Threads"), null, & hcthreads );
Hcounter hccpu = NULL;
Hcounter hcthreadcount = NULL;
Hcounter hchandlecount = NULL;
Hcounter hcworkingset = NULL;
Hcounter hcworkingsetpeak = NULL;
Hcounter hcpagefilebytes = NULL;
Pdh_counter_path_elements elements;
Char szbuf [1024] = "";
DWORD dwbufsize = 0;
Char szinstancename [256] = "360se ";
Elements. szmachinename = NULL;
Elements. szobjectname = "process ";
Elements. szinstancename = szinstancename;
Elements. szparentinstance = NULL;
Elements. dwinstanceindex =-1;
Elements. szcountername = const_cast <char *> ("% processor time ");
Dwbufsize = sizeof (szbuf );
Pdhmakecounterpath (& elements, szbuf, & dwbufsize, 0 );
Lstatus = pdhaddcounter (hquery, szbuf, null, & hccpu );
If (lstatus! = Error_success)
{
Return;
}
Elements. szcountername = const_cast <char *> ("thread count ");
Dwbufsize = sizeof (szbuf );
Pdhmakecounterpath (& elements, szbuf, & dwbufsize, 0 );
Lstatus = pdhaddcounter (hquery, szbuf, null, & hcthreadcount );
If (lstatus! = Error_success)
{
Return;
}
Elements. szcountername = const_cast <char *> ("handle count ");
Dwbufsize = sizeof (szbuf );
Pdhmakecounterpath (& elements, szbuf, & dwbufsize, 0 );
Lstatus = pdhaddcounter (hquery, szbuf, null, & hchandlecount );
If (lstatus! = Error_success)
{
Return;
}
Elements. szcountername = const_cast <char *> ("Working Set ");
Dwbufsize = sizeof (szbuf );
Pdhmakecounterpath (& elements, szbuf, & dwbufsize, 0 );
Lstatus = pdhaddcounter (hquery, szbuf, null, & hcworkingset );
If (lstatus! = Error_success)
{
Return;
}
Elements. szcountername = const_cast <char *> ("Working Set Peak ");
Dwbufsize = sizeof (szbuf );
Pdhmakecounterpath (& elements, szbuf, & dwbufsize, 0 );
Lstatus = pdhaddcounter (hquery, szbuf, null, & hcworkingsetpeak );
If (lstatus! = Error_success)
{
Return;
}
Elements. szcountername = const_cast <char *> ("page file bytes ");
Dwbufsize = sizeof (szbuf );
Pdhmakecounterpath (& elements, szbuf, & dwbufsize, 0 );
Lstatus = pdhaddcounter (hquery, szbuf, null, & hcpagefilebytes );
If (lstatus! = Error_success)
{
Return;
}
Pdh_fmt_countervalue CV;
Lstatus = pdhcollectquerydata (hquery );
If (lstatus! = Error_success)
{
Return;
}
// CPU time. Please wait.
Sleep (100 );
Lstatus = pdhcollectquerydata (hquery );
If (lstatus! = Error_success)
{
Return;
}
// Handle count
Lstatus = pdhgetformattedcountervalue (hcsyshandlecount, pdh_fmt_long, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("handle count: % u \ r \ n", cv. longvalue );
Strinfo + = strtemp;
}
// Total number of threads
Lstatus = pdhgetformattedcountervalue (hcthreads, pdh_fmt_long, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("Total threads: % u \ r \ n", cv. longvalue );
Strinfo + = strtemp;
}
// Process count
Lstatus = pdhgetformattedcountervalue (hcprocesses, pdh_fmt_long, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("total process count: % u \ r \ n", cv. longvalue );
Strinfo + = strtemp;
}
// Core memory: Number of pages
Lstatus = pdhgetformattedcountervalue (hckernelpaged, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("core memory: page number: % u \ r \ n", cv. largevalue/1024 );
Strinfo + = strtemp;
}
// Core memory: No pages
Lstatus = pdhgetformattedcountervalue (hckernelnonpaged, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("core memory: pages not divided: % u \ r \ n", cv. largevalue/1024 );
Strinfo + = strtemp;
}
// Approved usage: Total
Lstatus = pdhgetformattedcountervalue (hccommittotal, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("approved usage: % u \ r \ n", cv. largevalue/1024 );
Strinfo + = strtemp;
}
// Approved usage: Limit
Lstatus = pdhgetformattedcountervalue (hccommitlimit, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("approved usage: Limit: % u \ r \ n", cv. largevalue/1024 );
Strinfo + = strtemp;
}
// The following 360rp Process Information
Strinfo + = "\ r \ n ";
Strinfo + = szinstancename;
Strinfo + = "process: \ r \ n ";
// CPU time. Note that the pdh_fmt_nocap100 parameter must be added. Otherwise, there may be problems with the multi-core CPU. For details, see the msdn
Lstatus = pdhgetformattedcountervalue (hccpu, pdh_fmt_double | pdh_fmt_nocap100, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("CPU: % F \ r \ n", cv. doublevalue/2 );
Strinfo + = strtemp;
}
// Number of threads
Lstatus = pdhgetformattedcountervalue (hcthreadcount, pdh_fmt_long, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("Number of threads: % u \ r \ n", cv. longvalue );
Strinfo + = strtemp;
}
// Handle count
Lstatus = pdhgetformattedcountervalue (hchandlecount, pdh_fmt_long, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("handle count: % u \ r \ n", cv. longvalue );
Strinfo + = strtemp;
}
// Memory usage
Lstatus = pdhgetformattedcountervalue (hcworkingset, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("memory usage: % u \ r \ n", static_cast <long> (cv. largevalue/1024 ));
Strinfo + = strtemp;
}
// Peak memory usage
Lstatus = pdhgetformattedcountervalue (hcworkingsetpeak, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("peak memory usage: % u \ r \ n", static_cast <long> (cv. largevalue/1024 ));
Strinfo + = strtemp;
}
// Virtual memory size
Lstatus = pdhgetformattedcountervalue (hcpagefilebytes, pdh_fmt_large, null, & cv );
If (lstatus = error_success)
{
Strtemp. Format ("virtual memory size: % u \ r \ n", static_cast <long> (cv. largevalue/1024 ));
Strinfo + = strtemp;
}
M_staticinfo.setwindowtext (strinfo );
Pdhremovecounter (hccommittotal );
Pdhremovecounter (hccommitlimit );
Pdhremovecounter (hckernelpaged );
Pdhremovecounter (hckernelnonpaged );
Pdhremovecounter (hcsyshandlecount );
Pdhremovecounter (hcprocesses );
Pdhremovecounter (hcthreads );
Pdhremovecounter (hccpu );
Pdhremovecounter (hcthreadcount );
Pdhremovecounter (hchandlecount );
Pdhremovecounter (hcworkingset );
Pdhremovecounter (hcworkingsetpeak );
Pdhremovecounter (hcpagefilebytes );
Pdhclosequery (hquery );
}
4. Running result: