Passed
Pull Request — master (#8)
by Shinji
08:28
created

GetCurrentFunctionNameCommand::runPeriodically()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 17
rs 9.8333
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\Command\Inspector;
15
16
use PhpProfiler\Lib\Elf\Parser\ElfParserException;
17
use PhpProfiler\Lib\Elf\Process\ProcessSymbolReaderException;
18
use PhpProfiler\Lib\Elf\Tls\TlsFinderException;
19
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
20
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
21
use PhpProfiler\Lib\PhpProcessReader\PhpMemoryReader\ExecutorGlobalsReader;
22
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Symfony\Component\Console\Output\ConsoleOutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...\ConsoleOutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
final class GetCurrentFunctionNameCommand extends Command
29
{
30
    private const SLEEP_NANO_SECONDS_DEFAULT = 1000 * 1000 * 10;
31
32
    private PhpGlobalsFinder $php_globals_finder;
33
    private ExecutorGlobalsReader $executor_globals_reader;
34
    private TraceLoopProvider $loop_provider;
35
36
    /**
37
     * GetCurrentFunctionNameCommand constructor.
38
     *
39
     * @param PhpGlobalsFinder $php_globals_finder
40
     * @param ExecutorGlobalsReader $executor_globals_reader
41
     * @param TraceLoopProvider $loop_provider
42
     * @param string|null $name
43
     */
44
    public function __construct(
45
        PhpGlobalsFinder $php_globals_finder,
46
        ExecutorGlobalsReader $executor_globals_reader,
47
        TraceLoopProvider $loop_provider,
48
        string $name = null
49
    ) {
50
        parent::__construct($name);
51
        $this->php_globals_finder = $php_globals_finder;
52
        $this->executor_globals_reader = $executor_globals_reader;
53
        $this->loop_provider = $loop_provider;
54
    }
55
56
    public function configure(): void
57
    {
58
        $this->setName('inspector:current_function')
59
            ->setDescription('periodically get running function name from an outer process or thread')
60
            ->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id')
61
            ->addOption(
62
                'sleep-ns',
63
                's',
64
                InputOption::VALUE_OPTIONAL,
65
                'nanoseconds between traces (default: 1000 * 1000 * 10)'
66
            );
67
    }
68
69
    /**
70
     * @param InputInterface $input
71
     * @param OutputInterface $output
72
     * @return int
73
     * @throws MemoryReaderException
74
     * @throws ProcessSymbolReaderException
75
     * @throws ElfParserException
76
     * @throws TlsFinderException
77
     */
78
    public function execute(InputInterface $input, OutputInterface $output): int
79
    {
80
        $pid = $input->getOption('pid');
81
        if (is_null($pid)) {
82
            $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
83
            $error_output->writeln('pid is not specified');
84
            return 1;
85
        }
86
        $pid = filter_var($pid, FILTER_VALIDATE_INT);
87
        if ($pid === false) {
88
            $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
89
            $error_output->writeln('pid is not integer');
90
            return 2;
91
        }
92
93
        $sleep_nano_seconds = $input->getOption('sleep-ns');
94
        if (is_null($sleep_nano_seconds)) {
95
            $sleep_nano_seconds = self::SLEEP_NANO_SECONDS_DEFAULT;
96
        }
97
        $sleep_nano_seconds = filter_var($sleep_nano_seconds, FILTER_VALIDATE_INT);
98
        if ($sleep_nano_seconds === false) {
99
            $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
100
            $error_output->writeln('sleep-ns is not integer');
101
            return 2;
102
        }
103
104
        $eg_address = $this->php_globals_finder->findExecutorGlobals($pid);
105
106
        $this->loop_provider->getMainLoop(
107
            function () use ($pid, $eg_address, $output): bool {
108
                $output->writeln(
109
                    $this->executor_globals_reader->readCurrentFunctionName($pid, $eg_address)
110
                );
111
                return true;
112
            },
113
            $sleep_nano_seconds
114
        )->invoke();
115
116
        return 0;
117
    }
118
}
119