php
函數追蹤擴展 phptrace
phptrace
是一個低開銷的用於跟蹤、分析 php
運行狀況的工具。
它能夠跟蹤 php
在運行時的函數調用、請求信息、執行流程、而且提供有過濾器、統計信息、當前狀態等實用功能。
在任何環境下,它都能很好的定位阻塞的問題以及在高負載下進行 denbug
。
官方源碼:https://github.com/Qihoo360/phptrace。php
pecl
方式安裝$ pecl install trace
$ wget https://pecl.php.net/get/trace-1.0.0.tgz # 下載源碼 $ tar -xf trace-1.0.0.tgz # 解壓文件 $ cd trace-1.0.0/extension # 進入擴展目錄
$ whereis php-config # 找到 php-config 的路徑 $ phpize $ ./configure --with-php-config=/usr/bin/php-config # 這裏的 --with-php-config 是上一步找到的路徑 $ make # 編輯 $ make test # 編譯測試 $ make cli # 命令行工具 $ make install-all # 安裝 php 擴展,命令行工具到 php 目錄
php.ini
在 php.ini
配置文件中增長如下配置信息。git
[phptrace] extension=trace.so phptrace.enabled=1
注:修改 php.ini
後,須要手動重啓 php-fpm
github
root@639ca1f15214:~# php -m | grep trace trace
root@639ca1f15214:/var/www# php -r 'for ($i = 0; $i < 20; $i++) { sleep(5); }' & [1] 361 root@639ca1f15214:/var/www# phptrace -p $! process attached [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 4.966s 4.966s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.002s 5.002s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.002s 5.002s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 4.966s 4.966s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.003s 5.003s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s [pid 361] > sleep(5) called at [Command line code:1] [pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 4.967s 4.967s [pid 361]> swoole_call_user_shutdown_begin() called at [(null):0] [pid 361]< swoole_call_user_shutdown_begin() = true called at [(null):0] ~ 0.000s -79.912s [pid 361]< cli php for ($i = 0; $i < 20; $i++) { sleep(5); } process detached