<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>experimentalworks &#187; dtrace</title>
	<atom:link href="http://blog.experimentalworks.net/tag/dtrace/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.experimentalworks.net</link>
	<description></description>
	<lastBuildDate>Fri, 27 Jan 2012 10:07:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>php-trunk macport</title>
		<link>http://blog.experimentalworks.net/2010/05/php-trunk-macport/</link>
		<comments>http://blog.experimentalworks.net/2010/05/php-trunk-macport/#comments</comments>
		<pubDate>Mon, 24 May 2010 15:48:39 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dtrace]]></category>

		<guid isPermaLink="false">http://blog.experimentalworks.net/?p=435</guid>
		<description><![CDATA[macports is a widely used ports system for Mac OS. It's repository contains hundreds of application that can be compiled and installed. The repository contains php 5.3. So if you want to run PHP from subversion you still have to compile it yourself and install it yourself outside your managed ports environment. I created ...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.macports.org/ ">macports</a> is a widely used ports system for Mac OS. It&#8217;s repository contains hundreds of application that can be compiled and installed. The repository contains php 5.3. So if you want to run PHP from subversion you still have to compile it yourself and install it yourself outside your managed ports environment. I created a rather simple Portfile to build it from PHP&#8217;s <a href="http://svn.php.net/repository/php/php-src/trunk/">trunk</a>. </p>
<p>To use the php-trunk portsfile. Just extract the tarball into a directory and&#8230;<br />
<code><br />
 $ tar xzvf php-trunk-port.tar.gz<br />
 $ cd php-trunk-port<br />
<del datetime="2010-05-24T18:23:38+00:00"> $ echo "file://`pwd`" > /opt/local/etc/macports/sources.conf</del><br />
 $ echo "file://`pwd`" >> /opt/local/etc/macports/sources.conf<br />
 $ port install php-trunk<br />
</code></p>
<p>And the best thing about the port: It compiles PHP with dtrace support :).</p>
<p>Please note the Portfile is very simple and is not tested with the additional modules provided by macports. So use it at your own risk and enhance it.</p>
<p><a href="http://patches.experimentalworks.net/php-trunk-port.tar.gz">DOWNLOAD</a></p>
<p>UPDATE: as philip noted, it should be >> instead of >.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.experimentalworks.net/?flattrss_redirect&amp;id=435&amp;md5=c30dbdf415711fde1cf1109e756ca7f9" title="Flattr" target="_blank"><img src="http://blog.experimentalworks.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.experimentalworks.net/2010/05/php-trunk-macport/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP 5.3.99-dev and DTrace Part I</title>
		<link>http://blog.experimentalworks.net/2010/04/php-5-3-99-dev-and-dtrace-part-i/</link>
		<comments>http://blog.experimentalworks.net/2010/04/php-5-3-99-dev-and-dtrace-part-i/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 17:10:13 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[dtrace]]></category>

		<guid isPermaLink="false">http://blog.experimentalworks.net/?p=432</guid>
		<description><![CDATA[For those not following the PHP development. We backported the DTraces probes from the abandoned PHP 6.0 branch, back to the new trunk PHP 5.3.99-dev. It is called 5.3.99 because the PHP dev community has not decided yet on a version number (5.4 or 6.0).

To compile PHP with DTrace, on Solaris or Mac OS ...]]></description>
			<content:encoded><![CDATA[<p>For those not following the PHP development. We backported the DTraces probes from the abandoned PHP 6.0 branch, back to the new trunk PHP 5.3.99-dev. It is called 5.3.99 because the PHP dev community has not decided yet on a version number (5.4 or 6.0).</p>
<p>To compile PHP with DTrace, on Solaris or Mac OS do:</p>
<pre>
  $ svn co http://svn.php.net/repository/php/php-src/trunk php
  $ cd php
  $ ./buildconf --force
  $ ./configure --enable-dtrace
  $ make
</pre>
<p>To check if your PHP has DTraces probes enabled, you can check the phpinfo() using</p>
<pre>
 $ php -i | grep DTrace
</pre>
<p>or use DTrace directly></p>
<pre>
 $ sudo dtrace -ln 'php*:::' -c 'php -m'

88836    php4614               php               dtrace_compile_file compile-file-entry
88837    php4614               php               dtrace_compile_file compile-file-return
88838    php4614               php                        zend_error error
88839    php4614               php  ZEND_CATCH_SPEC_CONST_CV_HANDLER exception-caught
88840    php4614               php     zend_throw_exception_internal exception-thrown
88841    php4614               php           dtrace_execute_internal execute-entry
88842    php4614               php                    dtrace_execute execute-entry
88843    php4614               php           dtrace_execute_internal execute-return
88844    php4614               php                    dtrace_execute execute-return
88845    php4614               php                    dtrace_execute function-entry
88846    php4614               php                    dtrace_execute function-return
88847    php4614               php              php_request_shutdown request-shutdown
88848    php4614               php               php_request_startup request-startup
</pre>
<p>Have fun with DTrace in PHP.</p>
<p>A few examples will follow in the next days.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.experimentalworks.net/?flattrss_redirect&amp;id=432&amp;md5=3e14fabfad7b52726cb9e5e4394527f1" title="Flattr" target="_blank"><img src="http://blog.experimentalworks.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.experimentalworks.net/2010/04/php-5-3-99-dev-and-dtrace-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DTracing PHP</title>
		<link>http://blog.experimentalworks.net/2008/12/dtracing-php/</link>
		<comments>http://blog.experimentalworks.net/2008/12/dtracing-php/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 01:22:09 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dtrace]]></category>
		<category><![CDATA[opensolaris]]></category>

		<guid isPermaLink="false">http://wp.experimentalworks.net/?p=83</guid>
		<description><![CDATA[Dtrace is an extremely flexible and powerful tool to trace and debug applications. I recently dtraced PHP a little bit, so here is a tutorial how to start dtracing PHP.
Prequisites
You need a dtrace capable operating system like Solaris 10, OpenSolaris, Mac OS X 10.5 or FreeBSD 7. I use dtrace on OpenSolaris 2008.11. You ...]]></description>
			<content:encoded><![CDATA[<p>Dtrace is an extremely flexible and powerful tool to trace and debug applications. I recently dtraced PHP a little bit, so here is a tutorial how to start dtracing PHP.<br />
<span id="more-83"></span><strong>Prequisites</strong><br />
You need a dtrace capable operating system like Solaris 10, OpenSolaris, Mac OS X 10.5 or FreeBSD 7. I use dtrace on OpenSolaris 2008.11. You also need at least dtrace privileges or need to be root to be able to trace a program. I used PHP 5.3.0alpha3 in my tests, but it actually doesn&#8217;t matter. As PHP doesn&#8217;t provide DTrace probes itself we have to trace the underlying C-functions, therefore we need to know the Zend Engine and PHP&#8217;s internals.</p>
<p><strong>An Introduction into DTrace</strong><br />
Dtrace is really powerful and trying to do an introduction to all it&#8217;s features is just not possible. Therefore I will just focus on the basics, that are needed to get our stuff working. The basic idea behind Dtrace is that the kernel and userland programs fire probes on a specific location in the kernel or the userland program. As the probe is just fired if DTrace is running and trying to catch those <i>markers</i>, the probes itself don&#8217;t cost CPU power at all (in fact they are NOP&#8217;s). But if we trace them, we are able to see those probes and they are able to pass some data. To get way more complex information out of those probes, DTrace has an builtin scripting language that catches those probes and process them.</p>
<p><em>Dtrace program structure</em><br />
Every probe in DTrace is describe by a 4 tuple. The first one is the so-called provider. The second, the probemodule, the third is the probefunc and the last one is the probename. The parts are separated by a semicolon, therefore every probe is defined by: </p>
<blockquote><p>
&lt;probeprovider&gt;:&lt;providermodule&gt;:&lt;probefunc&gt;:&lt;probename&gt;
</p></blockquote>
<p>In our example, the provider will usually the pid provider that gives us probes for a given PID (the PHP controller we want to trace). The probemodule will be <i>php</i> and the probefunc will be the function name of the underlying C-function. The name can actually be everything but it is best-practice to have a probe at the entry of a function, called <i>entry</i> and <i>return</i>just before the function returns. A DTrace program have an probe identifier that matches a probe. Every identifier has a body that can contain initialization of variables or accumulation of data using a table. Also, every identifier can have a condition that is evaluated before the body is run. So we have the following structure:</p>
<blockquote><p>
&lt;probeprovider&gt;:&lt;providermodule&gt;:&lt;probefunc&gt;:&lt;probename&gt;<br />
/ &lt;condition&gt; /<br />
{<br />
    &lt;body&gt;<br />
}
</p></blockquote>
<p>As an example, we trace gc_collect_cylce function:</p>
<blockquote><p>
pid$target:php:gc_collect_cycle_function:entry<br />
{<br />
    trace(probename);<br />
}
</p></blockquote>
<p>Please notice that DTrace features a lot predefined variables, such as <i>probename</i>. Please refer to Sun&#8217;s Dtrace Guide to get a list of predefined variables.</p>
<p><em>PHP probes</em><br />
<strike>As PHP doesn&#8217;t provide own probes we have to use the underlying C-functions.</strike> PHP itself doesn&#8217;t provide own probes. There is ext/dtrace written by Wez Furlong which gives PHP some probes, but as we want to get a little bit deeper, we have to use the underlying C-functions. Userland programs are usually traced by the so-called pid-provider. It&#8217;s used to get all probes by a program with a given PID. Unlike userland programs, the kernel doesn&#8217;t have a pid, therefore kernel probes do not have a PID and can be identified easily be an unique name. As we sometimes do not know the PID of the php process that we want to trace, there is a variable called $target. If yo use it, $target will be automatically filled with the PID that was created when dtrace started the program. If I start a program with <i>dtrace -s test.d -c &#8216;php test.php&#8217;</i> the programm <i>php test.php</i> will be executed and the created PID will be assigned to the variable $target. DTrace also have an option just to display the available probes: Try <i>dtrace -ln &#8216;pid$target:php::entry&#8217; -c &#8216;php test.php&#8217;</i> to get all function entries called by PHP. You might from time to time a get a message that not enough memory was available to trace.  In those cases you just tried to capture too much probes. Therefore, I used the specific :::entry notation to just get function entries, which worked fine on my machine. So let&#8217;s take look at some interesting probes. If we just try to display all probes related to PHP with <i>dtrace -ln &#8216;pid$target:php::entry&#8217; -c &#8216;php test.php&#8217;</i>.<br />
The output might start with something like:</p>
<blockquote><p>
   ID   PROVIDER            MODULE                          FUNCTION NAME<br />
69608    pid2696               php                            _start entry<br />
69609    pid2696               php                             __fsr entry<br />
69610    pid2696               php                  _free_ereg_cache entry<br />
69611    pid2696               php                   zm_startup_ereg entry<br />
69612    pid2696               php                  zm_shutdown_ereg entry<br />
69613    pid2696               php                      zm_info_ereg entry<br />
69614    pid2696               php                          zif_ereg entry<br />
69615    pid2696               php                         zif_eregi entry<br />
69616    pid2696               php                  php_ereg_replace entry<br />
69617    pid2696               php                  zif_ereg_replace entry<br />
69618    pid2696               php                 zif_eregi_replace entry<br />
69619    pid2696               php                         zif_split entry<br />
69620    pid2696               php                        zif_spliti entry<br />
69621    pid2696               php                   zif_sql_regcase entry<br />
69622    pid2696               php                       php_regcomp entry<br />
69623    pid2696               php                             p_ere entry<br />
69624    pid2696               php                         p_ere_exp entry<br />
69625    pid2696               php                             p_str entry<br />
69626    pid2696               php                             p_bre entry<br />
69627    pid2696               php                         p_simp_re entry
</p></blockquote>
<p><strong>First example</strong><br />
So our first example will be that we want to trace the compilation time. The compile function is called compile_file in PHP. Therefore our probe identifier would be <i>pid$target:php:compile_file</i>. The PID provider will just get the probes for a specific probe, while the php probemodule will focus on all PHP related functions. We then select the compile_file function. Provided by the kernel, there are two probenames, calle <i>entry</i> and . Entry is called when the function is entered and return, when the function returns. So we need to trace <i>pid$target:php:compile_time:entry</i> and pid$target:php:compile_time:return. We know just have to get information how long the compilation takes. For that purpose DTrace defines a variable called <i>timestamp</i> that is accessible within the body of an DTrace identifier which holds the milliseconds from the beginning of a program. So know we catch our entry probe and set the time to a local value:</p>
<blockquote><p>
pid$target:php:compile_file:entry<br />
{<br />
   self->compile_start = timestamp;<br />
}
</p></blockquote>
<p>Note that the <i>self-></i> notation is used to get a per-thread variable. Therefore every thread would have it&#8217;s own value. It&#8217;s not particularly necessary for our PHP based example, but it is best-practice to use thread-local variables whenever possible.</p>
<p>We just need our return probe that calculates the real offset:</p>
<blockquote><p>
pid$target:php:compile_file:return<br />
{<br />
  printf(&#8220;Compile time: %dn&#8221;, timestamp &#8211; self->compile_start);<br />
}
</p></blockquote>
<p>This will output our actual compile time. Just put both identifiers and their bodies into &#8216;compiletime.d&#8217; and start dtrace with <i>dtrace -q -s compiletime.d -c &#8216;php test.php&#8217;</i> and it will display you the compilation time of test.php. Please notice that both self->compile_start and timestamp are integers. Dtrace in fact doesn&#8217;t have the notion of a float primitive, so you would not be able to output a float using printf.</p>
<p>The output might be similar to:</p>
<blockquote><p>
$ dtrace -q -s compiletime.d -c &#8216;php test.php&#8217;<br />
Compile Time: 99604
</p></blockquote>
<p><strong>Advcanced Example</strong><br />
Now let&#8217;s get a little bit deeper into the engine. We now want to trace when the garbage collector hit&#8217;s in and want to see the amount of freed refs. The garbage collector uses the function gc_collect_cycles.</p>
<blockquote><p>
pid$target:php:gc_collect_cycles:return<br />
{<br />
   printf(&#8220;%d refs freed&#8221;, arg1);<br />
}
</p></blockquote>
<p>Please notice the special arg1 variable. This always holds the return value in &#8220;return&#8221; probenames. In &#8220;entry&#8221;, arg0&#8230;argN will hold the function parameters.</p>
<p>For more detailed examples, see my next blog entry, which will contain more sophisticated DTrace scripts.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.experimentalworks.net/?flattrss_redirect&amp;id=83&amp;md5=ef5aa6a88ed45d55cd077dafa8bf0e84" title="Flattr" target="_blank"><img src="http://blog.experimentalworks.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.experimentalworks.net/2008/12/dtracing-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

