|
6 | 6 | use PHPStan\DependencyInjection\AutowiredParameter; |
7 | 7 | use PHPStan\DependencyInjection\AutowiredService; |
8 | 8 | use PHPStan\Diagnose\DiagnoseExtension; |
| 9 | +use PHPStan\Process\SystemResources; |
9 | 10 | use function array_values; |
10 | 11 | use function ceil; |
11 | 12 | use function count; |
|
19 | 20 | final class Scheduler implements DiagnoseExtension |
20 | 21 | { |
21 | 22 |
|
22 | | - /** @var array{int, int, int, int}|null */ |
| 23 | + public const AUTO = 'auto'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Used when the platform cannot say how much memory is available. This is the |
| 27 | + * historical fixed default: conservative on a big machine, but the only safe |
| 28 | + * answer when flying blind. |
| 29 | + */ |
| 30 | + private const UNKNOWN_MEMORY_PROCESSES_LIMIT = 8; |
| 31 | + |
| 32 | + /** @var array{int, int, int, int, string}|null */ |
23 | 33 | private ?array $storedData = null; |
24 | 34 |
|
25 | 35 | /** |
26 | 36 | * @param positive-int $jobSize |
27 | | - * @param positive-int $maximumNumberOfProcesses |
| 37 | + * @param positive-int|self::AUTO $maximumNumberOfProcesses |
28 | 38 | * @param positive-int $minimumNumberOfJobsPerProcess |
29 | 39 | */ |
30 | 40 | public function __construct( |
31 | 41 | #[AutowiredParameter(ref: '%parallel.jobSize%')] |
32 | 42 | private int $jobSize, |
33 | 43 | #[AutowiredParameter(ref: '%parallel.maximumNumberOfProcesses%')] |
34 | | - private int $maximumNumberOfProcesses, |
| 44 | + private int|string $maximumNumberOfProcesses, |
35 | 45 | #[AutowiredParameter(ref: '%parallel.minimumNumberOfJobsPerProcess%')] |
36 | 46 | private int $minimumNumberOfJobsPerProcess, |
| 47 | + private SystemResources $systemResources, |
| 48 | + private WorkerMemoryBudget $workerMemoryBudget, |
37 | 49 | ) |
38 | 50 | { |
39 | 51 | } |
@@ -78,25 +90,68 @@ public function scheduleWork( |
78 | 90 | $cpuCores, |
79 | 91 | ); |
80 | 92 |
|
81 | | - $usedNumberOfProcesses = min($numberOfProcesses, $this->maximumNumberOfProcesses); |
82 | | - $this->storedData = [$cpuCores, count($files), count($jobs), $usedNumberOfProcesses]; |
| 93 | + [$maximumNumberOfProcesses, $decision] = $this->resolveMaximumNumberOfProcesses($cpuCores); |
| 94 | + $usedNumberOfProcesses = min($numberOfProcesses, $maximumNumberOfProcesses); |
| 95 | + $this->storedData = [$cpuCores, count($files), count($jobs), $usedNumberOfProcesses, $decision]; |
83 | 96 |
|
84 | 97 | return new Schedule($usedNumberOfProcesses, $jobs); |
85 | 98 | } |
86 | 99 |
|
| 100 | + /** |
| 101 | + * How many workers may run at once, and a human-readable account of why - which |
| 102 | + * `diagnose` prints, because a user who thinks the number is wrong needs to see |
| 103 | + * which input produced it. |
| 104 | + * |
| 105 | + * @return array{positive-int, string} |
| 106 | + */ |
| 107 | + private function resolveMaximumNumberOfProcesses(int $cpuCores): array |
| 108 | + { |
| 109 | + if ($this->maximumNumberOfProcesses !== self::AUTO) { |
| 110 | + return [$this->maximumNumberOfProcesses, 'configured']; |
| 111 | + } |
| 112 | + |
| 113 | + $availableMemory = $this->systemResources->getAvailableMemoryBytes(); |
| 114 | + if ($availableMemory === null) { |
| 115 | + return [ |
| 116 | + self::UNKNOWN_MEMORY_PROCESSES_LIMIT, |
| 117 | + sprintf('auto, available memory unknown so capped at %d', self::UNKNOWN_MEMORY_PROCESSES_LIMIT), |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + $affordableProcesses = $this->workerMemoryBudget->getAffordableWorkerCount($availableMemory); |
| 122 | + |
| 123 | + if ($affordableProcesses < $cpuCores) { |
| 124 | + return [ |
| 125 | + $affordableProcesses, |
| 126 | + sprintf( |
| 127 | + 'auto, %d MB available memory fits %d workers of %d MB', |
| 128 | + (int) ($availableMemory / 1024 / 1024), |
| 129 | + $affordableProcesses, |
| 130 | + WorkerMemoryBudget::EXPECTED_WORKER_MEMORY_LIMIT / 1024 / 1024, |
| 131 | + ), |
| 132 | + ]; |
| 133 | + } |
| 134 | + |
| 135 | + return [ |
| 136 | + max(1, $cpuCores), |
| 137 | + sprintf('auto, limited by %d usable CPU cores', $cpuCores), |
| 138 | + ]; |
| 139 | + } |
| 140 | + |
87 | 141 | public function print(Output $output): void |
88 | 142 | { |
89 | 143 | if ($this->storedData === null) { |
90 | 144 | return; |
91 | 145 | } |
92 | 146 |
|
93 | | - [$cpuCores, $filesCount, $jobsCount, $usedNumberOfProcesses] = $this->storedData; |
| 147 | + [$cpuCores, $filesCount, $jobsCount, $usedNumberOfProcesses, $decision] = $this->storedData; |
94 | 148 |
|
95 | 149 | $output->writeLineFormatted('<info>Parallel processing scheduler:</info>'); |
96 | 150 | $output->writeLineFormatted(sprintf('# of detected CPU cores: %d', $cpuCores)); |
97 | 151 | $output->writeLineFormatted(sprintf('# of analysed files: %d', $filesCount)); |
98 | 152 | $output->writeLineFormatted(sprintf('# of jobs: %d', $jobsCount)); |
99 | 153 | $output->writeLineFormatted(sprintf('# of spawned processes: %d', $usedNumberOfProcesses)); |
| 154 | + $output->writeLineFormatted(sprintf('Process limit: %s', $decision)); |
100 | 155 | $output->writeLineFormatted(''); |
101 | 156 | } |
102 | 157 |
|
|
0 commit comments