Jun 25, 2015

Profiling Java in Production on Oracle Linux and Mac OS X with DTRACE

Profiling Java in Production


DTRACE on Oracle Linux

Today at the JavaOne Latin America 2015 I heard an interesting talk about Oracle Linux. I is basically another Red Hat Linux Clone. But it has one interesting tool on board which can save your life if you run into problems in a mission critical Java application.

This tool is called DTRACE. It was developed from Sun Microsystems for Solaris 10 and gives you access to thousands of instrumentation hooks called probes. This tool is non invasive for your application and has a very low performance overhead. So you can use this tool in production.

Since you can write custom probes on application level things get interesting. In the hotspot virtual machine there is a DTRACE provider integrated since JDK 6 and ready for use. Since DTRACE works on an OS level it has not idea of the virtual machine code running inside the native JVM wrapper. But the DTRACE provider inside the JVM gives DTRACE information about the Java code like the current Java call stack or method enter/leave information. I found a good slideshow from Gregg Brendan which explains things in more detail. Have a look here: http://www.brendangregg.com/Slides/dtrace_topics_java.pdf

Oracle has ported DTRACE to Oracle Linux and you can use DTRACE in production of your Java application. What is even better: DTRACE was also ported to Mac OS X and runs on every Mac out of the box. This helps a lot to build your own D scripts for profiling or debugging purposes.
http://dtrace.org/blogs/brendan/2011/10/10/top-10-dtrace-scripts-for-mac-os-x/

After the session I tried this with NetBeans. And it worked in one minute. The results are shown in the video. I am really impressed.

https://plus.google.com/u/0/117617685924196701039/posts/PUpEk59EBd2?pid=6163969210861427522&oid=117617685924196701039


As you have seen in the video there is only a short script necessary to count each method call and remember the name of the called method. Since method calling scripts could have impact on your performance you have to enable the method call probe by starting the JVM with an special -X parameter.


Why the Java community needs this Tool

There can be problems when your server will be slow and normal tools like stack-traces or heap dumps show nothing abnormal. Then you need any information what your program is really doing. A profiler can help you. But the problem here: In most enterprises you will not get access to the root level of your production servers. But sending a small DTRACE script to the administrator team for execution will work fine since this tool does not need any special system configuration like open ports or the installation of profiler software.
It is much simpler and easy to use than the JRocket based Flightrecorder which is also an option when you try to profile production systems.

Since the JVM has support for DTRACE it would be great to have this functionality available on every Linux Platform.