-
-
Notifications
You must be signed in to change notification settings - Fork 260
Description
Description:
In hardware.py, the method total_power() appends power readings from CPUs and calculates an average using:
cpu_power = sum(power_history_in_W) / len(power_history_in_W)
However, under certain runtime conditions, no power samples may be collected, resulting in an empty power_history_in_W. This causes a ZeroDivisionError when attempting to divide by zero.
Steps to Reproduce:
Run the system under conditions where no CPU power samples are collected (e.g., sensor failure, initial sampling delay, which is frequent).
Call total_power().
Expected Behavior:
The method should handle the empty sample case gracefully and return a default value (e.g., 0.0 W), optionally logging a warning.
Proposed Fix:
Add a check to handle empty sample history:
if not power_history_in_W: logger.warning("No power samples collected; returning 0.0 W") cpu_power = sum(power_history_in_W) / len(power_history_in_W) if power_history_in_W else 0.0