|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sj-i/php-profiler package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace PhpProfiler\Inspector\Daemon\Dispatcher; |
|
15
|
|
|
|
|
16
|
|
|
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContext; |
|
17
|
|
|
use PhpProfiler\Inspector\Settings\GetTraceSettings; |
|
18
|
|
|
use PhpProfiler\Inspector\Settings\TargetPhpSettings; |
|
19
|
|
|
use PhpProfiler\Inspector\Settings\TraceLoopSettings; |
|
20
|
|
|
|
|
21
|
|
|
final class DispatchTable |
|
22
|
|
|
{ |
|
23
|
|
|
private WorkerPool $worker_pool; |
|
24
|
|
|
private TargetProcessList $assigned; |
|
25
|
|
|
private TargetPhpSettings $target_php_settings; |
|
26
|
|
|
private TraceLoopSettings $trace_loop_settings; |
|
27
|
|
|
private GetTraceSettings $get_trace_settings; |
|
28
|
|
|
/** @var array<int, PhpReaderContext> */ |
|
29
|
|
|
private array $dispatch_table = []; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
WorkerPool $worker_pool, |
|
33
|
|
|
TargetPhpSettings $target_php_settings, |
|
34
|
|
|
TraceLoopSettings $trace_loop_settings, |
|
35
|
|
|
GetTraceSettings $get_trace_settings |
|
36
|
|
|
) { |
|
37
|
|
|
$this->worker_pool = $worker_pool; |
|
38
|
|
|
$this->target_php_settings = $target_php_settings; |
|
39
|
|
|
$this->trace_loop_settings = $trace_loop_settings; |
|
40
|
|
|
$this->get_trace_settings = $get_trace_settings; |
|
41
|
|
|
$this->assigned = new TargetProcessList(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function updateTargets(TargetProcessList $update): void |
|
45
|
|
|
{ |
|
46
|
|
|
$diff = $this->assigned->getDiff($update); |
|
47
|
|
|
$this->release($diff); |
|
48
|
|
|
$this->assigned = $this->assigned->getDiff($diff); |
|
49
|
|
|
$unassigned_new = $update->getDiff($this->assigned); |
|
50
|
|
|
for ($worker = $this->worker_pool->getFreeWorker(); $worker; $worker = $this->worker_pool->getFreeWorker()) { |
|
51
|
|
|
$picked = $unassigned_new->pickOne(); |
|
52
|
|
|
if (is_null($picked)) { |
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
$this->assigned->putOne($picked); |
|
56
|
|
|
$this->dispatch_table[$picked] = $worker; |
|
57
|
|
|
$worker->sendAttach($picked); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function release(TargetProcessList $targets): void |
|
62
|
|
|
{ |
|
63
|
|
|
foreach ($targets->getArray() as $pid) { |
|
64
|
|
|
$this->releaseOne($pid); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function releaseOne(int $pid): void |
|
69
|
|
|
{ |
|
70
|
|
|
$worker = $this->dispatch_table[$pid]; |
|
71
|
|
|
$this->worker_pool->returnWorkerToPool($worker); |
|
72
|
|
|
unset($this->dispatch_table[$pid]); |
|
73
|
|
|
$this->assigned = $this->assigned->getDiff(new TargetProcessList($pid)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|