Skip to content

Commit 98c65ac

Browse files
committed
ENH: test_perf.py - use psutil to set affinity (if absent functionality - then affinity module)
original affinity module is not commonly installed, has known issues, and was not updated since 2006. psutil is already used in parts of pandas, so it would be logical to use it instead by default.
1 parent 2f44c19 commit 98c65ac

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

vb_suite/test_perf.py

+33-11
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,15 @@
9898
dest='hrepeats',
9999
default=1,
100100
type=int,
101-
help='Implies -H, number of times to run the vbench suite on the head commit.\n'
102-
'Each iteration will yield another column in the output.'
103-
)
101+
help='implies -H, number of times to run the vbench suite on the head commit.\n'
102+
'Each iteration will yield another column in the output' )
104103
parser.add_argument('-a', '--affinity',
105104
metavar="a",
106105
dest='affinity',
107106
default=1,
108107
type=int,
109-
help='Set processor affinity of the process. THe default is to bind to cpu/core #1 only.'
110-
'requires the "affinity" python module.' )
111-
108+
help='set processor affinity of process by default bind to cpu/core #1 only. '
109+
'Requires the "affinity" or "psutil" python module, will raise Warning otherwise')
112110
parser.add_argument('-u', '--burnin',
113111
metavar="u",
114112
dest='burnin',
@@ -388,14 +386,38 @@ def main():
388386
random.seed(args.seed)
389387
np.random.seed(args.seed)
390388

389+
affinity_set = False
390+
391+
# try psutil first since it is more commonly present and better
392+
# maintained. Some people experienced problems with affinity package
393+
# (see https://code.google.com/p/psutil/issues/detail?id=238 for more references)
391394
try:
392-
import affinity
393-
affinity.set_process_affinity_mask(0,args.affinity)
394-
assert affinity.get_process_affinity_mask(0) == args.affinity
395-
print("CPU affinity set to %d" % args.affinity)
395+
import psutil
396+
if hasattr(psutil.Process, 'set_cpu_affinity'):
397+
psutil.Process(os.getpid()).set_cpu_affinity([args.affinity])
398+
affinity_set = True
396399
except ImportError:
397-
print("Warning: The 'affinity' module is not available.")
400+
pass
401+
402+
if not affinity_set:
403+
try:
404+
import affinity
405+
affinity.set_process_affinity_mask(0, args.affinity)
406+
assert affinity.get_process_affinity_mask(0) == args.affinity
407+
affinity_set = True
408+
except ImportError:
409+
pass
410+
411+
if not affinity_set:
412+
import warnings
413+
warnings.warn("\n\n"
414+
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
415+
"The 'affinity' or 'psutil' >= 0.5.0 modules are not available, results may be unreliable\n"
416+
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"
417+
)
398418
time.sleep(2)
419+
else:
420+
print("CPU affinity set to %d" % args.affinity)
399421

400422
print("\n")
401423
prprint("LOG_FILE = %s" % args.log_file)

0 commit comments

Comments
 (0)