feat(mem): CBM_MAX_MEMORY_MB explicit memory-budget override (#580)#586
Open
yangsec888 wants to merge 1 commit into
Open
feat(mem): CBM_MAX_MEMORY_MB explicit memory-budget override (#580)#586yangsec888 wants to merge 1 commit into
yangsec888 wants to merge 1 commit into
Conversation
Add a CBM_MAX_MEMORY_MB env var that overrides the ram_fraction × total_RAM budget with an explicit cap in MiB. Lets RAM-constrained hosts (no cgroup) cap the in-memory graph, and lets containers pin a budget below the cgroup limit so headroom is left for sibling processes. Same precedence as CBM_WORKERS: explicit override > implicit detection. Budget math is extracted into a pure, testable cbm_mem_resolve_budget() since cbm_mem_init is one-shot per process. Logs source=env|ram_fraction on mem.init and warns on invalid/clamped values. Closes DeusData#580. Signed-off-by: Sam Li <yangsec888@gmail.com>
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.
Summary
Adds a
CBM_MAX_MEMORY_MBenv var that overrides theram_fraction × total_RAMmemory budget with an explicit cap in MiB. Closes the--max-memoryrequest in #580 and gives #563 a workaround on hosts without cgroup limits (macOS / bare metal).Motivation
The knowledge graph is built fully in memory and the budget is sized to 50% of detected RAM. Two gaps remain even after cgroup-aware detection landed:
0.5 × cgroupcan still be too high. There was no way to pin a lower, explicit budget.CBM_MAX_MEMORY_MBlets the operator set the in-memory budget directly, with the same precedence shape as the existingCBM_WORKERSoverride: explicit override > implicit detection.Changes
src/foundation/mem.ccbm_mem_resolve_budget(total_ram, ram_fraction, max_memory_mb)— single source of truth for the budget number (no globals/env), testable.cbm_mem_initreadsCBM_MAX_MEMORY_MBviacbm_safe_getenv, applies it, logssource=env|ram_fractionon themem.initline, and warns onmem.max.invalid(non-numeric/≤0) /mem.max.clamped(above total RAM).src/foundation/mem.h— declarecbm_mem_resolve_budget; document the env var.README.md— addCBM_MAX_MEMORY_MBto the env-var table.tests/test_mem.c— 6 cases over the pure resolver.Semantics
CBM_MAX_MEMORY_MBram_fraction × total_ram(unchanged default)total_ram(+mem.max.clampedwarn)0, negative, non-numericmem.max.invalidwarn)mem.initnow logs the chosen source, e.g.:Design notes
total_ramwhen known so it can never claim more than physical/cgroup RAM, but is honored as-is when detection returns 0 (so it also rescues hosts where RAM detection fails).cbm_mem_initis one-shot per process (atomic CAS) and can't be re-exercised in-process.Testing
tests/test_mem.cresolver cases.cbm_mem_resolve_budget(12/12 checks pass).clang-format --dry-run --Werrorclean on all changed files.CBM_MAX_MEMORY_MB=512 codebase-memory-mcp …→mem.init … source=env.