Skip to content

Commit f83fa62

Browse files
committed
Fix too low cpu_times_percent values when interval < ~1 second (#1586)
Signed-off-by: Frank Kusters <frank.kusters@sioux.eu>
1 parent 0b0ea8e commit f83fa62

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

CREDITS

+3
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,6 @@ I: 2376
831831
N: Anthony Ryan
832832
W: https://github.com/anthonyryan1
833833
I: 2272
834+
835+
N: Frank Kusters
836+
I: 1586

HISTORY.rst

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
**Bug fixes**
2222

23+
- 1586_, `cpu_times_percent()`_ reports much too low values if the interval is
24+
less than 1 second (with ``percpu=True``) or less than ``1/cpu_count()``
25+
seconds (with ``percpu=False``).
2326
- 2395_, [OpenBSD]: `pid_exists()`_ erroneously return True if the argument is
2427
a thread ID (TID) instead of a PID (process ID).
2528
- 2254_, [Linux]: offline cpus raise NotImplementedError in cpu_freq() (patch by Shade Gladden)

psutil/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1846,10 +1846,9 @@ def calculate(t1, t2):
18461846
times_delta = _cpu_times_deltas(t1, t2)
18471847
all_delta = _cpu_tot_time(times_delta)
18481848
# "scale" is the value to multiply each delta with to get percentages.
1849-
# We use "max" to avoid division by zero (if all_delta is 0, then all
1850-
# fields are 0 so percentages will be 0 too. all_delta cannot be a
1851-
# fraction because cpu times are integers)
1852-
scale = 100.0 / max(1, all_delta)
1849+
# Avoid division by zero (if all_delta is 0, then all fields are 0 so
1850+
# percentages will be 0 too).
1851+
scale = 100.0 / all_delta if all_delta > 0 else 100.0
18531852
for field_delta in times_delta:
18541853
field_perc = field_delta * scale
18551854
field_perc = round(field_perc, 1)

0 commit comments

Comments
 (0)