<?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; opensolaris</title>
	<atom:link href="http://blog.experimentalworks.net/tag/opensolaris/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>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>
		<item>
		<title>Compiling PHP under OpenSolaris</title>
		<link>http://blog.experimentalworks.net/2008/09/compiling-php-under-opensolaris/</link>
		<comments>http://blog.experimentalworks.net/2008/09/compiling-php-under-opensolaris/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 01:17:37 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[opensolaris]]></category>

		<guid isPermaLink="false">http://wp.experimentalworks.net/?p=75</guid>
		<description><![CDATA[As I switched my main system recently from Linux to OpenSolaris I compiled PHP. Quiet obvious things are a little bit different on Solaris. The usual ./buildconf &#038;& ./configure &#038;& make install doesn't work anymore. The good news: It's not much harder. Let's start to get the prequisites:


~$ su
~# pkg install SUNWgm4 SUNWaconf SUNWgmake ...]]></description>
			<content:encoded><![CDATA[<p>As I switched my main system recently from Linux to OpenSolaris I compiled PHP. Quiet obvious things are a little bit different on Solaris. The usual <em>./buildconf &#038;&#038; ./configure &#038;&#038; make install</em> doesn&#8217;t work anymore. The good news: It&#8217;s not much harder. Let&#8217;s start to get the prequisites:<br />
<span id="more-75"></span></p>
<blockquote><p>
~$ su<br />
~# pkg install SUNWgm4 SUNWaconf SUNWgmake SUNWgcc SUNWbison
</p></blockquote>
<p>Finally we have to get the latest re2c sources. Re2c is needed to regenerate PHPs parsers but not yet available in OpenSolaris official package repository. So go to <a href="http://sourceforge.net/projects/re2c" target="_blank">re2c at sourceforge</a> and download the most recent sources.</p>
<blockquote><p>
~$ gtar xzvf re2c-0.13.5.tar.gz<br />
~$ cd re2c-0.13.5/<br />
~$ ./configure<br />
~$ gmake
</p></blockquote>
<p>The other necessary GNU tools should be installed, otherwise try to install SUNWgnu-coreutils. So let&#8217;s get our CVS snapshot and compile it:</p>
<blockquote><p>
~$ cd php-src-5.3/<br />
~$ MAKE=gmake ./buildconf<br />
~$ ./configure RE2C=&#8221;~/dev/c/re2c-0.13.5/re2c&#8221;<br />
~$ gmake<br />
~$ su<br />
~# gmake install
</p></blockquote>
<p>As we have to use GNUs make and not Solaris make implementation we have to set the <b>MAKE</b> environment variable used by buildconf. Since re2c is not in our path (we didn&#8217;t installed it), we pass the location of the <i>re2c</i> executable to the configure script with the <b>RE2C</b> env variable. After successfully executing gmake install, we have our PHP 5.3 installed.</p>
<blockquote><p>
~$ php -v<br />
PHP 5.3.0alpha3-dev (cli) (built: Sep 30 2008 02:37:10)<br />
Copyright (c) 1997-2008 The PHP Group<br />
Zend Engine v2.3.0, Copyright (c) 1998-2008 Zend Technologies
</p></blockquote>
<p>Please notice that PHP&#8217;s default configuration is to install the binaries in <i>/usr/local/bin</i>. This directory is usually not in $PATH, so update your PATH.</p>
<p><strong>UPDATE</strong><br />
As pointed out in the <a href="http://opensolaris.org/jive/thread.jspa?threadID=76826&#038;tstart=0" target="_blank">OpenSolaris Forums</a> GNU make is just required for the buildconf step, so you should be able to use Solaris make implementation if you don&#8217;t compile from CVS sources.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.experimentalworks.net/?flattrss_redirect&amp;id=75&amp;md5=02323d3519e054c42d2ed01afaa8f293" 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/09/compiling-php-under-opensolaris/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

