Java Code Examples for jdk.jfr.Recording#close()

The following examples show how to use jdk.jfr.Recording#close() . 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: TestAddListenerTwice.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 {
    MyListener listener = new MyListener();

    FlightRecorder.addListener(listener);
    FlightRecorder.addListener(listener);

    Recording r1 = new Recording();
    r1.start();
    listener.verifyState(2, RecordingState.RUNNING);

    FlightRecorder.removeListener(listener); // Should still get callback to one listener.
    r1.stop();
    listener.verifyState(3, RecordingState.STOPPED);
    r1.close();
    listener.verifyState(4, RecordingState.CLOSED);
}
 
Example 2
Source File: TestStateDuration.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Duration duration = Duration.ofSeconds(2);
    Recording r = new Recording();
    r.setDuration(duration);
    CommonHelper.verifyRecordingState(r, RecordingState.NEW);
    Instant start = Instant.now();
    System.out.println("Recording with duration " + duration + " started at " + start);
    r.start();

    // Wait for recording to stop automatically
    System.out.println("Waiting for recording to reach STOPPED state");
    CommonHelper.waitForRecordingState(r, RecordingState.STOPPED);
    Instant stop = Instant.now();
    Duration measuredDuration = Duration.between(start, stop);
    System.out.println("Recording stopped at " + stop + ". Measured duration " + measuredDuration);
    // Timer task uses System.currentMillis, and java.time uses other source.
    Duration deltaDueToClockNotInSync = Duration.ofMillis(100);
    Asserts.assertGreaterThan(measuredDuration.plus(deltaDueToClockNotInSync), duration);
    verifyIllegalState(() -> r.start(), "start() after stop()");
    r.close();
    CommonHelper.verifyRecordingState(r, RecordingState.CLOSED);
}
 
Example 3
Source File: TestGetId.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 {
    Map<Long, Recording> recordings = new HashMap<>();

    final int iterations = 100;
    for (int i = 0; i < iterations; ++i) {
        Recording newRecording = new Recording();
        System.out.println("created: " + newRecording.getId());
        Recording oldRecording = recordings.get(newRecording.getId());
        Asserts.assertNull(oldRecording, "Duplicate recording ID: " + newRecording.getId());
        recordings.put(newRecording.getId(), newRecording);

        if (i % 3 == 0) {
            Recording closeRecording = recordings.remove(recordings.keySet().iterator().next());
            Asserts.assertNotNull(closeRecording, "Could not find recording in map. Test error.");
            closeRecording.close();
            System.out.println("closed: " + closeRecording.getId());
        }
    }

    for (Recording r : recordings.values()) {
        r.close();
    }
}
 
Example 4
Source File: TestG1EvacMemoryStatsEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void runTest(String event_name) throws Exception {

        Recording recording = new Recording();

        // activate the event we are interested in and start recording
        recording.enable(event_name);
        recording.start();

        // Setting NewSize and MaxNewSize will limit eden, so
        // allocating 1024 5k byte arrays should trigger at
        // least one Young GC.
        for (int i = 0; i < 1024; i++) {
            bytes = new byte[5 * 1024];
        }
        recording.stop();

        // Verify recording
        List<RecordedEvent> all = Events.fromRecording(recording);
        for (RecordedEvent e : all) {
            Events.assertField(e, "statistics.gcId").above(0);
        }

        recording.close();
    }
 
Example 5
Source File: TestDestReadOnly.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 {
    Path readOnlyDir = FileHelper.createReadOnlyDir(Paths.get(".", "readonly"));
    if (!FileHelper.isReadOnlyPath(readOnlyDir)) {
        System.out.println("Failed to create read-only dir. Running as root?. Test ignored");
        return;
    }

    Path readOnlyDest = Paths.get(readOnlyDir.toString(), "readonly.jfr");
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);
    verifyException(()->{r.setDestination(readOnlyDest);}, "No exception for setDestination to read-only dir");

    System.out.println("r.getDestination() = " + r.getDestination());

    // Verify that it works if we set destination to a writable dir.
    Path dest = Paths.get(".", "my.jfr");
    r.setDestination(dest);
    r.start();
    r.stop();
    r.close();
    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    List<RecordedEvent> events = RecordingFile.readAllEvents(dest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s in %s%n", events.get(0).getEventType().getName(), dest.toString());
}
 
