|
19 | 19 | final class Scheduler implements DiagnoseExtension |
20 | 20 | { |
21 | 21 |
|
22 | | - /** @var array{int, int, int, int}|null */ |
| 22 | + public const AUTO = 'auto'; |
| 23 | + |
| 24 | + /** @var array{int, int, int, int, string}|null */ |
23 | 25 | private ?array $storedData = null; |
24 | 26 |
|
25 | 27 | /** |
26 | 28 | * @param positive-int $jobSize |
27 | | - * @param positive-int $maximumNumberOfProcesses |
| 29 | + * @param positive-int|self::AUTO $maximumNumberOfProcesses |
28 | 30 | * @param positive-int $minimumNumberOfJobsPerProcess |
29 | 31 | */ |
30 | 32 | public function __construct( |
31 | 33 | #[AutowiredParameter(ref: '%parallel.jobSize%')] |
32 | 34 | private int $jobSize, |
33 | 35 | #[AutowiredParameter(ref: '%parallel.maximumNumberOfProcesses%')] |
34 | | - private int $maximumNumberOfProcesses, |
| 36 | + private int|string $maximumNumberOfProcesses, |
35 | 37 | #[AutowiredParameter(ref: '%parallel.minimumNumberOfJobsPerProcess%')] |
36 | 38 | private int $minimumNumberOfJobsPerProcess, |
37 | 39 | ) |
@@ -78,25 +80,46 @@ public function scheduleWork( |
78 | 80 | $cpuCores, |
79 | 81 | ); |
80 | 82 |
|
81 | | - $usedNumberOfProcesses = min($numberOfProcesses, $this->maximumNumberOfProcesses); |
82 | | - $this->storedData = [$cpuCores, count($files), count($jobs), $usedNumberOfProcesses]; |
| 83 | + [$maximumNumberOfProcesses, $decision] = $this->resolveMaximumNumberOfProcesses($cpuCores); |
| 84 | + $usedNumberOfProcesses = min($numberOfProcesses, $maximumNumberOfProcesses); |
| 85 | + $this->storedData = [$cpuCores, count($files), count($jobs), $usedNumberOfProcesses, $decision]; |
83 | 86 |
|
84 | 87 | return new Schedule($usedNumberOfProcesses, $jobs); |
85 | 88 | } |
86 | 89 |
|
| 90 | + /** |
| 91 | + * How many workers may run at once, and a human-readable account of why - which |
| 92 | + * `diagnose` prints, because a user who thinks the number is wrong needs to see |
| 93 | + * which input produced it. |
| 94 | + * |
| 95 | + * @return array{positive-int, string} |
| 96 | + */ |
| 97 | + private function resolveMaximumNumberOfProcesses(int $cpuCores): array |
| 98 | + { |
| 99 | + if ($this->maximumNumberOfProcesses !== self::AUTO) { |
| 100 | + return [$this->maximumNumberOfProcesses, 'configured']; |
| 101 | + } |
| 102 | + |
| 103 | + return [ |
| 104 | + max(1, $cpuCores), |
| 105 | + sprintf('auto, limited by %d usable CPU cores', $cpuCores), |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
87 | 109 | public function print(Output $output): void |
88 | 110 | { |
89 | 111 | if ($this->storedData === null) { |
90 | 112 | return; |
91 | 113 | } |
92 | 114 |
|
93 | | - [$cpuCores, $filesCount, $jobsCount, $usedNumberOfProcesses] = $this->storedData; |
| 115 | + [$cpuCores, $filesCount, $jobsCount, $usedNumberOfProcesses, $decision] = $this->storedData; |
94 | 116 |
|
95 | 117 | $output->writeLineFormatted('<info>Parallel processing scheduler:</info>'); |
96 | 118 | $output->writeLineFormatted(sprintf('# of detected CPU cores: %d', $cpuCores)); |
97 | 119 | $output->writeLineFormatted(sprintf('# of analysed files: %d', $filesCount)); |
98 | 120 | $output->writeLineFormatted(sprintf('# of jobs: %d', $jobsCount)); |
99 | 121 | $output->writeLineFormatted(sprintf('# of spawned processes: %d', $usedNumberOfProcesses)); |
| 122 | + $output->writeLineFormatted(sprintf('Process limit: %s', $decision)); |
100 | 123 | $output->writeLineFormatted(''); |
101 | 124 | } |
102 | 125 |
|
|
0 commit comments