shanecode / psutil

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

Process.get_cpu_percent() gives wrong values #390

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. get any valid Process instance
2. call get_cpu_percent on this instance with interval == 0
3. value will be zero, even when the process is not idle

What is the expected output?
get_cpu_percent(interval=0) should return values > 0.0 at least some time

What do you see instead?
values at 0

What version of psutil are you using? What Python version?
I'm using psutil 0.7.1, directly from code.google.com (same from pypi), with 
python 2.7

On what operating system? Is it 32bit or 64bit version?
It's an Ubuntu 13.04 64bit

Please provide any additional information below.
There are 4 logical CPUs on this system.

Original issue reported on code.google.com by steve.ko...@gmail.com on 6 Jun 2013 at 12:26

GoogleCodeExporter commented 9 years ago
From psutil doc:

> When interval is 0.0 or None compares process times to system CPU 
> times elapsed since last call, returning immediately. In this case 
> is recommended for accuracy that this function be called with at 
> least 0.1 seconds between calls.

That means the very first get_cpu_percent(interval=0) call will produce a 
meaningless value as it has no previous value to compare against.
It therefore means you'll need at least TWO calls in order to produce a 
meaningful result and wait some time between one call and the other.

Original comment by g.rodola on 6 Jun 2013 at 12:30

GoogleCodeExporter commented 9 years ago
Thank you for your quick answer Giampaolo. I forgot to tell that several calls, 
for the same process, with 5 sec. intervals, always give 0.
Is it relevant to say that everytime I call get_cpu_percent, I create a new 
instance of Process ?

Original comment by steve.ko...@gmail.com on 6 Jun 2013 at 12:40

GoogleCodeExporter commented 9 years ago
You should not create new Process instance every time. That's the problem.

Original comment by g.rodola on 6 Jun 2013 at 1:04

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 6 Jun 2013 at 2:08

GoogleCodeExporter commented 9 years ago
Yes, you are right, this was the problem.

Original comment by steve.ko...@gmail.com on 6 Jun 2013 at 2:08