Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: count reclaimable active file cache as available host RAM under a cgroup limit (#14475)
Daniel Handcf5c40096e7
diff --git a/studio/backend/tests/test_host_memory_cgroup.py b/studio/backend/tests/test_host_memory_cgroup.pyindex 98b2de79ccb..465866762c9 100644--- a/studio/backend/tests/test_host_memory_cgroup.py+++ b/studio/backend/tests/test_host_memory_cgroup.py@@ -376,3 +376,138 @@ def test_api_system_memory_without_a_limit_is_unchanged(fake_cgroup, host, monkeassert memory["total_gb"] == round(host["total"] / GIB, 2)assert memory["available_gb"] == round(host["available"] / GIB, 2)assert memory["percent_used"] == 20.0+++def _stat(directory: Path, **counters) -> None:+ _write(directory / "memory.stat", "".join(f"{k} {v}\n" for k, v in counters.items()))+++@reader+def test_v2_active_file_cache_is_reclaimable(fake_cgroup, host):+ """Kaggle: twice-read model files sit on active_file; counting them as used shrank headroom 17 -> 3 GiB."""+ fake_cgroup.membership("0::/\n")+ _v2(fake_cgroup.root, "/", limit = 32 * GIB, current = 30 * GIB)+ _stat(+ fake_cgroup.root,+ anon = 15 * GIB,+ file = 15 * GIB,+ inactive_file = 1 * GIB,+ active_file = 14 * GIB,+ shmem = 0,+ )+ # 32 - (30 - 1 inactive - 14 active) = 17 GiB, not 32 - (30 - 1) = 3 GiB.+ assert host_memory.cgroup_headroom_mib() == 17 * 1024+ assert host_memory.usable_host_ram_mib() == 17 * 1024+ assert host_memory.cgroup_limit_mib() == 32 * 1024+++@reader+def test_v2_shmem_is_never_counted_as_free(fake_cgroup, host):+ """shmem/tmpfs is in ``file`` but on the anon LRU: not droppable."""+ fake_cgroup.membership("0::/\n")+ _v2(fake_cgroup.root, "/", limit = 32 * GIB, current = 28 * GIB)+ _stat(+ fake_cgroup.root,+ anon = 8 * GIB,+ file = 20 * GIB,+ shmem = 12 * GIB,+ inactive_anon = 4 * GIB,+ active_anon = 16 * GIB,+ inactive_file = 2 * GIB,+ active_file = 6 * GIB,+ )+ headroom = host_memory.cgroup_headroom_mib()+ # 32 - (28 - 2 - 6) = 12 GiB; crediting ``file`` (incl. the 12 GiB shmem) would claim 24 GiB.+ assert headroom == 12 * 1024+ assert headroom < (32 - (28 - 20)) * 1024+++@reader+def test_v2_mapped_dirty_and_writeback_active_pages_are_not_credited(fake_cgroup, host):+ fake_cgroup.membership("0::/\n")+ _v2(fake_cgroup.root, "/", limit = 32 * GIB, current = 30 * GIB)+ _stat(+ fake_cgroup.root,+ inactive_file = 2 * GIB,+ active_file = 10 * GIB,+ file_mapped = 4 * GIB,+ file_dirty = 1 * GIB,+ file_writeback = 1 * GIB,+ )+ # Active credit = 10 - 4 - 1 - 1 = 4 GiB: 32 - (30 - 2 - 4) = 8 GiB.+ assert host_memory.cgroup_headroom_mib() == 8 * 1024+++@reader+def test_v2_never_below_the_inactive_only_reading(fake_cgroup, host):+ """Over-subtraction clamps at zero."""+ fake_cgroup.membership("0::/\n")+ _v2(fake_cgroup.root, "/", limit = 32 * GIB, current = 30 * GIB)+ _stat(fake_cgroup.root, inactive_file = 2 * GIB, active_file = 3 * GIB, file_mapped = 9 * GIB)+ assert host_memory.cgroup_headroom_mib() == 4 * 1024+++@reader+def test_v2_host_meminfo_still_bounds_the_reading(fake_cgroup, host):+ fake_cgroup.membership("0::/\n")+ host["available"] = 5 * GIB+ _v2(fake_cgroup.root, "/", limit = 32 * GIB, current = 30 * GIB)+ _stat(fake_cgroup.root, inactive_file = 1 * GIB, active_file = 14 * GIB)+ assert host_memory.cgroup_headroom_mib() == 17 * 1024+ assert host_memory.usable_host_ram_mib() == 5 * 1024+++@reader+def test_v2_ancestor_level_uses_its_own_file_cache(fake_cgroup, host):+ fake_cgroup.membership("0::/kaggle.slice/run.scope\n")+ leaf = _v2(fake_cgroup.root, "kaggle.slice/run.scope", limit = "max", current = 10 * GIB)+ parent = _v2(fake_cgroup.root, "kaggle.slice", limit = 20 * GIB, current = 19 * GIB)+ _stat(leaf, inactive_file = 0, active_file = 9 * GIB)+ _stat(parent, inactive_file = 1 * GIB, active_file = 10 * GIB)+ assert host_memory.cgroup_headroom_mib() == 12 * 1024+++@reader+def test_v1_hierarchical_active_file_is_reclaimable_and_shmem_is_not(fake_cgroup, host):+ fake_cgroup.membership("12:memory:/docker/abc\n")+ d = fake_cgroup.root / "memory" / "docker" / "abc"+ _write(d / "memory.limit_in_bytes", str(16 * GIB))+ _write(d / "memory.usage_in_bytes", str(14 * GIB))+ _stat(+ d,+ inactive_file = 50 * GIB,+ active_file = 50 * GIB,+ total_cache = 9 * GIB,+ total_shmem = 2 * GIB,+ total_inactive_file = 1 * GIB,+ total_active_file = 6 * GIB,+ total_mapped_file = 1 * GIB,+ total_dirty = 0,+ total_writeback = 0,+ )+ # 16 - (14 - 1 - (6 - 1)) = 8 GiB; total_cache (incl. 2 GiB shmem) would claim 11 GiB.+ assert host_memory.cgroup_headroom_mib() == 8 * 1024+++@reader+def test_v1_without_total_counters_uses_the_level_ones(fake_cgroup, host):+ fake_cgroup.membership("12:memory:/docker/abc\n")+ d = fake_cgroup.root / "memory" / "docker" / "abc"+ _write(d / "memory.limit_in_bytes", str(16 * GIB))+ _write(d / "memory.usage_in_bytes", str(14 * GIB))+ _stat(d, cache = 8 * GIB, inactive_file = 1 * GIB, active_file = 6 * GIB, mapped_file = 2 * GIB)+ assert host_memory.cgroup_headroom_mib() == 7 * 1024+++@reader+def test_v1_memsw_headroom_credits_active_file_cache(fake_cgroup, host):+ fake_cgroup.membership("12:memory:/docker/abc\n")+ d = fake_cgroup.root / "memory" / "docker" / "abc"+ _write(d / "memory.limit_in_bytes", str(16 * GIB))+ _write(d / "memory.usage_in_bytes", str(14 * GIB))+ _write(d / "memory.memsw.limit_in_bytes", str(24 * GIB))+ _write(d / "memory.memsw.usage_in_bytes", str(15 * GIB))+ _stat(d, total_inactive_file = 1 * GIB, total_active_file = 6 * GIB)+ assert host_memory.cgroup_v1_memsw_headroom_bytes() == (24 - (15 - 7)) * GIB+ # RAM room 16 - (14 - 7) = 9, combined room 16: 7 GiB of swap left.+ assert host_memory.cgroup_swap_allowance_bytes(100 * GIB) == 7 * GIBdiff --git a/studio/backend/utils/host_memory.py b/studio/backend/utils/host_memory.pyindex fe008e68fc4..bfd4ed1280c 100644--- a/studio/backend/utils/host_memory.py+++ b/studio/backend/utils/host_memory.py@@ -38,20 +38,41 @@ def _integer(raw: Optional[str], *, limit: bool = False) -> Optional[int]:return value-def _stat_integer(path: str, *keys: str) -> int:- """The first requested byte counter present in a ``memory.stat`` file, else 0."""+# (inactive, active, mapped, dirty, writeback); v1 total_* pairs with usage_in_bytes, per-level is the fallback.+_V2_FILE_KEYS = ("inactive_file", "active_file", "file_mapped", "file_dirty", "file_writeback")+_V1_TOTAL_FILE_KEYS = (+ "total_inactive_file",+ "total_active_file",+ "total_mapped_file",+ "total_dirty",+ "total_writeback",+)+_V1_LOCAL_FILE_KEYS = ("inactive_file", "active_file", "mapped_file", "dirty", "writeback")+++def _reclaimable_file_bytes(path: str, *, v1: bool) -> int:+ """Droppable page cache in bytes: inactive_file + (active_file - mapped - dirty - writeback, conservative).++ shmem/tmpfs is on the anon LRU, never counted. Not crediting active_file counted twice-read models as used."""values: dict[str, int] = {}try:with open(path, "r", encoding = "utf-8") as f:for line in f:parts = line.split()- if len(parts) == 2 and parts[0] in keys:+ if len(parts) == 2:value = _integer(parts[1])if value is not None:values[parts[0]] = valueexcept (OSError, UnicodeDecodeError):return 0- return next((values[key] for key in keys if key in values), 0)+ if not v1:+ keys = _V2_FILE_KEYS+ elif _V1_TOTAL_FILE_KEYS[0] in values:+ keys = _V1_TOTAL_FILE_KEYS+ else:+ keys = _V1_LOCAL_FILE_KEYS+ inactive, active, mapped, dirty, writeback = (values.get(key, 0) for key in keys)+ return inactive + max(active - mapped - dirty - writeback, 0)def _directories(root: str, relative: Optional[str]) -> list[str]:@@ -75,7 +96,7 @@ def cgroup_memory_budgets(cgroup_root: Optional[str] = None, proc_self_cgroup: Optional[str] = None) -> list[tuple[int, int]]:"""``(remaining, limit)`` bytes per enforcing limit on this cgroup and its ancestors (an ancestor- can bind through sibling usage). Usage excludes ``inactive_file``; empty = no finite limit."""+ can bind through sibling usage), less reclaimable page cache; empty = no finite limit."""cgroup_root = CGROUP_ROOT if cgroup_root is None else cgroup_rootproc_self_cgroup = PROC_SELF_CGROUP if proc_self_cgroup is None else proc_self_cgrouptry:@@ -93,7 +114,8 @@ def cgroup_memory_budgets(used = _integer(_first_line(os.path.join(directory, "memory.current")))if used is not None:used = max(- 0, used - _stat_integer(os.path.join(directory, "memory.stat"), "inactive_file")+ 0,+ used - _reclaimable_file_bytes(os.path.join(directory, "memory.stat"), v1 = False),)budgets.append((limit if used is None else limit - used, limit))@@ -110,27 +132,19 @@ def cgroup_memory_budgets(continueused = _integer(_first_line(os.path.join(directory, "memory.usage_in_bytes")))if used is not None:- # Hierarchical v1 usage pairs with total_inactive_file.used = max(- 0,- used- - _stat_integer(- os.path.join(directory, "memory.stat"), "total_inactive_file", "inactive_file"- ),+ 0, used - _reclaimable_file_bytes(os.path.join(directory, "memory.stat"), v1 = True))budgets.append((limit if used is None else limit - used, limit))return budgetsdef _v1_reclaimable_usage(directory: str, raw: Optional[int]) -> Optional[int]:- """A v1 ``*.usage_in_bytes`` reading less the level's inactive file cache (``total_inactive_file``, which the- kernel reclaims before it charges against the limit), as ``cgroup_memory_budgets`` reads RAM usage."""+ """A v1 ``*.usage_in_bytes`` reading less reclaimable file cache, as ``cgroup_memory_budgets`` reads it."""if raw is None:return None- inactive = _stat_integer(- os.path.join(directory, "memory.stat"), "total_inactive_file", "inactive_file"- )- return max(raw - inactive, 0)+ reclaimable = _reclaimable_file_bytes(os.path.join(directory, "memory.stat"), v1 = True)+ return max(raw - reclaimable, 0)def cgroup_swap_allowance_bytes(