Performance counters CPU usage

Source: Internet
Author: User
 
http://msdn.microsoft.com/en-us/library/windows/desktop/aa373169(v=vs.85).aspx
#include <windows.h>#include <stdio.h>#include <pdh.h>#include <pdhmsg.h>#pragma comment(lib, "pdh.lib")CONST PWSTR COUNTER_PATH    = L"\\Processor(0)\\% Processor Time";void DisplayCommandLineHelp(void){    wprintf(L"The command line must contain a valid log file name.\n");}void wmain(int argc, WCHAR **argv){    HQUERY hQuery = NULL;    HCOUNTER hCounter = NULL;    PDH_STATUS status = ERROR_SUCCESS;    DWORD dwFormat = PDH_FMT_DOUBLE;     PDH_FMT_COUNTERVALUE ItemBuffer;    if (argc != 2)     {        DisplayCommandLineHelp();        goto cleanup;    }    // Opens the log file that Writein Performance Data to a Log File     // example created.    status = PdhOpenQuery(argv[1], 0, &hQuery);    if (ERROR_SUCCESS != status)    {        wprintf(L"PdhOpenQuery failed with 0x%x\n", status);        goto cleanup;    }       // Add the same counter used when writing the log file.    status = PdhAddCounter(hQuery, COUNTER_PATH, 0, &hCounter);       if (ERROR_SUCCESS != status)    {        wprintf(L"PdhAddCounter failed with 0x%x\n", status);        goto cleanup;    }    // Read a performance data record.    status = PdhCollectQueryData(hQuery);    if (ERROR_SUCCESS != status)    {        wprintf(L"PdhCollectQueryData failed with 0x%x\n", status);        goto cleanup;    }    while (ERROR_SUCCESS == status)     {         // Read the next record        status = PdhCollectQueryData(hQuery);        if (ERROR_SUCCESS == status)        {            // Format the performance data record.            status = PdhGetFormattedCounterValue(hCounter,                dwFormat,                (LPDWORD)NULL,                &ItemBuffer);            if (ERROR_SUCCESS != status)            {                wprintf(L"PdhGetFormattedCounterValue failed with 0x%x.\n", status);                goto cleanup;            }            wprintf(L"Formatted counter value = %.20g\n", ItemBuffer.doubleValue);        }        else        {            if (PDH_NO_MORE_DATA != status)            {                wprintf(L"PdhCollectQueryData failed with 0x%x\n", status);            }        }    }cleanup:    // Close the query.    if (hQuery)        PdhCloseQuery(hQuery);}

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.