Skip to content

Commit

Permalink
Merge pull request #198 from reliforp/move-binary-analysis-to-searcher
Browse files Browse the repository at this point in the history
move binary analysis to searcher
  • Loading branch information
sj-i committed Apr 30, 2022
2 parents 6a35ea8 + 496af30 commit 8b7bff5
Show file tree
Hide file tree
Showing 36 changed files with 624 additions and 174 deletions.
8 changes: 6 additions & 2 deletions src/Command/Inspector/DaemonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ public function execute(InputInterface $input, OutputInterface $output): int

$searcher_context = $this->php_searcher_context_creator->create();
Promise\wait($searcher_context->start());
Promise\wait($searcher_context->sendTargetRegex($daemon_settings->target_regex));
Promise\wait(
$searcher_context->sendTarget(
$daemon_settings->target_regex,
$target_php_settings,
)
);

$worker_pool = WorkerPool::create(
$this->php_reader_context_creator,
$daemon_settings->threads,
$target_php_settings,
$loop_settings,
$get_trace_settings
);
Expand Down
8 changes: 6 additions & 2 deletions src/Command/Inspector/TopLikeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ public function execute(InputInterface $input, OutputInterface $output): int

$searcher_context = $this->php_searcher_context_creator->create();
Promise\wait($searcher_context->start());
Promise\wait($searcher_context->sendTargetRegex($daemon_settings->target_regex));
Promise\wait(
$searcher_context->sendTarget(
$daemon_settings->target_regex,
$target_php_settings,
)
);

$worker_pool = WorkerPool::create(
$this->php_reader_context_creator,
$daemon_settings->threads,
$target_php_settings,
$loop_settings,
$get_trace_settings
);
Expand Down
6 changes: 3 additions & 3 deletions src/Inspector/Daemon/Dispatcher/DispatchTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function updateTargets(TargetProcessListInterface $update): void
break;
}
$this->assigned->putOne($picked);
$this->dispatch_table[$picked] = $worker;
$this->dispatch_table[$picked->pid] = $worker;
$worker->sendAttach($picked);
}
}

public function release(TargetProcessListInterface $targets): void
{
foreach ($targets->getArray() as $pid) {
$this->releaseOne($pid);
$this->releaseOne($pid->pid);
}
}

Expand All @@ -60,6 +60,6 @@ public function releaseOne(int $pid): void
$this->worker_pool->returnWorkerToPool($worker);
unset($this->dispatch_table[$pid]);
}
$this->assigned = $this->assigned->getDiff(new TargetProcessList($pid));
$this->assigned->removeByPid($pid);
}
}
35 changes: 35 additions & 0 deletions src/Inspector/Daemon/Dispatcher/TargetProcessDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of the sj-i/ package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Inspector\Daemon\Dispatcher;

use PhpProfiler\Lib\PhpInternals\ZendTypeReader;

final class TargetProcessDescriptor
{
/** @param value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS> $php_version */
public function __construct(
public int $pid,
public int $eg_address,
public string $php_version,
) {
}

public static function getInvalid(): self
{
static $invalid = null;
/** @var self */
$invalid ??= new self(0, 0, ZendTypeReader::V80);
return $invalid;
}
}
47 changes: 32 additions & 15 deletions src/Inspector/Daemon/Dispatcher/TargetProcessList.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,60 @@

namespace PhpProfiler\Inspector\Daemon\Dispatcher;

use function array_diff;
use function array_rand;
use function array_udiff;

