Reli 是一个用PHP编写的采样分析器(或VM状态检查器)。它可以从进程外部读取有关运行PHP脚本的信息。它是一个独立的CLI工具,因此目标程序不需要任何修改。
git clone git@github.com:reliforp/reli-prof.gitcd reli-prof/usr/local/php-8.2.14/bin/php /home/www/build/composer.phar install/usr/local/php-8.2.14/bin/php ./reli
1、获取跟踪配置
www@ShaoBoWan:~/build/reli-prof$ /usr/local/php-8.2.14/bin/php ./reli inspector:trace --helpDescription: periodically get call trace from an outer process or threadUsage: inspector:trace [options] [--] [<cmd> [<args>...]]Arguments: cmd command to execute as a target: either pid (via -p/--pid) or cmd must be specified args command line arguments for cmdOptions: -p, --pid=PID process id -d, --depth[=DEPTH] max depth -s, --sleep-ns[=SLEEP-NS] nanoseconds between traces (default: 1000 * 1000 * 10) -r, --max-retries[=MAX-RETRIES] max retries on contiguous errors of read (default: 10) -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 (auto|v7[0-4]|v8[0123]) 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) -o, --output=OUTPUT path to write output from this tool (default: stdout) -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
2、守护模式启动
www@ShaoBoWan:~/build/reli-prof$ /usr/local/php-8.2.14/bin/php ./reli inspector:daemon --helpDescription: concurrently get call traces from processes whose command-lines match a given regexUsage: inspector:daemon [options]Options: -P, --target-regex=TARGET-REGEX regex to find target processes which have matching command-line (required) -T, --threads[=THREADS] number of workers (default: 8) -d, --depth[=DEPTH] max depth -s, --sleep-ns[=SLEEP-NS] nanoseconds between traces (default: 1000 * 1000 * 10) -r, --max-retries[=MAX-RETRIES] max retries on contiguous errors of read (default: 10) -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 (auto|v7[0-4]|v8[0123]) 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) -o, --output=OUTPUT path to write output from this tool (default: stdout) -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
这里跟踪一个webman框架进程
www@ShaoBoWan:~/build/webman01$ php start.php statusWorkerman[start.php] status ----------------------------------------------GLOBAL STATUS----------------------------------------------------Workerman version:4.0.37 PHP version:8.0.7start time:2024-01-01 00:19:33 run 0 days 0 hours load average: 0.07, 0.12, 0.98 event-loop:/Workerman/Events/Event2 workers 3 processesworker_name exit_status exit_countwebman 0 0monitor 0 0----------------------------------------------PROCESS STATUS---------------------------------------------------pid memory listening worker_name connections send_fail timers total_request qps status2184553 0.65M http://0.0.0.0:8787 webman 0 0 0 0 0 [idle]2184554 0.65M http://0.0.0.0:8787 webman 0 0 0 0 0 [idle]2184555 0.57M none monitor 0 0 1 0 0 [idle]----------------------------------------------PROCESS STATUS---------------------------------------------------Summary 0M - - 0 0 1 0 0 [Summary]
1、跟踪脚本
/usr/local/php-8.2.14/bin/php ./reli i:trace -- php -r "fgets(STDIN);"0 fgets <internal>:-11 <main> Command line code:10 fgets <internal>:-11 <main> Command line code:1...
2、附加到正在运行的进程
sudo /usr/local/php-8.2.14/bin/php ./reli i:trace -p 2184553[sudo] password for www: 0 EventBase::loop <internal>:-11 Workerman/Events/Event::loop /home/www/build/webman01/vendor/workerman/workerman/Events/Event.php:1932 Workerman/Worker::run /home/www/build/webman01/vendor/workerman/workerman/Worker.php:24353 Workerman/Worker::forkOneWorkerForLinux /home/www/build/webman01/vendor/workerman/workerman/Worker.php:15554 Workerman/Worker::forkWorkersForLinux /home/www/build/webman01/vendor/workerman/workerman/Worker.php:13975 Workerman/Worker::forkWorkers /home/www/build/webman01/vendor/workerman/workerman/Worker.php:13716 Workerman/Worker::runAll /home/www/build/webman01/vendor/workerman/workerman/Worker.php:5497 <main> /home/www/build/webman01/start.php:112...
3、获取进程内存地址
sudo /usr/local/php-8.2.14/bin/php ./reli i:eg -p 21845530x55e17fe1b1a0
4、跟踪当前进程正在执行的操作码
如果用户想分析一个真正的CPU受限的应用程序,那么他或她不仅想知道哪一行是慢的,还想知道操作码是什么。在这种情况下,使用 --template=phpspy_with_opcode 与 inspector:trace 或 inspector:daemon 。
sudo /usr/local/php-8.2.14/bin/php ./reli i:trace --template=phpspy_with_opcode -p 2184553
输出如下所示
0 EventBase::loop <internal>:-11 Workerman/Events/Event::loop /home/www/build/webman01/vendor/workerman/workerman/Events/Event.php:193:ZEND_DO_FCALL2 Workerman/Worker::run /home/www/build/webman01/vendor/workerman/workerman/Worker.php:2435:ZEND_DO_FCALL3 Workerman/Worker::forkOneWorkerForLinux /home/www/build/webman01/vendor/workerman/workerman/Worker.php:1555:ZEND_DO_FCALL4 Workerman/Worker::forkWorkersForLinux /home/www/build/webman01/vendor/workerman/workerman/Worker.php:1397:ZEND_DO_FCALL5 Workerman/Worker::forkWorkers /home/www/build/webman01/vendor/workerman/workerman/Worker.php:1371:ZEND_DO_FCALL6 Workerman/Worker::runAll /home/www/build/webman01/vendor/workerman/workerman/Worker.php:549:ZEND_DO_FCALL7 <main> /home/www/build/webman01/start.php:112:ZEND_DO_FCALL...
当前执行的操作码成为调用堆栈的第一帧。因此,像火焰图这样的可视化跟踪可以显示操作码的使用情况。
出于信息目的,执行操作码也被添加到调用帧的每一端。除了第一个帧,函数调用的操作码(如ZEND_DO_FCALL)应该出现在那里。如果在目标流程中启用了JIT,则此信息可能会稍微不准确。
火焰图是由 Brendan Gregg 发明的一种可视化方法,用于展示某一种系统资源或性能指标,是如何定量分布在目标软件里所有的代码路径上的。
C:ngx_http_lua_ngx_timer_atatcache.lua:43cache.lua:record_timingrouter.lua:338router.lua:routev2_routing.lua:1214v2_routing.lua:routeaccess_by_lua.lua:130
Psalm 是一个用于PHP的开源静态分析工具,可以帮助识别代码中明显的和难以识别的bug。Psalm既适用于大型遗留代码库,也适用于小型现代代码库,可以帮助防止绝大多数类型相关的runtime 错误,并且能够利用其他语言中流行的安全编码模式。Psalm还可以自动修复发现的许多错误以改进代码。
cd /home/www/build/reli-profsudo /usr/local/php-8.2.14/bin/php ./reli i:trace -o traces -- /usr/local/php-8.2.14/bin/php ./vendor/bin/psalm.phar --no-cacheInstall the opcache extension to make use of JIT on PHP 8.0+ for a 20%+ performance boost!Target PHP version: 8.1 (inferred from composer.json) Enabled extensions: ffi.Scanning files...Analyzing files...░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 60 / 421 (14%)░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 120 / 421 (28%)░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 180 / 421 (42%)░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 240 / 421 (57%)░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 300 / 421 (71%)░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 360 / 421 (85%)░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 420 / 421 (99%)░------------------------------ No errors found! ------------------------------Checks took 7.42 seconds and used 134.539MB of memoryPsalm was able to infer types for 99.9825% of the codebase
以上会输出一个traces文件,大概内容如下
0 array_merge <internal>:-11 Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer::handleArrayItem phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php:3982 Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer::inferArrayType phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php:2923 Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer::infer phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php:2104 Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner::visitClassConstDeclaration phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php:7605 Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner::start phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php:4516 Psalm/Internal/PhpVisitor/ReflectorVisitor::enterNode phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php:1167 _HumbugBox427cd2d3980b/PhpParser/NodeTraverser::traverseArray phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:1768 _HumbugBox427cd2d3980b/PhpParser/NodeTraverser::traverseNode phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:1059 _HumbugBox427cd2d3980b/PhpParser/NodeTraverser::traverseArray phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:19610 _HumbugBox427cd2d3980b/PhpParser/NodeTraverser::traverse phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:8511 Psalm/Internal/Scanner/FileScanner::scan phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Scanner/FileScanner.php:5112 Psalm/Internal/Codebase/Scanner::scanFile phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Codebase/Scanner.php:39813 Psalm/Internal/Codebase/Scanner::scanAPath phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Codebase/Scanner.php:54714 Psalm/Internal/Codebase/Scanner::scanFilePaths phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Codebase/Scanner.php:31015 Psalm/Internal/Codebase/Scanner::scanFiles phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Codebase/Scanner.php:22016 Psalm/Config::visitComposerAutoloadFiles phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Config.php:184017 Psalm/Internal/Analyzer/ProjectAnalyzer::visitAutoloadFiles phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php:24918 Psalm/Internal/Analyzer/ProjectAnalyzer::check phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php:33519 Psalm/Internal/Cli/Psalm::run phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/src/Psalm/Internal/Cli/Psalm.php:27220 <main> phar:///home/www/build/reli-prof/vendor/psalm/phar/psalm.phar/psalm:721 <main> /home/www/build/reli-prof/vendor/psalm/phar/psalm.phar:1422 <main> /home/www/build/reli-prof/vendor/bin/psalm.phar:119
sudo /usr/local/php-8.2.14/bin/php ./reli c:flamegraph <traces >flame.svg
使用浏览器打开预览flame.svg 图片
图片
下来生成一个webman的
Workerman[start.php] status ----------------------------------------------GLOBAL STATUS----------------------------------------------------Workerman version:4.1.9 PHP version:7.4.16start time:2023-12-22 22:56:56 run 9 days 3 hours load average: 0.04, 0.03, 0.09 event-loop:/Workerman/Events/Select----------------------------------------------PROCESS STATUS---------------------------------------------------pid memory listening worker_name connections send_fail timers total_request qps status15456 16.42M http://0.0.0.0:7788 webman 0 0 3 939 0 [idle]----------------------------------------------PROCESS STATUS---------------------------------------------------Summary 131M - - 0 0 28 9191 0 [Summary]
pid = 15456
sudo /usr/local/php-8.2.14/bin/php ./reli i:trace -p 15456 -o traces-15456 -- /usr/local/php-8.2.14/bin/php ./vendor/bin/psalm.phar --no-cachesudo /usr/local/php-8.2.14/bin/php ./reli c:flamegraph <traces-15456 >flame-15456.svg
一个简单Workerman火焰图到手啦!
图片
google-chrome安装(非必要)
下载
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb--2023-12-31 10:43:53-- https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debResolving dl.google.com (dl.google.com)... 120.253.253.225Connecting to dl.google.com (dl.google.com)|120.253.253.225|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 104953176 (100M) [application/x-debian-package]Saving to: ‘google-chrome-stable_current_amd64.deb’google-chrome-stable_current_amd64.deb 100%[==============================================================================================================================>] 100.09M 11.9MB/s in 7.4s 2023-12-31 10:44:00 (13.5 MB/s) - ‘google-chrome-stable_current_amd64.deb’ saved [104953176/104953176]
安装
sudo apt install ./google-chrome-stable_current_amd64.deb
PHP编译安装启用FFI扩展(编译增加配置--with-ffi)时出现错误No package 'libffi' found,检查后发现是因为缺少libffi库文件,利用以下命令安装,解决问题:
sudo apt install libffi-dev
PHP Warning: proc_open(): Exec failed: Permission denied in /home/www/build/reli-prof/src/Command/Converter/FlameGraphCommand.php on line 44PHP Warning: proc_open(): Exec failed: Permission denied in /home/www/build/reli-prof/src/Command/Converter/FlameGraphCommand.php on line 55
赋予reli-prof目录执行权限
sudo chmod 0775 -R /home/www/build/reli-prof
本文链接:http://www.28at.com/showinfo-26-55075-0.htmlReli:PHP性能分析利器,生成PHP性能火焰图
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com
上一篇: JetBrains 发布 2023 年 C# 开发者主要趋势
下一篇: 八个DevOps中重要的Linux命令