-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Ubuntu 16.04.6 LTS] cpu_times_percent wrong idle values when percpu=True #1586
Comments
Why do you think it's the wrong idle value? |
Please read my description again.
|
Ah, you're right, I see the discrepancy now. It seems your namedtuple does not return |
Yes, it is a VM. I don't know what's the hypervisor though. I can ask for it.
|
Some information I found on https://www.ostechnix.com/check-linux-system-physical-virtual-machine/
|
OK, as I thought As per this code section: Lines 281 to 286 in c10df5a
...they should be available only on Linux > 2.6.24 and Linux > 3.2.0 respectively. What Linux kernel are you on? As per this comment: Lines 1756 to 1765 in c10df5a
... guest and guest_nice are accounted also in user and nice times. But since they are not available and the idle time is off, my best guess is that the kernel does not keep track of them at all, including in user and nice counters, which are lower than they should be (and that's why idle is off, it's missing some "busy time"). In summary, to me it appears like a kernel bug, and you'll probably see the same discrepancy by using top .
|
Kernel:
OS:
Looks like |
Can you run a CPU intensive task while you do top? Something like this:
|
OK, while you run that (CPU instensive task) also run this: import psutil
from pprint import pprint as pp
for x in range(5):
pp(psutil.cpu_times(percpu=True))
print()
pp(psutil.cpu_times_percent(interval=1, percpu=True))
print("\n-----\n") What does it print? |
It printed this:
edit: fixed output (forgot the first 4 outputs) |
Here is the output of the same test, using an
|
I have run into this problem as well, it looks like it is an issue with intervals that are smaller than 1 second. Lines 1800 to 1804 in 5a76cfa
What is the reason we cannot do fractions here? Calling cpu_times_percent with an interval less than about 1 second tends to return percentages that don't add up to 100% for each cpu. If you do: scale = 100.0 / all_delta if all_delta > 0 else 100.0 wouldn't it solve that problem? |
…aolo#1586) Signed-off-by: Frank Kusters <[email protected]>
I ran into this problem, as I'm trying to monitor at sub-second intervals. I modified the code to print With this script: import psutil
import time
psutil.cpu_times_percent(percpu=True)
time.sleep(1)
psutil.cpu_times_percent(percpu=True) I get this output on my 8 core machine: <skip first 8 lines>
1.0099999999995646
1.010000000000332
1.0000000000004547
0.9899999999995543
0.9800000000007092
0.9900000000000091
1.0000000000000568
1.0 So even at 1 second intervals, a nontrivial error is introduced (0.98 will be modified to 1.0). I was wondering how the originally reported problem doesn't show with psutil.cpu_times_percent()
psutil.cpu_percent()
interval = 1 / (psutil.cpu_count() * 2) # set interval to value significantly below 1/cpu_count
time.sleep(interval)
print(psutil.cpu_times_percent())
print(f'cpu_percent={psutil.cpu_percent()}') This is the output: 85.1400000000057 # should be ignored
0.499999999996362 # all_delta
scputimes(user=1.0, nice=6.0, system=6.0, idle=37.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
cpu_percent=26.0 The output of |
Platform
Bug description
psutil.cpu_times_percent(interval=1, percpu=True)
retrieves the wrongidle
value on my machineExamples
Example 1 (BUG):
Results - Example 1
idle
is around 66idle
should be around 95-100Example 2 (BUG):
Results - Example 2
idle
is around 33idle
should be around 95-100Example 3 (BUG):
Results - Example 3
idle
is around 17idle
should be around 95-100Example 4 (BUG):
Results - Example 4
idle
is around 8idle
should be around 95-100Example 5 (BUG):
Results - Example 5
idle
is around 70idle
should be around 95-100Example 6 (OK):
Results - Example 6
idle
is around 100 (OK)idle
should be around 95-100Other Examples and observations:
0 < interval < 2.0
the values ofidle
increasepercpu=True
, we have NO problem with ANY interval:The text was updated successfully, but these errors were encountered: