Skip to content

Commit

Permalink
Fix too low cpu_times_percent values when interval < ~1 second (giamp…
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkusters committed Apr 30, 2024
1 parent 0b0ea8e commit 516a39b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

**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`).
- 2395_, [OpenBSD]: `pid_exists()`_ erroneously return True if the argument is
a thread ID (TID) instead of a PID (process ID).
- 2254_, [Linux]: offline cpus raise NotImplementedError in cpu_freq() (patch by Shade Gladden)
Expand Down
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 516a39b

Please sign in to comment.