Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Monitoring CPU Usage: PDH always reporting 0% or 99%

Posted on 2003-03-10
Medium Priority
Last Modified: 2012-06-27
In attempting to monitor CPU usage, I have the following code:

     HQUERY q;
     HCOUNTER pCnt;
     long cpuload = -1;
     if (!PdhOpenQuery(NULL,0,&q))  {
          printf("PdhAddCounter return value: %d\n", PdhAddCounter(q,"\\Processor(0)\\% Processor Time",1,&pCnt));
          printf("PdhCollectQueryData return value: %d\n", PdhCollectQueryData(q));
          printf("PdhGetFormattedCounterValue return value: %d\n", PdhGetFormattedCounterValue(pCnt,PDH_FMT_LONG,NULL,&cpu));
          cpuload =  cpu.longValue;


     printf("%d", cpuload);

Running on Windows 2000, I always get 99 as my return value. On Windows NT, I always get 0.
I have also tried the following counter strings in place of "\\Processor(0)\\% Processor Time":
"\\System\\% Total Processor Time"
"\\Processor(_Total#0)\\% Processor Time"

If I use "\\Memory\\Available Bytes", I do get sensible return values for this, suggesting something is right in my code!

Any idea why I should be getting these return values regardless of the actual CPU activity? What am I doing wrong?

Many Thanks
Question by:dscriv
LVL 12

Accepted Solution

andrewjb earned 225 total points
ID: 8110174
Never used these, so this could be bogus....

Do you need to give some time between initialising the query, and collecting the data from it. At the moment you're doing these pretty damn close together. I don't know how often the counters get updated, but I could imaging 0 or 99% being the only answers you'll get, since either 1 'tick' occurs (and you're processing) or doesn't.

Maybe try shoving in a sleep(1000) just after the OpenQuery??
LVL 11

Expert Comment

ID: 9978880
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to andrewjb: Grade B

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question