|
3 | 3 | namespace PHPStan\Process; |
4 | 4 |
|
5 | 5 | use Fidry\CpuCoreCounter\CpuCoreCounter as FidryCpuCoreCounter; |
6 | | -use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound; |
| 6 | +use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; |
| 7 | +use Fidry\CpuCoreCounter\Finder\FinderRegistry; |
| 8 | +use Fidry\CpuCoreCounter\ParallelisationResult; |
7 | 9 | use PHPStan\DependencyInjection\AutowiredParameter; |
8 | 10 | use PHPStan\DependencyInjection\AutowiredService; |
9 | | -use function min; |
10 | 11 |
|
11 | 12 | #[AutowiredService] |
12 | 13 | final class CpuCoreCounter |
13 | 14 | { |
14 | 15 |
|
15 | | - private ?int $count = null; |
| 16 | + private ?ParallelisationResult $result = null; |
16 | 17 |
|
17 | | - /** |
18 | | - * What fidry/cpu-core-counter found: the raw count, the count left after the |
19 | | - * load limit and KUBERNETES_CPU_LIMIT were applied, and that environment limit. |
20 | | - * |
21 | | - * @var array{int, int, int|null}|null |
22 | | - */ |
23 | | - private ?array $detected = null; |
| 18 | + private CpuCoreFinder $countLimitFinder; |
24 | 19 |
|
25 | 20 | public function __construct( |
26 | 21 | #[AutowiredParameter(ref: '%parallel.loadLimit%')] |
27 | 22 | private ?float $loadLimit, |
28 | | - private SystemResources $systemResources, |
29 | 23 | ) |
30 | 24 | { |
| 25 | + $this->countLimitFinder = FinderRegistry::getDefaultCountLimitFinder(); |
31 | 26 | } |
32 | 27 |
|
33 | 28 | /** |
34 | 29 | * Cores PHPStan may actually use: what the machine reports, reduced by the load |
35 | | - * limit and capped by the CPU quota of the cgroup it runs in. |
| 30 | + * limit and capped by the cgroup CPU quota or, failing that, KUBERNETES_CPU_LIMIT. |
36 | 31 | */ |
37 | 32 | public function getNumberOfCpuCores(): int |
38 | 33 | { |
39 | | - if ($this->count !== null) { |
40 | | - return $this->count; |
41 | | - } |
42 | | - |
43 | | - $count = $this->getNumberOfCpuCoresAfterLimits(); |
44 | | - |
45 | | - // fidry/cpu-core-counter has no cgroup finder, and its nproc-based default |
46 | | - // honours a cpuset affinity mask but not a CFS bandwidth quota, so inside a |
47 | | - // `docker run --cpus=2` container it reports the host's core count |
48 | | - $quota = $this->systemResources->getCpuQuota(); |
49 | | - if ($quota !== null) { |
50 | | - $count = min($count, $quota); |
51 | | - } |
52 | | - |
53 | | - return $this->count = $count; |
| 34 | + return $this->detect()->availableCpus; |
54 | 35 | } |
55 | 36 |
|
56 | 37 | /** What the machine reports, before any limit is applied. */ |
57 | 38 | public function getDetectedNumberOfCpuCores(): int |
58 | 39 | { |
59 | | - return $this->detect()[0]; |
| 40 | + return $this->detect()->totalCoresCount; |
60 | 41 | } |
61 | 42 |
|
62 | | - /** The detected count after the load limit and KUBERNETES_CPU_LIMIT, before any cgroup quota. */ |
63 | | - public function getNumberOfCpuCoresAfterLimits(): int |
| 43 | + public function getCountLimitFinder(): CpuCoreFinder |
64 | 44 | { |
65 | | - return $this->detect()[1]; |
| 45 | + return $this->countLimitFinder; |
66 | 46 | } |
67 | 47 |
|
68 | | - public function getKubernetesCpuLimit(): ?int |
| 48 | + private function detect(): ParallelisationResult |
69 | 49 | { |
70 | | - return $this->detect()[2]; |
71 | | - } |
72 | | - |
73 | | - /** @return array{int, int, int|null} */ |
74 | | - private function detect(): array |
75 | | - { |
76 | | - if ($this->detected !== null) { |
77 | | - return $this->detected; |
78 | | - } |
79 | | - |
80 | | - try { |
81 | | - $result = (new FidryCpuCoreCounter())->getAvailableForParallelisation(0, null, $this->loadLimit); |
82 | | - $this->detected = [$result->totalCoresCount, $result->availableCpus, $result->correctedCountLimit]; |
83 | | - } catch (NumberOfCpuCoreNotFound) { |
84 | | - $this->detected = [1, 1, null]; |
85 | | - } |
86 | | - |
87 | | - return $this->detected; |
| 50 | + return $this->result ??= (new FidryCpuCoreCounter(countLimitFinder: $this->countLimitFinder))->getAvailableForParallelisation(0, null, $this->loadLimit); |
88 | 51 | } |
89 | 52 |
|
90 | 53 | } |
0 commit comments