?
Solved

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

Posted on 2003-03-10
3
Medium Priority
?
1,591 Views
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));
          PDH_FMT_COUNTERVALUE cpu;
          printf("PdhGetFormattedCounterValue return value: %d\n", PdhGetFormattedCounterValue(pCnt,PDH_FMT_LONG,NULL,&cpu));
          cpuload =  cpu.longValue;
     }

     PdhRemoveCounter(pCnt);
     PdhCloseQuery(q);

     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
Daniel
0
Comment
Question by:dscriv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Accepted Solution

by:
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??
0
 
LVL 11

Expert Comment

by:bcladd
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).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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