Example 6
Source File: TestRecordingBase.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void testGetSettings() throws Throwable {
    String eventPath = "my/test/enabledPath";
    String settingName = "myTestSetting";
    String settingValue = "myTestValue";

    Recording r = new Recording();
    r.enable(eventPath).with(settingName, settingValue);

    boolean isEnabledPathFound = false;
    boolean isSettingFound = false;
    Map<String, String> settings = r.getSettings();
    for (String name : settings.keySet()) {
        System.out.println("name=" + name + ", value=" + settings.get(name));
        if (name.contains(eventPath) && name.contains("#enabled")) {
            isEnabledPathFound = true;
            assertEquals("true", settings.get(name), "Wrong value for enabled path: " + name);
        }
        if  (name.contains(eventPath) && name.contains(settingName)) {
            isSettingFound = true;
            assertEquals(settingValue, settings.get(name), "Wrong value for setting: " + name);
        }
    }
    assertTrue(isEnabledPathFound, "Enabled path not found in settings");
    assertTrue(isSettingFound, "Test setting not found in settings");
    r.close();
}
 
Example 7
Source File: TestGetId.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 {
    Map<Long, Recording> recordings = new HashMap<>();

    final int iterations = 100;
    for (int i = 0; i < iterations; ++i) {
        Recording newRecording = new Recording();
        System.out.println("created: " + newRecording.getId());
        Recording oldRecording = recordings.get(newRecording.getId());
        Asserts.assertNull(oldRecording, "Duplicate recording ID: " + newRecording.getId());
        recordings.put(newRecording.getId(), newRecording);

        if (i % 3 == 0) {
            Recording closeRecording = recordings.remove(recordings.keySet().iterator().next());
            Asserts.assertNotNull(closeRecording, "Could not find recording in map. Test error.");
            closeRecording.close();
            System.out.println("closed: " + closeRecording.getId());
        }
    }

    for (Recording r : recordings.values()) {
        r.close();
    }
}
 
Example 8
Source File: TestDump.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 {
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.start();
    r.stop();

    Path path = Paths.get(".", "my.jfr");
    r.dump(path);
    r.close();

    Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);
    Asserts.assertFalse(RecordingFile.readAllEvents(path).isEmpty(), "No events found");
}
 
Example 9
Source File: TestIsEnabled.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    assertDisabled("Event enabled with no recording");

    Recording r = new Recording();
    assertDisabled("Event enabled at new Recording()");

    r.enable(SimpleEvent.class);
    assertDisabled("Event enabled before r.start()");

    r.start();

    // enable/disable by class
    assertEnabled("Event not enabled after r.start()");
    r.disable(SimpleEvent.class);
    assertDisabled("Event enabled after r.disable()");
    r.enable(SimpleEvent.class);
    assertEnabled("Event disabled afer r.enable()");

    // enable/disable by event setting name
    String eventSettingName = String.valueOf(EventType.getEventType(SimpleEvent.class).getId());
    System.out.println("eventSettingName=" + eventSettingName);

    r.disable(eventSettingName);
    assertDisabled("Event enabled after r.disable(name)");
    r.enable(eventSettingName);
    assertEnabled("Event disabled after r.enable(name)");

    r.stop();
    assertDisabled("Event enabled after r.stop()");

    r.close();
    assertDisabled("Event enabled after r.close()");
}
 
Example 10
Source File: TestRecordingBase.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testSetGetDestination() throws Throwable {
    Recording r = new Recording();
    final Path destination = Paths.get(".", "testSetGetDestination.jfr");
    r.setDestination(destination);
    assertEquals(destination, r.getDestination(), "Wrong set/get destination");
    r.close();
}
 
Example 11
Source File: TestG1MMUEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        Recording recording = new Recording();

        // activate the event we are interested in and start recording
        recording.enable(EVENT_NAME);
        recording.start();

        // Setting NewSize and MaxNewSize will limit eden, so
        // allocating 1024 5k byte arrays should trigger at
        // least one Young GC.
        for (int i = 0; i < 1024; i++) {
            bytes  = new byte[5 * 1024];
        }
        recording.stop();

        // Verify recording
        List<RecordedEvent> all = Events.fromRecording(recording);
        Events.hasEvents(all);

        for (RecordedEvent e : all) {
            Events.assertField(e, "gcId").above(0);
            Events.assertField(e, "timeSlice").isEqual(TIME_SLICE);
            Events.assertField(e, "pauseTarget").isEqual(MAX_GC_TIME);
        }

        recording.close();
    }
 
Example 12
Source File: TestStartMaxAgeSize.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Recording r = StartupHelper.getRecording("TestStartMaxAgeSize");
    CommonHelper.verifyRecordingState(r, RecordingState.RUNNING);
    Asserts.assertEquals(r.getMaxAge(), Duration.ofSeconds(10), "Wrong maxAge");
    Asserts.assertEquals(r.getMaxSize(), 1000000L, "Wrong maxSize");
    r.stop();
    r.close();
}
 
