Issue 482 in psutil: get_cpu_percent returns incorrect data

244 views
Skip to first unread message

psu...@googlecode.com

unread,
Feb 20, 2014, 4:45:41 AM2/20/14
to psutil-...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 482 by bharathi...@gmail.com: get_cpu_percent returns incorrect
data
http://code.google.com/p/psutil/issues/detail?id=482

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.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

psu...@googlecode.com

unread,
Feb 20, 2014, 5:25:22 AM2/20/14
to psutil-...@googlegroups.com

Comment #1 on issue 482 by g.rodola: get_cpu_percent returns incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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.

psu...@googlecode.com

unread,
Feb 20, 2014, 6:04:06 AM2/20/14
to psutil-...@googlegroups.com

Comment #2 on issue 482 by bharathi...@gmail.com: get_cpu_percent returns
incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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?

psu...@googlecode.com

unread,
Feb 20, 2014, 6:25:50 AM2/20/14
to psutil-...@googlegroups.com

Comment #3 on issue 482 by g.rodola: get_cpu_percent returns incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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.

psu...@googlecode.com

unread,
Feb 20, 2014, 6:33:43 AM2/20/14
to psutil-...@googlegroups.com

Comment #4 on issue 482 by bharathi...@gmail.com: get_cpu_percent returns
incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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?

psu...@googlecode.com

unread,
Feb 20, 2014, 6:37:13 AM2/20/14
to psutil-...@googlegroups.com

Comment #5 on issue 482 by bharathi...@gmail.com: get_cpu_percent returns
incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

And Its fine if I get the CpuUsage for the process pythonw.exe at the
particular point of function call.

psu...@googlecode.com

unread,
Feb 20, 2014, 6:42:14 AM2/20/14
to psutil-...@googlegroups.com

Comment #6 on issue 482 by g.rodola: get_cpu_percent returns incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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.

psu...@googlecode.com

unread,
Feb 20, 2014, 6:49:37 AM2/20/14
to psutil-...@googlegroups.com

Comment #7 on issue 482 by bharathi...@gmail.com: get_cpu_percent returns
incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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.

psu...@googlecode.com

unread,
Feb 20, 2014, 7:15:59 AM2/20/14
to psutil-...@googlegroups.com

Comment #8 on issue 482 by g.rodola: get_cpu_percent returns incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

> 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()

psu...@googlecode.com

unread,
Feb 20, 2014, 8:16:03 AM2/20/14
to psutil-...@googlegroups.com

Comment #9 on issue 482 by bharathi...@gmail.com: get_cpu_percent returns
incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

OK finally how ll I get the CPU usage here after calling my function...

psu...@googlecode.com

unread,
Feb 27, 2014, 11:47:48 PM2/27/14
to psutil-...@googlegroups.com

Comment #10 on issue 482 by bharathi...@gmail.com: get_cpu_percent returns
incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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.

psu...@googlecode.com

unread,
Feb 28, 2014, 6:57:12 AM2/28/14
to psutil-...@googlegroups.com
Updates:
Status: CantReproduce

Comment #11 on issue 482 by g.rodola: get_cpu_percent returns incorrect data
http://code.google.com/p/psutil/issues/detail?id=482

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.

psu...@googlecode.com

unread,
Apr 9, 2015, 4:04:46 PM4/9/15
to psutil-...@googlegroups.com

Comment #12 on issue 482 by nomad...@google.com: get_cpu_percent returns
incorrect data
https://code.google.com/p/psutil/issues/detail?id=482

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 ?
Reply all
Reply to author
Forward
0 new messages