{"id":446,"date":"2013-11-11T22:10:24","date_gmt":"2013-11-11T21:10:24","guid":{"rendered":"http:\/\/hirt.se\/blog\/?p=446"},"modified":"2014-07-14T17:15:54","modified_gmt":"2014-07-14T16:15:54","slug":"using-the-flight-recorder-parsers","status":"publish","type":"post","link":"https:\/\/hirt.se\/blog\/?p=446","title":{"rendered":"Using the Flight Recorder Parsers"},"content":{"rendered":"<p><em>This blog post is the second in a series of posts on using unsupported functionality in the Oracle JDK and\/or Java Mission Control.<\/em><\/p>\n<p><em><\/em><\/p>\n<p>Just as there is no supported way to add custom events to the Java Flight Recorder (yet), there is even less in terms of programmatic support to read recordings. There are however two different <em>unsupported<\/em> parsers available. Both are POJO APIs, and except for supportability \/ future API breakage issues, there is in practice nothing stopping you from using either one to read your recordings.<\/p>\n<p>Disclaimer:<\/p>\n<blockquote>\n<p><span style=\"background-color: #ffffff\">The following blog entry will describe <strong><span style=\"color: #ff0000\">UNSUPPORTED <\/span><\/strong>functionality. This means that relying on the described APIs or functionality may <span style=\"color: #ff0000\"><strong>BREAK <\/strong><\/span><span style=\"color: #000000\">your code\/plugin with any given update of the JDK and\/or Mission Control. <\/span><\/span><\/p>\n<\/blockquote>\n<p><strong><em>I strongly recommend against using these APIs in production code. <\/em><\/strong>There will be supported APIs for reading reading recordings eventually and they may be quite different. Also note that older versions of the parsers are not guaranteed to be forward compatible with newer versions of the binary format. In short, this is <strong><font color=\"#ff0000\">NOT <\/font><\/strong>supported and <strong><font color=\"#ff0000\">WILL CHANGE<\/font><\/strong>!<\/p>\n<p>That said, it can sometimes be quite useful to have programmatic access to flight recorder data. One could, for example, do automatic filtering and\/or reporting on recordings, and then use JMC to do detailed analysis on the ones that are flagged as interesting. <\/p>\n<h2>The Reference Parser<\/h2>\n<p>The reference parser shipped with the Oracle JDK can be likened to a SAX XML parser. It allows you to iterate through the recording, uses little memory, is somewhat limited, and provides a very simple Java model. If your needs are simple, this may be the perfect parser to use. It is available by default in JDK 7u4 and later. The parser API is located in the <font face=\"Courier New\">JDK_HOME\/jre\/lib\/jfr.jar <\/font>file.<\/p>\n<p>Here is an example snippet that iterates through a recording and prints out the total event count:<\/p>\n<p> <code>   <\/p>\n<p>import java.io.File;      <br \/>import java.io.IOException;       <br \/>import java.util.Iterator;<\/p>\n<p>import oracle.jrockit.jfr.parser.*;<\/p>\n<p>public class JDKParserTest {      <br \/>&#160;&#160;&#160; @SuppressWarnings(&quot;deprecation&quot;)       <br \/>&#160;&#160;&#160; public static void main(String[] args) throws IOException {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; File recordingFile = new File(&quot;C:\\\\demo\\\\wldf.jfr&quot;);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Parser parser = new Parser(recordingFile);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; int count = 0;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Iterator&lt;ChunkParser&gt; chunkIter = parser.iterator();       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; while (chunkIter.hasNext()) {&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ChunkParser chunkParser = chunkIter.next();       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (FLREvent event : chunkParser) {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; count++;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/System.out.println(event.toString());       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; parser.close();       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(&quot;Found &quot; + count + &quot; events&quot;);       <br \/>&#160;&#160;&#160; }       <br \/>}&#160;&#160;&#160; <\/p>\n<p> <\/code>  <\/p>\n<p><font face=\"Courier New\"><\/font><\/p>\n<p>I will not spend too much time on this parser.<\/p>\n<p>Strengths:<\/p>\n<ul>\n<li>Uses little memory <\/li>\n<li>Included in the Hotspot JDK (&gt;=7u4) <\/li>\n<\/ul>\n<p>Weaknesses:<\/p>\n<ul>\n<li>Slow <\/li>\n<li>Simple model\/API <\/li>\n<\/ul>\n<h2>The JMC Parser<\/h2>\n<p>If the reference parser is the SAX XML parser equivalent, then the JMC parser is the DOM equivalent. The jar for the parser is easily nicked from the JMC distribution, either from the update site or from the JDK installation. In the JDK, you\u2019ll find the jar containing the JMC flight recorder parser at <font face=\"Courier New\">JDK_HOME\/lib\/missioncontrol\/plugins\/com.jrockit.mc.flightrecorder_&lt;version&gt;.jar<\/font>.<\/p>\n<p>You will also need to add the <font face=\"Courier New\">JDK_HOME\/lib\/missioncontrol\/plugins\/com.jrockit.mc.common_&lt;version&gt;.jar<\/font> to your path.<\/p>\n<p>Here is the equivalent event counting example using the JMC parser:    <\/p>\n<p> <code>   <\/p>\n<p>import java.io.File;<\/p>\n<p>import com.jrockit.mc.flightrecorder.FlightRecording;      <br \/>import com.jrockit.mc.flightrecorder.FlightRecordingLoader;       <br \/>import com.jrockit.mc.flightrecorder.spi.IEvent;       <br \/>import com.jrockit.mc.flightrecorder.spi.IView;<\/p>\n<p>public class FlightRecorderParserTest {      <br \/>&#160;&#160;&#160; public static void main(String[] args) throws ClassNotFoundException {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; FlightRecording recording = FlightRecordingLoader.loadFile(new File(&quot;C:\\\\demo\\\\wldf.jfr&quot;));&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; IView view = recording.createView();<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; int count = 0;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (IEvent event : view) {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; count++;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(&quot;Fount &quot; + count + &quot; events&quot;);       <br \/>&#160;&#160;&#160; }       <br \/>}<\/p>\n<p> <\/code>  <\/p>\n<p>Strengths:<\/p>\n<ul>\n<li>Fast <\/li>\n<li>The jar can be used with any JDK (&gt;=6)<\/li>\n<li>Rich API <\/li>\n<li>Good for complex analysis scenarios<\/li>\n<\/ul>\n<p>Weaknesses:<\/p>\n<ul>\n<li>Builds a Java model of the recording \u2013 uses more memory <\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>This blog post lists two different <strong><font color=\"#ff0000\">UNSUPPORTED <\/font><\/strong>parsers that can be used to read java flight recordings programmatically from Java. A very simple example showing how to iterate through the events in a recording was shown for both of the parsers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is the second in a series of posts on using unsupported functionality in the Oracle JDK and\/or Java Mission Control. Just as there is no supported way to add custom events to the Java Flight Recorder (yet), there is even less in terms of programmatic support to read recordings. There are however [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-java-mission-control-fair-dj"],"_links":{"self":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=446"}],"version-history":[{"count":4,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}