Skip to content

Commit fcaec79

Browse files
committed
Cleaned up debug code
1 parent 6fe8391 commit fcaec79

File tree

3 files changed

+17
-48
lines changed

3 files changed

+17
-48
lines changed

nipype/interfaces/base.py

Lines changed: 16 additions & 29 deletions
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, called_from):
1208+
def _get_num_threads(proc):
12091209
"""Function to get the number of threads a process is using
12101210
NOTE: If
12111211
@@ -1222,41 +1222,28 @@ def _get_num_threads(proc, called_from):
12221222

12231223
# Import packages
12241224
import psutil
1225-
import logging
1226-
1227-
# Init variables
1228-
cb_log = logging.getLogger('callback')
1229-
cb_log.propogate = False
1230-
cb_log.debug('\n---------------------\nCalled from: %s' % called_from)
1231-
cb_log.debug('proc pid: %d, parent pid: %d, name: %s, exe: %s, cmdline: %s, status: %s, num_threads: %d' \
1232-
% (proc.pid, proc.ppid(), proc.name(), proc.exe(), proc.cmdline(), proc.status(), proc.num_threads()))
1233-
12341225

1226+
# If process is running
12351227
if proc.status() == psutil.STATUS_RUNNING:
12361228
num_threads = proc.num_threads()
12371229
else:
12381230
num_threads = 0
12391231

1240-
child_threads = 0
1241-
# Iterate through child processes and get number of their threads
1232+
# Try-block for errors
12421233
try:
1234+
child_threads = 0
1235+
# Iterate through child processes and get number of their threads
12431236
for child in proc.children(recursive=True):
1244-
if child.status() == psutil.STATUS_RUNNING:
1245-
# If leaf child process
1246-
if len(child.children()) == 0:
1247-
child_threads += child.num_threads()
1248-
cb_log.debug('child pid: %d, parent pid: %d, name: %s, exe: %s, cmdline: %s, status: %s, num_threads: %d' \
1249-
% (proc.pid, proc.ppid(), proc.name(), proc.exe(), proc.cmdline(), proc.status(), child.num_threads()))
1250-
cb_log.debug('child_threads: %d, num_threads: %d' % (child_threads, num_threads))
1251-
1252-
num_threads = max(child_threads, num_threads)
1237+
if child.status() == psutil.STATUS_RUNNING and len(child.children()) == 0:
1238+
child_threads += child.num_threads()
1239+
# Catch any NoSuchProcess errors
12531240
except psutil.NoSuchProcess:
12541241
pass
12551242

1256-
1243+
# Number of threads is max between found active children and parent
1244+
num_threads = max(child_threads, num_threads)
12571245

12581246
# Return number of threads found
1259-
cb_log.debug('RETURNING num_threads as: %d!\n---------------------------\n' % num_threads)
12601247
return num_threads
12611248

12621249

@@ -1312,7 +1299,7 @@ def _get_ram_mb(pid, pyfunc=False):
13121299

13131300

13141301
# Get max resources used for process
1315-
def get_max_resources_used(pid, mem_mb, num_threads, called_from, pyfunc=False):
1302+
def get_max_resources_used(pid, mem_mb, num_threads, pyfunc=False):
13161303
"""Function to get the RAM and threads usage of a process
13171304
13181305
Paramters
@@ -1337,7 +1324,7 @@ def get_max_resources_used(pid, mem_mb, num_threads, called_from, pyfunc=False):
13371324

13381325
try:
13391326
mem_mb = max(mem_mb, _get_ram_mb(pid, pyfunc=pyfunc))
1340-
num_threads = max(num_threads, _get_num_threads(psutil.Process(pid), called_from))
1327+
num_threads = max(num_threads, _get_num_threads(psutil.Process(pid)))
13411328
except Exception as exc:
13421329
iflogger.info('Could not get resources used by process. Error: %s'\
13431330
% exc)
@@ -1420,7 +1407,7 @@ def _process(drain=0):
14201407
while proc.returncode is None:
14211408
if runtime_profile:
14221409
mem_mb, num_threads = \
1423-
get_max_resources_used(proc.pid, mem_mb, num_threads, cmdline)
1410+
get_max_resources_used(proc.pid, mem_mb, num_threads)
14241411
proc.poll()
14251412
_process()
14261413
time.sleep(interval)
@@ -1440,7 +1427,7 @@ def _process(drain=0):
14401427
if runtime_profile:
14411428
while proc.returncode is None:
14421429
mem_mb, num_threads = \
1443-
get_max_resources_used(proc.pid, mem_mb, num_threads, cmdline)
1430+
get_max_resources_used(proc.pid, mem_mb, num_threads)
14441431
proc.poll()
14451432
time.sleep(interval)
14461433
stdout, stderr = proc.communicate()
@@ -1462,7 +1449,7 @@ def _process(drain=0):
14621449
if runtime_profile:
14631450
while proc.returncode is None:
14641451
mem_mb, num_threads = \
1465-
get_max_resources_used(proc.pid, mem_mb, num_threads, cmdline)
1452+
get_max_resources_used(proc.pid, mem_mb, num_threads)
14661453
proc.poll()
14671454
time.sleep(interval)
14681455
ret_code = proc.wait()
@@ -1475,7 +1462,7 @@ def _process(drain=0):
14751462
if runtime_profile:
14761463
while proc.returncode is None:
14771464
mem_mb, num_threads = \
1478-
get_max_resources_used(proc.pid, mem_mb, num_threads, cmdline)
1465+
get_max_resources_used(proc.pid, mem_mb, num_threads)
14791466
proc.poll()
14801467
time.sleep(interval)
14811468
proc.communicate()

nipype/interfaces/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def _function_handle_wrapper(queue, **kwargs):
492492
while proc.is_alive():
493493
mem_mb, num_threads = \
494494
get_max_resources_used(proc.pid, mem_mb, num_threads,
495-
function_handle.__name__, pyfunc=True)
495+
pyfunc=True)
496496

497497
# Get result from process queue
498498
out = queue.get()

nipype/pipeline/plugins/base.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,6 @@ def __init__(self, plugin_args=None):
223223
def run(self, graph, config, updatehash=False):
224224
"""Executes a pre-defined pipeline using distributed approaches
225225
"""
226-
# Global watcher inits
227-
from nipype.interfaces.base import get_max_resources_used
228-
gpid = os.getpid()
229-
num_threads = 0
230-
memory_mb = 0
231-
# Init logger
232-
import logging as lg
233-
gw_log = lg.getLogger('global_watcher')
234-
gw_log.setLevel(lg.INFO)
235-
formatter = lg.Formatter('%(asctime)s : %(message)s')
236-
237-
# Write logs to file
238-
file_handler = lg.FileHandler('/home/dclark/work-dir/gw.log')
239-
file_handler.setFormatter(formatter)
240-
gw_log.addHandler(file_handler)
241-
242226
logger.info("Running in parallel.")
243227
self._config = config
244228
# Generate appropriate structures for worker-manager model
@@ -252,8 +236,6 @@ def run(self, graph, config, updatehash=False):
252236
while np.any(self.proc_done == False) | \
253237
np.any(self.proc_pending == True):
254238

255-
#memory_mb, num_threads = get_max_resources_used(gpid, memory_mb, num_threads, )
256-
#gw_log.info('Memory GB usage: %.4f, Threads usage: %d' % (memory_mb/1024.0, num_threads))
257239
toappend = []
258240
# trigger callbacks for any pending results
259241
while self.pending_tasks:

0 commit comments

Comments
 (0)