diff --git a/docker-compose.yml b/docker-compose.yml index 755f733..016f864 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,12 +40,18 @@ services: restart: unless-stopped # Cap container memory so the *container* gets OOM-killed and restarted # by Docker's restart policy, not the host. Size as roughly: - # ~300 MB base + <= largest atlas you allow users to download + ~6 MB + # ~300 MB base + 2 x the largest atlas you allow users to compute with + # (decoding holds every decompressed chunk *and* the assembled array at + # once: whs_sd_rat_39um is a 1.07 GB array and peaks at +2.1 GB) + ~6 MB # per open browser tab. + # + # The app keeps decoded atlases in a cache budgeted at half this limit + # (read from the cgroup), so a larger limit also means fewer re-decodes + # when users switch atlases. PIXELMAP_ATLAS_CACHE_MB overrides the budget. # deploy: # resources: # limits: - # memory: 2g + # memory: 3g volumes: # Persists brainglobe atlas downloads across container restarts. diff --git a/docs/getting-started.md b/docs/getting-started.md index 32aeaf1..7b299b7 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -56,8 +56,11 @@ For production deployments, use **Docker Compose** — see the included `docker- **Memory & healthcheck**: point the healthcheck at `/liveness`, never `/app` — `/app` builds a full Bokeh session on every probe. Set a container memory limit (`deploy.resources.limits.memory` in Compose) so the container, not -the host, gets restarted under memory pressure. Keep `NUM_PROCS=1` unless -RAM is sized for multiple copies of every cache. Mount the +the host, gets restarted under memory pressure; size it at ~300 MB plus +twice the largest atlas users may compute with (decoding an atlas briefly +needs ~2x its array). Decoded atlases stay in a cache budgeted at half +that limit (`PIXELMAP_ATLAS_CACHE_MB` overrides it). Keep `NUM_PROCS=1` +unless RAM is sized for multiple copies of every cache. Mount the `brainglobe_cache` volume so downloaded atlases persist across restarts. RSS is logged to stdout every 60 s. ::: diff --git a/env.template b/env.template index fdbdadd..c0708b9 100644 --- a/env.template +++ b/env.template @@ -9,6 +9,11 @@ ADDRESS=localhost # sized for N x (base memory + atlases). NUM_PROCS=1 +# How many MB of decoded atlas volumes each process keeps resident. Unset, +# it is half the container's cgroup memory limit, or 2048 outside a +# container. Least-recently-used atlases are evicted past this. +# PIXELMAP_ATLAS_CACHE_MB=1536 + # Visitor analytics configuration ANALYTICS_DB_PATH=/app/output/visitor_analytics.sqlite3 ANALYTICS_TRUST_PROXY_HEADERS=false diff --git a/pixelmap/anatomy/_memory.py b/pixelmap/anatomy/_memory.py new file mode 100644 index 0000000..c534f60 --- /dev/null +++ b/pixelmap/anatomy/_memory.py @@ -0,0 +1,69 @@ +"""Process-memory helpers for the atlas layer. + +Two facts about the deployed server that the atlas code has to reason +about, kept here so that every caller reasons about them the same way: + +* The container runs under a cgroup memory cap (Compose's + ``deploy.resources.limits.memory``), and that cap is enforced on RSS. + :func:`cgroup_memory_limit_bytes` reads it, so cache budgets and size + gates can be derived from the real limit instead of guessed. +* Dropping the last reference to a large array does not lower RSS. glibc + keeps freed pages in its arenas for reuse (``MALLOC_ARENA_MAX`` caps how + many arenas there are, not what they retain). Measured after clearing + every atlas cache: 1,188 MB RSS, then 211 MB after ``malloc_trim``. An + eviction that doesn't trim is one the OOM killer never sees, which is + what :func:`release_freed_memory` is for. +""" + +from __future__ import annotations + +import ctypes +import gc +from pathlib import Path + +# Where a cgroup exposes the memory cap this process runs under -- v2 first, +# then v1. +_CGROUP_LIMIT_FILES = ( + "/sys/fs/cgroup/memory.max", + "/sys/fs/cgroup/memory/memory.limit_in_bytes", +) + + +def cgroup_memory_limit_bytes(paths=None) -> int | None: + """The container's memory cap, or ``None`` when there isn't one.""" + for path in _CGROUP_LIMIT_FILES if paths is None else paths: + try: + raw = Path(path).read_text().strip() + except OSError: + continue + if raw == "max": + return None + try: + limit = int(raw) + except ValueError: + continue + # cgroup v1 reports a huge number (~2**63) for "unlimited". + if limit <= 0 or limit >= 1 << 60: + return None + return limit + return None + + +_libc = None + + +def release_freed_memory() -> None: + """Return freed heap pages to the OS. + + Collects garbage first so that reference cycles holding large arrays are + actually freed, then asks glibc to trim. A no-op on platforms without + ``malloc_trim`` (macOS, musl). + """ + global _libc + gc.collect() + try: + if _libc is None: + _libc = ctypes.CDLL("libc.so.6") + _libc.malloc_trim(0) + except (OSError, AttributeError): + pass diff --git a/pixelmap/anatomy/atlas.py b/pixelmap/anatomy/atlas.py index 7fc1090..dca3f06 100644 --- a/pixelmap/anatomy/atlas.py +++ b/pixelmap/anatomy/atlas.py @@ -2,8 +2,10 @@ Why a wrapper: -* Cache atlas instances per-process so repeated lookups don't re-load the - annotation volume (hundreds of MB once decompressed). +* Cache atlas instances per-process so repeated lookups don't re-read the + atlas metadata, and hold the decompressed annotation volumes (hundreds of + MB to several GB each) in one byte-bounded cache -- see "Who owns the + annotation volumes" below. * Expose a tiny ``RegionInfo`` record so the rest of PixelMap doesn't depend on brainglobe's object model. * Keep every network call off the caller's thread, because on the hosted @@ -26,6 +28,28 @@ ~9 MB of compressed chunks, against ~1.3 GB of tiffs under v2. That is what makes baking them into the Docker image cheap and reliable. +Who owns the annotation volumes +------------------------------- +Exactly one thing: :data:`_VOLUMES`, a byte-budgeted LRU keyed by atlas name, +read through :func:`canonical_annotation`. Everything else holds volumes only +transiently. This is deliberate, and worth defending: + +* An ``lru_cache`` counts *entries*, not bytes. ``maxsize=4`` is four + 38 MB atlases or four 4.8 GB ones (allen_mouse_10um), so it never related to + the container's memory limit. The budget here is in bytes and defaults to + half the cgroup limit (``PIXELMAP_ATLAS_CACHE_MB`` overrides it). +* Two caches with independent LRU orders leak. ``get_atlas`` and the old + ``canonical_annotation`` each evicted on their own schedule; once one dropped + an atlas the other still pinned its array (a transposed *view* keeps the + base alive), and the next ``ensure_downloaded`` loaded a second copy. + brainglobe also caches the array on the atlas object itself, so + :func:`_take_annotation` detaches it and ``get_atlas`` only ever holds + cheap metadata objects. +* Dropping a reference is not freeing memory. glibc keeps freed pages in its + arenas -- after clearing every cache the process still sat at 1.2 GB RSS + until ``malloc_trim``. The container limit counts RSS, so every eviction + and every load ends with :func:`_memory.release_freed_memory`. + Why the network never runs inline --------------------------------- The deployed app is a single-process Panel/Bokeh server: a blocking call on @@ -40,7 +64,10 @@ from __future__ import annotations import functools +import logging +import os import threading +from collections import OrderedDict from dataclasses import dataclass from pathlib import Path @@ -52,10 +79,13 @@ get_downloaded_atlases, ) +from pixelmap.anatomy import _memory from pixelmap.anatomy._registry_snapshot import REGISTRY_SNAPSHOT _DEFAULT_ATLAS = "allen_mouse_25um" +log = logging.getLogger(__name__) + @dataclass(frozen=True) class RegionInfo: @@ -81,10 +111,12 @@ def get_atlas(name: str = _DEFAULT_ATLAS): neither is free on a cold cache — see :func:`ensure_downloaded` for getting that cost off the event loop. - The cache is bounded so a server that sees many atlases doesn't hold every - annotation volume it has ever decoded. Nothing else may keep a strong - reference to an atlas object, or that bound stops meaning anything (see - :func:`_region_info_from_id`). + Objects here are metadata only -- manifest, structures table, an S3 + handle -- a few MB each. The annotation volume is *not* kept on them: + :func:`_take_annotation` detaches it into :data:`_VOLUMES`, the one place + volumes live. Reading ``.annotation`` directly off one of these objects + re-materialises the array on the instance, so don't; go through + :func:`canonical_annotation`. """ return BrainGlobeAtlas(name, check_latest=False) @@ -96,8 +128,13 @@ def ensure_downloaded(name: str = _DEFAULT_ATLAS) -> None: a single place so callers can push it onto a worker thread — which the GUI does, because on the server the calling thread also serves every other session. A no-op once the atlas is local. + + Loads into :data:`_VOLUMES` rather than merely touching ``.annotation``: + the compute that follows reads the canonical volume, so this is the copy + it will use, and touching the attribute instead would leave a second copy + cached on the atlas object. """ - _ = get_atlas(name).annotation + canonical_annotation(name) @functools.lru_cache(maxsize=1) @@ -434,7 +471,6 @@ def anatomical_axes(atlas) -> dict[str, _AnatAxis]: return axes -@functools.lru_cache(maxsize=4) def canonical_annotation(atlas_name: str): """Return ``(annotation, resolution)`` reoriented to canonical ``(AP, DV, ML)``. @@ -444,11 +480,20 @@ def canonical_annotation(atlas_name: str): assumes this layout, so funnelling every atlas through here is what lets non-Allen orientations work. For an already-``asr`` atlas this is a no-op (identity transpose, no flips), so Allen behavior is unchanged. + + Served from :data:`_VOLUMES`, the byte-bounded cache that is the only + long-lived owner of annotation arrays. Callers get the cached array + itself: treat it as read-only. """ + return _VOLUMES.get(atlas_name) + + +def _load_canonical(atlas_name: str): + """Materialise ``atlas_name``'s volume in canonical layout (uncached).""" atlas = get_atlas(atlas_name) axes = anatomical_axes(atlas) order = (axes["AP"].array_axis, axes["DV"].array_axis, axes["ML"].array_axis) - arr = np.transpose(atlas.annotation, order) + arr = np.transpose(_take_annotation(atlas), order) flip_axes = tuple(i for i, kind in enumerate(("AP", "DV", "ML")) if axes[kind].flip) if flip_axes: arr = np.flip(arr, axis=flip_axes) @@ -458,6 +503,138 @@ def canonical_annotation(atlas_name: str): return arr, res +def _take_annotation(atlas) -> np.ndarray: + """Read the atlas's annotation and detach it from the atlas object. + + brainglobe's ``annotation`` property caches the array on the instance + (``_annotation``) after the first read. Left there, ``get_atlas``'s cache + becomes a second owner of every volume, with its own eviction order -- the + duplicate-copy leak described in the module docstring. Clearing the slot + leaves the object metadata-only again; a later read would simply reload + from the local zarr chunks (no network, the chunks are cached). + + Test doubles that expose ``annotation`` as a plain attribute have no slot + to clear, and the ``is`` check keeps this from touching anything else. + """ + arr = atlas.annotation + if getattr(atlas, "_annotation", None) is arr: + atlas._annotation = None + return arr + + +def _default_budget_bytes() -> int: + """How many bytes of annotation volumes to keep resident. + + ``PIXELMAP_ATLAS_CACHE_MB`` wins. Otherwise half the cgroup limit: the + other half covers the Bokeh baseline (~300 MB) and the transient peak of + decoding the next volume (dask holds every decompressed chunk *and* the + assembled array at once, ~2x the array). Outside a container, 2 GB. + """ + env = os.environ.get("PIXELMAP_ATLAS_CACHE_MB") + if env: + return int(float(env) * 2**20) + limit = _memory.cgroup_memory_limit_bytes() + if limit is None: + return 2 * 2**30 + return max(limit // 2, 256 * 2**20) + + +class _VolumeCache: + """Byte-bounded LRU of canonical annotation volumes. + + The sole long-lived owner of annotation arrays in the process. Evicts + least-recently-used volumes until the total fits ``budget_bytes``, then + trims the heap so the eviction actually lowers RSS. A single volume + larger than the whole budget still loads (evicting everything else) -- + refusing it is a UI decision, not a cache one. + + Loads run outside the lock so the Bokeh event loop can keep answering + lookups for a cached atlas while a worker thread decodes another one; + concurrent requests for the *same* atlas wait for the first load rather + than decoding a second copy. + """ + + def __init__(self, budget_bytes: int | None = None): + self._budget = budget_bytes + self._entries: OrderedDict[str, tuple[np.ndarray, np.ndarray]] = OrderedDict() + self._lock = threading.Lock() + self._loading: dict[str, threading.Event] = {} + + @property + def budget_bytes(self) -> int: + if self._budget is None: + self._budget = _default_budget_bytes() + return self._budget + + def resident_bytes(self) -> int: + with self._lock: + return sum(arr.nbytes for arr, _ in self._entries.values()) + + def get(self, name: str): + while True: + with self._lock: + entry = self._entries.get(name) + if entry is not None: + self._entries.move_to_end(name) + return entry + pending = self._loading.get(name) + if pending is None: + pending = self._loading[name] = threading.Event() + break + # Someone else is decoding this atlas; wait, then re-check. If + # their load failed the entry is still absent and we try ourselves. + pending.wait() + try: + arr, res = _load_canonical(name) + except BaseException: + # Waiters must wake either way; with no entry present they retry. + with self._lock: + del self._loading[name] + pending.set() + raise + return self._insert(name, arr, res, pending) + + def _insert(self, name, arr, res, pending): + evicted = [] + with self._lock: + self._entries[name] = (arr, res) + self._entries.move_to_end(name) + total = sum(a.nbytes for a, _ in self._entries.values()) + while total > self.budget_bytes and len(self._entries) > 1: + old, (old_arr, _) = self._entries.popitem(last=False) + total -= old_arr.nbytes + evicted.append((old, old_arr.nbytes)) + del self._loading[name] + pending.set() + for old, nbytes in evicted: + log.info("atlas cache: evicted %s (%d MB) to stay under %d MB", + old, nbytes >> 20, self.budget_bytes >> 20) + # Every load leaves dask's decompressed chunks in the heap; every + # eviction leaves the array there. Either way, give it back now. + _memory.release_freed_memory() + return arr, res + + def evict(self, name: str) -> None: + with self._lock: + dropped = self._entries.pop(name, None) is not None + if dropped: + _memory.release_freed_memory() + + def clear(self) -> None: + with self._lock: + had = bool(self._entries) + self._entries.clear() + if had: + _memory.release_freed_memory() + + # The lru_cache this replaced was cleared as ``canonical_annotation.cache_clear()``. + cache_clear = clear + + +_VOLUMES = _VolumeCache() +canonical_annotation.cache_clear = _VOLUMES.clear # type: ignore[attr-defined] + + def volume_center_um(name: str = _DEFAULT_ATLAS) -> tuple[float, float, float]: """Geometric center of the atlas volume as ``(AP, ML, DV)`` µm. diff --git a/pixelmap/anatomy/schematic.py b/pixelmap/anatomy/schematic.py index 141d915..37e85cd 100644 --- a/pixelmap/anatomy/schematic.py +++ b/pixelmap/anatomy/schematic.py @@ -14,8 +14,6 @@ from __future__ import annotations -import functools - import numpy as np from matplotlib.figure import Figure from matplotlib.lines import Line2D @@ -31,12 +29,16 @@ _OUTLINE_RGBA = (0.2, 0.2, 0.25, 0.8) -@functools.lru_cache(maxsize=4) def _atlas_data(atlas_name: str): - """Cached ``(atlas, annotation, resolution)`` in canonical ``(AP, DV, ML)``. + """``(atlas, annotation, resolution)`` in canonical ``(AP, DV, ML)``. Going through :func:`~pixelmap.anatomy.atlas.canonical_annotation` means the slicing/projection below works for any atlas orientation, not just Allen's. + + Deliberately not cached here: both pieces are already cached upstream, and + an ``lru_cache`` on this tuple was a third, independent owner of the + annotation volume -- it kept evicted atlases resident (see "Who owns the + annotation volumes" in :mod:`pixelmap.anatomy.atlas`). """ atlas = get_atlas(atlas_name) annotation, resolution = canonical_annotation(atlas_name) diff --git a/tests/test_anatomy_atlas.py b/tests/test_anatomy_atlas.py index 504763d..c48918f 100644 --- a/tests/test_anatomy_atlas.py +++ b/tests/test_anatomy_atlas.py @@ -13,6 +13,7 @@ import numpy as np import pytest +from pixelmap.anatomy import _memory from pixelmap.anatomy import atlas as atlas_module from pixelmap.anatomy import regions as regions_module from pixelmap.anatomy._registry_snapshot import REGISTRY_SNAPSHOT @@ -370,23 +371,187 @@ def test_unreadable_cache_falls_back_instead_of_raising(self, monkeypatch, tmp_p assert atlas_module._registry_from_disk() is None -class TestEnsureDownloaded: - def test_materialises_the_annotation(self, monkeypatch): - reads = [] +def _lazy_atlas_cls(shape=(2, 2, 2), reads=None): + """A fake that mimics brainglobe v3: ``annotation`` is a property that + materialises the array on first read and caches it on ``_annotation``.""" + reads = [] if reads is None else reads - class _A: - def __init__(self, name, **_kwargs): - self.name = name + class _A: + def __init__(self, name, **_kwargs): + self.name = name + self.orientation = "asr" + self.resolution = (25.0, 25.0, 25.0) + self.shape = shape + self.structures = {} + self._annotation = None - @property - def annotation(self): + @property + def annotation(self): + if self._annotation is None: reads.append(self.name) - return np.zeros((2, 2, 2), np.int32) + self._annotation = np.ones(shape, np.uint32) + return self._annotation + + return _A + - monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _A) +class TestEnsureDownloaded: + def test_materialises_the_annotation(self, monkeypatch): + reads = [] + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _lazy_atlas_cls(reads=reads)) atlas_module.ensure_downloaded("some_atlas") assert reads == ["some_atlas"] + def test_loads_the_copy_the_overlay_will_use(self, monkeypatch): + # The worker-thread download and the later compute must share one + # array, not decode the volume twice. + reads = [] + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _lazy_atlas_cls(reads=reads)) + atlas_module.ensure_downloaded("some_atlas") + atlas_module.canonical_annotation("some_atlas") + atlas_module.lookup_regions("some_atlas", np.array([[25.0, 25.0, 25.0]])) + assert reads == ["some_atlas"] + + +class TestVolumeCache: + """:data:`_VOLUMES` is the only long-lived owner of annotation arrays. + + Regression tests for the second production OOM: ``get_atlas`` and + ``canonical_annotation`` were separate ``lru_cache``s with separate + eviction orders, and brainglobe caches the array on the atlas object too. + Evict one, and the other still pinned the volume; reload, and a second + copy appeared. Meanwhile ``maxsize=4`` bounded a *count*, not bytes. + """ + + @pytest.fixture(autouse=True) + def _no_trim(self, monkeypatch): + # malloc_trim is real and harmless, but keep the tests about ownership. + monkeypatch.setattr(_memory, "release_freed_memory", lambda: None) + + @staticmethod + def _budget(monkeypatch, nbytes): + monkeypatch.setattr(atlas_module._VOLUMES, "_budget", nbytes) + + def test_annotation_is_detached_from_the_atlas_object(self, monkeypatch): + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _lazy_atlas_cls()) + arr, _ = atlas_module.canonical_annotation("x") + atlas = atlas_module.get_atlas("x") + assert atlas._annotation is None, "get_atlas must hold metadata only" + assert arr.base is not None and arr.base.shape == (2, 2, 2) + + def test_evicting_the_atlas_object_does_not_reload_the_volume(self, monkeypatch): + reads = [] + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _lazy_atlas_cls(reads=reads)) + before, _ = atlas_module.canonical_annotation("x") + atlas_module.get_atlas.cache_clear() # the old duplicate-copy trigger + atlas_module.ensure_downloaded("x") + after, _ = atlas_module.canonical_annotation("x") + assert after is before + assert reads == ["x"] + + def test_evicts_by_bytes_not_by_count(self, monkeypatch): + cls = _lazy_atlas_cls(shape=(4, 4, 4)) # 256 B per volume + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", cls) + self._budget(monkeypatch, 3 * 256) + for name in "abcde": # 5 loads, budget fits 3 + atlas_module.canonical_annotation(name) + assert list(atlas_module._VOLUMES._entries) == ["c", "d", "e"] + assert atlas_module._VOLUMES.resident_bytes() == 3 * 256 + + def test_recent_use_protects_from_eviction(self, monkeypatch): + cls = _lazy_atlas_cls(shape=(4, 4, 4)) + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", cls) + self._budget(monkeypatch, 2 * 256) + atlas_module.canonical_annotation("a") + atlas_module.canonical_annotation("b") + atlas_module.canonical_annotation("a") # touch a: b is now oldest + atlas_module.canonical_annotation("c") + assert list(atlas_module._VOLUMES._entries) == ["a", "c"] + + def test_a_volume_larger_than_the_budget_still_loads(self, monkeypatch): + cls = _lazy_atlas_cls(shape=(4, 4, 4)) + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", cls) + self._budget(monkeypatch, 10) + atlas_module.canonical_annotation("a") + atlas_module.canonical_annotation("b") + # Refusing an oversized atlas is the GUI's call; the cache just keeps + # only the one that's in use. + assert list(atlas_module._VOLUMES._entries) == ["b"] + + def test_eviction_releases_freed_memory(self, monkeypatch): + calls = [] + monkeypatch.setattr(_memory, "release_freed_memory", lambda: calls.append(1)) + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _lazy_atlas_cls()) + atlas_module.canonical_annotation("a") # trim after every load + atlas_module.canonical_annotation("a") # cache hit: no trim + n = len(calls) + atlas_module._VOLUMES.clear() + assert n == 1 and len(calls) == 2 + + def test_failed_load_does_not_wedge_later_callers(self, monkeypatch): + calls = [] + + def _flaky(name): + calls.append(name) + if len(calls) == 1: + raise RuntimeError("s3 hiccup") + return np.ones((2, 2, 2), np.uint32), np.array([25.0, 25.0, 25.0]) + + monkeypatch.setattr(atlas_module, "_load_canonical", _flaky) + with pytest.raises(RuntimeError): + atlas_module.canonical_annotation("x") + assert "x" not in atlas_module._VOLUMES._loading + arr, _ = atlas_module.canonical_annotation("x") + assert arr.shape == (2, 2, 2) + + def test_concurrent_requests_decode_once(self, monkeypatch): + import threading + + started = threading.Event() + release = threading.Event() + loads = [] + + def _slow(name): + loads.append(name) + started.set() + release.wait(5) + return np.ones((2, 2, 2), np.uint32), np.array([25.0, 25.0, 25.0]) + + monkeypatch.setattr(atlas_module, "_load_canonical", _slow) + results = [] + t1 = threading.Thread(target=lambda: results.append(atlas_module.canonical_annotation("x")[0])) + t1.start() + started.wait(5) + t2 = threading.Thread(target=lambda: results.append(atlas_module.canonical_annotation("x")[0])) + t2.start() + release.set() + t1.join(5); t2.join(5) + assert loads == ["x"] + assert len(results) == 2 and results[0] is results[1] + + +class TestBudgetDefaults: + def test_env_override_wins(self, monkeypatch): + monkeypatch.setenv("PIXELMAP_ATLAS_CACHE_MB", "512") + assert atlas_module._default_budget_bytes() == 512 * 2**20 + + def test_half_the_cgroup_limit(self, monkeypatch, tmp_path): + monkeypatch.delenv("PIXELMAP_ATLAS_CACHE_MB", raising=False) + f = tmp_path / "memory.max" + f.write_text("3221225472\n") # a 3 GB `deploy` limit + monkeypatch.setattr(_memory, "_CGROUP_LIMIT_FILES", (str(f),)) + assert atlas_module._default_budget_bytes() == 3221225472 // 2 + + def test_unlimited_cgroup_means_2gb(self, monkeypatch, tmp_path): + monkeypatch.delenv("PIXELMAP_ATLAS_CACHE_MB", raising=False) + v2 = tmp_path / "memory.max"; v2.write_text("max\n") + v1 = tmp_path / "limit"; v1.write_text(str(2**63 - 4096) + "\n") + assert _memory.cgroup_memory_limit_bytes((str(v2),)) is None + assert _memory.cgroup_memory_limit_bytes((str(v1),)) is None + assert _memory.cgroup_memory_limit_bytes((str(tmp_path / "missing"),)) is None + monkeypatch.setattr(_memory, "_CGROUP_LIMIT_FILES", (str(v2), str(v1))) + assert atlas_module._default_budget_bytes() == 2 * 2**30 + class TestAtlasesAreReleased: """Atlas objects must not outlive ``get_atlas``'s cache. diff --git a/tests/test_anatomy_schematic.py b/tests/test_anatomy_schematic.py index a9c23e0..904ac0e 100644 --- a/tests/test_anatomy_schematic.py +++ b/tests/test_anatomy_schematic.py @@ -35,11 +35,9 @@ def __init__(self, name: str, **_kwargs): def _reset_caches(): atlas_module.get_atlas.cache_clear() atlas_module.canonical_annotation.cache_clear() - schematic_module._atlas_data.cache_clear() yield atlas_module.get_atlas.cache_clear() atlas_module.canonical_annotation.cache_clear() - schematic_module._atlas_data.cache_clear() @pytest.fixture