Java Code Examples for jdk.jfr.FlightRecorder#getFlightRecorder()

The following examples show how to use jdk.jfr.FlightRecorder#getFlightRecorder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestNative.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.loadLibrary("TestNative");
    FlightRecorder.getFlightRecorder();
    recording = new Recording();
    recording.setToDisk(true);
    recording.setDestination(Paths.get(JFR_DUMP));
    recording.enable(NATIVE_EVENT).withPeriod(Duration.ofMillis(10));
    recording.start();

    longTime();

    recording.stop();
    recording.close();

    try (RecordingFile rf = new RecordingFile(Paths.get(JFR_DUMP))) {
        while (rf.hasMoreEvents()) {
            RecordedEvent re = rf.readEvent();
            if (re.getEventType().getName().equals(NATIVE_EVENT)) {
                return;
            }
        }
    }

    throw new Exception("No native samples found");
}
 
Example 2
Source File: TestUnloadEventClassCount.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    FlightRecorder.getFlightRecorder();
    myClassLoader = createClassLoaderWithEventClass();
    System.out.println("MyClassLoader instance created");
    long initialCount = JVM.getJVM().getUnloadedEventClassCount();
    System.out.println("Initiali unloaded count is " + initialCount);
    myClassLoader = null;
    System.out.println("Reference to class loader cleared");
    long count = 0;
    do {
        System.gc();
        System.out.println("GC triggered");
        count = JVM.getJVM().getUnloadedEventClassCount();
        System.out.println("Unloaded count was " + count);
        Thread.sleep(1000); // sleep to reduce log
    } while (count != initialCount + 1);
}
 
Example 3
Source File: TestFlightRecorderListenerRecorderInitialized.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    FlightRecorder.addListener(new FlightRecorderListener() {

        @Override
        public void recorderInitialized(FlightRecorder recorder) {
            log("Recorder initialized");
            Signal.signal();
        }

    });
    FlightRecorder.getFlightRecorder();

    Signal.waitFor(3, TimeUnit.SECONDS);

    assertTrue(Signal.signalled, "FlightRecorderListener.recorderInitialized has not been called");

}
 
Example 4
Source File: TestFlightRecorderListenerRecorderInitialized.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    FlightRecorder.addListener(new FlightRecorderListener() {

        @Override
        public void recorderInitialized(FlightRecorder recorder) {
            log("Recorder initialized");
            Signal.signal();
        }

    });
    FlightRecorder.getFlightRecorder();

    Signal.waitFor(3, TimeUnit.SECONDS);

    assertTrue(Signal.signalled, "FlightRecorderListener.recorderInitialized has not been called");

}
 
Example 5
Source File: TestSnapshot.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testStopped() throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    try (Recording r = new Recording()) {
        r.enable(SimpleEvent.class);
        r.start();
        SimpleEvent se = new SimpleEvent();
        se.commit();
        r.stop();

        try (Recording snapshot = recorder.takeSnapshot()) {

            Asserts.assertEquals(snapshot.getSize(), r.getSize());
            Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), r.getStartTime());
            Asserts.assertLessThanOrEqual(snapshot.getStopTime(), r.getStopTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
            assertStaticOptions(snapshot);
            try (InputStream is = snapshot.getStream(null, null)) {
                Asserts.assertNotNull(is);
            }

            List<RecordedEvent> events = Events.fromRecording(snapshot);
            Events.hasEvents(events);
            Asserts.assertEquals(events.size(), 1);
            Asserts.assertEquals(events.get(0).getEventType().getName(), SimpleEvent.class.getName());
        }
    }
}
 
Example 6
Source File: TestSnapshot.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testOngoing(boolean disk) throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    try (Recording r = new Recording()) {
        r.setToDisk(disk);
        r.enable(SimpleEvent.class);
        r.start();
        SimpleEvent se = new SimpleEvent();
        se.commit();

        try (Recording snapshot = recorder.takeSnapshot()) {

            Asserts.assertGreaterThan(snapshot.getSize(), 0L);
            Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), r.getStartTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getStopTime(), r.getStartTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
            assertStaticOptions(snapshot);
            try (InputStream is = snapshot.getStream(null, null)) {
                Asserts.assertNotNull(is);
            }

            List<RecordedEvent> events = Events.fromRecording(snapshot);
            Events.hasEvents(events);
            Asserts.assertEquals(events.size(), 1);
            Asserts.assertEquals(events.get(0).getEventType().getName(), SimpleEvent.class.getName());
        }

        r.stop();
    }
}
 
Example 7
Source File: TestSnapshot.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testEmpty() throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    Instant before = Instant.now().minusNanos(1);
    try (Recording snapshot = recorder.takeSnapshot()) {
        Instant after = Instant.now().plusNanos(1);
        Asserts.assertEquals(snapshot.getSize(), 0L);
        Asserts.assertLessThan(before, snapshot.getStartTime());
        Asserts.assertGreaterThan(after, snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getStartTime(), snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getDuration(), Duration.ZERO);
        assertStaticOptions(snapshot);
        Asserts.assertEquals(snapshot.getStream(null, null), null);
    }
}
 
Example 8
Source File: TestGetStackTraceId.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) {
    FlightRecorder.getFlightRecorder();
    JVM jvm = JVM.getJVM();

    long id10 = getStackIdOfDepth(10);
    assertValid(id10);

    long id5 = getStackIdOfDepth(5);
    assertValid(id5);

    Asserts.assertNotEquals(id5, id10, "Different stack depth must return different stack trace ids");

    assertMaxSkip(jvm);
}
 
