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

The following examples show how to use jdk.jfr.Recording#setDestination() . 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: TestDestFileReadOnly.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 {
    Path dest = FileHelper.createReadOnlyFile(Paths.get(".", "readonly.txt"));
    System.out.println("dest=" + dest.toFile().getAbsolutePath());
    if (!FileHelper.isReadOnlyPath(dest)) {
        System.out.println("Failed to create a read-only file. Test ignored.");
        return;
    }

    Recording r = new Recording();
    r.setToDisk(true);
    try {
        r.setDestination(dest);
        Asserts.fail("No exception when destination is read-only");
    } catch (IOException e) {
        // Expected exception
    }
    r.close();
}
 
Example 2
Source File: TestDestReadOnly.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 {
    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 3
Source File: TestRecordingInfo.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 {
    Recording recording = new Recording(Configuration.getConfiguration("profile"));
    recording.setDestination(Paths.get(".", "my.jfr"));
    recording.setDumpOnExit(true);
    recording.setDuration(Duration.ofSeconds(60));
    recording.setMaxAge(Duration.ofHours(1));
    recording.setMaxSize(123456789);
    recording.setName("myName");
    recording.enable("java.exception_throw").with("threashold", "2 s");
    recording.setToDisk(true);

    recording.start();
    CommonHelper.verifyRecordingState(recording, RecordingState.RUNNING); // Wait until running

    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    RecordingInfo info = JmxHelper.verifyExists(recording.getId(), bean.getRecordings());

    System.out.println(JmxHelper.asString(recording));
    System.out.println(JmxHelper.asString(info));
    JmxHelper.verifyEquals(info, recording);

    recording.stop();
    recording.close();
}
 
Example 4
Source File: TestNative.java    From TencentKona-8 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 5
Source File: TestNative.java    From openjdk-jdk8u 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 6
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 7
Source File: TestDestWithDuration.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 {
    Path dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    SimpleEventHelper.enable(r, true);
    r.setDestination(dest);
    r.start();
    SimpleEventHelper.createEvent(1);

    // Waiting for recording to auto close after duration
    r.setDuration(Duration.ofSeconds(1));
    System.out.println("Waiting for recording to auto close after duration");
    CommonHelper.waitForRecordingState(r, RecordingState.CLOSED);
    System.out.println("recording state = " + r.getState());
    Asserts.assertEquals(r.getState(), RecordingState.CLOSED, "Recording not closed");

    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    System.out.printf("Recording file size=%d%n", Files.size(dest));
    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());
}
 
Example 8
Source File: TestDestWithDuration.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 dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    SimpleEventHelper.enable(r, true);
    r.setDestination(dest);
    r.start();
    SimpleEventHelper.createEvent(1);

    // Waiting for recording to auto close after duration
    r.setDuration(Duration.ofSeconds(1));
    System.out.println("Waiting for recording to auto close after duration");
    CommonHelper.waitForRecordingState(r, RecordingState.CLOSED);
    System.out.println("recording state = " + r.getState());
    Asserts.assertEquals(r.getState(), RecordingState.CLOSED, "Recording not closed");

    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    System.out.printf("Recording file size=%d%n", Files.size(dest));
    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());
}
 
Example 9
Source File: TestDestFileReadOnly.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 dest = FileHelper.createReadOnlyFile(Paths.get(".", "readonly.txt"));
    System.out.println("dest=" + dest.toFile().getAbsolutePath());
    if (!FileHelper.isReadOnlyPath(dest)) {
        System.out.println("Failed to create a read-only file. Test ignored.");
        return;
    }

    Recording r = new Recording();
    r.setToDisk(true);
    try {
        r.setDestination(dest);
        Asserts.fail("No exception when destination is read-only");
    } catch (IOException e) {
        // Expected exception
    }
    r.close();
}
 
Example 10
Source File: TestDestToDiskTrue.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 dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);
    r.setDestination(dest);
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set destination");
    r.start();
    r.stop();

    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set destination after stop");

    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%n", events.get(0).getEventType().getName());
    System.out.printf("File size=%d, getSize()=%d%n", Files.size(dest), r.getSize());
    assertEquals(r.getSize(), 0L, "getSize() should return 0, chunks should have be released at stop");
    Asserts.assertNotEquals(Files.size(dest), 0L, "File length 0. Should at least be some bytes");
    r.close();
}
 
Example 11
Source File: TestDestFileExist.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 {
    Path dest = Paths.get(".", "my.jfr");
    System.out.println("dest=" + dest);
    Files.write(dest, "Dummy data".getBytes());
    assertTrue(Files.exists(dest), "Test error: Failed to create file");
    System.out.println("file size before recording:" + Files.size(dest));
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setDestination(dest);
    r.start();
    r.stop();
    List<RecordedEvent> events = RecordingFile.readAllEvents(dest);
    Asserts.assertFalse(events.isEmpty(), "No events found");
    System.out.println(events.iterator().next());
    r.close();
}
 
