Skip to content

support cgroups on linux for memory detection#4197

Open
nerzhul wants to merge 4 commits into
music-assistant:devfrom
nerzhul:feat/better_memory_detection
Open

support cgroups on linux for memory detection#4197
nerzhul wants to merge 4 commits into
music-assistant:devfrom
nerzhul:feat/better_memory_detection

Conversation

@nerzhul

@nerzhul nerzhul commented Jun 13, 2026

Copy link
Copy Markdown

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

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • New music/player/metadata/plugin provider — new-provider
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — documentation
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Checklist

  • The code change is tested and works locally.
  • [ x pre-commit run --all-files passes.
  • pytest passes, and tests have been added/updated under tests/ where applicable.
  • For changes to shared models, the companion PR in music-assistant/models is linked.
  • For changes affecting the UI, the companion PR in music-assistant/frontend is linked.
  • I have read and complied with the project's AI Policy for any AI-assisted contributions.
  • I have raised a PR against the documentation repository targeting the main or beta branch as appropriate.

This will permit to better tune music-assistant when memory constraints are sets on containers
Copilot AI review requested due to automatic review settings June 13, 2026 09:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 sysconf error cases.

Comment thread music_assistant/helpers/util.py Outdated
Comment thread music_assistant/helpers/util.py Outdated
@OzGav

OzGav commented Jun 13, 2026

Copy link
Copy Markdown
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.

Copilot AI review requested due to automatic review settings June 13, 2026 09:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

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
)
@OzGav

OzGav commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

mypy is failing. Make sure you always run pre-commit before pushing.

@marcelveldt marcelveldt changed the title feat: memory detection now support cgroups on linux support cgroups on linux for memory detection Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants