Skip to content

Commit 037348e

Browse files
sunshinecogitster
authored andcommitted
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/[email protected]/ [2]: https://lore.kernel.org/git/8baa12f8d044265f1ddeabd64209e7ac0d3700ae.camel@physik.fu-berlin.de/ Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b9b439e commit 037348e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

t/chainlint.pl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,22 @@ sub fd_colors {
707707

708708
sub ncores {
709709
# Windows
710-
return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS});
710+
if (exists($ENV{NUMBER_OF_PROCESSORS})) {
711+
my $ncpu = $ENV{NUMBER_OF_PROCESSORS};
712+
return $ncpu > 0 ? $ncpu : 1;
713+
}
711714
# Linux / MSYS2 / Cygwin / WSL
712-
do { local @ARGV='/proc/cpuinfo'; return scalar(grep(/^processor[\s\d]*:/, <>)); } if -r '/proc/cpuinfo';
715+
if (open my $fh, '<', '/proc/cpuinfo') {
716+
my $cpuinfo = do { local $/; <$fh> };
717+
close($fh);
718+
my @matches = ($cpuinfo =~ /^processor[\s\d]*:/mg);
719+
return @matches ? scalar(@matches) : 1;
720+
}
713721
# macOS & BSD
714-
return qx/sysctl -n hw.ncpu/ if $^O =~ /(?:^darwin$|bsd)/;
722+
if ($^O =~ /(?:^darwin$|bsd)/) {
723+
my $ncpu = qx/sysctl -n hw.ncpu/;
724+
return $ncpu > 0 ? $ncpu : 1;
725+
}
715726
return 1;
716727
}
717728

0 commit comments

Comments
 (0)