Example 12
Source File: TestRecordingBase.java    From openjdk-jdk8u 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 13
Source File: TestDestState.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 {
    Recording r = new Recording();
    SimpleEventHelper.enable(r, true);

    final Path newDest = Paths.get(".", "new.jfr");
    r.setDestination(newDest);
    System.out.println("new dest: " + r.getDestination());
    Asserts.assertEquals(newDest, r.getDestination(), "Wrong get/set dest when new");

    r.start();
    SimpleEventHelper.createEvent(0);
    Thread.sleep(100);
    final Path runningDest = Paths.get(".", "running.jfr");
    r.setDestination(runningDest);
    System.out.println("running dest: " + r.getDestination());
    Asserts.assertEquals(runningDest, r.getDestination(), "Wrong get/set dest when running");
    SimpleEventHelper.createEvent(1);

    r.stop();
    SimpleEventHelper.createEvent(2);

    // Expect recording to be saved at destination that was set when
    // the recording was stopped, which is runningDest.
    Asserts.assertTrue(Files.exists(runningDest), "No recording file: " + runningDest);
    List<RecordedEvent> events = RecordingFile.readAllEvents(runningDest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
    r.close();
}
 
Example 14
Source File: TestDestState.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 {
    Recording r = new Recording();
    SimpleEventHelper.enable(r, true);

    final Path newDest = Paths.get(".", "new.jfr");
    r.setDestination(newDest);
    System.out.println("new dest: " + r.getDestination());
    Asserts.assertEquals(newDest, r.getDestination(), "Wrong get/set dest when new");

    r.start();
    SimpleEventHelper.createEvent(0);
    Thread.sleep(100);
    final Path runningDest = Paths.get(".", "running.jfr");
    r.setDestination(runningDest);
    System.out.println("running dest: " + r.getDestination());
    Asserts.assertEquals(runningDest, r.getDestination(), "Wrong get/set dest when running");
    SimpleEventHelper.createEvent(1);

    r.stop();
    SimpleEventHelper.createEvent(2);

    // Expect recording to be saved at destination that was set when
    // the recording was stopped, which is runningDest.
    Asserts.assertTrue(Files.exists(runningDest), "No recording file: " + runningDest);
    List<RecordedEvent> events = RecordingFile.readAllEvents(runningDest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
    r.close();
}
 
Example 15
Source File: TestGetSizeToMem.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 {
    Path dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    r.setToDisk(false);
    r.setDestination(dest);
    r.enable(EventNames.OSInformation);
    r.start();
    r.stop();
    r.close();
    assertTrue(Files.exists(dest), "TestGetSize recording missing: " + dest);
    System.out.printf("%s size: %d%n", dest, Files.size(dest));
    System.out.printf("r.getSize(): %d%n", r.getSize());
    Asserts.assertNotEquals(Files.size(dest), 0L, "File should not be empty");
    assertEquals(r.getSize(), 0L, "r.getSize() should be 0 after setToDisk(false)");
}
 
Example 16
Source File: TestDestLongPath.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 {
    Path dir = FileHelper.createLongDir(Paths.get("."));
    Path dest = Paths.get(dir.toString(), "my.jfr");
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);
    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%n", events.get(0).getEventType().getName());
}
 
Example 17
Source File: TestGetSizeToMem.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 {
    Path dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    r.setToDisk(false);
    r.setDestination(dest);
    r.enable(EventNames.OSInformation);
    r.start();
    r.stop();
    r.close();
    assertTrue(Files.exists(dest), "TestGetSize recording missing: " + dest);
    System.out.printf("%s size: %d%n", dest, Files.size(dest));
    System.out.printf("r.getSize(): %d%n", r.getSize());
    Asserts.assertNotEquals(Files.size(dest), 0L, "File should not be empty");
    assertEquals(r.getSize(), 0L, "r.getSize() should be 0 after setToDisk(false)");
}
 
Example 18
Source File: TestDestLongPath.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 {
    Path dir = FileHelper.createLongDir(Paths.get("."));
    Path dest = Paths.get(dir.toString(), "my.jfr");
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);
    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%n", events.get(0).getEventType().getName());
}
 
Example 19
Source File: TestDestInvalid.java    From openjdk-jdk8u 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());
}
 
Example 20
Source File: TestDestMultiple.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 rA = new Recording();
    Recording rB = new Recording();

    Path destA = Paths.get(".", "recA.jfr");
    Path destB = Paths.get(".", "recB.jfr");
    rA.setDestination(destA);
    rB.setDestination(destB);

    // Enable event in one recording and disable in the other.
    // Both recordings should still get the events, since we always
    // get the "union" of all settings.
    SimpleEventHelper.enable(rA, true);
    SimpleEventHelper.enable(rB, false);

    SimpleEventHelper.createEvent(0); // To no recording

    rA.start();
    SimpleEventHelper.createEvent(1); // Only to recA

    rB.start();
    SimpleEventHelper.createEvent(2); // To both recordings

    rA.stop();

    // This event will not be in recB.
    // The reason is that recA has stopped so event is no longer enabled.
    SimpleEventHelper.createEvent(3);

    // Enable the event and create a new event for recB
    SimpleEventHelper.enable(rB, true);
    SimpleEventHelper.createEvent(4);

    rB.stop();
    SimpleEventHelper.createEvent(5); // To no recording

    rB.close();
    rA.close();

    verifyRecording(destA, 1, 2);
    verifyRecording(destB, 2, 4);
}