Dtrace Topics Java
Dtrace Topics Java
d
<- java/lang/Thread.sleep
-> Greeting.greet
-> java/io/PrintStream.println
-> java/io/PrintStream.print
-> java/io/PrintStream.write
-> java/io/PrintStream.ensureOpen
<- java/io/PrintStream.ensureOpen
-> java/io/Writer.write
-> java/io/BufferedWriter.write
-> java/io/BufferedWriter.ensureO
<- java/io/BufferedWriter.ensureO
-> java/io/BufferedWriter.min
<- java/io/BufferedWriter.min
DTrace Topics:
Java
-> java/lang/String.getChars
-> java/lang/System.arraycopy
<- java/lang/System.arraycopy
<- java/lang/String.getChars
<- java/io/BufferedWriter.write
<- java/io/Writer.write
-> java/io/BufferedWriter.flushBuff
Brendan Gregg
Sun Microsystems
March 2007
-> java/io/BufferedWriter.ensureOp
<- java/io/BufferedWriter.ensureOp
-> java/io/OutputStreamWriter.writ
-> sun/nio/cs/StreamEncoder.write
-> sun/nio/cs/StreamEncoder.ensu
<- sun/nio/cs/StreamEncoder.ensu
-> sun/nio/cs/StreamEncoder.impl
-> java/nio/CharBuffer.wrap
DTrace Recap
Java and DTrace
The hotspot Provider
hotspot Examples
Resources
DTrace Recap
A general understanding of DTrace is assumed
knowledge for this presentation.
If you are new to DTrace, try starting with the
presentation called DTrace Topics: Intro.
The next two slides are a short summary of DTrace,
if needed.
What is DTrace
DTrace is a dynamic troubleshooting and analysis
tool first introduced in the Solaris 10 and
OpenSolaris operating systems.
DTrace is many things, in particular:
> A tool, /usr/sbin/dtrace
> A programming language interpreter, the D language
> An instrumentation framework
What is DTrace
DTrace can observe the entire software stack from
one tool. It is like a combination of,
> truss, sotruss, apptrace, mdb, lockstat, prex/tnf*, C, awk
jstack()
This can be used with:
> The profile provider, to sample frequent stack traces.
jstack() example
The following shows why Java caused a read():
# dtrace -n 'syscall::read:entry /execname == "java"/ { jstack(); }'
[...]
syscall
0 75943
read:entry
libc.so.1`_read+0x7
libX11.so.4`_X11TransSocketRead+0x25
libX11.so.4`_X11TransRead+0x17
user
libX11.so.4`_XRead+0x58
libraries libX11.so.4`_XReply+0xcd
libX11.so.4`XGetInputFocus+0x68
libmawt.so`Java_sun_awt_X11_XlibWrapper_XGetInputFocus+0x27
sun/awt/X11/XlibWrapper.XGetInputFocus(J)J
sun/awt/X11/XBaseWindow.xGetInputFocus()J
Java sun/awt/X11/XWindowPeer.handleFocusEvent(J)V
code sun/awt/X11/XDecoratedPeer.handleFocusEvent(J)V
sun/awt/X11/XFocusProxyWindow.handleFocusEvent(J)V
sun/awt/X11/XFocusProxyWindow.dispatchEvent(Lsun/awt/X11/IXAnyEve
sun/awt/X11/XBaseWindow.dispatchToWindow(Lsun/awt/X11/IXAnyEvent;
8
djvm/dvm Provider
If possible, move the application to JDK 6.0 and use
the integrated hotspot provider.
If you are stuck on JDK 1.4.2 or 5.0, you can try the
djvm/dvm prototype provider.
They require command line configuration and the
application to be restarted.
The provider can be downloaded from,
> https://solaris10-dtrace-vm-agents.dev.java.net
A class loaded
A class unloaded
A method begins
A method completed
An object was allocated
System wide GC begins
System wide GC ended
10
hotspot Example #1
The hotspot provider will be demonstrated by
tracing a simple Java program.
The following Greeting.java code may look familiar,
many Java tutorials begin with something similar:
Greeting class
$ cat Greeting.java
public class Greeting {
public void greet() {
System.out.println("Hello DTrace!");
}
}
12
hotspot Example #1
Now the test harness:
TestGreeting class
$ cat TestGreeting.java
public class TestGreeting {
public static void main(String[] args) {
Greeting hello = new Greeting();
while (true) {
hello.greet(); call greet method
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
every
}
second
}
}
}
13
hotspot Example #1
Compiling, executing:
$ javac TestGreeting.java
$ java TestGreeting
Hello DTrace!
Hello DTrace!
Hello DTrace!
slowly scrolling
Hello DTrace!
^C
output
14
Ah-ha!
sun/nio/cs/StreamEncoder.flushBuffer()V
java/io/OutputStreamWriter.flushBuffer()V
java/io/PrintStream.newLine()V
java/io/PrintStream.println(Ljava/lang/String;)V
Greeting.greet()V
TestGreeting.main([Ljava/lang/String;)V
StubRout
libjvm.so`__1cJJavaCallsLcall_helper6FpnJJavaValue_pnMmethodHan
libjvm.so`__1cCosUos_exception_wrapper6FpFpnJJavaValue_pnMmetho
libjvm.so`__1cJJavaCallsEcall6FpnJJavaValue_nMmethodHandle_pnRJ
libjvm.so`__1cRjni_invoke_static6FpnHJNIEnv__pnJJavaValue_pnI_j
libjvm.so`jni_CallStaticVoidMethod+0x15d
java`JavaMain+0xd30
libc.so.1`_thr_setup+0x52
libc.so.1`_lwp_start
16
8
8
8
8
8
8
16
16
19
20
8
8
8
8
8
8
8
16
16
496
496
20
30
24
3
3
3
3
3
3
[...]
java/nio/charset/CoderResult.isUnderflow
java/nio/Buffer.position
java/nio/CharBuffer.arrayOffset
java/nio/ByteBuffer.arrayOffset
12
18
18
24
25
# cat jagg.d
#!/usr/sbin/dtrace -qs
dtrace:::BEGIN
{
printf("Tracing... Hit Ctrl-C to end.\n");
}
hotspot*:::method-entry
{
this->class = (char *) copyin(arg1, arg2 + 1);
this->class[arg2] = '\0';
this->method = (char *) copyin(arg3, arg4 + 1);
this->method[arg4] = '\0';
@calls[stringof(this->class), stringof(this->method)] = count();
}
dtrace:::END
{
printa("%48s.%-24s %@4d\n", @calls);
}
26
Java
29
30
overlapping methods
multiple Java threads executing concurrently
Java threads context switching off the CPUs
DTrace overheads at nanosecond resolutions
recursive methods?
31
32
Resources
To learn more about DTrace and Java,
http://java.sun.com/javase/6/docs/technotes/guides/vm/dtrace.html
dtrace:::END
Brendan Gregg
brendan@sun.com
34