support cgroups on linux for memory detection#4197
Open
nerzhul wants to merge 4 commits into
Open
Conversation
This will permit to better tune music-assistant when memory constraints are sets on containers
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates system memory detection to account for Linux cgroup limits so memory-based auto-sizing reflects the process’s effective memory budget (especially in containers).
Changes:
- Extend
get_total_system_memory()to return the smaller of host RAM and cgroup memory limit on Linux. - Add helpers to read cgroup v1/v2 memory limit files and interpret “unlimited” sentinels.
- Improve robustness of host RAM detection by handling additional
sysconferror cases.
16 tasks
Contributor
|
Don't explain inner workings of the code in the docstrings (you can use inline comments for that if/when needed). The docstring should provide clarity to the caller of the function/method, not explain how it works technically/internally. |
Comment on lines
+181
to
+195
| for line in fh: | ||
| # mountinfo format: | ||
| # mount_id parent_id major:minor root mountpoint options - | ||
| # fstype source super_options | ||
| fields = line.split() | ||
| if len(fields) < 10: | ||
| continue | ||
| if fields[-3] != "cgroup": | ||
| continue | ||
| # The list of controllers a v1 cgroup mount provides lives in | ||
| # the super_options field (the last field) of mountinfo. | ||
| super_opts = fields[-1].split(",") | ||
| if "memory" not in super_opts: | ||
| continue | ||
| mountpoint = fields[4] |
Comment on lines
+208
to
+216
| if rel == "/": | ||
| rel_path = "memory.max" | ||
| candidates = ("/sys/fs/cgroup/memory.max",) | ||
| else: | ||
| rel_path = f"{rel.lstrip('/')}/memory.max" | ||
| candidates = ( | ||
| f"/sys/fs/cgroup/{rel_path}", | ||
| "/sys/fs/cgroup/memory.max", # fall back to the root cgroup's limit | ||
| ) |
Contributor
|
mypy is failing. Make sure you always run pre-commit before pushing. |
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.
What does this implement/fix?
Currently we look for sysconf, which is not exact available memory when using containers. We must read cgroup values under Linux.
This will permit to better tune music-assistant when memory constraints are sets on containers
Types of changes
bugfixnew-featureenhancementnew-providerbreaking-changerefactordocumentationmaintenancecidependenciesChecklist
pre-commit run --all-filespasses.pytestpasses, and tests have been added/updated undertests/where applicable.music-assistant/modelsis linked.music-assistant/frontendis linked.