Skip to content
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

Fix too low cpu_times_percent values when interval < ~1 second (#1586) #2413

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -831,3 +831,6 @@ I: 2376
N: Anthony Ryan
W: https://github.com/anthonyryan1
I: 2272

N: Frank Kusters
I: 1586
15 changes: 13 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
*Bug tracker at https://github.com/giampaolo/psutil/issues*

6.0.0 2024-06-18
================
6.0.1
=====

**Bug fixes**

- 1586_, `cpu_times_percent()`_ reports much too low values if the interval is
less than 1 second (with ``percpu=True``) or less than ``1/cpu_count()``
seconds (with ``percpu=False``).

6.0.0
=====

2024-06-18

**Enhancements**

Expand Down
7 changes: 3 additions & 4 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,10 +1851,9 @@ def calculate(t1, t2):
times_delta = _cpu_times_deltas(t1, t2)
all_delta = _cpu_tot_time(times_delta)
# "scale" is the value to multiply each delta with to get percentages.
# We use "max" to avoid division by zero (if all_delta is 0, then all
# fields are 0 so percentages will be 0 too. all_delta cannot be a
# fraction because cpu times are integers)
scale = 100.0 / max(1, all_delta)
# Avoid division by zero (if all_delta is 0, then all fields are 0 so
# percentages will be 0 too).
scale = 100.0 / all_delta if all_delta > 0 else 100.0
for field_delta in times_delta:
field_perc = field_delta * scale
field_perc = round(field_perc, 1)
Expand Down
Loading