Skip to content

Commit

Permalink
Merge pull request #163 from sj-i/0.4.x
Browse files Browse the repository at this point in the history
merge-up 0.4.x to 0.5.x
  • Loading branch information
sj-i committed Feb 13, 2022
2 parents 1b82a3e + 41ab283 commit 74627c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
47 changes: 35 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@

php-profiler is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the process. It's a stand alone CLI tool, so target programs don't need any modifications.

# What can I use this for?
- Detecting and visualizing bottlenecks in PHP scripts
- It provides not only at the function level of profiling but also at line level or opcode level resolution
- Profiling without accumulated overhead even when a lot of fast functions called as this is a sampling profiler (see the links below, tideways, xhprof, and the profiler of xdebug, many profilers have this overhead)
- [Profiling Overhead and PHP 7](https://tideways.com/profiler/blog/profiling-overhead-and-php-7)
- [nikic/sample_prof](https://github.com/nikic/sample_prof)
- Investigating the cause of a bug or performance failure
- Even if a PHP script is in an unexplained unresponsive state, you can use this to find out what it is doing internally.

# How it works
It's implemented by using following techniques:

- parsing ELF binary of the interpreter
- reading memory map from /proc/\<pid\>/maps
- reading memory of outer process by using ptrace(2) and process_vm_readv(2) via FFI
- analyzing internal data structure in the PHP VM (aka Zend Engine)
- Parsing ELF binary of the interpreter
- Reading memory map from /proc/\<pid\>/maps
- Reading memory of outer process by using ptrace(2) and process_vm_readv(2) via FFI
- Analyzing internal data structure in the PHP VM (aka Zend Engine)

If you have a bit of extra CPU resource, the overhead of this software would be negligible.

Expand All @@ -33,7 +43,7 @@ Other features of php-profiler that phpspy does not currently have include:
- Output more accurate line numbers
- Customize output format with PHP templates
- Get running opcodes of the PHP-VM
- Automatic retrieval of the version of stripped PHP binaries
- Automatic retrieval of the target PHP version from stripped PHP binaries
- Output traces in speedscope format

There is no particular reason why these features cannot be implemented on the phpspy side, so it may be possible to do them on phpspy in the future.
Expand Down Expand Up @@ -102,7 +112,7 @@ Options:
-S, --stop-process[=STOP-PROCESS] stop the target process while reading its trace (default: off)
--php-regex[=PHP-REGEX] regex to find the php binary loaded in the target process
--libpthread-regex[=LIBPTHREAD-REGEX] regex to find the libpthread.so loaded in the target process
--php-version[=PHP-VERSION] php version of the target (default: auto)
--php-version[=PHP-VERSION] php version (auto|v7[0-4]|v8[01]) of the target (default: auto)
--php-path[=PHP-PATH] path to the php binary (only needed in tracing chrooted ZTS target)
--libpthread-path[=LIBPTHREAD-PATH] path to the libpthread.so (only needed in tracing chrooted ZTS target)
-t, --template[=TEMPLATE] template name (phpspy|phpspy_with_opcode|json_lines) (default: phpspy)
Expand Down Expand Up @@ -132,7 +142,7 @@ Options:
-S, --stop-process[=STOP-PROCESS] stop the target process while reading its trace (default: off)
--php-regex[=PHP-REGEX] regex to find the php binary loaded in the target process
--libpthread-regex[=LIBPTHREAD-REGEX] regex to find the libpthread.so loaded in the target process
--php-version[=PHP-VERSION] php version of the target (default: auto)
--php-version[=PHP-VERSION] php version (auto|v7[0-4]|v8[01]) of the target (default: auto)
--php-path[=PHP-PATH] path to the php binary (only needed in tracing chrooted ZTS target)
--libpthread-path[=LIBPTHREAD-PATH] path to the libpthread.so (only needed in tracing chrooted ZTS target)
-t, --template[=TEMPLATE] template name (phpspy|phpspy_with_opcode|json_lines) (default: phpspy)
Expand Down Expand Up @@ -162,7 +172,7 @@ Options:
-S, --stop-process[=STOP-PROCESS] stop the target process while reading its trace (default: off)
--php-regex[=PHP-REGEX] regex to find the php binary loaded in the target process
--libpthread-regex[=LIBPTHREAD-REGEX] regex to find the libpthread.so loaded in the target process
--php-version[=PHP-VERSION] php version of the target (default: auto)
--php-version[=PHP-VERSION] php version (auto|v7[0-4]|v8[01]) of the target (default: auto)
--php-path[=PHP-PATH] path to the php binary (only needed in tracing chrooted ZTS target)
--libpthread-path[=LIBPTHREAD-PATH] path to the libpthread.so (only needed in tracing chrooted ZTS target)
-h, --help Display help for the given command. When no command is given display help for the list command
Expand Down Expand Up @@ -190,7 +200,7 @@ Options:
-p, --pid=PID process id
--php-regex[=PHP-REGEX] regex to find the php binary loaded in the target process
--libpthread-regex[=LIBPTHREAD-REGEX] regex to find the libpthread.so loaded in the target process
--php-version[=PHP-VERSION] php version of the target (default: auto)
--php-version[=PHP-VERSION] php version (auto|v7[0-4]|v8[01]) of the target (default: auto)
--php-path[=PHP-PATH] path to the php binary (only needed in tracing chrooted ZTS target)
--libpthread-path[=LIBPTHREAD-PATH] path to the libpthread.so (only needed in tracing chrooted ZTS target)
-h, --help Display help for the given command. When no command is given display help for the list command
Expand Down Expand Up @@ -220,7 +230,7 @@ $ ./php-profiler i:trace -- php -r "fgets(STDIN);"
### Attach to a running process
```bash
$ sudo ~/.phpenv/versions/8.1snapshot/bin/php ./php-profiler i:tr -p 2182685
$ sudo php ./php-profiler i:trace -p 2182685
0 time_nanosleep <internal>:-1
1 PhpProfiler\Lib\Loop\LoopMiddleware\NanoSleepMiddleware::invoke /home/sji/work/php-profiler/src/Lib/Loop/LoopMiddleware/NanoSleepMiddleware.php:33
2 PhpProfiler\Lib\Loop\LoopMiddleware\KeyboardCancelMiddleware::invoke /home/sji/work/php-profiler/src/Lib/Loop/LoopMiddleware/KeyboardCancelMiddleware.php:39
Expand Down Expand Up @@ -300,7 +310,7 @@ $ docker run -it --security-opt="apparmor=unconfined" --cap-add=SYS_PTRACE --pid
### Generate flamegraphs from traces
```bash
$ sudo php ./php-profiler i:trace -p <pid of the target process or thread> >traces
./php-profiler c:flamegraph <traces >flame.svg
$ ./php-profiler c:flamegraph <traces >flame.svg
```
### Generate the [speedscope](https://github.com/jlfwong/speedscope) format from phpspy compatible traces
Expand All @@ -312,14 +322,27 @@ $ speedscope profile.speedscope.json
See [#101](https://github.com/sj-i/php-profiler/pull/101).
# Troubleshooting
## I get an error message "php module not found" and can't get a trace!
If your PHP binary uses a non-standard binary name that does not end with `/php`, use the `--php-regex` option to specify the name of the executable (or shared object) that contains the PHP interpreter.
## I don't think the trace is accurate.
The `-S` option will give you better results. Using this option stops the execution of the target process for a moment at every sampling, but the trace obtained will be more accurate. If you don't stop the VMs from running when profiling CPU-heavy programs such as benchmarking programs, you may misjudge the bottleneck, because you will miss more VM states that transition very quickly and are not detected well.
## Trace retrieval from ZTS target does not work on Ubuntu 21.10 or later.
Try to specify `--libpthread-regex="libc.so"` as an option.
## I can't get traces on Amazon Linux 2.
First, try `cat /proc/<pid>/maps` to check the memory map of the target PHP process. If the first module does not indicate the location of the PHP binary and looks like an anonymous region, try to specify `--php-regex="^$"` as an option.
# Goals
I would like to achieve the following 5 goals through this project.
- To be able to closely observe what is happening inside a running PHP script.
- To be a framework for PHP programmers to create a freely customizable PHP profiler.
- To be experimentation for the use of PHP outside of the web, where recent improvements of PHP like JIT and FFI have opened the door.
- Another entry point for PHP programmers to learn about PHP's internal implementation.
- To create programs that are fun to write for me.
- To create a program that is fun to write for me.
# LICENSE
- MIT (mostly)
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
'paths' => [
'templates' => __DIR__ . '/../resources/templates',
'tools' => __DIR__ . '/../tools',
],
'output' => [
'template' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function setOptions(Command $command): void
'php-version',
null,
InputOption::VALUE_OPTIONAL,
'php version of the target (default: v80)'
'php version (auto|v7[0-4]|v8[01]) of the target (default: auto)'
)
->addOption(
'php-path',
Expand Down
2 changes: 1 addition & 1 deletion src/PhpProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
final class PhpProfiler
{
public const TOOL_NAME = 'php-profiler';
public const VERSION = '0.3.6';
public const VERSION = '0.4.1';
}

0 comments on commit 74627c7

Please sign in to comment.