Vincenzo Marrazzo
Vincenzo Marrazzo

Reputation: 119

How to profile thown exception in Java Mission Control?

I discovered an unpleasure behavior with JMC when execute a "Flight Recording" with tracking of all exceptions enabled.

After recording into panels:

there are listed not all thrown exception but all created ones (as object).

There are a manner (or a plugin) to produce a list of thrown exception during profiling procedure?

Regards

Upvotes: 2

Views: 1652

Answers (1)

Kire Haglin
Kire Haglin

Reputation: 7069

Java Flight Recorder (JFR) can only record when exceptions are created. Future versions may be able to record when exception are thrown, or filter per class type.

Here is a snippet that show how things work in JDK 11. Oracle JDK 7, 8, 9 and 10, work similarly, but the events have different names.

try (Recording r = new Recording()) {
        r.enable("jdk.JavaExceptionThrow");
        r.start();
        new IOException();
        r.stop();
        dumpAndPrint(r, "Events for exception deriving from java.lang.Throwable");
    }

    try (Recording r = new Recording()) {
        r.enable("jdk.JavaErrorThrow");
        r.start();
        new InternalError();
        r.stop();
        dumpAndPrint(r, "Events for exception deriving from java.lang.Error");
    }
}

 void dumpAndPrint(Recording r, String title) throws IOException {
    System.out.println(title);
    Path p = Files.createTempFile("test", ".jfr");
    r.dump(p);
    for (RecordedEvent e : RecordingFile.readAllEvents(p))  {
        System.out.println(e);
    }
    System.out.println();
}

There are two event types

1) jdk.JavaErrorThrow which will be emitted for all exceptions created that derives from java.lang.Error

2) jdk.JavaExceptionThrow which will be emitted for all exceptions created that derives from java.lang.Throwable.

To keep overhead low only Errors event (jdk.JavaErrorThrow) is enabled by default, for instance if you start your application with -XX:StartFlightRecording MyProgram

If you select "All" in JMC, the "jdk.JavaErrorThrow" event is disabled (to avoid double count) and "jdk.JavaExceptionThrow" is enabled.

Upvotes: 1

Related Questions