da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Copyright 2007 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Use is subject to license terms.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ident "%Z%%M% %I% %E% SMI"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw fields enclosed by brackets "[]" replaced with your own identifying
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw CDDL HEADER END
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright<h1><a name="Quick_Start_Guide_to_the_Java_DTrace_API_"></a>Quick Start
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw<h1><small><small>to the</small> Java DTrace API</small></h1>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw <li><a href="#Hello_World">"hello, world" Example</a></li>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw <li><a href="#Writing_a_Simple_Consumer">Writing a Simple Consumer</a></li>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb <li><a href="#Running_hello.d_Script">Running the <tt>hello.d</tt>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw <li><a href="#Target_Process">Target Process ID</a></li>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb <li><a href="#Closing_Consumers">Closing Consumers</a></li>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwTo demonstrate how to use the Java DTrace API, let's write a simple Java
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbprogram that runs a D script, in this case <tt>hello.d</tt> (prints
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb"hello, world" and exits). You will need root permission to use the
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbJava DTrace API (just as you do to use the <tt>dtrace(1M)</tt> command).
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbYou may want to eliminate this inconvenience by adding the following
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb<tt><i>user-name</i>::::defaultpriv=basic,dtrace_kernel,dtrace_proc</tt>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbhref=http://docs.sun.com/app/docs/doc/817-6223/6mlkidln0?a=view>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb<b>Security</b></a> chapter of the <i>Solaris Dynamic Tracing Guide</i>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbfor more information.
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego<h4><a name="Writing_a_Simple_Consumer"></a>Writing a Simple Consumer</h4>
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borregoCreating a DTrace <a
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borregohref="/api/org/opensolaris/os/dtrace/Consumer.html">consumer</a>
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego Consumer consumer = new LocalConsumer();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbBefore you can do anything with the consumer, you must first open it.
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbThen you simply compile and enable one or more D programs and run it:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw consumer.go(); // runs in a background thread
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwTo get the data generated by DTrace, you also need to add a <a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwhref="/api/org/opensolaris/os/dtrace/ConsumerListener.html">listener</a>:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw consumer.addConsumerListener(new ConsumerAdapter() {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw public void dataReceived(DataEvent e) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwHere is a simple example that runs a given D script:<br>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw<b>Java program (<a href="/examples/TestAPI.java">TestAPI.java</a>)</b>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw public class TestAPI {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw public static void
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw main(String[] args)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (args.length < 1) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw System.err.println("Usage: java TestAPI <script> [ macroargs... ]");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw File file = new File(args[0]);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw String[] macroArgs = new String[args.length - 1];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw System.arraycopy(args, 1, macroArgs, 0, (args.length - 1));</font>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Consumer consumer = new LocalConsumer();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw consumer.addConsumerListener(new ConsumerAdapter() {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw public void dataReceived(DataEvent e) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw try {</font>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw consumer.compile(file, macroArgs);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } catch (Exception e) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwCompile the test program as follows:
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego<h4><a name="Running_hello.d_Script"></a>Running the <tt>hello.d</tt> Script</h4>
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borregoNow we need a D script for the program to run. The following is a
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borregosimple example that prints "hello, world" and exits:<br>
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego<b>D script (<a href="/examples/hello.d">hello.d</a>)</b>
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego dtrace:::BEGIN
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego trace("hello, world");
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwRun as follows:<br>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw java -cp .:/usr/share/lib/java/dtrace.jar TestAPI hello.d
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwThe output should look like this:
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego enabledProbeDescription = dtrace:::BEGIN, flow = null, records =
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego ["hello, world", 0]]
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borregoThere is one record in the <a
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borregohref="/api/org/opensolaris/os/dtrace/ProbeData.html"><tt>ProbeData</tt></a>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwfor each action in the D script. The first record is generated by the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw<tt>trace()</tt> action. The second is generated by the <tt>exit()</tt>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwaction. For prettier output, you could change the <tt>ConsumerAdapter <a
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb href="/api/org/opensolaris/os/dtrace/ConsumerAdapter.html#dataReceived%28org.opensolaris.os.dtrace.DataEvent%29">dataReceived()</a></tt>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbimplementation as follows:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw consumer.addConsumerListener(new ConsumerAdapter() {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw public void dataReceived(DataEvent e) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ProbeData data = e.getProbeData();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw java.util.List < Record > records = data.getRecords();
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (Record r : records) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (r instanceof ExitRecord) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw });</font>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwThe example Java program can just as easily run a more complex script,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsuch as an aggregation:<br>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw<b>D script (<a href="/examples/syscall.d">syscall.d</a>)</b>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw syscall:::entry
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb / execname == $$1 /
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw @[probefunc] = count();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb profile:::tick-1sec
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw printa(@);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwThe above script uses the <tt>$$1</tt> macro variable as a placeholder
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbfor whatever executable you'd like to trace. See the <a
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbhref=http://docs.sun.com/app/docs/doc/817-6223/6mlkidliq?a=view><b>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbMacro Arguments</b></a> section of the <b>Scripting</b> chapter of the
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wright<i>Solaris Dynamic Tracing Guide</i>. Using two dollar signs (<tt>$$1</tt>)
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wrightinstead of one (<tt>$1</tt>) forces expansion of the macro variable to
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wrighttype string.<br>
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan WrightTo run the example Java program using the above D script, you need to
94fff7907278e4540aa7abee2b1b0ea71d36f7faAlan Wrightspecify an argument to the <tt>execname</tt> placeholder, such as
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb java -cp .:/usr/share/lib/java/dtrace.jar TestAPI syscall.d java
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbA data record generated by the <tt>printa()</tt> action is printed to
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbthe console once every second. It contains counts of system calls by
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbfunction name made by java. No record is generated by the
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbIf you omit the argument to the <tt>execname</tt> placeholder, the
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbprogram fails to compile and the API throws the following exception:
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb org.opensolaris.os.dtrace.DTraceException: failed to compile script
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb syscall.d: line 2: macro argument $$1 is not defined
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb at org.opensolaris.os.dtrace.LocalConsumer._compileFile(Native Method)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb at org.opensolaris.os.dtrace.LocalConsumer.compile(LocalConsumer.java:342)
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbA DTrace script may have more than one aggregation. In that case, each
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbaggregation needs a distinct name:<br>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb<b>D script (<a href="/examples/intrstat.d">intrstat.d</a>)</b>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb sdt:::interrupt-start
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb self->ts = vtimestamp;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb sdt:::interrupt-complete
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw / self->ts && arg0 /
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw this->devi = (struct dev_info *)arg0;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb @counts[stringof(`devnamesp[this->devi->devi_major].dn_name),
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb this->devi->devi_instance, cpu] = count();
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb @times[stringof(`devnamesp[this->devi->devi_major].dn_name),
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw this->devi->devi_instance, cpu] = sum(vtimestamp - self->ts);
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb self->ts = 0;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbThe <tt>@counts</tt> and <tt>@times</tt> aggregations both accumulate
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbvalues for each unique combination of device name, device instance, and
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbCPU (a three-element tuple). In this example we drop the <tt>tick</tt>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbprobe to demonstrate a more convenient way to get aggregation data
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borregohref="/api/org/opensolaris/os/dtrace/Consumer.html#getAggregate%28%29">
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego<tt>getAggregate()</tt></a> method allows us to get a read-consistent
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbsnapshot of all aggregations at once on a programmatic interval.<br>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb<b>Java program (<a href="/examples/TestAPI2.java">TestAPI2.java</a>)</b>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw consumer.compile(file, macroArgs);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Aggregate a;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb Thread.sleep(1000); // 1 second
f9a15d2c042edba37d61b071eb7ea274eb2754d1jose borrego if (!a.asMap().isEmpty()) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } catch (Exception e) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ...</font>
faa1795a28a5c712eed6d0a3f84d98c368a316c6jbCompile and run:
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb java -cp .:/usr/share/lib/java/dtrace.jar TestAPI2 intrstat.d
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwTry removing the <tt>tick</tt> probe from the <tt>syscall.d</tt> example
faa1795a28a5c712eed6d0a3f84d98c368a316c6jband running it again with the above modification (<tt>TestAPI2</tt>).<br>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwBy default, the requested aggregate includes every aggregation and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwaccumulates running totals. To display values per time interval
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb(instead of running totals), clear the aggregations each time you call
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw<tt>getAggregate()</tt>. Clearing an aggregation resets all counts to
a = consumer.getAggregate(null, null); // included, cleared
href="/api/org/opensolaris/os/dtrace/ConsumerAdapter.html#dataDropped%28org.opensolaris.os.dtrace.DropEvent%29">
href="/api/org/opensolaris/os/dtrace/Aggregation.html">
Aggregate a = consumer.getAggregate();
Aggregation total = a.getAggregation("total");
href="/api/org/opensolaris/os/dtrace/Consumer.html#go%28%29">
href="/api/org/opensolaris/os/dtrace/Consumer.html#createProcess%28java.lang.String%29">
consumer.compile(file);
consumer.go();
href="/api/org/opensolaris/os/dtrace/ConsumerAdapter.html#processStateChanged%28org.opensolaris.os.dtrace.ProcessEvent%29">
consumer.addConsumerListener(new ConsumerAdapter() {
Aggregate a = consumer.getAggregate();
for (Aggregation agg : a.asMap().values()) {
for (AggregationRecord rec : agg.asMap().values()) {
rec.getValue());
System.exit(1);
consumer.addConsumerListener(new ConsumerAdapter() {
Consumer consumer = (Consumer)e.getSource();
This releases the resources held by the consumer in all cases, i.e.
Aggregate a = consumer.getAggregate();
href="/api/org/opensolaris/os/dtrace/Consumer.html#getAggregate%28%29">
href="/api/org/opensolaris/os/dtrace/Consumer.html#setOption%28java.lang.String%29">
href="/api/org/opensolaris/os/dtrace/Option.html#cpp">