Example 9
Source File: TestRecorderInitializationCallback.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void recorderInitialized(FlightRecorder recorder) {
    count.incrementAndGet();
    System.out.println("recorderInitialized: " + recorder + " count=" + count);
    // Get the recorder again, should not trigger listener
    FlightRecorder.getFlightRecorder();
}
 
Example 10
Source File: TestIsInitialized.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Asserts.assertFalse(FlightRecorder.isInitialized());
    FlightRecorder.addListener(new FlightRecorderListener() {
        public void recorderInitialized(FlightRecorder recorder) {
            Asserts.assertTrue(FlightRecorder.isInitialized());
        }
    });
    FlightRecorder.getFlightRecorder();
    Asserts.assertTrue(FlightRecorder.isInitialized());
}
 
Example 11
Source File: TestEventTypes.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    FlightRecorder jfr = FlightRecorder.getFlightRecorder();

    Recording r = new Recording();
    r.enable(MyEvent.class);
    new MyEvent(); // triggers <clinit>
    List<EventTypeInfo> infos = bean.getEventTypes();
    List<EventType> types = jfr.getEventTypes();
    Asserts.assertFalse(infos.isEmpty(), "No EventTypeInfos found");
    verifyMyEventType(infos);
    assertSame(infos, types);
    r.close();
}
 
Example 12
Source File: DCmdStart.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void initializeWithForcedInstrumentation(Map<String, String> settings) {
    if (!hasJDKEvents(settings)) {
        return;
    }
    JVM jvm = JVM.getJVM();
    try {
       jvm.setForceInstrumentation(true);
        FlightRecorder.getFlightRecorder();
    } finally {
       jvm.setForceInstrumentation(false);
    }
}
 
Example 13
Source File: TestIsInitialized.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Asserts.assertFalse(FlightRecorder.isInitialized());
    FlightRecorder.addListener(new FlightRecorderListener() {
        public void recorderInitialized(FlightRecorder recorder) {
            Asserts.assertTrue(FlightRecorder.isInitialized());
        }
    });
    FlightRecorder.getFlightRecorder();
    Asserts.assertTrue(FlightRecorder.isInitialized());
}
 
Example 14
Source File: TestRecorderInitializationCallback.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    TestListener t = new TestListener();
    FlightRecorder.addListener(t);
    // trigger initialization
    FlightRecorder.getFlightRecorder();
    assertEquals(1, t.count.intValue(), "Expected 1 notification, got " + t.count);
}
 
Example 15
Source File: TestGetRecordings.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();

    // Recording should be empty at start.
    List<Recording> recordings = recorder.getRecordings();
    assertTrue(recordings.isEmpty(), "recordings should be empty at start");

    // Create first recording
    Recording r1 = new Recording();
    recordings = recorder.getRecordings();
    assertEquals(recordings.size(), 1, "Expected 1 recording");
    assertTrue(recordings.contains(r1), "r1 should be in list");

    // Create second recording
    Recording r2 = new Recording();
    recordings = recorder.getRecordings();
    assertEquals(recordings.size(), 2, "Expected 2 recordings");
    assertTrue(recordings.contains(r2), "r2 should be in list");
    assertTrue(recordings.contains(r1), "r1 should still be in list");

    // Close first recording
    r1.close();
    recordings = recorder.getRecordings();
    assertEquals(recordings.size(), 1, "Expected 1 remaining recording");
    assertTrue(recordings.contains(r2), "r2 should still be in list");
    assertFalse(recordings.contains(r1), "r1 should be removed");

    // Close second recording
    r2.close();
    recordings = recorder.getRecordings();
    assertTrue(recordings.isEmpty(), "recordings should be empty after close");

    // Create recording with new Recording()
    Recording r3 = new Recording();
    recordings = recorder.getRecordings();
    assertTrue(recordings.contains(r3 ), "new Recording() should be in list");
    r3.close();
}
 
Example 16
Source File: TestGetStackTraceId.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) {
    FlightRecorder.getFlightRecorder();
    JVM jvm = JVM.getJVM();

    long id10 = getStackIdOfDepth(10);
    assertValid(id10);

    long id5 = getStackIdOfDepth(5);
    assertValid(id5);

    Asserts.assertNotEquals(id5, id10, "Different stack depth must return different stack trace ids");

    assertMaxSkip(jvm);
}
 
Example 17
Source File: DCmdStart.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void initializeWithForcedInstrumentation(Map<String, String> settings) {
    if (!hasJDKEvents(settings)) {
        return;
    }
    JVM jvm = JVM.getJVM();
    try {
       jvm.setForceInstrumentation(true);
        FlightRecorder.getFlightRecorder();
    } finally {
       jvm.setForceInstrumentation(false);
    }
}
 
Example 18
Source File: AbstractDCmd.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected final FlightRecorder getFlightRecorder() {
    return FlightRecorder.getFlightRecorder();
}
 
Example 19
Source File: AbstractDCmd.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected final FlightRecorder getFlightRecorder() {
    return FlightRecorder.getFlightRecorder();
}
 
Example 20
Source File: JmxHelper.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void verifyState(long recId, RecordingState state, FlightRecorderMXBean bean) throws Exception {
    FlightRecorder jfr = FlightRecorder.getFlightRecorder();
    Recording recording = CommonHelper.verifyExists(recId, jfr.getRecordings());
    CommonHelper.verifyRecordingState(recording, state);
    verifyState(recId, state, bean.getRecordings());
}