Skip to content

Commit aa52e0c

Browse files
authored
Improve CPU cores count detection (#189)
1 parent 3a23cbf commit aa52e0c

File tree

3 files changed

+14
-37
lines changed

3 files changed

+14
-37
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"require": {
1717
"php": "^7.4 || ^8.0",
18+
"fidry/cpu-core-counter": "^0.3.0",
1819
"nikic/iter": "^2.2",
1920
"psr/log": "^1.1 || ^2.0 || ^3.0",
2021
"symfony/console": "^4.4 || ^5.4 || ^6.0",

src/CpuCoreCounter.php

+8-37
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
namespace Webmozarts\Console\Parallelization;
1515

16+
use Fidry\CpuCoreCounter\CpuCoreCounter as FidryCpuCoreCounter;
17+
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
1618
use Webmozart\Assert\Assert;
1719

1820
/**
1921
* @internal
20-
* From https://github.com/phpstan/phpstan-src/blob/1.8.x/src/Process/CpuCoreCounter.php
2122
*/
2223
final class CpuCoreCounter
2324
{
@@ -32,11 +33,7 @@ public static function getNumberOfCpuCores(): int
3233
return self::$count;
3334
}
3435

35-
if (!function_exists('proc_open')) {
36-
return self::$count = 1;
37-
}
38-
39-
$count = $_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT'];
36+
$count = $_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT'] ?? false;
4037

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

48-
// from brianium/paratest
49-
if (@is_file('/proc/cpuinfo')) {
50-
// Linux (and potentially Windows with linux sub systems)
51-
$cpuinfo = @file_get_contents('/proc/cpuinfo');
52-
if (false !== $cpuinfo) {
53-
preg_match_all('/^processor/m', $cpuinfo, $matches);
54-
55-
return self::$count = count($matches[0]);
56-
}
57-
}
58-
59-
if (DIRECTORY_SEPARATOR === '\\') {
60-
// Windows
61-
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
62-
if (is_resource($process)) {
63-
fgets($process);
64-
$cores = (int) fgets($process);
65-
pclose($process);
66-
67-
return self::$count = $cores;
68-
}
69-
}
70-
71-
$process = @popen('sysctl -n hw.ncpu', 'rb');
72-
if (is_resource($process)) {
73-
// *nix (Linux, BSD and Mac)
74-
$cores = (int) fgets($process);
75-
pclose($process);
76-
77-
return self::$count = $cores;
45+
try {
46+
self::$count = (new FidryCpuCoreCounter())->getCount();
47+
} catch (NumberOfCpuCoreNotFound $exception) {
48+
self::$count = 1;
7849
}
7950

80-
return self::$count = 2;
51+
return self::$count;
8152
}
8253
}

tests/CpuCoreCounterTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
*/
2323
final class CpuCoreCounterTest extends TestCase
2424
{
25+
/**
26+
* @backupGlobals
27+
*/
2528
public function test_can_get_the_number_of_cpu_cores(): void
2629
{
30+
unset($_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT']);
31+
2732
$cpuCoresCount = CpuCoreCounter::getNumberOfCpuCores();
2833

2934
self::assertGreaterThan(0, $cpuCoresCount);

0 commit comments

Comments
 (0)