Skip to content

Commit 6ceb470

Browse files
Merge pull request #254 from katzlabbrandeis/242-log-ram-usage
242 log ram usage during blech_run_process
2 parents 507ffa7 + c2c6764 commit 6ceb470

File tree

5 files changed

+44
-37
lines changed

5 files changed

+44
-37
lines changed

blech_process.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
from utils.blech_utils import imp_metadata, pipeline_graph_check
3333
import utils.blech_process_utils as bpu
34-
from utils import memory_monitor as mm
3534
import pylab as plt
3635
import json
3736
import sys
@@ -230,10 +229,6 @@
230229
cluster_handler.create_classifier_plots(classifier_handler)
231230

232231

233-
# Make file for dumping info about memory usage
234-
f= open(f'./memory_monitor_clustering/{electrode_num:02}.txt', 'w')
235-
print(mm.memory_usage_resource(), file=f)
236-
f.close()
237232
print(f'Electrode {electrode_num} complete.')
238233

239234
# Write successful execution to log

blech_run_process.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ if [ ! -d "$DIR" ]; then
4040
fi
4141
fi
4242

43+
# Start RAM monitoring in background
44+
python3 utils/ram_monitor.py "$DIR" &
45+
RAM_MONITOR_PID=$!
46+
4347
echo "Processing $DIR"
4448
for i in {1..10}; do
4549
echo Retry $i
4650
bash $DIR/temp/blech_process_parallel.sh
4751
done
52+
53+
# Kill RAM monitor when done
54+
kill $RAM_MONITOR_PID

requirements/pip_requirements_base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ prefect==2.10.5
55
pingouin==0.3
66
tqdm
77
gdown
8+
psutil>=6.1.0

utils/memory_monitor.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

utils/ram_monitor.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
import psutil
3+
import time
4+
import sys
5+
import os
6+
from datetime import datetime
7+
8+
def monitor_ram(output_dir):
9+
"""Monitor RAM usage and write to a log file"""
10+
log_file = os.path.join(output_dir, "ram_usage.log")
11+
12+
with open(log_file, 'a') as f:
13+
f.write("Timestamp,RAM_Used_GB,RAM_Total_GB,RAM_Percent\n")
14+
15+
while True:
16+
try:
17+
mem = psutil.virtual_memory()
18+
used_gb = mem.used / (1024 * 1024 * 1024) # Convert to GB
19+
total_gb = mem.total / (1024 * 1024 * 1024)
20+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
21+
22+
log_line = f"{timestamp},{used_gb:.2f},{total_gb:.2f},{mem.percent}\n"
23+
f.write(log_line)
24+
f.flush()
25+
26+
time.sleep(1) # Log every 1 seconds
27+
28+
except KeyboardInterrupt:
29+
break
30+
31+
if __name__ == "__main__":
32+
if len(sys.argv) != 2:
33+
print("Usage: python ram_monitor.py <output_directory>")
34+
sys.exit(1)
35+
36+
monitor_ram(sys.argv[1])

0 commit comments

Comments
 (0)