'PHP: Setting Up XDebug With KCacheGrind'

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.

If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.