-
Notifications
You must be signed in to change notification settings - Fork 0
Fix memory alert message formatting #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,11 +117,16 @@ def _check_memory_and_alert(self, device: str) -> None: | |
| self.memory_history.append(memory_usage_gb) | ||
| self.history_timestamps.append(current_time) | ||
|
|
||
| # Calculate memory ratio | ||
| # Calculate memory ratio and human-readable usage string | ||
| if memory_limit_gb > 0: | ||
| memory_ratio = memory_usage_gb / memory_limit_gb | ||
| usage_message = ( | ||
| f"memory {memory_usage_gb:.1f}/{memory_limit_gb:.1f} GB " | ||
| f"({memory_ratio:.0%})" | ||
| ) | ||
| else: | ||
| memory_ratio = 0 | ||
| usage_message = f"memory {memory_usage_gb:.1f} GB (limit unknown)" | ||
|
|
||
| # Check for alerts | ||
| alert = None | ||
|
|
@@ -132,7 +137,7 @@ def _check_memory_and_alert(self, device: str) -> None: | |
| if current_time - self.last_alert_time > self.alert_cooldown: | ||
| alert = MemoryAlert( | ||
| alert_type='critical', | ||
| message=".1f", | ||
| message=usage_message, | ||
| memory_usage_gb=memory_usage_gb, | ||
| memory_limit_gb=memory_limit_gb, | ||
| timestamp=current_time, | ||
|
|
@@ -153,7 +158,7 @@ def _check_memory_and_alert(self, device: str) -> None: | |
| if current_time - self.last_alert_time > self.alert_cooldown: | ||
| alert = MemoryAlert( | ||
| alert_type='warning', | ||
| message=".1f", | ||
| message=usage_message, | ||
|
||
| memory_usage_gb=memory_usage_gb, | ||
| memory_limit_gb=memory_limit_gb, | ||
| timestamp=current_time, | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import logging | ||
|
|
||
| from azchess.utils import memory_monitor as memory_monitor_module | ||
| from azchess.utils.memory_monitor import MemoryMonitor | ||
|
|
||
|
|
||
| def test_memory_alert_messages_include_usage(monkeypatch, caplog): | ||
| monitor = MemoryMonitor(warning_threshold=0.5, critical_threshold=0.8, alert_cooldown=0) | ||
| device = "cuda" | ||
| monitor.device_limits[device] = 10.0 | ||
|
|
||
| monkeypatch.setattr(memory_monitor_module, "get_memory_usage", lambda d: {"memory_gb": 9.0}) | ||
| monkeypatch.setattr(memory_monitor_module, "emergency_memory_cleanup", lambda d: None) | ||
| monkeypatch.setattr(memory_monitor_module, "clear_memory_cache", lambda d: None) | ||
|
|
||
| recorded_alerts = [] | ||
| monitor.alert_callbacks.append(recorded_alerts.append) | ||
|
|
||
| with caplog.at_level(logging.CRITICAL): | ||
| monitor._check_memory_and_alert(device) | ||
|
|
||
| assert recorded_alerts, "Expected a critical alert to be recorded" | ||
| critical_alert = recorded_alerts[-1] | ||
| assert critical_alert.alert_type == "critical" | ||
| assert critical_alert.message == "memory 9.0/10.0 GB (90%)" | ||
| assert any( | ||
| message == "MEMORY_ALERT: memory 9.0/10.0 GB (90%)" | ||
| for message in caplog.messages | ||
| ), "Critical MEMORY_ALERT log should include usage details" | ||
|
|
||
| caplog.clear() | ||
| monkeypatch.setattr(memory_monitor_module, "get_memory_usage", lambda d: {"memory_gb": 7.0}) | ||
| monitor.last_alert_time -= 1 # allow another alert immediately | ||
|
|
||
| with caplog.at_level(logging.WARNING): | ||
| monitor._check_memory_and_alert(device) | ||
|
|
||
| warning_alert = recorded_alerts[-1] | ||
| assert warning_alert.alert_type == "warning" | ||
| assert warning_alert.message == "memory 7.0/10.0 GB (70%)" | ||
| assert any( | ||
| message == "MEMORY_ALERT: memory 7.0/10.0 GB (70%)" | ||
| for message in caplog.messages | ||
| ), "Warning MEMORY_ALERT log should include usage details" |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original message was '.1f' which appears to be a corrupted format string. The fix correctly uses usage_message, but this suggests there may have been a systematic issue with message formatting that should be verified across the codebase.