Skip to content

Commit

Permalink
add log_func option to MemoryMonitor, reformat docstring in numpy format
Browse files Browse the repository at this point in the history
  • Loading branch information
bbean23 committed Mar 27, 2024
1 parent 7ccd13c commit 5d175c5
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions opencsp/common/lib/process/MemoryMonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import multiprocessing
from multiprocessing.synchronize import Event as multiprocessing_event_type
import time
from typing import Callable

import opencsp.common.lib.process.ParallelPartitioner as ppart
import opencsp.common.lib.tool.system_tools as st
Expand All @@ -18,6 +19,7 @@ def __init__(
print_threshold=0.95,
print_on_new_min=True,
always_print=False,
log_func: Callable = lt.info,
):
"""Monitors total system memory usage in a separate subprocess while the rest of the application continues running.
Expand All @@ -32,13 +34,24 @@ def __init__(
finally:
monitor.stop()
Args:
- server_index (int): Used to identify this monitor when running multiple tasks on the same system.
- cpu_index (int): Used to identify this monitor when running multiple tasks on the same system.
- max_lifetime_hours (float): Maximum lifetime for the monitor subprocess. Setting this guarantees that the subprocess will at some point stop. Defaults to 2 hours.
- print_threshold (float): If the memory used/total exceeds this threshold, then print out the usage every second. Defaults to 0.95.
- print_on_new_min (bool): If true, then for each second, if the memory available has reached a new minimum print out the usage for that second. Defaults to True.
- always_print (bool): If True, then print out the usage every second. Defaults to False.
Params:
-------
server_index: int
Used to identify this monitor when running multiple tasks on the same system.
cpu_index: int
Used to identify this monitor when running multiple tasks on the same system.
max_lifetime_hours: float
Maximum lifetime for the monitor subprocess. Setting this guarantees that the subprocess will at some point
stop. Defaults to 2 hours.
print_threshold: float
If the memory used/total exceeds this threshold, then print out the usage every second. Defaults to 0.95.
print_on_new_min: bool
If true, then for each second, if the memory available has reached a new minimum print out the usage for
that second. Defaults to True.
always_print: bool
If True, then print out the usage every second. Defaults to False.
log_func: Callable
The function to call to log output to. Must accept a string as the first argument. Default is log_tools.info
"""
partitioner = ppart.ParallelPartitioner(server_index + 1, server_index, cpu_index + 1, cpu_index)
self.identifier = partitioner.identifier()
Expand All @@ -56,6 +69,8 @@ def __init__(
self._print_threshold = print_threshold
self._print_on_new_min = print_on_new_min
self._always_print = always_print
self._log_func = log_func

self._start_datetime: datetime.datetime = None
self._end_datetime: datetime.datetime = None
self._process_finished = False
Expand Down Expand Up @@ -128,6 +143,7 @@ def _run(self):
self._print_threshold,
self._print_on_new_min,
self._always_print,
self._log_func
],
)
self._proc.start()
Expand Down Expand Up @@ -201,6 +217,7 @@ def _monitor_sys_memory(
print_threshold: float,
print_on_new_min: bool,
always_print: bool,
log_func: Callable,
):
start = time.time()
# print("monitor started")
Expand All @@ -221,7 +238,7 @@ def _monitor_sys_memory(

queue.put(tuple([elapsed, sys_tot, sys_used, sys_free]))
if do_print_min or do_print_threshold or always_print:
lt.info(
log_func(
"MM: %s %s %s %s"
% (
f"{int(elapsed):>3d}",
Expand Down

0 comments on commit 5d175c5

Please sign in to comment.