No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

XHProf FlameGraphs

A script to convert a directory of xhprof or uprofiler sampling files to a format that can be read by Brendan Gregg's FlameGraph script.

Note: This works with files generated by (uprofiler|xhprof)_sample_enable/(uprofiler|xhprof)_sample_disable. It will not work with the more common hierarchical xhprof/uprofiler format.


First, generate the sampling data, writing the files with a sample_xhprof (resp. sample_uprofiler) file extension. Then run the script passing the directory containing those files as an argument like so:

Generating the sample data

One way to generate the sample data is prepend the following fragment to the entry point of the application, often index.php:

if (extension_loaded('uprofiler')) {
  $profiler = 'uprofiler';
elseif (extension_loaded('xhprof')) {
  $profiler = 'xhprof';
else {
  $profiler = NULL;
if ($profiler) {
  $enable = "{$profiler}_sample_enable";
  $disable = "{$profiler}_sample_disable";
  $uri = substr($_SERVER['REQUEST_URI'], 1);
  $uri = preg_replace('/[?\/]/', '-', $uri);
  register_shutdown_function(function () use($disable, $profiler, $uri) {
    $filename = "/tmp/xhprof/{$uri}." . uniqid() . ".{$profiler}";
    file_put_contents($filename, serialize($disable()));
    chmod($filename, 0777);

Building the flamegraph from the data

./xhprof-sample-to-flamegraph-stacks /directory/with/sample/xhprof/files | > xhprof-flamegraph.svg