diff --git a/scripts/monitor_fuzz.py b/scripts/monitor_fuzz.py index 40b088ebd5a..9f03304cc9f 100755 --- a/scripts/monitor_fuzz.py +++ b/scripts/monitor_fuzz.py @@ -31,6 +31,20 @@ import time +# Duplicated from test.shared (importing test.shared has unwanted side effects). +def get_num_cores(): + # Prefer `os.process_cpu_count` when available (3.13 and above) since it + # takes into account thread affinity. Fall back to `os.sched_getaffinity` + # where available and finally `os.cpu_count`, which should work everywhere. + if hasattr(os, 'process_cpu_count'): + cpu_count = os.process_cpu_count() + elif hasattr(os, 'sched_getaffinity'): + cpu_count = len(os.sched_getaffinity(0)) + else: + cpu_count = os.cpu_count() + return int(os.getenv('BINARYEN_CORES', cpu_count)) + + class FuzzMonitor: """Monitors fuzzer output stream, manages log files, and tracks state.""" @@ -164,7 +178,7 @@ def parse_args(): binaryen_root = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) default_log_dir = os.path.join(binaryen_root, 'out', 'test') - cores = os.cpu_count() or 1 + cores = get_num_cores() or 1 parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( '-j', diff --git a/scripts/test/shared.py b/scripts/test/shared.py index 0e65e159056..146b9e9c44a 100644 --- a/scripts/test/shared.py +++ b/scripts/test/shared.py @@ -141,12 +141,26 @@ def red_stderr(): return red_output(file=sys.stderr) +# Adapted from Emscripten's tools/utils.py +def get_num_cores(): + # Prefer `os.process_cpu_count` when available (3.13 and above) since it + # takes into account thread affinity. Fall back to `os.sched_getaffinity` + # where available and finally `os.cpu_count`, which should work everywhere. + if hasattr(os, 'process_cpu_count'): + cpu_count = os.process_cpu_count() + elif hasattr(os, 'sched_getaffinity'): + cpu_count = len(os.sched_getaffinity(0)) + else: + cpu_count = os.cpu_count() + return int(os.getenv('BINARYEN_CORES', cpu_count)) + + def run_parallel_tests(run_one_test_func, tests, show_worker_count=True): global num_failures tests = list(tests) if not tests: return - worker_count = min(os.cpu_count() or 1, len(tests)) + worker_count = min(get_num_cores() or 1, len(tests)) if show_worker_count: print(f"Running with {worker_count} workers")