Skip to content

Commit

Permalink
add parameter sleep nano seconds as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sasezaki committed May 9, 2020
1 parent 7a9c740 commit 1703965
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/Command/Inspector/GetCurrentFunctionNameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@

final class GetCurrentFunctionNameCommand extends Command
{
private const SLEEP_NANO_SECONDS_DEFAULT = 1000 * 1000 * 10;

public function configure(): void
{
$this->setName('inspector:current_function')
->setDescription('periodically get running function name from an outer process or thread')
->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id');
->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id')
->addOption(
'sleep-ns',
's',
InputOption::VALUE_OPTIONAL,
'nanoseconds between traces (default: 1000 * 1000 * 10)'
);
}

/**
Expand All @@ -61,6 +69,17 @@ public function execute(InputInterface $input, OutputInterface $output): int
return 2;
}

$sleep_nano_seconds = $input->getOption('sleep-ns');
if (is_null($sleep_nano_seconds)) {
$sleep_nano_seconds = self::SLEEP_NANO_SECONDS_DEFAULT;
}
$sleep_nano_seconds = filter_var($sleep_nano_seconds, FILTER_VALIDATE_INT);
if ($sleep_nano_seconds === false) {
$error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
$error_output->writeln('sleep-ns is not integer');
return 2;
}

$memory_reader = new MemoryReader();
$php_globals_finder = new PhpGlobalsFinder(
(new PhpSymbolReaderCreator($memory_reader))->create($pid)
Expand All @@ -82,7 +101,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
try {
echo $eg_reader->readCurrentFunctionName($pid, $eg_address) , PHP_EOL;
$count_retry = 0;
time_nanosleep(0, 1000 * 1000 * 10);
time_nanosleep(0, $sleep_nano_seconds);
} catch (MemoryReaderException $e) {
$count_retry++;
}
Expand Down
23 changes: 21 additions & 2 deletions src/Command/Inspector/GetTraceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@

final class GetTraceCommand extends Command
{
private const SLEEP_NANO_SECONDS_DEFAULT = 1000 * 1000 * 10;

public function configure(): void
{
$this->setName('inspector:trace')
->setDescription('periodically get call trace from an outer process or thread')
->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id')
->addOption('depth', 'd', InputOption::VALUE_OPTIONAL, 'max depth');
->addOption('depth', 'd', InputOption::VALUE_OPTIONAL, 'max depth')
->addOption(
'sleep-ns',
's',
InputOption::VALUE_OPTIONAL,
'nanoseconds between traces (default: 1000 * 1000 * 10)'
);
}

/**
Expand Down Expand Up @@ -73,6 +81,17 @@ public function execute(InputInterface $input, OutputInterface $output): int
return 2;
}

$sleep_nano_seconds = $input->getOption('sleep-ns');
if (is_null($sleep_nano_seconds)) {
$sleep_nano_seconds = self::SLEEP_NANO_SECONDS_DEFAULT;
}
$sleep_nano_seconds = filter_var($sleep_nano_seconds, FILTER_VALIDATE_INT);
if ($sleep_nano_seconds === false) {
$error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
$error_output->writeln('sleep-ns is not integer');
return 2;
}

$memory_reader = new MemoryReader();
$php_globals_finder = new PhpGlobalsFinder(
(new PhpSymbolReaderCreator($memory_reader))->create($pid)
Expand All @@ -94,7 +113,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
try {
echo join(PHP_EOL, $eg_reader->readCallTrace($pid, $eg_address, $depth)) , PHP_EOL, PHP_EOL;
$count_retry = 0;
time_nanosleep(0, 1000 * 1000 * 10);
time_nanosleep(0, $sleep_nano_seconds);
} catch (MemoryReaderException $e) {
$count_retry++;
}
Expand Down

0 comments on commit 1703965

Please sign in to comment.