Skip to content

Commit d465f10

Browse files
authored
Fix used ram count (#27)
1 parent bce5216 commit d465f10

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

entrypoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# 1. Run setup (replaces --setup flag)
1313
from aind_ephys_portal.setup import * # noqa: F401,F403
14-
from aind_ephys_portal.panel.logging import list_gui_sessions, get_max_number_of_gui_sessions, get_container_total_memory, LOG_DIR # noqa: F401
14+
from aind_ephys_portal.panel.logging import list_gui_sessions, get_max_number_of_gui_sessions, get_container_total_memory, get_container_used_memory, LOG_DIR # noqa: F401
1515

1616

1717
TARGET_MEMORY_TRIGGER_PERCENT = 70
@@ -80,7 +80,7 @@ def get(self):
8080
if _tmp_arr is None and not _tmp_array_triggered:
8181
_tmp_array_triggered = True
8282
total_memory = get_container_total_memory()
83-
used_memory = psutil.virtual_memory().used
83+
used_memory = get_container_used_memory()
8484
target_memory = total_memory * TARGET_MEMORY_TRIGGER_PERCENT / 100
8585
array_size = int((target_memory - used_memory) / 8) # assuming float64 (8 bytes)
8686
if array_size > 0:

src/aind_ephys_portal/ephys_monitor_app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import psutil
22
import panel as pn
33

4-
from aind_ephys_portal.panel.logging import list_sessions, remove_session, get_container_total_memory
4+
from aind_ephys_portal.panel.logging import list_sessions, remove_session, get_container_total_memory, get_container_used_memory
55

66
pn.extension()
77

88

99
# --- Memory info ---
1010
def get_mem_info():
11-
mem = psutil.virtual_memory()
1211
container_total = get_container_total_memory()
13-
used_gb = mem.used / (1024**3)
12+
container_used = get_container_used_memory()
13+
used_gb = container_used / (1024**3)
1414
total_gb = container_total / (1024**3)
15-
percent = mem.used / container_total * 100
15+
percent = container_used / container_total * 100
1616
return used_gb, total_gb, percent
1717

1818

src/aind_ephys_portal/panel/logging.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ def get_container_total_memory():
108108
return psutil.virtual_memory().total
109109

110110

111+
def get_container_used_memory():
112+
"""Return the container's current memory usage in bytes, falling back to host used.
113+
114+
Must read from the same cgroup as get_container_total_memory() — psutil reads
115+
host-level /proc/meminfo, which produces the wrong array size in the inflation calc.
116+
"""
117+
# cgroups v2
118+
try:
119+
with open("/sys/fs/cgroup/memory.current") as f:
120+
return int(f.read().strip())
121+
except (FileNotFoundError, ValueError):
122+
pass
123+
# cgroups v1
124+
try:
125+
with open("/sys/fs/cgroup/memory/memory.usage_in_bytes") as f:
126+
return int(f.read().strip())
127+
except (FileNotFoundError, ValueError):
128+
pass
129+
return psutil.virtual_memory().used
130+
131+
111132
def get_max_number_of_gui_sessions():
112133
# Estimate number of sessions per worker for health check.
113134
SESSION_AVG_RAM_USAGE_GB = 2

0 commit comments

Comments
 (0)