Skip to content

Commit

Permalink
Fix wrong cpu_times_percent values when interval < ~1 second (giampao…
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkusters committed Apr 30, 2024
1 parent 0b0ea8e commit 5ced102
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,10 +1846,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

0 comments on commit 5ced102

Please sign in to comment.