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.
7 Responses to PHP: Setting Up XDebug with KCacheGrind
Leave a Reply Cancel reply
Recent Comments
- Anil on MySQL Triggers Tutorial
- Ashish on MySQL Triggers Tutorial
- David on iCal Agenda
- jon on IP address geolocation SQL database
- pim on IP address geolocation SQL database
- jnns on Redis Wildcard Delete
- K.C. Murphy on iCal Agenda
- BA on Experts Exchange should be removed from Google search results
- Andrew on Executing multiple curl requests in parallel with PHP and curl_multi_exec
- Stu on Executing multiple curl requests in parallel with PHP and curl_multi_exec
Recent Posts
- New Project: Jester
- Open New Terminal Tip
- Installing MySQLdb on MacOS Lion
- Headless VM Server Using Ubuntu 11.10
- Get rid of Facebook’s Awful Ticker
- Api Tester now hosted on Github
- Trac .11 jQuery bug
- Multiple Filetypes in Vim
- Git Tip: Setting Up Your Remote Server
- Install issue pymongo on OSX (setuptools out of date)
Categories
- amazon (1)
- answerbag (6)
- apache (9)
- apple (8)
- awk (2)
- bbedit (2)
- c++ (3)
- chrome (2)
- cluster (1)
- cocoa (1)
- collective intelligence (1)
- curl (3)
- db2 (1)
- demand media (1)
- ebay (1)
- eclipse (4)
- erlang (13)
- facebook (1)
- fortran (1)
- gen_server (1)
- git (5)
- google (4)
- haddad (1)
- hdf5 (1)
- html (1)
- innodb (1)
- itunes (1)
- java (2)
- jester (1)
- kvm (1)
- launchbar (1)
- leex (1)
- letsgetnuts.com (1)
- libvirt (1)
- links (6)
- linux (27)
- lucene (1)
- mac (16)
- memcached (1)
- misconception (1)
- mobile (1)
- mono (1)
- mssql (1)
- munin (1)
- mysql (31)
- numpy (1)
- oracle (1)
- php (23)
- puppet (4)
- pyparsing (1)
- pytables (1)
- python (11)
- q&a (1)
- quicksilver (1)
- rant (6)
- readynas (1)
- redis (2)
- regex (1)
- replication (1)
- search (1)
- shitty code (1)
- solr (3)
- spaces (1)
- sshfs (1)
- stored procedure (1)
- svn (5)
- textmate (2)
- tips (22)
- trac (1)
- tutorial (4)
- ubuntu (3)
- Uncategorized (4)
- unix (1)
- vim (3)
- virtual box (6)
- vmware (1)
- weird (3)
- wikipedia (1)
- windows (1)
- xcode (1)








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.
If somebody could find a cachegrind program for Mac OS X, that’d be wonderful.
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.
Hey WC – do you have (or know of) instructions on how to do it?
FYI, if you want to enable this in .htaccess, you can do it like so;
php_value xdebug.profiler_enable_trigger 1
Christian – great tip. I’ll try it out today. Thanks.
For those interested, there is a web based grind called webgrind, check http://code.google.com/p/webgrind/