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

Improve CPU cores count detection #189

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"require": {
"php": "^7.4 || ^8.0",
"fidry/cpu-core-counter": "^0.3.0",
"nikic/iter": "^2.2",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"symfony/console": "^4.4 || ^5.4 || ^6.0",
Expand Down
45 changes: 8 additions & 37 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

namespace Webmozarts\Console\Parallelization;

use Fidry\CpuCoreCounter\CpuCoreCounter as FidryCpuCoreCounter;
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
use Webmozart\Assert\Assert;

/**
* @internal
* From https://github.com/phpstan/phpstan-src/blob/1.8.x/src/Process/CpuCoreCounter.php
*/
final class CpuCoreCounter
{
Expand All @@ -32,11 +33,7 @@ public static function getNumberOfCpuCores(): int
return self::$count;
}

if (!function_exists('proc_open')) {
return self::$count = 1;
}

$count = $_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT'];
$count = $_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT'] ?? false;

if (false !== $count) {
Assert::numeric($count);
Expand All @@ -45,38 +42,12 @@ public static function getNumberOfCpuCores(): int
return self::$count = (int) $count;
}

// from brianium/paratest
if (@is_file('/proc/cpuinfo')) {
// Linux (and potentially Windows with linux sub systems)
$cpuinfo = @file_get_contents('/proc/cpuinfo');
if (false !== $cpuinfo) {
preg_match_all('/^processor/m', $cpuinfo, $matches);

return self::$count = count($matches[0]);
}
}

if (DIRECTORY_SEPARATOR === '\\') {
// Windows
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
if (is_resource($process)) {
fgets($process);
$cores = (int) fgets($process);
pclose($process);

return self::$count = $cores;
}
}

$process = @popen('sysctl -n hw.ncpu', 'rb');
if (is_resource($process)) {
// *nix (Linux, BSD and Mac)
$cores = (int) fgets($process);
pclose($process);

return self::$count = $cores;
try {
self::$count = (new FidryCpuCoreCounter())->getCount();
} catch (NumberOfCpuCoreNotFound $exception) {
self::$count = 1;
}

return self::$count = 2;
return self::$count;
}
}
5 changes: 5 additions & 0 deletions tests/CpuCoreCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
*/
final class CpuCoreCounterTest extends TestCase
{
/**
* @backupGlobals
*/
public function test_can_get_the_number_of_cpu_cores(): void
{
unset($_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT']);

$cpuCoresCount = CpuCoreCounter::getNumberOfCpuCores();

self::assertGreaterThan(0, $cpuCoresCount);
Expand Down