PHP: Setting Up XDebug with KCacheGrind

July 26, 2007 – 3:50 pm

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
  1. 7 Responses to “PHP: Setting Up XDebug with KCacheGrind”

  2. 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.

    By Topbit on Jul 27, 2007

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

    By David McCabe on Jul 31, 2007

  4. 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.

    By WC on Apr 8, 2008

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

    By jon on Apr 8, 2008

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

    php_value xdebug.profiler_enable_trigger 1

    By Christian on May 11, 2008

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

    By jon on May 12, 2008

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

    By forchy on Jan 20, 2010

Post a Comment