Skip to content

Commit

Permalink
Make CPU cores count more tolerant towards system command errors (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Oct 15, 2022
1 parent cac814f commit fbd8c44
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Resource/Processor/CpuCoresCountProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use function function_exists;
use function is_int;
use function is_readable;
use Safe\Exceptions\ExecException;
use function Safe\file_get_contents;
use function Safe\shell_exec;
use function substr_count;
Expand All @@ -65,24 +66,30 @@ public static function provide(): int
return 1;
}

// for Linux
$hasNproc = trim(@shell_exec('command -v nproc'));
try {
// for Linux
$hasNproc = trim(@shell_exec('command -v nproc'));

if ($hasNproc !== '') {
$nproc = trim(shell_exec('nproc'));
$cpuCount = filter_var($nproc, FILTER_VALIDATE_INT);
if ($hasNproc !== '') {
$nproc = trim(shell_exec('nproc'));
$cpuCount = filter_var($nproc, FILTER_VALIDATE_INT);

if (is_int($cpuCount)) {
return $cpuCount;
if (is_int($cpuCount)) {
return $cpuCount;
}
}
} catch (ExecException) {
}

// for MacOS
$ncpu = trim(shell_exec('sysctl -n hw.ncpu'));
$cpuCount = filter_var($ncpu, FILTER_VALIDATE_INT);
try {
// for MacOS
$ncpu = trim(shell_exec('sysctl -n hw.ncpu'));
$cpuCount = filter_var($ncpu, FILTER_VALIDATE_INT);

if (is_int($cpuCount)) {
return $cpuCount;
if (is_int($cpuCount)) {
return $cpuCount;
}
} catch (ExecException) {
}

if (is_readable('/proc/cpuinfo')) {
Expand Down

0 comments on commit fbd8c44

Please sign in to comment.