Skip to content

Commit 013be14

Browse files
committed
prevent writing to same monitor
1 parent d09ca59 commit 013be14

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

nipype/interfaces/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,10 +1095,9 @@ def run(self, **inputs):
10951095
if enable_rm:
10961096
mon_freq = float(config.get('execution', 'resource_monitor_frequency', 1))
10971097
proc_pid = os.getpid()
1098-
mon_fname = os.path.abspath('.prof-%d_freq-%0.3f' % (proc_pid, mon_freq))
1099-
iflogger.debug('Creating a ResourceMonitor on a %s interface: %s',
1100-
self.__class__.__name__, mon_fname)
1101-
mon_sp = ResourceMonitor(proc_pid, freq=mon_freq, fname=mon_fname)
1098+
iflogger.debug('Creating a ResourceMonitor on a %s interface, PID=%d.',
1099+
self.__class__.__name__, proc_pid)
1100+
mon_sp = ResourceMonitor(proc_pid, freq=mon_freq)
11021101
mon_sp.start()
11031102

11041103
# Grab inputs now, as they should not change during execution
@@ -1145,7 +1144,7 @@ def run(self, **inputs):
11451144
runtime.nthreads_max = None
11461145

11471146
# Read .prof file in and set runtime values
1148-
vals = np.loadtxt(mon_fname, delimiter=',')
1147+
vals = np.loadtxt(mon_sp.logfile, delimiter=',')
11491148
if vals.size:
11501149
vals = np.atleast_2d(vals)
11511150
_, mem_peak_mb, nthreads = vals.max(0).astype(float).tolist()

nipype/utils/profiler.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# @Author: oesteban
33
# @Date: 2017-09-21 15:50:37
44
# @Last Modified by: oesteban
5-
# @Last Modified time: 2017-09-28 13:11:03
5+
# @Last Modified time: 2017-09-29 16:42:27
66
"""
77
Utilities to keep track of performance
88
"""
@@ -33,41 +33,44 @@
3333

3434
class ResourceMonitor(threading.Thread):
3535
def __init__(self, pid, freq=5, fname=None):
36-
if freq <= 0:
37-
raise RuntimeError('Frequency (%0.2fs) cannot be lower than zero' % freq)
36+
if freq < 0.2:
37+
raise RuntimeError('Frequency (%0.2fs) cannot be lower than 0.2s' % freq)
3838

3939
if fname is None:
40-
fname = '.nipype.prof'
40+
fname = '.proc-%d_time-%s_freq-%0.2f' % (pid, time(), freq)
4141

4242
self._pid = pid
4343
self._fname = fname
4444
self._freq = freq
4545

46-
self._log = open(self._fname, 'w')
47-
print('%s,0.0,0' % time(), file=self._log)
48-
self._log.flush()
46+
self._logfile = open(self._fname, 'w')
47+
self._sample()
48+
4949
threading.Thread.__init__(self)
5050
self._event = threading.Event()
5151

52+
@property
53+
def fname(self):
54+
return self._fname
55+
5256
def stop(self):
5357
if not self._event.is_set():
5458
self._event.set()
5559
self.join()
56-
ram = _get_ram_mb(self._pid) or 0
57-
cpus = _get_num_threads(self._pid) or 0
58-
print('%s,%f,%d' % (time(), ram, cpus),
59-
file=self._log)
60-
self._log.flush()
61-
self._log.close()
60+
self._sample()
61+
self._logfile.flush()
62+
self._logfile.close()
63+
64+
def _sample(self):
65+
ram = _get_ram_mb(self._pid) or 0
66+
cpus = _get_num_threads(self._pid) or 0
67+
print('%s,%f,%d' % (time(), ram, cpus),
68+
file=self._logfile)
69+
self._logfile.flush()
6270

6371
def run(self):
6472
while not self._event.is_set():
65-
ram = _get_ram_mb(self._pid)
66-
cpus = _get_num_threads(self._pid)
67-
if ram is not None and cpus is not None:
68-
print('%s,%f,%d' % (time(), ram, cpus),
69-
file=self._log)
70-
self._log.flush()
73+
self._sample()
7174
self._event.wait(self._freq)
7275

7376

0 commit comments

Comments
 (0)