final class TargetProcessList implements TargetProcessListInterface
{
/** @var int[] */
private array $pid_list;
/** @var TargetProcessDescriptor[] */
private array $process_list;

public function __construct(int ...$pid_list)
public function __construct(TargetProcessDescriptor ...$process_list)
{
$this->pid_list = $pid_list;
$this->process_list = $process_list;
}

public function pickOne(): ?int
public function pickOne(): ?TargetProcessDescriptor
{
if ($this->pid_list === []) {
if ($this->process_list === []) {
return null;
}
$key = array_rand($this->pid_list);
$value = $this->pid_list[$key];
unset($this->pid_list[$key]);
$key = array_rand($this->process_list);
$value = $this->process_list[$key];
unset($this->process_list[$key]);
return $value;
}

public function putOne(int $pid): void
public function putOne(TargetProcessDescriptor $process_descriptor): void
{
$this->pid_list[] = $pid;
$this->process_list[] = $process_descriptor;
}

public function getDiff(TargetProcessListInterface $compare_list): self
{
return new self(...array_diff($this->pid_list, $compare_list->getArray()));
/** @var TargetProcessDescriptor[] $diff */
$diff = array_udiff(
$this->process_list,
$compare_list->getArray(),
fn (TargetProcessDescriptor $a, TargetProcessDescriptor $b) => $a <=> $b,
);
return new self(
...$diff
);
}

/** @return int[] */
/** @return TargetProcessDescriptor[] */
public function getArray(): array
{
return $this->pid_list;
return $this->process_list;
}

public function removeByPid(int $pid): void
{
foreach ($this->process_list as $key => $process_descriptor) {
if ($process_descriptor->pid === $pid) {
unset($this->process_list[$key]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

interface TargetProcessListInterface
{
public function pickOne(): ?int;
public function pickOne(): ?TargetProcessDescriptor;

public function putOne(int $pid): void;
public function putOne(TargetProcessDescriptor $process_descriptor): void;

public function getDiff(TargetProcessListInterface $compare_list): self;

/** @return int[] */
/** @return TargetProcessDescriptor[] */
public function getArray(): array;

public function removeByPid(int $pid): void;
}
3 changes: 0 additions & 3 deletions src/Inspector/Daemon/Dispatcher/WorkerPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreatorInterface;
use PhpProfiler\Inspector\Daemon\Reader\Controller\PhpReaderControllerInterface;
use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettings;
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;

use function array_fill;
Expand All @@ -42,7 +41,6 @@ public function __construct(PhpReaderControllerInterface ...$contexts)
public static function create(
PhpReaderContextCreatorInterface $creator,
int $number,
TargetPhpSettings $target_php_settings,
TraceLoopSettings $loop_settings,
GetTraceSettings $get_trace_settings
): self {
Expand All @@ -57,7 +55,6 @@ public static function create(
$send_settings = [];
for ($i = 0; $i < $number; $i++) {
$send_settings[] = $contexts[$i]->sendSettings(
$target_php_settings,
$loop_settings,
$get_trace_settings
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
namespace PhpProfiler\Inspector\Daemon\Reader\Controller;

use Amp\Promise;
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessDescriptor;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\DetachWorkerMessage;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\TraceMessage;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\AttachMessage;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\SetSettingsMessage;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\PhpReaderControllerProtocolInterface;
use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettings;
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;
use PhpProfiler\Lib\Amphp\ContextInterface;

Expand All @@ -44,24 +44,22 @@ public function isRunning(): bool

/** @return Promise<int> */
public function sendSettings(
TargetPhpSettings $target_php_settings,
TraceLoopSettings $loop_settings,
GetTraceSettings $get_trace_settings
): Promise {
return $this->context->getProtocol()->sendSettings(
new SetSettingsMessage(
$target_php_settings,
$loop_settings,
$get_trace_settings
)
);
}

/** @return Promise<int> */
public function sendAttach(int $pid): Promise
public function sendAttach(TargetProcessDescriptor $process_descriptor): Promise
{
return $this->context->getProtocol()->sendAttach(
new AttachMessage($pid)
new AttachMessage($process_descriptor)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
namespace PhpProfiler\Inspector\Daemon\Reader\Controller;

use Amp\Promise;
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessDescriptor;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\DetachWorkerMessage;
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\TraceMessage;
use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettings;
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;

interface PhpReaderControllerInterface
Expand All @@ -28,13 +28,12 @@ public function isRunning(): bool;

/** @return Promise<int> */
public function sendSettings(
TargetPhpSettings $target_php_settings,
TraceLoopSettings $loop_settings,
GetTraceSettings $get_trace_settings
): Promise;

/** @return Promise<int> */
public function sendAttach(int $pid): Promise;
public function sendAttach(TargetProcessDescriptor $process_descriptor): Promise;

/** @return Promise<TraceMessage|DetachWorkerMessage> */
public function receiveTraceOrDetachWorker(): Promise;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace PhpProfiler\Inspector\Daemon\Reader\Protocol\Message;

use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessDescriptor;

final class AttachMessage
{
public function __construct(
public int $pid
public TargetProcessDescriptor $process_descriptor,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
namespace PhpProfiler\Inspector\Daemon\Reader\Protocol\Message;

use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettings;
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;
use PhpProfiler\Lib\PhpInternals\ZendTypeReader;

final class SetSettingsMessage
{
/** @param TargetPhpSettings<value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS>|'auto'> $target_php_settings */
public function __construct(
public TargetPhpSettings $target_php_settings,
public TraceLoopSettings $trace_loop_settings,
public GetTraceSettings $get_trace_settings
) {
Expand Down
17 changes: 2 additions & 15 deletions src/Inspector/Daemon/Reader/Worker/PhpReaderEntryPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@
use PhpProfiler\Inspector\Daemon\Reader\Protocol\PhpReaderWorkerProtocolInterface;
use PhpProfiler\Lib\Amphp\WorkerEntryPointInterface;
use PhpProfiler\Lib\Log\Log;
use PhpProfiler\Lib\PhpProcessReader\PhpVersionDetector;
use PhpProfiler\Lib\Process\ProcessSpecifier;

final class PhpReaderEntryPoint implements WorkerEntryPointInterface
{
public function __construct(
private PhpReaderTraceLoopInterface $trace_loop,
private PhpReaderWorkerProtocolInterface $protocol,
private PhpVersionDetector $php_version_detector,
) {
}

Expand All @@ -48,20 +45,10 @@ public function run(): \Generator
$attach_message = yield $this->protocol->receiveAttach();
Log::debug('attach_message', [$attach_message]);

$process_specifier = new ProcessSpecifier(
$attach_message->pid
);

$target_php_settings = $this->php_version_detector->decidePhpVersion(
$process_specifier,
$set_settings_message->target_php_settings
);

try {
$loop_runner = $this->trace_loop->run(
$process_specifier,
$set_settings_message->trace_loop_settings,
$target_php_settings,
$attach_message->process_descriptor,
$set_settings_message->get_trace_settings
);
Log::debug('start trace');
Expand All @@ -78,7 +65,7 @@ public function run(): \Generator

Log::debug('detaching worker');
yield $this->protocol->sendDetachWorker(
new DetachWorkerMessage($attach_message->pid)
new DetachWorkerMessage($attach_message->process_descriptor->pid)
);
Log::debug('detached worker');
}
Expand Down
Loading

0 comments on commit 8b7bff5

Please sign in to comment.