線上 PHP-FPM 進程 CPU 長期佔用 100%,請求堆積、響應超時。常規日誌無報錯,Xdebug Profiler 開啟會拖垮性能。如何在不修改代碼、不重啟服務的情況下,精準定位到正在執行的 PHP 代碼行?
PHP 解釋器執行時,全局結構体 zend_executor_globals (EG) 保存著當前執行上下文:
EG(current_execute_data) → 當前執行的 zend_execute_data (棧幀)execute_data->func → 當前函數 zend_functionexecute_data->opline → 當前執行的 opcode 指針opline->lineno → **源代碼行號**func->op_array.filename → **源文件路徑**通過 GDB 附掛進程,讀取這些結構體,即可得到「正在執行的文件:行號」。
# 查看所有 worker
ps aux | grep php-fpm
# 或
pgrep -f php-fpm
# 假設 PID=12345 (CPU 最高的那個)
gdb -p 12345
# 進入 GDB 後,執行以下命令序列 (可保存為 .gdbinit 腳本)
# 定義便利變量
set $eg = &executor_globals
# 或 PHP 8.1+ 可能為 zend_executor_globals *executor_globals_ptr; 先找地址
# info address executor_globals
# set $eg = *(zend_executor_globals *)$address
# 獲取當前執行數據
set $ex = $eg.current_execute_data
if $ex == 0
echo "No active execute data (可能在 C 函數/擴展中)\n"
else
# 遍歷調用棧
while $ex != 0
set $func = $ex.func
if $func != 0
# 函數名
set $fname = $func.common.function_name
if $fname != 0
printf "Function: %s\n", $fname.val
end
# 文件名
if $func.type == ZEND_USER_FUNCTION
set $op_array = (zend_op_array *)$func
if $op_array.filename != 0
printf " File: %s\n", $op_array.filename.val
end
# 當前行號
if $ex.opline != 0
printf " Line: %d\n", $ex.opline.lineno
end
end
end
set $ex = $ex.prev_execute_data
end
end
detach
quit
保存為 php_cpu_stack.gdb:
# php_cpu_stack.gdb
define php_bt
set $eg = &executor_globals
set $ex = $eg.current_execute_data
if $ex == 0
echo "No PHP execute data\n"
else
while $ex != 0
set $func = $ex.func
if $func != 0
set $fname = $func.common.function_name
if $fname != 0
printf " %s\n", $fname.val
end
if $func.type == ZEND_USER_FUNCTION
set $op_array = (zend_op_array *)$func
if $op_array.filename != 0
printf " at %s:", $op_array.filename.val
end
if $ex.opline != 0
printf "%d\n", $ex.opline.lineno
else
echo "\n"
end
end
end
set $ex = $ex.prev_execute_data
end
end
end
document php_bt
Print PHP backtrace with file:line
end
使用:
gdb -p 12345 -x php_cpu_stack.gdb -batch -ex "php_bt"
execute_ex
at /var/www/html/app/Services/OrderService.php:142
execute_ex
at /var/www/html/app/Http/Controllers/OrderController.php:56
execute_ex
at /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
...
zend_execute_scripts
php_execute_script
main
立即定位到: `OrderService.php:142` — 打開文件查看該行邏輯 (如死循環、大數據遍歷、正則回溯、遞歸無出口)。
若需統計一段時間內的熱點分布,用 Linux `perf`:
# 1. 安裝 perf 與 PHP 符號表 (需 PHP 編譯時 --enable-debug 或安裝 php-debuginfo)
perf record -g -p 12345 -- sleep 30
# 2. 生成報告
perf report --stdio
# 3. 或生成火焰圖
perf script | ./stackcollapse-perf.pl | ./flamegraph.pl > cpu.svg
火焰圖可視化展示 CPU 時間分布,快速識別熱點函數調用鏈。
| 模式 | 典型代碼特徵 | 排查方向 |
|---|---|---|
| 死循環/遞歸無出口 | while(true) { } / function f() { f(); } | 檢查循環條件、遞歸基線 |
| 正則回溯災難 | preg_match('/(a+)+b/', 'aaaaaaaaac') | 優化正則、使用具備 DFA 的引擎 (re2) |
| 大數組循環嵌套 | foreach($big as $a) foreach($big as $b) ... | 算法優化 (哈希查找/排序雙指針) |
| JSON 編碼/解碼巨大數據 | json_encode($hugeArray) | 分塊處理、改用 MessagePack/Protobuf |
| Composer 自動加載器掃描 | 大量文件 require_once | 優化 autoload (classmap/apcu)、opcache |
| 垃圾回收 (GC) 風暴 | 大量循環引用對象 | gc_disable() 關鍵路徑、手動 gc_collect_cycles() |
GDB + `zend_executor_globals` 是 PHP 線上 CPU 100% 定位的「核武器」:無需修改代碼、無需重啟、秒級定位到文件行號。配合 `perf` 火焰圖做趨勢分析,APM 做持續監控,構建「事後定位 + 事前預防」雙重保障。建議運維團隊將 `php_cpu_stack.gdb` 納入應急預案 SOP,關鍵時刻可救命。