之前我们使用 XDebug
工具来生成 profile
,后来鸟哥写了个更简单的扩展 php-valgrind
,它依赖于 valgrind
。
鸟哥文章地址 https://www.laruence.com/2013/08/14/2899.html
安装 valgrind
yum install -y valgrind
或者去 官网 下载 tar 包
cd /usr/local/src
wget https://sourceware.org/pub/valgrind/valgrind-3.16.1.tar.bz2
tar -jxvf valgrind-3.16.1.tar.bz2
cd valgrind-3.16.1
./configure
make && make install
valgrind -h
安装 php-valgrind 扩展
cd /usr/local/src
wget https://github.com/laruence/php-valgrind/archive/valgrind-0.1.0.tar.gz
tar -xf valgrind-0.1.0.tar.gz
cd php-valgrind-valgrind-0.1.0
phpize
./configure
make && make install
扩展地址
/usr/local/src/php-valgrind-valgrind-0.1.0/modules/valgrind.so
查找扩展目录
php -i | grep extension_dir
extension_dir => /usr/local/lib/php/extensions/no-debug-non-zts-20200930 => /usr/local/lib/php/extensions/no-debug-non-zts-20200930
cd /usr/local/lib/php/extensions/no-debug-non-zts-20200930
opcache.so sodium.so valgrind.so
看来已经被复制过去了,接下来修改 php.ini
echo extension=valgrind >> /usr/local/etc/php/php.ini
php -m | grep valgrind
安装完毕
测试
代码 code.php
:
<?php
callgrind_toggle();
test();
callgrind_toggle();
function test() {
$tt = 0;
$temp = funa($tt);
$res = fun1($temp);
var_dump($res);
}
function funa(int $a): int {
$a++;
return funb($a);
}
function funb(int $a): int {
$a++;
return func($a);
}
function func(int $a): int {
$a++;
return $a;
}
function fun1($x): int {
return $x * $x;
}
执行:
valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no php code.php
当前目录下多出了一个文件 callgrind.out.14937
,此文件就是 profile 文件。
使用 qcachegrind 软件打开 callgrind.out.14937
,效果如下: