Skip to content

Latest commit

 

History

History

native-jfr-demo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Recording JFR events with Native Image

This demo shows how to create a custom JDK Flight Recorder (JFR) event and use it in a native executable. JFR is a tool, built into the JVM, to collect diagnostic and profiling data in a running Java application. GraalVM Native Image supports JFR events and users can use the jdk.jfr.Event API with a similar experience to using JFR in the JVM.

Follow the steps below to record JFR events when running a native executable, enable JFR support, and JFR recording.

Note: JFR event recording is not yet supported on GraalVM JDK for Windows.

Preparation

  1. Download and install the latest GraalVM JDK using SDKMAN!.
    sdk install java 21.0.1-graal
  2. Download the demos repository or clone it as follows:
    git clone https://github.com/graalvm/graalvm-demos
    
  3. Navigate into the native-jfr-demo directory:
    cd graalvm-demos/native-jfr-demo

Build and Run a Native Executable with JFR

  1. Compile the Java file using the GraalVM JDK:

    $JAVA_HOME/bin/javac JFRDemo.java

    It creates two class files: JFRDemo$HelloWorldEvent.class and JFRDemo.class.

  2. Build a native executable with VM inspection enabled:

    $JAVA_HOME/bin/native-image --enable-monitoring=jfr JFRDemo

    The --enable-monitoring=jfr option enables features such as JFR that can be used to inspect the VM.

  3. Run the executable, emitting standard JFR events, and start recording:

    ./jfrdemo -XX:+FlightRecorder -XX:StartFlightRecording="filename=recording.jfr"

    This command runs the application as a native executable. The -XX:+FlightRecorder and -XX:StartFlightRecording options enable the built-in Flight Recorder and start recording to a specified binary file, recording.jfr.

  4. Start VisualVM to view the contents of the recording file in a user-friendly way.

  5. Go to File, then Add JFR Snapshot, browse recording.jfr, and open the selected file. Confirm the display name and click OK. Once opened, there is a bunch of options you can check: Monitoring, Threads, Exceptions, etc., but you should be mostly interested in the events browsing. It will look something like this:

    JDK Flight Recorder

    Alternatively, you can view the contents of the recording file in the console window by running this command:

    $JAVA_HOME/bin/jfr print recording.jfr

    It prints all the events recorded by Flight Recorder.

Related Documentation