Example 13
Source File: TestDestToDiskFalse.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    final Path dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    SimpleEventHelper.enable(r, true);
    r.setToDisk(false);
    r.setDestination(dest);
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set destination");
    r.start();
    SimpleEventHelper.createEvent(0);
    r.stop();

    // setToDisk(false) should not effect setDestination.
    // We should still get a file when the recording stops.
    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    System.out.printf("File size=%d, getSize()=%d%n", Files.size(dest), r.getSize());
    Asserts.assertNotEquals(Files.size(dest), 0L, "File length 0. Should at least be some bytes");

    List<RecordedEvent> events = RecordingFile.readAllEvents(dest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s%n", events.get(0).getEventType().getName());

    assertEquals(r.getSize(), 0L, "getSize() should return 0, chunks should have been released at stop");
    // getDestination() should return null after recording have been written to file.
    Asserts.assertNull(r.getDestination(), "getDestination() should return null after file created");

    r.close();
}
 
Example 14
Source File: TestStateMultiple.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording rA = new Recording();
    CommonHelper.verifyRecordingState(rA, RecordingState.NEW);
    verifyIllegalState(() -> rA.stop(), "stop() when not started");

    rA.start();
    CommonHelper.verifyRecordingState(rA, RecordingState.RUNNING);

    Recording rB = new Recording();
    CommonHelper.verifyRecordingState(rA, RecordingState.RUNNING);
    verifyIllegalState(() -> rA.start(), "double start()");
    CommonHelper.verifyRecordingState(rB, RecordingState.NEW);
    verifyIllegalState(() -> rB.stop(), "stop() when not started");

    rB.start();
    CommonHelper.verifyRecordingState(rA, RecordingState.RUNNING);
    CommonHelper.verifyRecordingState(rB, RecordingState.RUNNING);

    rB.stop();
    CommonHelper.verifyRecordingState(rA, RecordingState.RUNNING);
    CommonHelper.verifyRecordingState(rB, RecordingState.STOPPED);
    verifyIllegalState(() -> rB.start(), "start() after stop()");

    rB.close();
    CommonHelper.verifyRecordingState(rA, RecordingState.RUNNING);
    CommonHelper.verifyRecordingState(rB, RecordingState.CLOSED);
    verifyIllegalState(() -> rB.start(), "start() after close()");

    rA.stop();
    CommonHelper.verifyRecordingState(rA, RecordingState.STOPPED);
    verifyIllegalState(() -> rA.start(), "start() after stop()");
    CommonHelper.verifyRecordingState(rB, RecordingState.CLOSED);

    rA.close();
    CommonHelper.verifyRecordingState(rA, RecordingState.CLOSED);
    CommonHelper.verifyRecordingState(rB, RecordingState.CLOSED);
    verifyIllegalState(() -> rA.stop(), "stop() after close()");
    verifyIllegalState(() -> rB.start(), "start() after close()");
}
 
Example 15
Source File: TestStartDelayRunning.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.out.println("Test started at " + Instant.now());
    Recording r = StartupHelper.getRecording("TestStartDelay");
    CommonHelper.waitForRecordingState(r, RecordingState.RUNNING);
    System.out.println("Recording startTime = " + r.getStartTime());
    Asserts.assertNotNull(r.getStartTime(), "StartTime was null after delay");
    Asserts.assertGreaterThan(Instant.now(), r.getStartTime(), "Current time should exceed start time");
    r.stop();
    r.close();
}
 
Example 16
Source File: TestRecorderListener.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 {
    Listener recordingListener = new Listener(RecordingState.RUNNING);
    FlightRecorder.addListener(recordingListener);

    Listener stoppedListener = new Listener(RecordingState.STOPPED);
    FlightRecorder.addListener(stoppedListener);

    Listener finishedListener = new Listener(RecordingState.CLOSED);
    FlightRecorder.addListener(finishedListener);

    Recording recording = new Recording();
    if (recording.getState() != RecordingState.NEW) {
        recording.close();
        throw new Exception("New recording should be in NEW state");
    }

    recording.start();
    recordingListener.waitFor();

    recording.stop();
    stoppedListener.waitFor();

    recording.close();
    finishedListener.waitFor();

    testDefaultrecordingStateChangedListener();

}
 
Example 17
Source File: TestDisassemble.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void makeRecordingWithChunks(int count, Path file) throws IOException, ParseException {
    Recording main = new Recording(Configuration.getConfiguration("default"));
    main.setToDisk(true);
    main.start();
    for (int i = 0; i < count; i++) {
        Recording r = new Recording();
        r.setToDisk(true);
        r.start();
        r.stop();
        r.close();
    }
    main.stop();
    main.dump(file);
    main.close();
}
 
