Skip to content

Commit a515c77

Browse files
committed
Added more code for debugging runtime profiler
1 parent ace7368 commit a515c77

File tree

4 files changed

+365
-32
lines changed

4 files changed

+365
-32
lines changed

nipype/interfaces/base.py

+32-14
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ def _read(self, drain):
12051205

12061206

12071207
# Get number of threads for process
1208-
def _get_num_threads(proc):
1208+
def _get_num_threads(proc, log_flg=False):
12091209
"""Function to get the number of threads a process is using
12101210
12111211
Parameters
@@ -1221,12 +1221,26 @@ def _get_num_threads(proc):
12211221

12221222
# Import packages
12231223
import psutil
1224+
import logging
12241225

12251226
# Init variables
12261227
num_threads = proc.num_threads()
1228+
if log_flg:
1229+
from CPAC.utils.utils import setup_logger
1230+
logger = setup_logger('memory_profiler', '/home/dclark/memory_profiler.log',
1231+
logging.INFO, to_screen=False)
1232+
12271233
try:
12281234
num_children = len(proc.children())
1235+
if log_flg:
1236+
logger.debug('len(proc.children()): %d' % num_children)
1237+
logger.debug('proc.id: %s' % str(proc.pid))
12291238
for child in proc.children():
1239+
if log_flg:
1240+
logger.debug('child.pid: %d' % child.pid)
1241+
logger.debug('child.threads(): %s' % str(child.threads()))
1242+
logger.debug('child.num_threads(): %d' % child.num_threads())
1243+
logger.debug('len(child.children()): %d' % len(child.children()))
12301244
num_threads = max(num_threads, num_children,
12311245
child.num_threads(), len(child.children()))
12321246
except psutil.NoSuchProcess:
@@ -1237,19 +1251,17 @@ def _get_num_threads(proc):
12371251

12381252

12391253
# Get max resources used for process
1240-
def _get_max_resources_used(proc, mem_mb, num_threads, poll=False):
1254+
def get_max_resources_used(pid, mem_mb, num_threads, log_flg=False):
12411255
"""Function to get the RAM and threads usage of a process
12421256
12431257
Paramters
12441258
---------
1245-
proc : subprocess.Popen instance
1246-
the process to profile
1259+
pid : integer
1260+
the process ID of process to profile
12471261
mem_mb : float
12481262
the high memory watermark so far during process execution (in MB)
12491263
num_threads: int
12501264
the high thread watermark so far during process execution
1251-
poll : boolean
1252-
whether to poll the process or not
12531265
12541266
Returns
12551267
-------
@@ -1264,10 +1276,8 @@ def _get_max_resources_used(proc, mem_mb, num_threads, poll=False):
12641276
import psutil
12651277

12661278
try:
1267-
mem_mb = max(mem_mb, _get_memory(proc.pid, include_children=True))
1268-
num_threads = max(num_threads, _get_num_threads(psutil.Process(proc.pid)))
1269-
if poll:
1270-
proc.poll()
1279+
mem_mb = max(mem_mb, _get_memory(pid, include_children=True, log_flg=log_flg))
1280+
num_threads = max(num_threads, _get_num_threads(psutil.Process(pid), log_flg=log_flg))
12711281
except Exception as exc:
12721282
iflogger.info('Could not get resources used by process. Error: %s'\
12731283
% exc)
@@ -1331,6 +1341,7 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
13311341
# Init variables for memory profiling
13321342
mem_mb = -1
13331343
num_threads = -1
1344+
interval = .5
13341345

13351346
if output == 'stream':
13361347
streams = [Stream('stdout', proc.stdout), Stream('stderr', proc.stderr)]
@@ -1350,9 +1361,10 @@ def _process(drain=0):
13501361
while proc.returncode is None:
13511362
if runtime_profile:
13521363
mem_mb, num_threads = \
1353-
_get_max_resources_used(proc, mem_mb, num_threads)
1364+
get_max_resources_used(proc.pid, mem_mb, num_threads)
13541365
proc.poll()
13551366
_process()
1367+
time.sleep(interval)
13561368
_process(drain=1)
13571369

13581370
# collect results, merge and return
@@ -1369,7 +1381,9 @@ def _process(drain=0):
13691381
if runtime_profile:
13701382
while proc.returncode is None:
13711383
mem_mb, num_threads = \
1372-
_get_max_resources_used(proc, mem_mb, num_threads, poll=True)
1384+
get_max_resources_used(proc.pid, mem_mb, num_threads)
1385+
proc.poll()
1386+
time.sleep(interval)
13731387
stdout, stderr = proc.communicate()
13741388
if stdout and isinstance(stdout, bytes):
13751389
try:
@@ -1389,7 +1403,9 @@ def _process(drain=0):
13891403
if runtime_profile:
13901404
while proc.returncode is None:
13911405
mem_mb, num_threads = \
1392-
_get_max_resources_used(proc, mem_mb, num_threads, poll=True)
1406+
get_max_resources_used(proc.pid, mem_mb, num_threads)
1407+
proc.poll()
1408+
time.sleep(interval)
13931409
ret_code = proc.wait()
13941410
stderr.flush()
13951411
stdout.flush()
@@ -1400,7 +1416,9 @@ def _process(drain=0):
14001416
if runtime_profile:
14011417
while proc.returncode is None:
14021418
mem_mb, num_threads = \
1403-
_get_max_resources_used(proc, mem_mb, num_threads, poll=True)
1419+
get_max_resources_used(proc.pid, mem_mb, num_threads)
1420+
proc.poll()
1421+
time.sleep(interval)
14041422
proc.communicate()
14051423
result['stdout'] = []
14061424
result['stderr'] = []

0 commit comments

Comments
 (0)