|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sj-i/ 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 Reli\Command\Inspector; |
|
15
|
|
|
|
|
16
|
|
|
use Amp\Loop; |
|
|
|
|
|
|
17
|
|
|
use Amp\Promise; |
|
|
|
|
|
|
18
|
|
|
use Reli\Inspector\Daemon\Dispatcher\DispatchTable; |
|
19
|
|
|
use Reli\Inspector\Daemon\Dispatcher\WorkerPool; |
|
20
|
|
|
use Reli\Inspector\Daemon\Reader\Context\PhpReaderContextCreator; |
|
21
|
|
|
use Reli\Inspector\Daemon\Reader\Protocol\Message\TraceMessage; |
|
22
|
|
|
use Reli\Inspector\Daemon\Searcher\Context\PhpSearcherContextCreator; |
|
23
|
|
|
use Reli\Inspector\Output\TopLike\TopLikeFormatter; |
|
24
|
|
|
use Reli\Inspector\Output\TopLike\TopLikeFormatterFactory; |
|
25
|
|
|
use Reli\Inspector\Settings\DaemonSettings\DaemonSettingsFromConsoleInput; |
|
26
|
|
|
use Reli\Inspector\Settings\GetTraceSettings\GetTraceSettingsFromConsoleInput; |
|
27
|
|
|
use Reli\Inspector\Settings\TargetPhpSettings\TargetPhpSettingsFromConsoleInput; |
|
28
|
|
|
use Reli\Inspector\Settings\TraceLoopSettings\TraceLoopSettingsFromConsoleInput; |
|
29
|
|
|
use Reli\Lib\Console\EchoBackCanceller; |
|
30
|
|
|
use Reli\Lib\Log\Log; |
|
31
|
|
|
use Reli\Lib\PhpProcessReader\CallTrace; |
|
32
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
|
|
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
|
|
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
use function Amp\call; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
final class TopLikeCommand extends Command |
|
39
|
|
|
{ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
private PhpSearcherContextCreator $php_searcher_context_creator, |
|
42
|
|
|
private PhpReaderContextCreator $php_reader_context_creator, |
|
43
|
|
|
private DaemonSettingsFromConsoleInput $daemon_settings_from_console_input, |
|
44
|
|
|
private GetTraceSettingsFromConsoleInput $get_trace_settings_from_console_input, |
|
45
|
|
|
private TargetPhpSettingsFromConsoleInput $target_php_settings_from_console_input, |
|
46
|
|
|
private TraceLoopSettingsFromConsoleInput $trace_loop_settings_from_console_input, |
|
47
|
|
|
private TopLikeFormatterFactory $formatter_factory, |
|
48
|
|
|
) { |
|
49
|
|
|
parent::__construct(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function configure(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->setName('inspector:top') |
|
55
|
|
|
->setDescription( |
|
56
|
|
|
'show an aggregated view of traces in real time in a form similar to the UNIX top command.' |
|
57
|
|
|
) |
|
58
|
|
|
; |
|
59
|
|
|
$this->daemon_settings_from_console_input->setOptions($this); |
|
60
|
|
|
$this->get_trace_settings_from_console_input->setOptions($this); |
|
61
|
|
|
$this->trace_loop_settings_from_console_input->setOptions($this); |
|
62
|
|
|
$this->target_php_settings_from_console_input->setOptions($this); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
|
66
|
|
|
{ |
|
67
|
|
|
$get_trace_settings = $this->get_trace_settings_from_console_input->createSettings($input); |
|
68
|
|
|
$daemon_settings = $this->daemon_settings_from_console_input->createSettings($input); |
|
69
|
|
|
$target_php_settings = $this->target_php_settings_from_console_input->createSettings($input); |
|
70
|
|
|
$loop_settings = $this->trace_loop_settings_from_console_input->createSettings($input); |
|
71
|
|
|
$formatter = $this->formatter_factory->create( |
|
72
|
|
|
$daemon_settings->target_regex, |
|
73
|
|
|
$output |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$searcher_context = $this->php_searcher_context_creator->create(); |
|
77
|
|
|
Promise\wait($searcher_context->start()); |
|
|
|
|
|
|
78
|
|
|
Promise\wait( |
|
79
|
|
|
$searcher_context->sendTarget( |
|
80
|
|
|
$daemon_settings->target_regex, |
|
81
|
|
|
$target_php_settings, |
|
82
|
|
|
) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$worker_pool = WorkerPool::create( |
|
86
|
|
|
$this->php_reader_context_creator, |
|
87
|
|
|
$daemon_settings->threads, |
|
88
|
|
|
$loop_settings, |
|
89
|
|
|
$get_trace_settings |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$dispatch_table = new DispatchTable( |
|
93
|
|
|
$worker_pool, |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$_echo_back_canceler = new EchoBackCanceller(); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
Loop::onReadable( |
|
99
|
|
|
STDIN, |
|
100
|
|
|
/** @param resource $stream */ |
|
101
|
|
|
function (string $watcher_id, $stream) { |
|
102
|
|
|
$key = fread($stream, 1); |
|
103
|
|
|
if ($key === 'q') { |
|
104
|
|
|
Loop::cancel($watcher_id); |
|
105
|
|
|
Loop::stop(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
); |
|
109
|
|
|
Loop::run(function () use ($dispatch_table, $searcher_context, $worker_pool, $formatter) { |
|
110
|
|
|
$promises = []; |
|
111
|
|
|
$promises[] = call(function () use ($searcher_context, $dispatch_table) { |
|
|
|
|
|
|
112
|
|
|
while (1) { |
|
113
|
|
|
Log::debug('receiving pid List'); |
|
114
|
|
|
$update_target_message = yield $searcher_context->receivePidList(); |
|
115
|
|
|
Log::debug('update targets', [ |
|
116
|
|
|
'update' => $update_target_message->target_process_list->getArray(), |
|
117
|
|
|
'current' => $dispatch_table->worker_pool->debugDump(), |
|
118
|
|
|
]); |
|
119
|
|
|
$dispatch_table->updateTargets($update_target_message->target_process_list); |
|
120
|
|
|
Log::debug('target updated', [$dispatch_table->worker_pool->debugDump()]); |
|
121
|
|
|
} |
|
122
|
|
|
}); |
|
123
|
|
|
foreach ($worker_pool->getWorkers() as $reader) { |
|
124
|
|
|
$promises[] = call( |
|
125
|
|
|
function () use ($reader, $dispatch_table, $formatter) { |
|
126
|
|
|
while (1) { |
|
127
|
|
|
$result = yield $reader->receiveTraceOrDetachWorker(); |
|
128
|
|
|
if ($result instanceof TraceMessage) { |
|
129
|
|
|
$this->outputTrace($formatter, $result); |
|
130
|
|
|
} else { |
|
131
|
|
|
Log::debug('releaseOne', [$result]); |
|
132
|
|
|
$dispatch_table->releaseOne($result->pid); |
|
133
|
|
|
$this->outputTrace($formatter, new TraceMessage( |
|
134
|
|
|
new CallTrace() |
|
135
|
|
|
)); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
yield $promises; |
|
142
|
|
|
}); |
|
143
|
|
|
|
|
144
|
|
|
return 0; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function outputTrace( |
|
148
|
|
|
TopLikeFormatter $formatter, |
|
149
|
|
|
TraceMessage $message |
|
150
|
|
|
): void { |
|
151
|
|
|
$formatter->format($message->trace); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths