forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.php
More file actions
103 lines (88 loc) · 3.19 KB
/
Copy pathScheduler.php
File metadata and controls
103 lines (88 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php declare(strict_types = 1);
namespace PHPStan\Parallel;
use PHPStan\Command\Output;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Diagnose\DiagnoseExtension;
use function array_values;
use function ceil;
use function count;
use function floor;
use function max;
use function min;
use function sprintf;
use function usort;
#[AutowiredService]
final class Scheduler implements DiagnoseExtension
{
/** @var array{int, int, int, int}|null */
private ?array $storedData = null;
/**
* @param positive-int $jobSize
* @param positive-int $maximumNumberOfProcesses
* @param positive-int $minimumNumberOfJobsPerProcess
*/
public function __construct(
#[AutowiredParameter(ref: '%parallel.jobSize%')]
private int $jobSize,
#[AutowiredParameter(ref: '%parallel.maximumNumberOfProcesses%')]
private int $maximumNumberOfProcesses,
#[AutowiredParameter(ref: '%parallel.minimumNumberOfJobsPerProcess%')]
private int $minimumNumberOfJobsPerProcess,
)
{
}
/**
* @param array<string> $files
* @param callable(string): int $fileSizeCallback
*/
public function scheduleWork(
int $cpuCores,
array $files,
callable $fileSizeCallback,
): Schedule
{
// sort by size and deal files round-robin across jobs so every job mixes
// large and small files - chunking a sorted list would concentrate the
// heaviest files into a single job and create one long-running straggler
$fileSizes = [];
$originalOrder = [];
foreach ($files as $i => $file) {
$fileSizes[$file] = $fileSizeCallback($file);
$originalOrder[$file] = $i;
}
usort($files, static fn (string $a, string $b): int => $fileSizes[$b] <=> $fileSizes[$a]);
$numberOfJobs = (int) ceil(count($files) / $this->jobSize);
$stripedJobs = [];
foreach ($files as $i => $file) {
$stripedJobs[$i % $numberOfJobs][] = $file;
}
// only the job composition should change, not the order in which files
// of a job get analysed - analysis results can be sensitive to it
foreach ($stripedJobs as &$stripedJob) {
usort($stripedJob, static fn (string $a, string $b): int => $originalOrder[$a] <=> $originalOrder[$b]);
}
unset($stripedJob);
$jobs = array_values($stripedJobs);
$numberOfProcesses = min(
max((int) floor(count($jobs) / $this->minimumNumberOfJobsPerProcess), 1),
$cpuCores,
);
$usedNumberOfProcesses = min($numberOfProcesses, $this->maximumNumberOfProcesses);
$this->storedData = [$cpuCores, count($files), count($jobs), $usedNumberOfProcesses];
return new Schedule($usedNumberOfProcesses, $jobs);
}
public function print(Output $output): void
{
if ($this->storedData === null) {
return;
}
[$cpuCores, $filesCount, $jobsCount, $usedNumberOfProcesses] = $this->storedData;
$output->writeLineFormatted('<info>Parallel processing scheduler:</info>');
$output->writeLineFormatted(sprintf('# of detected CPU cores: %d', $cpuCores));
$output->writeLineFormatted(sprintf('# of analysed files: %d', $filesCount));
$output->writeLineFormatted(sprintf('# of jobs: %d', $jobsCount));
$output->writeLineFormatted(sprintf('# of spawned processes: %d', $usedNumberOfProcesses));
$output->writeLineFormatted('');
}
}