KCacheGrind is a very useful tool to identify bottlenecks in your applications. This will explain the steps to using it to find issues with your PHP scripts. For me, the scripts are all web pages.

I’m already assuming you’re running a current version of PHP. I did this using PHP 5.2.1. These instructions are based on a Unix/Linux server, if you’re running Windows I can’t help you.

Step 1: Install XDebug.

XDebug can be found at xdebug.org, oddly enough. Fortunately, it’s available through PECL, so lets just use that.

pecl install xdebug

Make sure to add the appropriate line to your php.ini. Change the path accordingly to where the module is installed.

zend_extension="/usr/local/php/modules/xdebug.so"
xdebug.profiler_enable_trigger = on

The second line will let you turn on debugging for specific pages, rather than blindly writing debug files for every single page.

Step 2: Setup Linux with KDE (if you don’t already have it)

KCacheGrind only runs in KDE, to my knowledge. After a little research, I ended up downloading a copy of Kubuntu, which is Ubuntu Linux running KDE. Pretty awesome, easy to install. I installed this in a VMWare machine. If you don’t already have VMWare, you’re missing out, big time. For windows, there’s a few free versions, you’ll need the one capable of creating images. I did this using VMWare Fusion (beta), which is $40 but still awesome. Go get it. I installed Kubuntu which is very straight forward.

Step 3: Install KCacheGrind

Go to the K Menu (bottom left buttun), then Add/Remove Programs. Enter your password at the prompt.

Choose Development in the left, make sure unsupported is checked, and pick KCacheGrind from the application list, then click Next to install. It’ll do it’s thing.

Step 4: Generate a cachegrind file to profile

By now you’re probably very eager to use this thing. Open whatever URL you’d like to profile, and append

?XDEBUG_PROFILE=1

to the end. Check the /tmp directory of your web server, and look for the file that looks something like cachegrind.out.6351. (the number at the end will probably be different for you). Copy this file to your machine that’s running KDE.

Step 4: Fire up KCacheGrind

K-Menu > Development > KCacheGrind. Click the open button, and pick your file. You’ll have before you a breakdown of where your application is spending it’s time. It’s sorted by % of time taken in decending order, so it should be pretty easy to figure out what’s taking a while.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
 

7 Responses to PHP: Setting Up XDebug with KCacheGrind

  1. Topbit says:

    For those without KDE – http://sourceforge.net/projects/wincachegrind/ The project itself is quiescent, but I used it myself a few months ago with an earlier version of Xdebug and it was very useful, and certainly informative.

  2. David McCabe says:

    If somebody could find a cachegrind program for Mac OS X, that’d be wonderful.

  3. WC says:

    kCacheGrind can be run on OS X… It requires a little work to install, but it can be done. You’ll need all the Qt libs and such to do so.

  4. jon says:

    Hey WC – do you have (or know of) instructions on how to do it?

  5. Christian says:

    FYI, if you want to enable this in .htaccess, you can do it like so;

    php_value xdebug.profiler_enable_trigger 1

  6. jon says:

    Christian – great tip. I’ll try it out today. Thanks.

  7. forchy says:

    For those interested, there is a web based grind called webgrind, check http://code.google.com/p/webgrind/

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=""> <strike> <strong>