Example 18
Source File: TestIsEnabledMultiple.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void assertEnabled(RecState recStateA, RecState recStateB, EnableState enableStateA, EnableState enableStateB) {

        Recording a = new Recording();
        Recording b = new Recording();
        assertEnablement(false); // no recording running

        if (enableStateA == EnableState.Disabled) {
            a.disable(MyEvent.class);
        }
        if (enableStateA == EnableState.Enabled) {
            a.enable(MyEvent.class);
        }
        if (enableStateB == EnableState.Disabled) {
            b.disable(MyEvent.class);
        }
        if (enableStateB == EnableState.Enabled) {
            b.enable(MyEvent.class);
        }
        if ( enableStateA == EnableState.Disabled && recStateA == RecState.Running) {
            if ( enableStateB == EnableState.Disabled && recStateB == RecState.Running) {
                System.out.println();
            }
        }
        if (recStateA == RecState.Running) {
            a.start();
        }
        if (recStateB == RecState.Running) {
            b.start();
        }

        System.out.println("Recording a is " + a.getState() + ". Event is " + enableStateA);
        System.out.println("Recording b is " + b.getState() + ". Event is " + enableStateB);
        // an event is enabled if at least one recording is running
        // and the event is on by default or has been enabled.
        boolean aEnabled = recStateA == RecState.Running && enableStateA == EnableState.Enabled;
        boolean bEnabled = recStateB == RecState.Running && enableStateB == EnableState.Enabled;
        boolean enabled = aEnabled || bEnabled;
        System.out.println("Expected enablement: " + enabled);
        System.out.println();
        assertEnablement(enabled);
        if (recStateA == RecState.Running) {
            a.stop();
        }
        if (recStateB == RecState.Running) {
            b.stop();
        }
        assertEnablement(false); // no recording running
        a.close();
        b.close();
    }
 
Example 19
Source File: TestLargeJavaEvent512k.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    final String name = "MyLargeJavaEvent512k"; // name of synthetically generated event
    final String fieldNamePrefix = "myfield";
    final int numberOfFields = 64; // 64*8k = 512k event size
    final Map<String, Object> eventMap = new HashMap<>();
    final int numberOfThreads = 10; // 10 threads will run the test
    final int numberOfEventsPerThread = 50; // each thread will generate 50 events

    List<ValueDescriptor> fields = new ArrayList<>();
    for (int i = 0; i < numberOfFields; ++i) {
        String fieldName = fieldNamePrefix + i;
        eventMap.put(fieldName, largeString());
        fields.add(new ValueDescriptor(String.class, fieldName));
    }

    EventTypePrototype prototype = new EventTypePrototype(name,Collections.emptyList(),  fields);

    EventFactory ef = EventFactory.create(prototype.getAnnotations(), prototype.getFields());

    Recording r = new Recording();
    r.enable(prototype.getName()).withThreshold(Duration.ofNanos(0)).withoutStackTrace();
    r.start();

    Thread.UncaughtExceptionHandler eh = (t, e) -> TestLargeJavaEvent512k.setError();

    Stressor.execute(numberOfThreads, eh, () -> {
        for (int n = 0; n < numberOfEventsPerThread; ++n) {
            try {
                Event event = ef.newEvent();
                setEventValues(event, ef, prototype, eventMap);
                event.commit();
                Thread.sleep(1);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    r.stop();
    try {
        if (hasError()) {
            throw new RuntimeException("One (or several) of the threads had an exception/error, test failed");
        }
        verifyEvents(r, numberOfThreads, numberOfEventsPerThread, eventMap);
    } finally {
        r.close();
    }
}
 
Example 20
Source File: TestDestInvalid.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);

    Asserts.assertNull(r.getDestination(), "dest not null by default");

    // Set destination to empty path (same as curr dir, not a file)
    verifyException(()->{r.setDestination(Paths.get(""));}, "No exception for setDestination(\"\")", IOException.class);
    System.out.println("1 destination: " + r.getDestination());
    Asserts.assertNull(r.getDestination(), "default dest not null after failed setDest");

    // Set dest to a valid path. This should be kept when a new setDest fails.
    Path dest = Paths.get(".", "my.jfr");
    r.setDestination(dest);
    System.out.println("2 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Null is allowed for setDestination()
    r.setDestination(null);
    System.out.println("3 destination: " + r.getDestination());
    Asserts.assertNull(r.getDestination(), "dest not null after setDest(null)");

    // Reset dest to correct value and make ssure it is not overwritten
    r.setDestination(dest);
    System.out.println("4 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Set destination to an existing dir. Old dest is saved.
    verifyException(()->{r.setDestination(Paths.get("."));}, "No exception for setDestination(.)", IOException.class);
    System.out.println("5 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Set destination to a non-existing dir. Old dest is saved.
    verifyException(()->{r.setDestination(Paths.get(".", "missingdir", "my.jfr"));}, "No exception for setDestination(dirNotExists)", IOException.class);
    System.out.println("6 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Verify that it works with the old setDest value.
    r.start();
    r.stop();
    r.close();
    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    List<RecordedEvent> events = RecordingFile.readAllEvents(dest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s in %s%n", events.get(0).getEventType().getName(), dest.toString());
}