Skip to content

Commit 323451a

Browse files
theofidryondrejmirtes
authored andcommitted
Improve the CPU core count detection
See theofidry/cpu-core-counter#11 for the motivations. The code should be sensibly the same, the notable differences are: - fixed the deprecated usage of `hw.ncpu` - /proc/cpuinfo is check _last_ - nproc is checked first (before was not checked at all, it's considered to be more accurate and less convoluted than cpuinfo though) - not sure about the `return 2`, but this was not too clear [here](phpstan#514 (review)) neither. I could otherwise change the fallback value to return `1` if `proc_open` doesn't exist and 2 otherwise - add more ways to find the CPU cores count
1 parent 3c3b17f commit 323451a

3 files changed

Lines changed: 70 additions & 45 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"clue/ndjson-react": "^1.0",
1111
"composer/ca-bundle": "^1.2",
1212
"composer/xdebug-handler": "^3.0.3",
13+
"fidry/cpu-core-counter": "^0.4.0",
1314
"hoa/compiler": "3.17.08.08",
1415
"hoa/exception": "^1.0",
1516
"hoa/regex": "1.17.01.13",

composer.lock

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Process/CpuCoreCounter.php

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22

33
namespace PHPStan\Process;
44

5-
use function count;
6-
use function fgets;
7-
use function file_get_contents;
8-
use function function_exists;
9-
use function is_file;
10-
use function is_resource;
11-
use function pclose;
12-
use function popen;
13-
use function preg_match_all;
14-
use const DIRECTORY_SEPARATOR;
5+
use Fidry\CpuCoreCounter\CpuCoreCounter as FidryCpuCoreCounter;
6+
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
157

168
class CpuCoreCounter
179
{
@@ -24,42 +16,13 @@ public function getNumberOfCpuCores(): int
2416
return $this->count;
2517
}
2618

27-
if (!function_exists('proc_open')) {
28-
return $this->count = 1;
19+
try {
20+
$this->count = (new FidryCpuCoreCounter())->getCount();
21+
} catch (NumberOfCpuCoreNotFound) {
22+
$this->count = 1;
2923
}
3024

31-
// from brianium/paratest
32-
if (@is_file('/proc/cpuinfo')) {
33-
// Linux (and potentially Windows with linux sub systems)
34-
$cpuinfo = @file_get_contents('/proc/cpuinfo');
35-
if ($cpuinfo !== false) {
36-
preg_match_all('/^processor/m', $cpuinfo, $matches);
37-
return $this->count = count($matches[0]);
38-
}
39-
}
40-
41-
if (DIRECTORY_SEPARATOR === '\\') {
42-
// Windows
43-
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
44-
if (is_resource($process)) {
45-
fgets($process);
46-
$cores = (int) fgets($process);
47-
pclose($process);
48-
49-
return $this->count = $cores;
50-
}
51-
}
52-
53-
$process = @popen('sysctl -n hw.ncpu', 'rb');
54-
if (is_resource($process)) {
55-
// *nix (Linux, BSD and Mac)
56-
$cores = (int) fgets($process);
57-
pclose($process);
58-
59-
return $this->count = $cores;
60-
}
61-
62-
return $this->count = 2;
25+
return $this->count;
6326
}
6427

6528
}

0 commit comments

Comments
 (0)