-
Notifications
You must be signed in to change notification settings - Fork 11
DM-54645: Add garbage collection metrics to task metadata #562
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8c8982
Add garbage collection metrics to task metadata (DM-54645)
andy-slac 8d29326
Add news fragment
andy-slac ac0bcb1
Fix mypy2 complaint
andy-slac fad68ac
Update doc/changes/DM-54645.misc.md
andy-slac d993585
Apply review suggestions
andy-slac 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added garbage collection metrics to `SingleQuantumExecutor` with metrics stored in task metadata under `quantum.gc_metrics` key. |
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,129 @@ | ||
| # This file is part of pipe_base. | ||
| # | ||
| # Developed for the LSST Data Management System. | ||
| # This product includes software developed by the LSST Project | ||
| # (https://www.lsst.org). | ||
| # See the COPYRIGHT file at the top-level directory of this distribution | ||
| # for details of code ownership. | ||
| # | ||
| # This software is dual licensed under the GNU General Public License and also | ||
| # under a 3-clause BSD license. Recipients may choose which of these licenses | ||
| # to use; please see the files gpl-3.0.txt and/or bsd_license.txt, | ||
| # respectively. If you choose the GPL option then the following text applies | ||
| # (but note that there is still no warranty even if you opt for BSD instead): | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| __all__ = ["GcMetrics"] | ||
|
|
||
| import gc | ||
| from collections import defaultdict | ||
| from types import TracebackType | ||
| from typing import Self | ||
|
|
||
| import pydantic | ||
|
|
||
| from ._task_metadata import TaskMetadata | ||
|
|
||
|
|
||
| def _gc_stats() -> dict[str, list[int]]: | ||
| """Convert result of `gc.get_stats` to a dictionary of lists.""" | ||
| result: dict[str, list[int]] = defaultdict(list) | ||
| for gen_stats in gc.get_stats(): | ||
| for key, stat in gen_stats.items(): | ||
| result[key].append(stat) | ||
| return result | ||
|
|
||
|
|
||
| class GcMetrics(pydantic.BaseModel): | ||
| """Context manager which collects GC metrics and converts them into | ||
| a dictionary suitable for TaskMetadata. | ||
| """ | ||
|
|
||
| start_isenabled: bool | None = None | ||
| """Whether GC is enabled on entering context (`bool` or `None`).""" | ||
|
|
||
| end_isenabled: bool | None = None | ||
| """Whether GC is enabled on exiting context (`bool` or `None`).""" | ||
|
|
||
| start_threshold: list[int] | None = None | ||
| """GC thresholds on entering context (`list`[`int`] or `None`).""" | ||
|
|
||
| end_threshold: list[int] | None = None | ||
| """GC thresholds on exiting context (`list`[`int`] or `None`).""" | ||
|
|
||
| start_count: list[int] | None = None | ||
| """GC collection counts on entering context (`list`[`int`] or `None`).""" | ||
|
|
||
| end_count: list[int] | None = None | ||
| """GC collection counts on exiting context (`list`[`int`] or `None`).""" | ||
|
|
||
| start_stats: dict[str, list[int]] | None = None | ||
| """GC stats on entering context (`dict`[`str`, `list`[`int`]] or `None`). | ||
|
|
||
| These are the same values as returned from `gc.get_stats` but rearranged | ||
| to be indexed by string key first and generation second. | ||
| """ | ||
|
|
||
| end_stats: dict[str, list[int]] | None = None | ||
| """GC stats on exiting context, same format as `start_stats` | ||
| (`dict`[`str`, `list`[`int`]] or `None`). | ||
| """ | ||
|
|
||
| def __enter__(self) -> Self: | ||
| self.start_isenabled = gc.isenabled() | ||
| self.start_threshold = list(gc.get_threshold()) | ||
| self.start_count = list(gc.get_count()) | ||
| self.start_stats = _gc_stats() | ||
| return self | ||
|
|
||
| def __exit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc_val: BaseException | None, | ||
| exc_tb: TracebackType | None, | ||
| ) -> None: | ||
| self.end_isenabled = gc.isenabled() | ||
| self.end_threshold = list(gc.get_threshold()) | ||
|
andy-slac marked this conversation as resolved.
|
||
| self.end_count = list(gc.get_count()) | ||
| self.end_stats = _gc_stats() | ||
|
|
||
| @classmethod | ||
| def from_task_metadata(cls, metadata: TaskMetadata) -> GcMetrics | None: | ||
| """Extract GC metrics from task metadata. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| metadata : `TaskMetadata` | ||
| Metadata written by | ||
| `.single_quantum_executor.SingleQuantumExecutor`. | ||
|
|
||
| Returns | ||
| ------- | ||
| gc_metrics : `GcMetrics` or `None` | ||
| GC metrics for this quantum, or `None` if the expected fields were | ||
| not found. | ||
| """ | ||
| try: | ||
| quantum_metadata = metadata["quantum"] | ||
| except KeyError: | ||
| return None | ||
| try: | ||
| gc_metadata = quantum_metadata["gc_metrics"] | ||
| except KeyError: | ||
| return None | ||
|
|
||
| return GcMetrics(**gc_metadata.to_dict()) | ||
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
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
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
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.
You are worried a task is going to disable GC?
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.
I'm not worried, but I want to know if it happens. I imagine some tasks may want to do that (but they may also want to re-enable it on return).