longxinH / xhprof

PHP7/PHP8 support
http://pecl.php.net/package/xhprof
Apache License 2.0
1.04k stars 165 forks source link

Second profile run in one php process returns data with pmu = 0 #63

Closed astronom closed 2 years ago

astronom commented 2 years ago

First of all, I appreciate your work on this project. We use xhprof in our applications and it helps us to investigate bottlenecks. Now we try to profile some RPC php daemon work. Sometimes we enable profile for one rpc request process, collect data and save it. The first profiled request return data with pmu key with correct value, the second is not (pmu = 0) and this may be some other request. This php script can reproduce the problem

<?php
require_once 'vendor/autoload.php';

// First iteration
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_NO_BUILTINS);
$uuid = \Ramsey\Uuid\Uuid::uuid4();
echo $uuid->toString().PHP_EOL;
$xhprof_data = xhprof_disable();
print_r($xhprof_data);

// Second iteration
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_NO_BUILTINS);
$uuid = \Ramsey\Uuid\Uuid::uuid6();
echo $uuid->toString().PHP_EOL;
$xhprof_data = xhprof_disable();
print_r($xhprof_data);

result in gist https://gist.github.com/astronom/d57152c650784f93c639d3c26aa9d5be

PHP 7.4.16 (cli) (built: Apr 15 2021 00:56:09) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

XHPROF_VERSION 2.3.5

longxinH commented 2 years ago

pmu is the current process memory peak. When uuid6 is executed, the memory peak has not changed, so pmu=0, use memory_get_peak_usage to prove

<?php
require_once 'vendor/autoload.php';

// First iteration
var_dump(memory_get_peak_usage());
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_NO_BUILTINS);
$uuid = \Ramsey\Uuid\Uuid::uuid4();
echo $uuid->toString().PHP_EOL;
$xhprof_data = xhprof_disable();
var_dump(memory_get_peak_usage());

// Second iteration
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_NO_BUILTINS);
$uuid = \Ramsey\Uuid\Uuid::uuid6();
echo $uuid->toString().PHP_EOL;
$xhprof_data = xhprof_disable();
var_dump(memory_get_peak_usage());