Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion scripts/monitor_fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely get_num_cores should never return zero / None? Maybe just assert cores instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably fine, but this idiom is apparently a best practice and it seems harmless enough.

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-j',
Expand Down
16 changes: 15 additions & 1 deletion scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading