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

Need to change the method for determining the number of CPUs on Linux #28388

Conversation

mcatanzaro
Copy link
Contributor

@mcatanzaro mcatanzaro commented May 10, 2024

https://bugs.webkit.org/show_bug.cgi?id=273675

Reviewed by NOBODY (OOPS!).

nproc is unreliable, so remove it and rely on the fallback that greps
/proc/cpuinfo instead. This looks a little bit fragile (e.g. it would
fail if my AMD Ryzen "Processor" were instead an AMD Ryzen "processor")
but surely an improvement.

* Tools/Scripts/webkitdirs.pm:
(determineNumberOfCPUs):
@mcatanzaro mcatanzaro self-assigned this May 10, 2024
@mcatanzaro mcatanzaro added the Tools / Tests Tools in the Tools directory, build issues, test infrastructure, and bugs in test cases label May 10, 2024
@aperezdc
Copy link
Contributor

How is nproc unreliable? I felt tempted to suggest getconf _NPROCESSORS_ONLN but the getconf utility might not be present (it comes with glibc, and not all Linux systems are on glibc)... How about lscpu, which comes with util-linux (expected to be installed in all Linux systems) and has a parseable output mode? The best I can come up using that is this:

lscpu -b --parse=cpu | awk '/^[^#]/{ ncpus += 1 } END { print ncpus }

Surely counting lines that do not start with a # can be done in Perl instead of also shelling out to Awk.

@mcatanzaro
Copy link
Contributor Author

How is nproc unreliable?

Guess: it's counting deactivated core complexes? I don't know, but I don't see any reason to second guess the bug reporter.

lscpu -b --parse=cpu | awk '/^[^#]/{ ncpus += 1 } END { print ncpus }

We could do this, but it's nice to depend on as few external programs as possible, and this is already running in a really niche case: it's only used if you don't have ninja installed.

Comment on lines +733 to +734
# Don't use nproc because it returns unreliable results, https://webkit.org/b/273675
$numberOfCPUs = (grep /processor/, `cat /proc/cpuinfo`);
Copy link
Contributor

@aperezdc aperezdc May 14, 2024

Choose a reason for hiding this comment

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

I still don't like this. And it is not really needed to shell out to nproc, or even to grep... remember that Perl is supposed to be a glorified grep. So I would instead suggest:

Suggested change
# Don't use nproc because it returns unreliable results, https://webkit.org/b/273675
$numberOfCPUs = (grep /processor/, `cat /proc/cpuinfo`);
use POSIX;
$numberOfCPUs = POSIX::sysconf(83); # _SC_NPROCESSORS_ONLN = 83
if ($numberOfCPUs == "") {
open CPUINFO, "/proc/cpuinfo";
$numberOfCPUs = 0;
while (<CPUINFO>) {
if (/^[Pp]rocessor\s/) { $numberOfCPUs++; }
}
close CPUINFO;
}

(Yes, I tested both the sysconf code path and the fallback to parsing /proc/cpuinfo here.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, but now you've written 100% of the code in this pull request, so we might as well close this one and let you open a new one for me to review?

Copy link
Contributor

Choose a reason for hiding this comment

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

Done: #28645

@aperezdc
Copy link
Contributor

Closing in favor of #28645

@aperezdc aperezdc closed this May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools / Tests Tools in the Tools directory, build issues, test infrastructure, and bugs in test cases
Projects
None yet
3 participants