Skip to content

Commit

Permalink
feat(profile): add peak memory monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ljgray committed May 13, 2024
1 parent 1f1ac2e commit e5ff19e
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions caput/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import math
import os
import threading
import time
from pathlib import Path
from typing import ClassVar, Optional
Expand Down Expand Up @@ -263,6 +264,7 @@ def __init__(
"memory_change_uss",
"current_available_memory_bytes",
"current_total_used_memory_bytes",
"peak_used_memory_bytes",
]
cw = csv.writer(fp)
cw.writerow(colnames)
Expand Down Expand Up @@ -309,6 +311,13 @@ def start(self):
else:
self._start_disk_io = self.io_counters()

self._thread_flag = threading.Event()
self._thread_flag.set()

self._peak_memory = self._start_memory
self._monitor_thread = threading.Thread(target=self.monitor)
self._monitor_thread.start()

def stop(self):
"""Stop profiler. Dump results to csv file and/or log and/or set results on self.usage.
Expand Down Expand Up @@ -339,6 +348,13 @@ def stop(self):
RuntimeError
If stop was called before start.
"""
if self._start_cpu_times is None:
raise RuntimeError("PSUtilProfiler.stop was called before start.")

# Stop the monitoring process
self._thread_flag.clear()
self._monitor_thread.join()

stop_time = time.time()

# Get all stats at the same time
Expand All @@ -353,9 +369,6 @@ def stop(self):
else:
disk_io = self.io_counters()

if self._start_cpu_times is None:
raise RuntimeError("PSUtilProfiler.stop was called before start.")

# Construct results
self._usage = {"task_name": self._label}

Expand Down Expand Up @@ -383,6 +396,7 @@ def bytes2human(num):
self._usage["memory"] = memory
self._usage["used_memory"] = used_memory
self._usage["available_memory"] = available_memory
self._usage["peak_memory"] = self._peak_memory

time_s = stop_time - self._start_time

Expand All @@ -403,6 +417,7 @@ def bytes2human(num):
f"current available memory: {bytes2human(available_memory)}"
)
self._logger.info(f"current total used memory: {bytes2human(used_memory)}")
self._logger.info(f"peak used memory: {bytes2human(self._peak_memory)}")

with open(self.path, mode="a", newline="") as fp:
import csv
Expand All @@ -427,9 +442,17 @@ def bytes2human(num):
memory,
available_memory,
used_memory,
self._peak_memory,
]
)

def monitor(self):
"""Track peak memory."""
while self._thread_flag.is_set():
current_mem = psutil.virtual_memory().used
self._peak_memory = max(self._peak_memory, current_mem)
time.sleep(0.5)

@property
def cpu_count(self):
"""Number of cores available to this process."""
Expand Down

0 comments on commit e5ff19e

Please sign in to comment.