fix: handle empty power samples in total_power() to prevent ZeroDivisionError#1129
Open
wangwangbobo wants to merge 5 commits intomlco2:masterfrom
Open
fix: handle empty power samples in total_power() to prevent ZeroDivisionError#1129wangwangbobo wants to merge 5 commits intomlco2:masterfrom
wangwangbobo wants to merge 5 commits intomlco2:masterfrom
Conversation
Closes mlco2#1125 Replaced all mlco2.github.io/codecarbon links with docs.codecarbon.io: - README.md (6 links) - CONTRIBUTING.md (2 links) - .github/copilot-instructions.md (2 links)
- Replace mlco2.github.io/codecarbon with docs.codecarbon.io in: - webapp/src/app/(dashboard)/home/page.tsx (2 links) - codecarbon/core/powermetrics.py (1 link) - codecarbon/cli/main.py (1 link) - codecarbon/external/ram.py (1 link) - examples/notebooks/codecarbon_workshop.ipynb (1 link) Fixes: mlco2#1125
- Update mkdocs.yml edit_uri to use main branch - Update CONTRIBUTING.md rebase instructions - Update emissions_tracker.py docstring URLs - Add process_tree tracking mode to CPU module for consistency with RAM Fixes mlco2#1084 and addresses mlco2#976
- Add 'process_tree' as an alias for 'process' tracking mode - Provides consistent naming with RAM module - Tracks main process and all children recursively - Addresses mlco2#976 (Inconsistent behaviour for RAM and CPU Monitoring) The process_tree mode uses the same implementation as process mode, which already includes child processes via psutil.children(recursive=True). This alias improves API clarity and consistency.
…ionError - Move empty check after extracting power values from history - Add warning log when no power samples are collected - Return 0.0 W as safe default value Fixes mlco2#832
Contributor
|
Thanks for you contribution. Be careful to start new contribution from a fresh environnement of Here you also push the content of #1126 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1129 +/- ##
==========================================
- Coverage 80.74% 80.09% -0.66%
==========================================
Files 41 41
Lines 3921 3953 +32
==========================================
Hits 3166 3166
- Misses 755 787 +32 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a ZeroDivisionError that can occur in the
total_power()method when no power samples are collected.Problem
The original code checked if
self._power_historywas empty after appending to it, which meant the check would never catch an empty list. Additionally, the list comprehension that extracts power values could result in an empty list, causing a division by zero error.Solution
power_history_in_Wis empty before dividingChanges
CPU.total_power()Testing
The fix handles the edge case where no power samples are collected, preventing crashes during runtime.
Fixes #832