Monday Nov 05, 2007

PHP dtrace extension for Cool Stack 1.2

We have fixed the issue with the PHP dtrace extension not working in Cool Stack 1.2. As I mentioned in my announcement post, we had already identified the issue but didn't have time to fix it before the release. The issue was that /usr/ccs/bin/ld was being used to do the linking but this doesn't work for dtrace as some initialization code needs to be called from the .init section and this is not setup correctly if we don't use 'cc' to do the linking (rather than 'ld'). 

So a simple addition to the configure line:
LD="cc"

did the trick.

We now have two files : dtrace_1.2_sparc.so and dtrace_1.2_x86.so posted. Download the one for your machine and do the following :

  • Copy it to the php5 extensions directory of /opt/coolstack/php5/lib/php/extensions/no-debug-non-zts-20060613 and re-name it as dtrace.so.
  • Add extension="dtrace.so" to your /opt/coolstack/php5/lib/php.ini. 

See my earlier post, for an example of how to use dtrace to trace through the AMP stack. 

Friday Jun 01, 2007

Debugging AMP

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

php\*:::function-entry
/arg0/
{
        @[copyinstr(arg0)]=count();
}

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
 \*
 \*/

pid$1::_Z16dispatch_command19enum_server_commandP3THDPcj:entry
{
        printf("%d::%s\\n",tid,copyinstr(arg2));
}

 

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
 \*/
 
php\*:::function-entry
/arg0/
{
        printf("\\033[01;34m%s\\033[0m\\n",copyinstr(arg0));
}

php\*:::function-return
/arg0/
{
        printf("\\033[01;34m%s\\033[0m\\n",copyinstr(arg0));
}

pid$1::_Z16dispatch_command19enum_server_commandP3THDPcj:\*
{
        self->sql = copyinstr(arg2);
        printf("\\033[01;32m%s\\033[0m\\n",self->sql);
}

 

 

Tuesday May 01, 2007

Dtrace support for PHP

Dtrace is one of the coolest things in Solaris 10, adding great observability to applications. A Dtrace provider for PHP that adds probes to function entry and exit points has long been available. I finally got around to integrating this with the php in Cool Stack. 

Bryan Cantrill has some excellent examples of how to use dtrace to trace through your php code, down to the system libraries and kernel (if you want to go that far !) .

Instructions for installing the php dtrace extension

1. Download the shared library for the extension :

    dtrace.so.x86.bz2

    dtrace.so.sparc.bz2

2. Install the extension :

    # bunzip2 dtrace.so.<arch>.bz2
  # cp dtrace.so.<arch> /opt/coolstack/php5/lib/php/extensions/no-debug\*/dtrace.so

3. Enable the dtrace extension :

  Edit your php.ini (default is in /opt/coolstack/php5/lib/php.ini) and add the line :

  extension="dtrace.so"

4. Check if the extension has been correctly loaded by executing a sample php script that takes a long time to run (you can use Bryan's blastoff.php to do this). 

#/opt/coolstack# dtrace -l |grep php
39131   php16088         dtrace.so                php_dtrace_execute function-entry
39132   php16088         dtrace.so       php_dtrace_execute_internal function-entry
39133   php16088         dtrace.so                php_dtrace_execute function-return
39134   php16088         dtrace.so       php_dtrace_execute_internal function-return
#

5. You should now be able to use the dtrace provider whenever you want using dtrace scripts. The dtrace provider will work with mod_php if you're running Apache or if you're running php stand-alone. Here is a sample command that will show which functions are called :

# dtrace -n function-entry'{printf("called %s() in %s at line %d\\n", \\
copyinstr(arg0), copyinstr(arg1), arg2)}' -q


Please feel free to contribute additional dtrace scripts.

 

Shanti



About

I'm a Senior Staff Engineer in the Performance & Applications Engineering Group (PAE). This blog focuses on tips to build, configure, tune and measure performance of popular open source web applications on Solaris.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today