Using Xdebug and Qcachegrind to do PHP Profiling on a remote server from your Mac.

Xdebug and Php Profiling

Segment 3.

Profiling, Researching, Troubleshooting

Segment 1. Installing and configuring XDebug
Segment 2. Installing Qcachegrind

General Usage of Qcachegrind (Kcachegrind)

Pre-req: Installing Qcachegrind

 Kcachegrind is the leading GUI software used to do Php profiling with XDebug.
 Generic instructions on how to generally use Qcachegrind can be found here.

 I could not write it any better so I am not even going to try.

I use Qcachegrind with profile traces pulled from various Cloud Servers I am working remotely with. They typically do not have gui installed on them so KCachegrind run on the server is obviously out of the equation.  So what I do instead is rsync the profile log to my Mac then open it with Qcachegrind .

If you need to know more about the Source Code and the output given in  Qcachegrind
Feel free to create the source file location and paste the source code there if you need it, or you can tell  Qcachegrind where the code is by following the Generic Instructions above under the Getting started section.
Click the ‘Menu’ Qcachegrind > Preferences > Annotations > Add:

Finding Hotspots with Qcachegrind

 Start sorting by “Self” or “Incl.” in the function-list.
Find the Function that takes the most time to load check both “self” and “incl” and note the one with the biggest “incl.”
Follow the bread crumbs from there.  Further detailed instructions in  the Generic Instructions.

Troubleshooting and profiling with Linux Terminal, Php, and XDebug

Pre-req: Installing and Configuring XDebug

 Please note that I am writing this article based on my siteup in the Pre-req article. This is done mostly for ease of use and learning. If your setup is not exactly the same everything should still work with a little tweaking on your part.

Prep Work:
First check that you have the  XDebuger module loaded and see that you have the options needed to Profile enabled. To do this create a info.php file containing the below code and then browse to Http;//your-domain/info.php:


Scroll down to the xdebug section
Make sure to take note of where your Trace and Profile output are sent.
I suggest to generally have these profiling related parameters set for my troubleshooting: 
xdebug.profiler_enable=1 (this does not need to be on if Profile Trigger is enabled)

Generating XDebug Trace log:

First I am going to show you how to generate your XDebug Trace log. The code I am using is my example code located at the bottom of this article titled `Example Code` as a PHP script test.php.  Feel free to use it.

When you have xdebug.trace_enable_trigger turned on you can then do url request XDEBUG_TRACE as POST/GET Parameter or as even as cookie.
Personally I just do either of the following:

Browse to:

Or run the command:
When you now look at your Xdebug output path mine being /var/log/php/, you should a new trace file the file naming convention differs depending on your php ini config’s parameter: xdebug.trace_output_name. One of my trace files looks like this: 

Example Code:
echo “Hello World”.”n”;
echo “<br>”;
echo “<br>”;
echo “<br>”;
echo date( ‘m/d/y h:ia’ );
echo “<br>”;
echo “<br>”;
$var = ‘122.34343The’;
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343

Memory Analyzing with Xdebug:  

 There is a script called tracefile-analyser.php you can read about here that comes installed with XDebug that will analyze your last running php process’s to see what resources were used, how many times they were called and how long it took to load them for both inclusive time and own time.  I generally use this to grab the Function that is using the most memory.

First we find the file by running either commands:

Locate Command:

 locate tracefile-analyser.php

Find Command:
  find / -name ‘tracefile-analyser.php’ 2> /dev/null
php /path/to/tracefile-analyser.php trace.20534533421.xt memory-own 20
We need to run the command to use the feature memory-own with a x-amount of lines.
php /path/to/tracefile-analyser.php trace.20534533421.xt memory-own 20

The Results:

Showing the 20 most costly calls sorted by 'memory-own'.
Inclusive Own
function #calls time memory time memory
require_once 9 0.0541 4595160 0.0277 2548104
{main} 1 0.0600 2906032 0.0034 249744
fread 4 0.0001 33296 0.0001 33296
session_start 1 0.0002 31824 0.0002 31824
XML_Presentation->startHandler 38 0.0073 36360 0.0035 18424
_pres_slide->_pres_slide 27 0.0009 10152 0.0009 10152
_presentation->_presentation 1 0.0001 7912 0.0001 7912
strtolower 67 0.0017 6456 0.0017 6456
compact 1 0.0000 4832 0.0000 4832
each 5 0.0001 4320 0.0001 4320
XML_Presentation->endHandler 38 0.0014 3800 0.0014 3960
_slide->_slide 1 0.0001 3896 0.0001 3896
XML_Slide->startHandler 4 0.0009 10800 0.0004 3736
_image->_image 1 0.0000 3040 0.0000 3040
fopen 2 0.0001 2816 0.0001 2816
getimagesize 1 0.0001 2296 0.0001 2296
display->display 1 0.0001 2120 0.0001 2120
explode 2 0.0001 2120 0.0001 2120
xml_parser_create 2 0.0001 1680 0.0001 1680
XML_Parser->_initHandlers 2 0.0011 1600 0.0005 1360

XDebug Trace tool:
Another option is a PHP GUI  based  XDebug Trace tool This tool will simply look at all of the traces in your xdebug.profile_output_name Php parameter and whow them to you nicely. 
Install the Script xdebug-trace-gui  from here:

Install it to whatever vhost directory you want, then edit the example directory path below, and and that to your existing Apache vhost.

<Directory /var/www/xdebug/xdebug-trace-gui>
    Order allow,deny
allow from x.x.x.x (your ip)
deny from all
    php_value 'xdebug.auto_trace' 'Off'
php_value short_open_tag 1

There are 3 main files:

Xdebug trace gui with pagination, 1024 sentences in a page. For big traces. Memory and time consumition calculed by difference with previous instruction.

 Old style trace gui all one page, with summaries.

Xdebug trace graph. Statistics memory consumition MB per centesims. You need the parent folder of jpGraph in the include_path.

jim has written 83 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>