@@ -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 , called_from ):
1208
+ def _get_num_threads (proc ):
1209
1209
"""Function to get the number of threads a process is using
1210
1210
NOTE: If
1211
1211
@@ -1222,41 +1222,28 @@ def _get_num_threads(proc, called_from):
1222
1222
1223
1223
# Import packages
1224
1224
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 ---------------------\n Called 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
-
1234
1225
1226
+ # If process is running
1235
1227
if proc .status () == psutil .STATUS_RUNNING :
1236
1228
num_threads = proc .num_threads ()
1237
1229
else :
1238
1230
num_threads = 0
1239
1231
1240
- child_threads = 0
1241
- # Iterate through child processes and get number of their threads
1232
+ # Try-block for errors
1242
1233
try :
1234
+ child_threads = 0
1235
+ # Iterate through child processes and get number of their threads
1243
1236
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
1253
1240
except psutil .NoSuchProcess :
1254
1241
pass
1255
1242
1256
-
1243
+ # Number of threads is max between found active children and parent
1244
+ num_threads = max (child_threads , num_threads )
1257
1245
1258
1246
# Return number of threads found
1259
- cb_log .debug ('RETURNING num_threads as: %d!\n ---------------------------\n ' % num_threads )
1260
1247
return num_threads
1261
1248
1262
1249
@@ -1312,7 +1299,7 @@ def _get_ram_mb(pid, pyfunc=False):
1312
1299
1313
1300
1314
1301
# 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 ):
1316
1303
"""Function to get the RAM and threads usage of a process
1317
1304
1318
1305
Paramters
@@ -1337,7 +1324,7 @@ def get_max_resources_used(pid, mem_mb, num_threads, called_from, pyfunc=False):
1337
1324
1338
1325
try :
1339
1326
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 )))
1341
1328
except Exception as exc :
1342
1329
iflogger .info ('Could not get resources used by process. Error: %s' \
1343
1330
% exc )
@@ -1420,7 +1407,7 @@ def _process(drain=0):
1420
1407
while proc .returncode is None :
1421
1408
if runtime_profile :
1422
1409
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 )
1424
1411
proc .poll ()
1425
1412
_process ()
1426
1413
time .sleep (interval )
@@ -1440,7 +1427,7 @@ def _process(drain=0):
1440
1427
if runtime_profile :
1441
1428
while proc .returncode is None :
1442
1429
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 )
1444
1431
proc .poll ()
1445
1432
time .sleep (interval )
1446
1433
stdout , stderr = proc .communicate ()
@@ -1462,7 +1449,7 @@ def _process(drain=0):
1462
1449
if runtime_profile :
1463
1450
while proc .returncode is None :
1464
1451
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 )
1466
1453
proc .poll ()
1467
1454
time .sleep (interval )
1468
1455
ret_code = proc .wait ()
@@ -1475,7 +1462,7 @@ def _process(drain=0):
1475
1462
if runtime_profile :
1476
1463
while proc .returncode is None :
1477
1464
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 )
1479
1466
proc .poll ()
1480
1467
time .sleep (interval )
1481
1468
proc .communicate ()
0 commit comments