From 037348e99ada4d819460326c21d6cebe6338bfe6 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Mon, 20 May 2024 15:01:29 -0400 Subject: chainlint.pl: make CPU count computation more robust There have been reports[1,2] of chainlint.pl failing to produce output when output is expected. In fact, the underlying problem is more severe: in these cases, it isn't doing any work at all, thus not checking Git tests for semantic problems. In the reported cases, the problem was tracked down to ncores() returning 0 for the CPU count, which resulted in chainlint.pl not performing any work (since it thought it had no cores on which to process). In the reported cases, the reason for the failure was that the regular expression counting the number of processors reported by /proc/cpuinfo failed to find any matches, hence it counted 0 processors. Although fixing each case as it is reported allows chaining.pl to work correctly on that architecture, it does nothing to improve the overall robustness of the core count computation which may still return 0 on some yet untested architecture. Address this shortcoming by ensuring that ncores() returns a sensible fallback value in all cases. [1]: https://lore.kernel.org/git/pull.1385.git.git.1669148861635.gitgitgadget@gmail.com/ [2]: https://lore.kernel.org/git/8baa12f8d044265f1ddeabd64209e7ac0d3700ae.camel@physik.fu-berlin.de/ Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- t/chainlint.pl | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/t/chainlint.pl b/t/chainlint.pl index e966412999..28e2506032 100755 --- a/t/chainlint.pl +++ b/t/chainlint.pl @@ -707,11 +707,22 @@ sub fd_colors { sub ncores { # Windows - return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS}); + if (exists($ENV{NUMBER_OF_PROCESSORS})) { + my $ncpu = $ENV{NUMBER_OF_PROCESSORS}; + return $ncpu > 0 ? $ncpu : 1; + } # Linux / MSYS2 / Cygwin / WSL - do { local @ARGV='/proc/cpuinfo'; return scalar(grep(/^processor[\s\d]*:/, <>)); } if -r '/proc/cpuinfo'; + if (open my $fh, '<', '/proc/cpuinfo') { + my $cpuinfo = do { local $/; <$fh> }; + close($fh); + my @matches = ($cpuinfo =~ /^processor[\s\d]*:/mg); + return @matches ? scalar(@matches) : 1; + } # macOS & BSD - return qx/sysctl -n hw.ncpu/ if $^O =~ /(?:^darwin$|bsd)/; + if ($^O =~ /(?:^darwin$|bsd)/) { + my $ncpu = qx/sysctl -n hw.ncpu/; + return $ncpu > 0 ? $ncpu : 1; + } return 1; } -- cgit v1.2.3 From 45db5ed3b2f9f1c4768633f3d691bbe1305cf9ca Mon Sep 17 00:00:00 2001 From: John Paul Adrian Glaubitz Date: Mon, 20 May 2024 15:01:30 -0400 Subject: chainlint.pl: fix incorrect CPU count on Linux SPARC On SPARC systems running Linux, individual processors are denoted with "CPUnn:" in /proc/cpuinfo instead of the usual "processor : NN". As a result, the regexp in ncores() matches 0 times. Address this shortcoming by extending the regexp to also match lines with "CPUnn:". Signed-off-by: John Paul Adrian Glaubitz [es: simplified regexp; tweaked commit message] Signed-off-by: Eric Sunshine Tested-by: John Paul Adrian Glaubitz Signed-off-by: Junio C Hamano --- t/chainlint.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/chainlint.pl b/t/chainlint.pl index 28e2506032..ea154a206a 100755 --- a/t/chainlint.pl +++ b/t/chainlint.pl @@ -715,7 +715,7 @@ sub ncores { if (open my $fh, '<', '/proc/cpuinfo') { my $cpuinfo = do { local $/; <$fh> }; close($fh); - my @matches = ($cpuinfo =~ /^processor[\s\d]*:/mg); + my @matches = ($cpuinfo =~ /^(processor|CPU)[\s\d]*:/mg); return @matches ? scalar(@matches) : 1; } # macOS & BSD -- cgit v1.2.3 From 2e7e9205beb5a745f91d0d0ce772c7b447c3aba8 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Mon, 20 May 2024 15:01:31 -0400 Subject: chainlint.pl: latch CPU count directly reported by /proc/cpuinfo On Linux, ncores() computes the number of CPUs by counting the "processor" or "CPU" lines emitted by /proc/cpuinfo. However, on some platforms, /proc/cpuinfo does not enumerate the CPUs at all, but instead merely mentions the total number of CPUs. In such cases, pluck the CPU count directly from the /proc/cpuinfo line which reports the number of active CPUs. (In particular, check for "cpus active: NN" and "ncpus active: NN" since both variants have been seen in the wild[1,2].) [1]: https://lore.kernel.org/git/503a99f3511559722a3eeef15d31027dfe617fa1.camel@physik.fu-berlin.de/ [2]: https://lore.kernel.org/git/7acbd5c6c68bd7ba020e2d1cc457a8954fd6edf4.camel@physik.fu-berlin.de/ Reported-by: John Paul Adrian Glaubitz Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- t/chainlint.pl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/chainlint.pl b/t/chainlint.pl index ea154a206a..f00a3b937b 100755 --- a/t/chainlint.pl +++ b/t/chainlint.pl @@ -715,6 +715,9 @@ sub ncores { if (open my $fh, '<', '/proc/cpuinfo') { my $cpuinfo = do { local $/; <$fh> }; close($fh); + if ($cpuinfo =~ /^n?cpus active\s*:\s*(\d+)/m) { + return $1 if $1 > 0; + } my @matches = ($cpuinfo =~ /^(processor|CPU)[\s\d]*:/mg); return @matches ? scalar(@matches) : 1; } -- cgit v1.2.3