Skip to content

Commit ba305ca

Browse files
committed
Introduce CpuCoreCounter
1 parent 9d9f16e commit ba305ca

3 files changed

Lines changed: 59 additions & 37 deletions

File tree

conf/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ services:
514514
-
515515
class: PHPStan\Parser\FunctionCallStatementFinder
516516

517+
-
518+
class: PHPStan\Process\CpuCoreCounter
519+
517520
-
518521
implement: PHPStan\Reflection\FunctionReflectionFactory
519522

src/Command/AnalyserRunner.php

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\AnalyserResult;
77
use PHPStan\Parallel\ParallelAnalyser;
88
use PHPStan\Parallel\Scheduler;
9+
use PHPStan\Process\CpuCoreCounter;
910
use Symfony\Component\Console\Input\InputInterface;
1011

1112
class AnalyserRunner
@@ -17,15 +18,19 @@ class AnalyserRunner
1718

1819
private ParallelAnalyser $parallelAnalyser;
1920

21+
private CpuCoreCounter $cpuCoreCounter;
22+
2023
public function __construct(
2124
Scheduler $scheduler,
2225
Analyser $analyser,
23-
ParallelAnalyser $parallelAnalyser
26+
ParallelAnalyser $parallelAnalyser,
27+
CpuCoreCounter $cpuCoreCounter
2428
)
2529
{
2630
$this->scheduler = $scheduler;
2731
$this->analyser = $analyser;
2832
$this->parallelAnalyser = $parallelAnalyser;
33+
$this->cpuCoreCounter = $cpuCoreCounter;
2934
}
3035

3136
/**
@@ -54,7 +59,7 @@ public function runAnalyser(
5459
return new AnalyserResult([], [], [], [], false);
5560
}
5661

57-
$schedule = $this->scheduler->scheduleWork($this->getNumberOfCpuCores(), $files);
62+
$schedule = $this->scheduler->scheduleWork($this->cpuCoreCounter->getNumberOfCpuCores(), $files);
5863
$mainScript = null;
5964
if (isset($_SERVER['argv'][0]) && file_exists($_SERVER['argv'][0])) {
6065
$mainScript = $_SERVER['argv'][0];
@@ -77,39 +82,4 @@ public function runAnalyser(
7782
);
7883
}
7984

80-
private function getNumberOfCpuCores(): int
81-
{
82-
// from brianium/paratest
83-
$cores = 2;
84-
if (is_file('/proc/cpuinfo')) {
85-
// Linux (and potentially Windows with linux sub systems)
86-
$cpuinfo = @file_get_contents('/proc/cpuinfo');
87-
if ($cpuinfo !== false) {
88-
preg_match_all('/^processor/m', $cpuinfo, $matches);
89-
return count($matches[0]);
90-
}
91-
}
92-
93-
if (\DIRECTORY_SEPARATOR === '\\') {
94-
// Windows
95-
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
96-
if ($process !== false) {
97-
fgets($process);
98-
$cores = (int) fgets($process);
99-
pclose($process);
100-
}
101-
102-
return $cores;
103-
}
104-
105-
$process = @\popen('sysctl -n hw.ncpu', 'rb');
106-
if ($process !== false) {
107-
// *nix (Linux, BSD and Mac)
108-
$cores = (int) fgets($process);
109-
pclose($process);
110-
}
111-
112-
return $cores;
113-
}
114-
11585
}

src/Process/CpuCoreCounter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Process;
4+
5+
class CpuCoreCounter
6+
{
7+
8+
private ?int $count = null;
9+
10+
public function getNumberOfCpuCores(): int
11+
{
12+
if ($this->count !== null) {
13+
return $this->count;
14+
}
15+
16+
// from brianium/paratest
17+
$cores = 2;
18+
if (is_file('/proc/cpuinfo')) {
19+
// Linux (and potentially Windows with linux sub systems)
20+
$cpuinfo = @file_get_contents('/proc/cpuinfo');
21+
if ($cpuinfo !== false) {
22+
preg_match_all('/^processor/m', $cpuinfo, $matches);
23+
return $this->count = count($matches[0]);
24+
}
25+
}
26+
27+
if (\DIRECTORY_SEPARATOR === '\\') {
28+
// Windows
29+
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
30+
if ($process !== false) {
31+
fgets($process);
32+
$cores = (int) fgets($process);
33+
pclose($process);
34+
}
35+
36+
return $this->count = $cores;
37+
}
38+
39+
$process = @\popen('sysctl -n hw.ncpu', 'rb');
40+
if ($process !== false) {
41+
// *nix (Linux, BSD and Mac)
42+
$cores = (int) fgets($process);
43+
pclose($process);
44+
}
45+
46+
return $this->count = $cores;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)