shiva21 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

get_cpu_percent returns incorrect data #482

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Using the get_cpu_percent returns an 100% value.

What is the expected output?
Expected as per the task manager CPU usage value

What do you see instead?
100% instead of other values like 30% or 40%

What version of psutil are you using? What Python version?
psutil 1.2.1 and python 2.7 32bit version on 64bit machine

On what operating system? Is it 32bit or 64bit version?
Windows 7 64 bit but using the 32bit version on 64bit

Please provide any additional information below.
The purpose is, I am using the get_cpu_percent to get the cpu usage of the 
pythonw.exe process when the particular function is called.

A simple snippet:
process.get_cpu_percent(interval=1)            
SDKTestSuite.DijSDK_CalculateFps(int(timeForFPS),int(index),cameraName)
cpuUsage = process.get_cpu_percent(interval=0)

This is the exact code which I am using in my project. The call to 
DijSDK_CalculateFps does some project related task. Now the thing we need to 
find out how much percentage of CPU ahas been used by this call. Sometimes the 
value  for cpuUsage is 100%. When we compare the CPU usage percentage with the 
task manager data, it is not comparable. For example if the task manager value 
is 10% it returns a value around 40-50% which is not expected. 

Can you tell me, Am I using the correct function to get this value or how to 
use this function to get the value approximate to the task manager data.

Original issue reported on code.google.com by bharathi...@gmail.com on 20 Feb 2014 at 9:45

GoogleCodeExporter commented 9 years ago
In order to obtain a meaningful result you should call get_cpu_percent() 
different times, in a loop, with 1 second interval.
That way you should observe results which are *similar* to taskmgr's.
What it seems you're doing is that you called get_cpu_percent() once (or twice, 
with NO timeout between calls), then complained because taskmgr.exe shows 
something different.

Original comment by g.rodola on 20 Feb 2014 at 10:25

GoogleCodeExporter commented 9 years ago
As you can see the code snippet which I posted, there is a function between the 
two get_cpu_percent() call. That particular function will sleep for around 10 - 
20 seconds approximately. 

I need to get the cpu usage used by that particular function call?Is there a 
way to get that data?

Original comment by bharathi...@gmail.com on 20 Feb 2014 at 11:04

GoogleCodeExporter commented 9 years ago
You can get the CPU percentage of a given *process*, not a given function.
You can't do what you're trying to do because it doesn't make sense.
What you can do, if you want to time a function, is to benchmark it, as in:

>>> t = time.time()
>>> yourfun()
>>> print(time.time() - t)
0.08523121315878872

...or (better) by using timeit module:

>>> import timeit
>>> min(timeit.repeat(yourfun))
0.07578110694885254

...then apply improvements to "yourfun" in order to decrease execution time, 
benchmark again, etc. until you're satisfied (or give up).
You can also use p.get_cpu_times() in order to differentiate between user and 
system times, but that's something "extra" and you're probably not interested 
in that. 
I think what you're looking for is what I did in the first code sample.

Original comment by g.rodola on 20 Feb 2014 at 11:25

GoogleCodeExporter commented 9 years ago
Thanks for the data, but our client has requested to give them the Cpu Usage 
for that call. 
As per my understanding of get_cpu_percent() call is:
When we first call the process.get_cpu_percent(interval=1), it just remembers 
the value of user and system times and when we once again call the 
process.get_cpu_percent(interval=0),it gives the user and system times 
subtracted with the old value stored something like time= endtime - starttime  
functionality.

If thats the functionality of get_cpu_percent() why shouldn't it returns the 
accurate value?

Original comment by bharathi...@gmail.com on 20 Feb 2014 at 11:33

GoogleCodeExporter commented 9 years ago
And Its fine if I get the CpuUsage for the process pythonw.exe at the 
particular point of function call.

Original comment by bharathi...@gmail.com on 20 Feb 2014 at 11:37

GoogleCodeExporter commented 9 years ago
You should use interval=0 on the first call as in:

p = psutil.Process(os.getpid())
p.get_cpu_percent(timeout=0)
yourcall()
print(p.get_cpu_percent(timeout=0))

IMO this is not very useful but there you have it.
You cannot really compare the value you get out of that with taskmgr's because 
the timings are just different and you basically end up comparing orange and 
apples.

Original comment by g.rodola on 20 Feb 2014 at 11:41

GoogleCodeExporter commented 9 years ago
Yes I know I shouldn't compare orange and apples. But In my case, I will be 
running my application by displaying the output and task manager at the same 
time. So that it may vary around 10% but not around 30-40% difference right?. 
Anyway as you suggested I try to work on it and will get back to you tomorrow 
with the result.

Original comment by bharathi...@gmail.com on 20 Feb 2014 at 11:49

GoogleCodeExporter commented 9 years ago
> But In my case, I will be running my application by
> displaying the output and task manager at the same time.

Yes, but the taskmgr will measure CPU percent with an interval of 1 sec on a 
total time span of, say, 20 secs (the total time of your call) whereas psutil 
will use an interval of 20 secs on a total time span of 20 secs and provide a 
single value instead of 20.
That's why I say you can't compare the two things.

In order to have a "real" comparison psutil should do the same as taskmgr. 
You can emulate that by using a thread, like this:

import threading, psutil

def print_cpu_percent():
     while 1:
         print p.cpu_percent(interval=1)

p = psutil.Process(os.getpid())
t = threading.Thread(target=print_cpu_percent)
t.start()
yourfun()

Original comment by g.rodola on 20 Feb 2014 at 12:15

GoogleCodeExporter commented 9 years ago
OK finally how ll I get the CPU usage here after calling my function...

Original comment by bharathi...@gmail.com on 20 Feb 2014 at 1:15

GoogleCodeExporter commented 9 years ago
Hi, 
In the above code, the line " print p.cpu_percent(interval=1)" we can't call 
the cpu_percent() with the particular process ID since its not a member of the 
Process class. It also gives us the system wide CPU utilization and not the 
process value.

I replaced the same code instead of cpu_percent with get_cpu_percent() but I am 
still not getting the expected result. It giving a value of 100% for most of 
the cases when the task manager is showing around 40% of cpu Usage.

The cpu_percent() function is giving the similar value to taskmanager and why 
not the get_cpu_percent() is not displaying the expected result. 

Please someone help me to get the cpu usage of a particular process.

Original comment by bharathi...@gmail.com on 28 Feb 2014 at 4:47

GoogleCodeExporter commented 9 years ago
I cannot reproduce the issue you describe and you fail to provide any useful 
info in order to fix it or reproduce it.
Keep shouting "please somebody help me" is not gonna help you get your problem 
fixed.
Closing this out.

Original comment by g.rodola on 28 Feb 2014 at 11:56

GoogleCodeExporter commented 9 years ago
bharathi...@gmail.com I think you can use performance counters in Windows to 
get the task manager percentage. 

g.rodola@, I tried your approach but the values I get are still around 100% for 
a process that shows 25% on task manager consistently. 

Maybe Task manager is using some kind of averaging over the a sliding window ? 

Original comment by nomad...@google.com on 9 Apr 2015 at 8:04