@@ -1205,7 +1205,7 @@ def _read(self, drain):
1205
1205
1206
1206
1207
1207
# Get number of threads for process
1208
- def _get_num_threads (proc ):
1208
+ def _get_num_threads (proc , log_flg = False ):
1209
1209
"""Function to get the number of threads a process is using
1210
1210
1211
1211
Parameters
@@ -1221,12 +1221,26 @@ def _get_num_threads(proc):
1221
1221
1222
1222
# Import packages
1223
1223
import psutil
1224
+ import logging
1224
1225
1225
1226
# Init variables
1226
1227
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
+
1227
1233
try :
1228
1234
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 ))
1229
1238
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 ()))
1230
1244
num_threads = max (num_threads , num_children ,
1231
1245
child .num_threads (), len (child .children ()))
1232
1246
except psutil .NoSuchProcess :
@@ -1237,19 +1251,17 @@ def _get_num_threads(proc):
1237
1251
1238
1252
1239
1253
# 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 ):
1241
1255
"""Function to get the RAM and threads usage of a process
1242
1256
1243
1257
Paramters
1244
1258
---------
1245
- proc : subprocess.Popen instance
1246
- the process to profile
1259
+ pid : integer
1260
+ the process ID of process to profile
1247
1261
mem_mb : float
1248
1262
the high memory watermark so far during process execution (in MB)
1249
1263
num_threads: int
1250
1264
the high thread watermark so far during process execution
1251
- poll : boolean
1252
- whether to poll the process or not
1253
1265
1254
1266
Returns
1255
1267
-------
@@ -1264,10 +1276,8 @@ def _get_max_resources_used(proc, mem_mb, num_threads, poll=False):
1264
1276
import psutil
1265
1277
1266
1278
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 ))
1271
1281
except Exception as exc :
1272
1282
iflogger .info ('Could not get resources used by process. Error: %s' \
1273
1283
% exc )
@@ -1331,6 +1341,7 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
1331
1341
# Init variables for memory profiling
1332
1342
mem_mb = - 1
1333
1343
num_threads = - 1
1344
+ interval = .5
1334
1345
1335
1346
if output == 'stream' :
1336
1347
streams = [Stream ('stdout' , proc .stdout ), Stream ('stderr' , proc .stderr )]
@@ -1350,9 +1361,10 @@ def _process(drain=0):
1350
1361
while proc .returncode is None :
1351
1362
if runtime_profile :
1352
1363
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 )
1354
1365
proc .poll ()
1355
1366
_process ()
1367
+ time .sleep (interval )
1356
1368
_process (drain = 1 )
1357
1369
1358
1370
# collect results, merge and return
@@ -1369,7 +1381,9 @@ def _process(drain=0):
1369
1381
if runtime_profile :
1370
1382
while proc .returncode is None :
1371
1383
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 )
1373
1387
stdout , stderr = proc .communicate ()
1374
1388
if stdout and isinstance (stdout , bytes ):
1375
1389
try :
@@ -1389,7 +1403,9 @@ def _process(drain=0):
1389
1403
if runtime_profile :
1390
1404
while proc .returncode is None :
1391
1405
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 )
1393
1409
ret_code = proc .wait ()
1394
1410
stderr .flush ()
1395
1411
stdout .flush ()
@@ -1400,7 +1416,9 @@ def _process(drain=0):
1400
1416
if runtime_profile :
1401
1417
while proc .returncode is None :
1402
1418
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 )
1404
1422
proc .communicate ()
1405
1423
result ['stdout' ] = []
1406
1424
result ['stderr' ] = []
0 commit comments