Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build] Use os.sched_getaffinity only if available #1727

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

sideshowbarker
Copy link
Member

@sideshowbarker sideshowbarker commented Feb 15, 2024

This change un-breaks the build in macOS and Windows environments, by only calling the Python os.sched_getaffinity() function where it’s available (which seems to be only on Linux) — and where it’s not available, using multiprocessing.cpu_count() instead.

Otherwise, the build fails in macOS and Windows environment, because the document/core/util/mathjax2katex.py program fails — and fails the build — with a “module 'os' has no attribute 'sched_getaffinity'” message (because os.sched_getaffinity(0) is hardcoded into the program).

This patch will have no effect on CI (which runs under a Linux environment and is working fine) but should have the effect of enabling anybody in macOS and Windows environments to actually run the build successfully.

I’ve tested in my macOS 14.4 environment with Python 3.11.7, and the docs say that multiprocessing.cpu_count() is supported in all environments all the way back to Python 2.6 — so I can’t see any reason why it wouldn’t always work.

All that said, there’s a big difference between os.sched_getaffinity() and multiprocessing.cpu_count(); which is: os.sched_getaffinity() returns “the set of CPUs the calling thread of the current process is restricted to” while multiprocessing.cpu_count() simply returns “the number of CPUs in the system” — some of which may not actually be usable by the current process.

But I think for the purposes of this particular build, the difference between the two isn’t critical. I believe that what probably will happen if that part of the mathjax2katex.py program ends up being given a number of CPUs that exceeds the actually-usable number of CPUs is: that program will anyway end up spawning only the number of threads equal to the number of actually-usable CPUs — and if it tries to spawn any threads beyond that number, those threads just don’t spawn. I think.

Finally, for the record here: There is a os.cpu_count() that does apparently the same thing as multiprocessing.cpu_count() — but the docs indicate that os.cpu_count() is available only in Python 3.4+. So it seems safer to instead use multiprocessing.cpu_count() — which as noted above, is supported all the way back to Python 2.6.

Copy link
Member

@rossberg rossberg left a comment

Choose a reason for hiding this comment

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

Makes sense. I agree that the exact amount parallelism is not critical.

@rossberg
Copy link
Member

Hm, CI does not like the Python, though.

@sideshowbarker
Copy link
Member Author

Hm, CI does not like the Python, though.

Yeah I just noticed https://github.com/WebAssembly/spec/actions/runs/7914013430/job/21602858413?pr=1727

I’ll attempt to fix it right now

This change un-breaks the build in macOS and Windows environments, by
only calling the Python os.sched_getaffinity() function where it’s
available (which seems to be only on Linux) — and where it’s not
available, using multiprocessing.cpu_count() instead.

Otherwise, the build fails in macOS and Windows environment, because the
document/core/util/mathjax2katex.py program fails — and fails the build —
with a “module 'os' has no attribute 'sched_getaffinity'” message
(because os.sched_getaffinity(0) is hardcoded into the program).
@sideshowbarker sideshowbarker force-pushed the sideshowbarker/os-sched_getaffinity-workaround branch from 35be9ce to 165f9f2 Compare February 15, 2024 10:14
@sideshowbarker
Copy link
Member Author

OK, CI all green now

@rossberg rossberg merged commit bcd1924 into main Feb 15, 2024
4 checks passed
@rossberg rossberg deleted the sideshowbarker/os-sched_getaffinity-workaround branch February 15, 2024 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants