Closed GoogleCodeExporter closed 8 years ago
> The problem is that every single call to get_cpu_percent() internally invokes
> sum(cpu_times()). With maybe some weird exceptions (hybernation?) that should
be
> pretty much the same as calling time.time() and multiply by the number of
CPUs -
> which is MUCH faster.
Mmm are you sure? The two results look very different:
>>> sum(psutil.cpu_times())
459405.7900000001
>>> time.time()
1392336739.063232
>>>
Anyway, I agree calculating sum(cpu_times()) on every iteration, for every
process, is sub-optimal.
I see two solutions, and I still don't know whether I like them:
1) - allow to pass system-wide CPU times as an argument as in
"cpu_percent(self, interval=None, cputimes=None)"
2) - cache sum(cpu_times()) value in a global constant and update it every 0.1
seconds or something
A third option might be reimplementing psutil.cpu_times() in C and avoid to
read a file in /proc, but I'm not sure if that's even possible.
Original comment by g.rodola
on 14 Feb 2014 at 12:24
Well, duh? Of course they're different, one is the seconds from 1970, the other
is the seconds from the last boot. However, since you only care about
differentials, they're perfectly interchangeable.
t1 = time.time()
t2 = sum(psutil.cpu_times())
time.sleep(10)
t1 = time.time() - t1
t2 = sum(psutil.cpu_times()) - t2
print t1 * psutil.NUM_CPUS
print t2
output:
319.998527527
319.979999989
Original comment by crusade...@gmail.com
on 14 Feb 2014 at 11:45
Mmm, interesting. So are you suggesting to replace all sum(psutil.cpu_times())
occurrences with time.time()?
I just tried that and that appears to produce higher CPU percentages which no
longer reflect what's shown by top.
Maybe you want to provide a patch?
Original comment by g.rodola
on 14 Feb 2014 at 1:33
Note: in order to observe CPU usage of the ongoing processes I simply used
examples/top.py script.
Original comment by g.rodola
on 14 Feb 2014 at 1:35
Did you remember to multiply time.time() * psutil.NUM_CPUS?
Original comment by crusade...@gmail.com
on 14 Feb 2014 at 3:50
You're right.
I committed this change in revision 44329a06f95e.
According to my benchmarks Process.cpu_percent() should now be about 30% faster
on Linux.
This was a great catch, thanks.
Original comment by g.rodola
on 15 Feb 2014 at 2:15
Closing out as fixed as 2.0.0 version is finally out.
Original comment by g.rodola
on 10 Mar 2014 at 11:36
Original issue reported on code.google.com by
crusade...@gmail.com
on 13 Feb 2014 at 1:31