This post will be a little bit wider than the title implies; it will be about using the JMC core JFR APIs in general. That said, one of the things we will be using it for, towards the end, will be to run the JMC 6 automated analysis headless.
This article will also cheat by talking about JMC 6.1, so that anyone reading this article will have relevant information for JMC 6.1 as well.
The JAR-files Needed
To run these examples you will need to have the following Jar-files from the JDK_HOME/lib/missioncontrol/plugins folder on your classpath:
Prefix |
Version needed |
Comment |
---|---|---|
com.oracle.jmc.common |
>= 6.0.0 |
Classes common to JMC, such as stacktrace definitions, the content type system (quantities/ units of measurement) etc. |
com.oracle.jmc.flightrecorder |
>= 6.0.0 |
The Java Flight Recorder parser, classes for extracting information from a recording. |
com.oracle.jmc.flightrecorder.rules |
>= 6.0.0 |
The core definitions and classes for automated analysis of recordings. |
com.oracle.jmc.flightrecorder.rules.jdk |
>= 6.0.0 |
Contains the rules for the JDK (such as rules for synchronization trouble, long lasting VM operations and much more). |
com.oracle.jmc.flightrecorder.ext.wls.parser |
>= 6.0.0 |
(Optional, only relevant for users wanting WebLogic Server extensions) |
com.oracle.jmc.flightrecorder.ext.wls.rules |
>= 6.0.0 |
(Optional, only relevant for users wanting WebLogic Server extensions) |
Loading a Recording
Loading a recording is done through a call to the JfrLoaderToolkit#loadEvents(). It takes a file argument, and returns an IItemCollection. The IItemCollection can be thought of as a collection of events.
The IItemCollection supports operations like filter(), and getAggregate(). Used correctly, you should rarely need to rely on external iteration.
Here is an example which loads a recording, and calculates the standard deviation for the java monitor enter events in a recording:
import java.io.File; import com.oracle.jmc.common.IDisplayable; import com.oracle.jmc.common.item.Aggregators; import com.oracle.jmc.common.item.IItemCollection; import com.oracle.jmc.common.item.ItemFilters; import com.oracle.jmc.common.unit.IQuantity; import com.oracle.jmc.flightrecorder.JfrAttributes; import com.oracle.jmc.flightrecorder.JfrLoaderToolkit; import com.oracle.jmc.flightrecorder.jdk.JdkTypeIDs; /** * Finds out the standard deviation for the java monitor enter events. */ public class LoadRecording { public static void main(String[] args) throws Exception { DemoToolkit.verifyFirstFileArgument(RunRulesOnFile.class, args); IItemCollection events = JfrLoaderToolkit.loadEvents(new File(args[0])); IQuantity aggregate = events.apply(ItemFilters.type(JdkTypeIDs.MONITOR_ENTER)) .getAggregate(Aggregators.stddev(JfrAttributes.DURATION)); System.out.println("The standard deviation for the Java monitor enter events was " + aggregate.displayUsing(IDisplayable.AUTO)); } }
Note the stream-like syntax. The JMC libraries work well together with streams, but are compiled on JDK 7, and can run on a JDK 7 compliant runtime. Also note that some class names start with Jfr whilst others class names start with Jdk. The difference being that some concepts, like DURATION, are intrinsic to JFR, whilst other are defined in terms of the Java JDK classes or runtime, like MONITOR_ENTER.
The JMC core libraries provide common statistical aggregators, and accessors for common attributes. Should you feel something is missing, it is easy to add to the built-in operations.
Also note the IQuantity returned by the aggregator. The built-in system for handling quantities and units of measurement makes it easy to calculate with, and display, quantities in different units of measurement.
Performing an Automated Analysis Headless
It is also easy to run the JMC rules headless using these core libraries. Simply evaluate the rules against the IItemCollection. Here is a simple example iterating through the rules and evaluating them one by one:
import java.io.File; import java.util.concurrent.RunnableFuture; import com.oracle.example.jmc6jfr.rules.util.DemoToolkit; import com.oracle.jmc.common.item.IItemCollection; import com.oracle.jmc.common.util.IPreferenceValueProvider; import com.oracle.jmc.flightrecorder.JfrLoaderToolkit; import com.oracle.jmc.flightrecorder.rules.IRule; import com.oracle.jmc.flightrecorder.rules.Result; import com.oracle.jmc.flightrecorder.rules.RuleRegistry; public class RunRulesOnFileSimple { public static void main(String[] args) throws Exception { File recording = DemoToolkit.verifyRecordingArgument(RunRulesOnFileSimple.class, args); IItemCollection events = JfrLoaderToolkit.loadEvents(recording); for (IRule rule : RuleRegistry.getRules()) { RunnableFuture<Result> future = rule.evaluate(events, IPreferenceValueProvider.DEFAULT_VALUES); future.run(); Result result = future.get(); if (result.getScore() > 50) { System.out.println(String.format("[Score: %3.0f] Rule ID: %s, Rule name: %s, Short description: %s", result.getScore(), result.getRule().getId(), result.getRule().getName(), result.getShortDescription())); } } } }
That said, if you are not constrained to have to run on JDK 7, you can always run the rules in parallel, for example by employing parallel streams:
import java.io.File; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.RunnableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; import com.oracle.jmc.common.item.IItemCollection; import com.oracle.jmc.common.util.IPreferenceValueProvider; import com.oracle.jmc.flightrecorder.JfrLoaderToolkit; import com.oracle.jmc.flightrecorder.rules.IRule; import com.oracle.jmc.flightrecorder.rules.Result; import com.oracle.jmc.flightrecorder.rules.RuleRegistry; /** * Runs the rules on the events in the specified file in parallel, then prints * them in order of descending score. */ public class RunRulesOnFile { private final static Executor EXECUTOR = Executors .newFixedThreadPool(Runtime.getRuntime().availableProcessors() - 1); private static int limit; public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println( "Usage: RunRulesOnFile <recording file> [<limit>]\n\tThe recording file must be a flight recording from JDK 7 or above. The limit, if set, will only report rules triggered with a score higher or equal than the limit."); System.exit(2); } IItemCollection events = JfrLoaderToolkit.loadEvents(new File(args[0])); if (args.length > 1) { limit = Integer.parseInt(args[1]); } Stream<RunnableFuture<Result>> resultFutures = RuleRegistry.getRules().stream() .map((IRule r) -> evaluate(r, events)); List<Result> results = resultFutures.parallel().map((RunnableFuture<Result> runnable) -> get(runnable)) .collect(Collectors.toList()); results.sort((Result r1, Result r2) -> Double.compare(r2.getScore(), r1.getScore())); results.stream().forEach(RunRulesOnFile::printResult); } public static RunnableFuture<Result> evaluate(IRule rule, IItemCollection events) { RunnableFuture<Result> evaluation = rule.evaluate(events, IPreferenceValueProvider.DEFAULT_VALUES); EXECUTOR.execute(evaluation); return evaluation; } public static Result get(RunnableFuture<Result> resultFuture) { try { return resultFuture.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return null; } private static void printResult(Result result) { if (result.getScore() >= limit) { System.out.printf("(%.0f) [%s]: %s\nDetails:\n%s\n============<End of Result>============\n", result.getScore(), result.getRule().getId(), result.getShortDescription(), result.getLongDescription() == null ? "<no description>" : result.getLongDescription()); } } }
If you do not need that kind of control, there is a class available for performing automated analysis included with the JMC core library. To run an automatic analysis, simply run the class:
com.oracle.jmc.flightrecorder.rules.report.JfrRulesReport <files> –format <format> –min <severity>
…where files is one or more recordings, and <format> is and <min> is minimum severity to report [ok | info | warning]. In JMC 6.1.0 there is one additional class for generating the file, which produces an HTML in a format and style very similar to the looks of the JMC 6.0.0 Automated Analysis page:
com.oracle.jmc.flightrecorder.rules.report.html.JfrHtmlRulesReport <file> [<outputfile>]
…where <file> is the (single) recording to analyze, and where the optional <outputfile> is where the resulting HTML should be written. If no outputfile is specified, the result of the analysis will be written on stdout.
Adding Custom Rules
It’s easy to add you own heuristics. Do you find yourself with thousands of recordings, and would like to add a Bayesiean network to do some machine learning? The rules are all pure Java, so you can pretty much do whatever you want in a rule.
The easiest way to get started writing your own custom rules is to get yourself an Eclipse Oxygen or later, and install JMC and the experimental PDE plug-in into your Eclipse.
- Install Eclipse Oxygen or later.
- Got to the Mission Control homepage, and find the Eclipse Update site.
- Follow the instructions to install the Mission Control plug-ins.
- Got to the Mission Control homepage, and find the Experimental Update site.
- Install the PDE plug-in.
Here is a cheat sheet for the eclipse update sites for the JMC 6.0.0 release:
http://download.oracle.com/technology/products/missioncontrol/updatesites/base/6.0.0/eclipse/
http://download.oracle.com/technology/products/missioncontrol/updatesites/supported/6.0.0/eclipse/
(JMC 6.1.0 will be released according to the same URL pattern.)
To start building your rule, press ctrl-n (or click the File | New | Other… menu) to bring up the New wizard.
Select Plug-in Project and hit Next. Name your rule project something exciting.
Unclick that this plug-in will make contributions to the UI and hit Next.
Next select the Simple JFR Rule Wizard and click Finish (or Next, if you really wish to do some further customizations).
You will now have a new project in your workspace, containing an example rule. If you have compilation errors, you need to set JMC to be your target platform (see the next section). You can either just export your rule, put it on the class path with the other core libraries, or you can try out your rule by running JMC from within Eclipse with your new rule.
Starting JMC from within Eclipse with Workspace Plug-ins
Running JMC from within Eclipse with any plug-ins you are currently developing is a simple matter of setting the plug-in development target platform to your JMC installation, and launching that platform with your workspace plug-ins.
First set JMC to be your target platform.
- Go to Preferences in the main menu (Window | Preferences on Windows).
- Find Target Platform by typing Tar in the filter box:
- Click Add… and type JMC 6 as name.
- Press Add… and select Installation. Hit Next.
- Browse to the JMC 6 installation directory (JDK_9_HOME/lib/missioncontrol), and hit Ok.
- Hit Finish, and Finish. You should now see your new platform.
- Select the new JMC 6 platform, and hit Apply and Close to activate it.
Everything should now compile cleanly. Next step is to run JMC with your new rule.
- Context click on your project and select Run as Eclipse Application
- This should normally be it. If you for some reason run into trouble, go to Run Configurations, and make sure that your launcher is using the com.oracle.jmc.rcp.application.product.
You should now be able to see your rule in action. If you have not changed the rule code, try setting the environment variable in the launcher to various values between 0 and 100 and see what happens when you run the rule in JMC:
Exporting a Plug-in
You can export your rule by context-clicking on the project and selecting Export. In the Export wizard, select Deployable plug-ins and Fragments.
Click Next, select a folder to export the plug-in to and Finish. The resulting jar can either be put on the class path to be included in the headless analysis, or put in the dropins folder of any Java Mission Control installation where you would want the rule to be available.
Summary
This blog described how to:
- Get started using the JMC core libraries to read Java Flight Recordings (JDK 7, 8 and 9)
- Get started doing headless analysis of Java Flight Recordings (JDK 7, 8 and 9)
- Get started creating custom rules for analyzing Java Flight Recordings
Hope it helps someone!