Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b9014ef

Browse filesBrowse files
authored
fix: minor improvements to monitor
1 parent ecd5871 commit b9014ef
Copy full SHA for b9014ef

File tree

2 files changed

+29
-22
lines changed
Filter options

2 files changed

+29
-22
lines changed

‎nipype/interfaces/base/support.py

Copy file name to clipboardExpand all lines: nipype/interfaces/base/support.py
+10-9Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ... import logging, config
2020
from ...utils.misc import is_container, rgetcwd
2121
from ...utils.filemanip import md5, hash_infile
22+
from ...utils.profiler import ResourceMonitor, ResourceMonitorMock
2223

2324
iflogger = logging.getLogger("nipype.interface")
2425

@@ -33,16 +34,8 @@ class RuntimeContext(AbstractContextManager):
3334
def __init__(self, resource_monitor=False, ignore_exception=False):
3435
"""Initialize the context manager object."""
3536
self._ignore_exc = ignore_exception
36-
_proc_pid = os.getpid()
37-
if resource_monitor:
38-
from ...utils.profiler import ResourceMonitor
39-
else:
40-
from ...utils.profiler import ResourceMonitorMock as ResourceMonitor
37+
self._resmon = resource_monitor
4138

42-
self._resmon = ResourceMonitor(
43-
_proc_pid,
44-
freq=float(config.get("execution", "resource_monitor_frequency", 1)),
45-
)
4639

4740
def __call__(self, interface, cwd=None, redirect_x=False):
4841
"""Generate a new runtime object."""
@@ -51,6 +44,14 @@ def __call__(self, interface, cwd=None, redirect_x=False):
5144
if cwd is None:
5245
cwd = _syscwd
5346

47+
_proc_pid = os.getpid()
48+
_ResourceMonitor = ResourceMonitor if self._resmon else ResourceMonitorMock
49+
self._resmon = _ResourceMonitor(
50+
_proc_pid,
51+
cwd=cwd,
52+
freq=float(config.get("execution", "resource_monitor_frequency", 1)),
53+
)
54+
5455
self._runtime = Bunch(
5556
cwd=str(cwd),
5657
duration=None,

‎nipype/utils/profiler.py

Copy file name to clipboardExpand all lines: nipype/utils/profiler.py
+19-13Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,27 @@ class ResourceMonitor(threading.Thread):
4848
to a file
4949
"""
5050

51-
def __init__(self, pid, freq=5, fname=None, python=True):
51+
def __init__(self, pid, freq=0.2, fname=None, cwd=None):
5252
# Make sure psutil is imported
5353
import psutil
5454

55-
if freq < 0.2:
56-
raise RuntimeError("Frequency (%0.2fs) cannot be lower than 0.2s" % freq)
55+
# Leave process initialized and make first sample
56+
self._process = psutil.Process(pid)
57+
_sample = self._sample(cpu_interval=0.2)
5758

58-
if fname is None:
59-
fname = ".proc-%d_time-%s_freq-%0.2f" % (pid, time(), freq)
60-
self._fname = os.path.abspath(fname)
59+
# Continue monitor configuration
60+
freq = max(freq, 0.2)
61+
fname = fname or f".proc-{pid}"
62+
self._fname = os.path.abspath(
63+
os.path.join(cwd, fname) if cwd is not None else fname
64+
)
6165
self._logfile = open(self._fname, "w")
6266
self._freq = freq
6367
self._python = python
6468

65-
# Leave process initialized and make first sample
66-
self._process = psutil.Process(pid)
67-
self._sample(cpu_interval=0.2)
69+
# Dump first sample to file
70+
print(",".join(_sample), file=self._logfile)
71+
self._logfile.flush()
6872

6973
# Start thread
7074
threading.Thread.__init__(self)
@@ -80,7 +84,8 @@ def stop(self):
8084
if not self._event.is_set():
8185
self._event.set()
8286
self.join()
83-
self._sample()
87+
# Dump last sample to file
88+
print(",".join(self._sample()), file=self._logfile)
8489
self._logfile.flush()
8590
self._logfile.close()
8691

@@ -132,15 +137,16 @@ def _sample(self, cpu_interval=None):
132137
except psutil.NoSuchProcess:
133138
pass
134139

135-
print("%f,%f,%f,%f" % (time(), cpu, rss / _MB, vms / _MB), file=self._logfile)
136-
self._logfile.flush()
140+
return (time(), cpu, rss / _MB, vms / _MB)
137141

138142
def run(self):
139143
"""Core monitoring function, called by start()"""
140144
start_time = time()
141145
wait_til = start_time
142146
while not self._event.is_set():
143-
self._sample()
147+
# Dump sample to file
148+
print(",".join(self._sample()), file=self._logfile)
149+
self._logfile.flush()
144150
wait_til += self._freq
145151
self._event.wait(max(0, wait_til - time()))
146152

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.