Skip to content

Commit d88c17a

Browse files
AJenboondrejmirtes
authored andcommitted
Update fidry/cpu-core-counter
1 parent 61a0d9f commit d88c17a

6 files changed

Lines changed: 29 additions & 553 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"composer-runtime-api": "^2.0",
2121
"clue/ndjson-react": "^1.0",
2222
"composer/semver": "^3.4",
23-
"fidry/cpu-core-counter": "^1.2",
23+
"fidry/cpu-core-counter": "^1.4",
2424
"guzzlehttp/guzzle": "^7.9",
2525
"hoa/compiler": "3.17.08.08",
2626
"hoa/exception": "^1.0",

‎composer.lock‎

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Diagnose/SystemResourcesDiagnoseExtension.php‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace PHPStan\Diagnose;
44

5+
use Fidry\CpuCoreCounter\Diagnoser;
56
use PHPStan\Command\Output;
67
use PHPStan\DependencyInjection\AutowiredParameter;
78
use PHPStan\DependencyInjection\AutowiredService;
89
use PHPStan\Process\CpuCoreCounter;
9-
use PHPStan\Process\SystemResources;
1010
use function sprintf;
1111

1212
/**
@@ -19,7 +19,6 @@ final class SystemResourcesDiagnoseExtension implements DiagnoseExtension
1919

2020
public function __construct(
2121
private CpuCoreCounter $cpuCoreCounter,
22-
private SystemResources $systemResources,
2322
#[AutowiredParameter(ref: '%parallel.loadLimit%')]
2423
private ?float $loadLimit,
2524
)
@@ -32,21 +31,13 @@ public function print(Output $output): void
3231
$output->writeLineFormatted(sprintf('Detected CPU cores: %d', $this->cpuCoreCounter->getDetectedNumberOfCpuCores()));
3332
$output->writeLineFormatted(sprintf('Load limit: %s', $this->loadLimit === null ? 'none' : (string) $this->loadLimit));
3433

35-
$kubernetesLimit = $this->cpuCoreCounter->getKubernetesCpuLimit();
36-
$output->writeLineFormatted(sprintf(
37-
'KUBERNETES_CPU_LIMIT: %s',
38-
$kubernetesLimit === null ? 'none' : sprintf('%d cores', $kubernetesLimit),
39-
));
40-
$output->writeLineFormatted(sprintf('Available after limits: %d', $this->cpuCoreCounter->getNumberOfCpuCoresAfterLimits()));
41-
42-
$quota = $this->systemResources->getCpuQuota();
43-
$output->writeLineFormatted(sprintf(
44-
'cgroup CPU quota: %s',
45-
$quota === null ? 'none' : sprintf('%d cores', $quota),
46-
));
47-
4834
$output->writeLineFormatted(sprintf('Usable CPU cores: %d', $this->cpuCoreCounter->getNumberOfCpuCores()));
4935
$output->writeLineFormatted('');
36+
37+
// lists every limit the counter looks at, also one that is overridden by the limit found before it
38+
$output->writeLineFormatted('<info>CPU core limit:</info>');
39+
$output->writeRaw(Diagnoser::diagnose([$this->cpuCoreCounter->getCountLimitFinder()]));
40+
$output->writeLineFormatted('');
5041
}
5142

5243
}

‎src/Process/CpuCoreCounter.php‎

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,51 @@
33
namespace PHPStan\Process;
44

55
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;
79
use PHPStan\DependencyInjection\AutowiredParameter;
810
use PHPStan\DependencyInjection\AutowiredService;
9-
use function min;
1011

1112
#[AutowiredService]
1213
final class CpuCoreCounter
1314
{
1415

15-
private ?int $count = null;
16+
private ?ParallelisationResult $result = null;
1617

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;
2419

2520
public function __construct(
2621
#[AutowiredParameter(ref: '%parallel.loadLimit%')]
2722
private ?float $loadLimit,
28-
private SystemResources $systemResources,
2923
)
3024
{
25+
$this->countLimitFinder = FinderRegistry::getDefaultCountLimitFinder();
3126
}
3227

3328
/**
3429
* 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.
3631
*/
3732
public function getNumberOfCpuCores(): int
3833
{
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;
5435
}
5536

5637
/** What the machine reports, before any limit is applied. */
5738
public function getDetectedNumberOfCpuCores(): int
5839
{
59-
return $this->detect()[0];
40+
return $this->detect()->totalCoresCount;
6041
}
6142

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
6444
{
65-
return $this->detect()[1];
45+
return $this->countLimitFinder;
6646
}
6747

68-
public function getKubernetesCpuLimit(): ?int
48+
private function detect(): ParallelisationResult
6949
{
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);
8851
}
8952

9053
}

0 commit comments

Comments
 (0)