PHP Flame Graphs

2018/09/20

In my quest to optimise the PHP sites I work on, I found that the Flame Graph repository contains a script to convert XDebug traces. Flame graphs, created by Brendan Gregg, visualize the most frequently hit code from profiled software. The x-axis represents the profiled stack traces, ordered alphabetically, and the y-axis is the stack depth. Functions taking up more CPU time are wider.

I wrote a simple PHP script to list all the trace files and pipe them into the necessary scripts. This script is at the bottom of the page. You’ll need to clone a copy of the Flame Graph repo into the same directory or change the script to alter the path. It’s not safe enough to put on a production machine, as it simply takes a file path and passes it to the script. However, it’s totally fine to run on your development machine.

To generate the traces, you’ll need to alter the php.ini file. It’s a similar setup to the PHP profiling I did.

You can then browse to the URL of the page you want to check out and add ?XDEBUG_TRACE=<secret key>.

Finally, load up the PHP script, choose the trace file that was just generated and you should see the associated flame graph. The labels might seem a bit odd but according to the original pull request, “Each millionth of a second spent in a function is treated as one sample”.

When using flame graphs to optimise your code, you want to look for the widest stack frames. This indicates something taking up more CPU time. To dig down into the actual lines of code that are hot, you could check out my PHP profiling post.