Skip to content

Commit

Permalink
Need to change the method for determining the number of CPUs on Linux
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273675

Reviewed by Carlos Garcia Campos.

Try to use sysconf(_SC_NPROCESSORS_ONLN) on Linux to determine the
amount of usable CPU cores. While Perl provides access to the function
in its POSIX module, the _SC_NPROCESSORS_ONLN constant is not defined
and the value of the constant is used instead. Using the value is fine
as it is part of the user space API and not expected to ever change.
As a fallback, make Perl parse /proc/cpuinfo directly instead of
shelling out to a cat|grep pipeline: Perl knows how to regex and how
to count just fine.

* Tools/Scripts/webkitdirs.pm:
(determineNumberOfCPUs): Replace usage of nproc command with sysconf,
update /proc/cpuinfo parsing fallback.

Canonical link: https://commits.webkit.org/278879@main
  • Loading branch information
aperezdc committed May 16, 2024
1 parent 8de0ecb commit 9bf347a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Tools/Scripts/webkitdirs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,18 @@ sub determineNumberOfCPUs
if (defined($ENV{NUMBER_OF_PROCESSORS})) {
$numberOfCPUs = $ENV{NUMBER_OF_PROCESSORS};
} elsif (isLinux()) {
# First try the nproc utility, if it exists. If we get no
# results fall back to just interpretting /proc directly.
chomp($numberOfCPUs = `nproc --all 2> /dev/null`);
use POSIX;
$numberOfCPUs = POSIX::sysconf(83); # _SC_NPROCESSORS_ONLN = 83
if ($numberOfCPUs eq "") {
$numberOfCPUs = (grep /processor/, `cat /proc/cpuinfo`);
$numberOfCPUs = 0;
open CPUINFO, "/proc/cpuinfo";
while (<CPUINFO>) {
if (/[Pp]rocessor\s/) { $numberOfCPUs++; }
}
close CPUINFO;
}
if ($numberOfCPUs == 0) {
$numberOfCPUs = 1;
}
} elsif (isAnyWindows()) {
# Assumes cygwin
Expand Down

0 comments on commit 9bf347a

Please sign in to comment.