Performance Blog

Debugging AMP

Posted on: June 1, 2007

In a previous post, I mentioned the availability of the dtrace extension for Cool Stack’s PHP.  Using this extension and the Cool Stack MySQL, it is possible to analyze the performance of your application running on this stack. At JavaOne, we demoed this using the open source MediaWiki and SugarCRM applications.  dtrace is especially useful in analyzing complex multi-tier applications like AMP. Thanks to Angelo Rajadurai for the creation of the scripts that I describe below.

Analyzing PHP calls

So, let’s look at a simple dtrace script that counts how many times a particular PHP function is called :

#!/usr/sbin/dtrace -Zqs


You can copy the above to a file named php.d and start it just before the operation you want to analyze. For example, if you want to see which php functions are called when you click on Edit in your mediawiki site, start the script, click the link, then terminate the script via ^C.

Analyzing MySQL calls

The following script will print the SQL command issued by the mysql process. Invoke the script with the pid of the mysqld process as an argument, just before you execute the operation you want to debug.

#!/usr/sbin/dtrace -qs

/* This program prints the SQL commands executed by MySQL
 * process. It takes the pid of the mysqld process as its
 * only argument.
 * This script observes the occurance of dispatch_command()
 * function and prints the SQL statement (arg2). Before you
 * use this D-script replace the mangled name below with the
 * output run from the mysql bin directory :
 */usr/ccs/bin/nm mysqld | awk -F ‘{ print $NF; }’ | grep dispatch_command



Tracing thru PHP & MySQL 

Finally, here’s a script to trace through both the PHP and MySQL calls. The output is color-coded : blue for PHP functions and green for MySQL functions (admittedly there are many different ways to format the output and this one’s a bit of a hack – but it works). This script should be passed the pid of the mysqld process as an argument. Note that this script assumes that both PHP and MySQL are running on the same system. If they are running on different systems, then you will have to run the individual scripts shown above on the different systems.

#!/usr/sbin/dtrace -ZqFs

/* We use terminal escape sequence to change color
 * PHP – Blue
 * MYSQL – Green


        self->sql = copyinstr(arg2);




2 Responses to "Debugging AMP"

hi Shanti
i’m useing the latest script in "Tracing thru PHP & MySQL"
traces are coming out for both php and mysql but after a while display is corrupted with exotic characters and i’m obliged to hard rest terminal window but i’ve lost a lot of info
did you experienced such issue ?

I don’t know what you mean by ‘after a while’. We didn’t see any issues with it per se, since we did the tracing only when we clicked on something via the browser.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Shanti's Photo


Latest Tweets



%d bloggers like this: