Java Code Examples for jdk.management.jfr.FlightRecorderMXBean#newRecording()

The following examples show how to use jdk.management.jfr.FlightRecorderMXBean#newRecording() . 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: TestStartRecording.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    long recId = bean.newRecording();
    bean.startRecording(recId);

    // TODO: Remove debug logs
    List<ConfigurationInfo> configs = bean.getConfigurations();
    for (ConfigurationInfo config : configs) {
        System.out.println("config=" + config.toString());
    }
    Map<String, String> settings = bean.getRecordingSettings(recId);
    for (String key : settings.keySet()) {
        System.out.println("setting: " + key + "=" + settings.get(key));
    }
    List<EventTypeInfo> types = bean.getEventTypes();
    for (EventTypeInfo type : types) {
        System.out.println("type=" + type.getName());
    }
    //////////////////////

    bean.stopRecording(recId);
    bean.closeRecording(recId);
}
 
Example 2
Source File: TestCopyTo.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    Path path = Paths.get(".", "my.jfr");
    bean.copyTo(recId, path.toString());

    List<RecordedEvent> events = RecordingFile.readAllEvents(path);
    Asserts.assertTrue(events.iterator().hasNext(), "No events found");
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
    }

    bean.closeRecording(recId);
}
 
Example 3
Source File: TestCopyToInvalidPath.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    CommonHelper.verifyException(()->{bean.copyTo(recId, null);}, "copyTo(null)", NullPointerException.class);
    CommonHelper.verifyException(()->{bean.copyTo(recId, "");}, "copyTo('')", IOException.class);

    String p = Paths.get(".", "thisdir", "doesnot", "exists").toString();
    CommonHelper.verifyException(()->{bean.copyTo(recId, p);}, "copyTo(dirNotExist)", IOException.class);

    bean.closeRecording(recId);
}
 
Example 4
Source File: TestRecordingState.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    List<RecordingInfo> preCreateRecordings = bean.getRecordings();
    long recId = bean.newRecording();
    JmxHelper.verifyNotExists(recId, preCreateRecordings);
    JmxHelper.verifyState(recId, RecordingState.NEW, bean);

    bean.startRecording(recId);
    JmxHelper.verifyState(recId, RecordingState.RUNNING, bean);

    bean.stopRecording(recId);
    JmxHelper.verifyState(recId, RecordingState.STOPPED, bean);

    bean.closeRecording(recId);
    JmxHelper.verifyNotExists(recId, bean.getRecordings());
}
 
Example 5
Source File: TestCopyToInvalidPath.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    CommonHelper.verifyException(()->{bean.copyTo(recId, null);}, "copyTo(null)", NullPointerException.class);
    CommonHelper.verifyException(()->{bean.copyTo(recId, "");}, "copyTo('')", IOException.class);

    String p = Paths.get(".", "thisdir", "doesnot", "exists").toString();
    CommonHelper.verifyException(()->{bean.copyTo(recId, p);}, "copyTo(dirNotExist)", IOException.class);

    bean.closeRecording(recId);
}
 
Example 6
Source File: TestEnoughPermission.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static long testRecording(FlightRecorderMXBean bean) throws Exception {
    System.out.println("A");
    long recId = bean.newRecording();
    System.out.println("B");
    bean.setPredefinedConfiguration(recId, "profile");
    System.out.println("C");
    bean.startRecording(recId);
    System.out.println("D");
    Asserts.assertTrue(bean.getRecordings().stream().anyMatch(r -> { return r.getId() == recId; }), "recId not found");
    System.out.println("E");
    bean.stopRecording(recId);

    final Path path = Paths.get(".", "rec" + recId + ".jfr");
    bean.copyTo(recId, path.toString());
    //EventSet events = EventSet.fromFile(path);
    return recId;
}
 
Example 7
Source File: TestCopyTo.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    Path path = Paths.get(".", "my.jfr");
    bean.copyTo(recId, path.toString());

    List<RecordedEvent> events = RecordingFile.readAllEvents(path);
    Asserts.assertTrue(events.iterator().hasNext(), "No events found");
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
    }

    bean.closeRecording(recId);
}
 
Example 8
Source File: TestGetRecordings.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 {
    FlightRecorderMXBean bean =JmxHelper.getFlighteRecorderMXBean();

    List<RecordingInfo> preCreateRecordings = bean.getRecordings();
    long recId = bean.newRecording();
    JmxHelper.verifyNotExists(recId, preCreateRecordings);
    bean.closeRecording(recId);
    JmxHelper.verifyNotExists(recId, bean.getRecordings());
}
 
Example 9
Source File: TestClone.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long orgId = bean.newRecording();
    bean.startRecording(orgId);
    SimpleEventHelper.createEvent(1); // Should be in both org and clone

    long cloneId = bean.cloneRecording(orgId, false);
    Asserts.assertNotEquals(orgId, cloneId, "clone id should not be same as org id");

    List<RecordingInfo> recordings = bean.getRecordings();
    JmxHelper.verifyState(orgId, RecordingState.RUNNING, recordings);
    JmxHelper.verifyState(cloneId, RecordingState.RUNNING, recordings);

    bean.stopRecording(orgId);
    recordings = bean.getRecordings();
    JmxHelper.verifyState(orgId, RecordingState.STOPPED, recordings);
    JmxHelper.verifyState(cloneId, RecordingState.RUNNING, recordings);

    SimpleEventHelper.createEvent(2);  // Should only be in clone

    bean.stopRecording(cloneId);
    recordings = bean.getRecordings();
    JmxHelper.verifyState(orgId, RecordingState.STOPPED, recordings);
    JmxHelper.verifyState(cloneId, RecordingState.STOPPED, recordings);

    Path orgPath = Paths.get(".", "org.jfr");
    Path clonePath = Paths.get(".", "clone.jfr");
    bean.copyTo(orgId, orgPath.toString());
    bean.copyTo(cloneId, clonePath.toString());

    verifyEvents(orgPath, 1);
    verifyEvents(clonePath, 1, 2);

    bean.closeRecording(orgId);
    bean.closeRecording(cloneId);
}
 
Example 10
Source File: TestRecordingStateInvalid.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static long createRecording(FlightRecorderMXBean bean) throws Exception {
    List<RecordingInfo> preCreateRecordings = bean.getRecordings();
    long recId = bean.newRecording();
    JmxHelper.verifyNotExists(recId, preCreateRecordings);
    JmxHelper.verifyState(recId, RecordingState.NEW, bean);
    return recId;
}
 
Example 11
Source File: TestRecordingSettingsInvalid.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 {
    Map<String, String> settings = new HashMap<>();
    settings.put(null, "true");
    settings.put("java.exception_throw#stackTrace", null);
    settings.put("java.exception_throw#threshold", "not-a-number");
    settings.put("os.information#period", "4 x");

    // TODO: No exception for these settings. Not sure how much validation can be done on settings.
    //settings.put("java.exception_throw#enabled", "maybe");
    //settings.put("os.information#period", "-4 s");
    //settings.put("java.exception_throw#thread", "");
    //settings.put("", "true");
    //settings.put("os.information#what", "4 ms");
    //settings.put("#", "4 what");
    //settings.put("java.exception_throw#", "true");
    //settings.put("java.exception_throwenabled", "false");

    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    for (String key : settings.keySet()) {
        System.out.printf("settings: %s=%s%n", key, settings.get(key));
        Map<String, String> temp = new HashMap<String, String>();
        temp.put(key, settings.get(key));
        long recId = -1;
        try {
            recId = bean.newRecording();
            bean.setRecordingSettings(recId, temp);
            bean.startRecording(recId);
            bean.stopRecording(recId);
            Asserts.fail("Missing exception");
        } catch (Exception e) {
            System.out.println("Got expected exception: " + e.getMessage());
        } finally {
            bean.closeRecording(recId);
        }
    }
}
 
Example 12
Source File: TestRecordingSettingsInvalid.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 {
    Map<String, String> settings = new HashMap<>();
    settings.put(null, "true");
    settings.put("java.exception_throw#stackTrace", null);
    settings.put("java.exception_throw#threshold", "not-a-number");
    settings.put("os.information#period", "4 x");

    // TODO: No exception for these settings. Not sure how much validation can be done on settings.
    //settings.put("java.exception_throw#enabled", "maybe");
    //settings.put("os.information#period", "-4 s");
    //settings.put("java.exception_throw#thread", "");
    //settings.put("", "true");
    //settings.put("os.information#what", "4 ms");
    //settings.put("#", "4 what");
    //settings.put("java.exception_throw#", "true");
    //settings.put("java.exception_throwenabled", "false");

    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    for (String key : settings.keySet()) {
        System.out.printf("settings: %s=%s%n", key, settings.get(key));
        Map<String, String> temp = new HashMap<String, String>();
        temp.put(key, settings.get(key));
        long recId = -1;
        try {
            recId = bean.newRecording();
            bean.setRecordingSettings(recId, temp);
            bean.startRecording(recId);
            bean.stopRecording(recId);
            Asserts.fail("Missing exception");
        } catch (Exception e) {
            System.out.println("Got expected exception: " + e.getMessage());
        } finally {
            bean.closeRecording(recId);
        }
    }
}
 
Example 13
Source File: TestClone.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();

    long orgId = bean.newRecording();
    bean.startRecording(orgId);
    SimpleEventHelper.createEvent(1); // Should be in both org and clone

    long cloneId = bean.cloneRecording(orgId, false);
    Asserts.assertNotEquals(orgId, cloneId, "clone id should not be same as org id");

    List<RecordingInfo> recordings = bean.getRecordings();
    JmxHelper.verifyState(orgId, RecordingState.RUNNING, recordings);
    JmxHelper.verifyState(cloneId, RecordingState.RUNNING, recordings);

    bean.stopRecording(orgId);
    recordings = bean.getRecordings();
    JmxHelper.verifyState(orgId, RecordingState.STOPPED, recordings);
    JmxHelper.verifyState(cloneId, RecordingState.RUNNING, recordings);

    SimpleEventHelper.createEvent(2);  // Should only be in clone

    bean.stopRecording(cloneId);
    recordings = bean.getRecordings();
    JmxHelper.verifyState(orgId, RecordingState.STOPPED, recordings);
    JmxHelper.verifyState(cloneId, RecordingState.STOPPED, recordings);

    Path orgPath = Paths.get(".", "org.jfr");
    Path clonePath = Paths.get(".", "clone.jfr");
    bean.copyTo(orgId, orgPath.toString());
    bean.copyTo(cloneId, clonePath.toString());

    verifyEvents(orgPath, 1);
    verifyEvents(clonePath, 1, 2);

    bean.closeRecording(orgId);
    bean.closeRecording(cloneId);
}
 
Example 14
Source File: TestStreamMultiple.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    SimpleEventHelper.createEvent(0); // No recordings

    long recIdA = bean.newRecording();

    bean.startRecording(recIdA);
    SimpleEventHelper.createEvent(1); // recA

    long recIdB = bean.newRecording();
    Asserts.assertNotEquals(recIdA, recIdB, "Recording Ids should be unique");
    bean.startRecording(recIdB);
    SimpleEventHelper.createEvent(2); // recA and recB

    bean.stopRecording(recIdA);
    SimpleEventHelper.createEvent(3); // recB

    bean.stopRecording(recIdB);
    SimpleEventHelper.createEvent(4); // No recordings

    // Check recA
    long streamIdA = bean.openStream(recIdA, null);
    List<RecordedEvent> events = JmxHelper.parseStream(streamIdA, bean);
    SimpleEventHelper.verifyContains(events, 1, 2);
    SimpleEventHelper.verifyNotContains(events, 0, 3, 4);
    bean.closeStream(streamIdA);
    // check recB
    long streamIdB = bean.openStream(recIdB, null);
    events = JmxHelper.parseStream(streamIdB, bean);
    SimpleEventHelper.verifyContains(events, 2, 3);
    SimpleEventHelper.verifyNotContains(events, 0, 1, 4);
    bean.closeStream(streamIdB);

    bean.closeRecording(recIdA);
    bean.closeRecording(recIdB);
}
 
Example 15
Source File: TestStreamMultiple.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();

    SimpleEventHelper.createEvent(0); // No recordings

    long recIdA = bean.newRecording();

    bean.startRecording(recIdA);
    SimpleEventHelper.createEvent(1); // recA

    long recIdB = bean.newRecording();
    Asserts.assertNotEquals(recIdA, recIdB, "Recording Ids should be unique");
    bean.startRecording(recIdB);
    SimpleEventHelper.createEvent(2); // recA and recB

    bean.stopRecording(recIdA);
    SimpleEventHelper.createEvent(3); // recB

    bean.stopRecording(recIdB);
    SimpleEventHelper.createEvent(4); // No recordings

    // Check recA
    long streamIdA = bean.openStream(recIdA, null);
    List<RecordedEvent> events = JmxHelper.parseStream(streamIdA, bean);
    SimpleEventHelper.verifyContains(events, 1, 2);
    SimpleEventHelper.verifyNotContains(events, 0, 3, 4);
    bean.closeStream(streamIdA);
    // check recB
    long streamIdB = bean.openStream(recIdB, null);
    events = JmxHelper.parseStream(streamIdB, bean);
    SimpleEventHelper.verifyContains(events, 2, 3);
    SimpleEventHelper.verifyNotContains(events, 0, 1, 4);
    bean.closeStream(streamIdB);

    bean.closeRecording(recIdA);
    bean.closeRecording(recIdB);
}
 
Example 16
Source File: TestPredefinedConfigurationInvalid.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();

    // Test invalid named configs.
    verifyNullPointer(()->{ bean.setPredefinedConfiguration(recId, null); }, "setNamedConfig(null)");
    setInvalidConfigName(recId, "");
    setInvalidConfigName(recId, "invalidname");

    // Verify we can set named config after failed attempts.
    Configuration config = Configuration.getConfigurations().get(0);
    bean.setPredefinedConfiguration(recId, config.getName());
    JmxHelper.verifyMapEquals(bean.getRecordingSettings(recId), config.getSettings());
}
 
Example 17
Source File: TestMultipleRecordings.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static long createRecording(FlightRecorderMXBean bean) throws Exception {
    List<RecordingInfo> preCreateRecordings = bean.getRecordings();
    long recId = bean.newRecording();
    JmxHelper.verifyNotExists(recId, preCreateRecordings);
    JmxHelper.verifyState(recId, RecordingState.NEW, bean);
    return recId;
}
 
Example 18
Source File: TestCopyToReadOnlyDir.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    Path readOnlyDir = FileHelper.createReadOnlyDir(Paths.get(".", "readOnlyDir"));
    System.out.println("readOnlyDir=" + readOnlyDir.toString());
    Asserts.assertTrue(readOnlyDir.toFile().isDirectory(), "Could not create directory. Test error");
    if (!FileHelper.isReadOnlyPath(readOnlyDir)) {
        System.out.println("Failed to create read-only dir. Maybe running as root? Skipping test");
        return;
    }

    Path file = Paths.get(readOnlyDir.toString(), "my.jfr");
    System.out.println("file=" + file.toString());
    try {
        bean.copyTo(recId, file.toString());
        Asserts.fail("Should be able to dump to read only file");
    } catch (AccessDeniedException e) {
        // ok as expected
    }

    bean.closeRecording(recId);
}
 
Example 19
Source File: TestCopyToReadOnlyDir.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    Path readOnlyDir = FileHelper.createReadOnlyDir(Paths.get(".", "readOnlyDir"));
    System.out.println("readOnlyDir=" + readOnlyDir.toString());
    Asserts.assertTrue(readOnlyDir.toFile().isDirectory(), "Could not create directory. Test error");
    if (!FileHelper.isReadOnlyPath(readOnlyDir)) {
        System.out.println("Failed to create read-only dir. Maybe running as root? Skipping test");
        return;
    }

    Path file = Paths.get(readOnlyDir.toString(), "my.jfr");
    System.out.println("file=" + file.toString());
    try {
        bean.copyTo(recId, file.toString());
        Asserts.fail("Should be able to dump to read only file");
    } catch (AccessDeniedException e) {
        // ok as expected
    }

    bean.closeRecording(recId);
}
 
Example 20
Source File: TestRecordingSettingsMultiple.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Map<String, String> settingsA = new HashMap<>();
    settingsA.put("java.exception_throw#enabled", "false");
    settingsA.put("java.exception_throw#threshold", "2 s");
    settingsA.put("java.exception_throw#thread", "true");
    settingsA.put("java.exception_throw#stackTrace", "false");
    settingsA.put("os.information#enabled", "true");
    settingsA.put("os.information#period", "400 ms");

    Map<String, String> settingsB = new HashMap<>();
    settingsB.put("vm/code_sweeper/config#enabled", "true");
    settingsB.put("vm/code_sweeper/config#period", "everyChunk");
    settingsA.put("java.exception_throw#enabled", "true");
    settingsA.put("java.exception_throw#threshold", "6 m");
    settingsB.put("os.information#enabled", "true");
    settingsB.put("os.information#period", "0 ms");

    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    long recIdA = bean.newRecording();
    long recIdB = bean.newRecording();
    bean.setRecordingSettings(recIdA, settingsA);
    bean.setRecordingSettings(recIdB, settingsB);

    JmxHelper.verifyMapEquals(settingsA, bean.getRecordingSettings(recIdA));
    JmxHelper.verifyMapEquals(settingsB, bean.getRecordingSettings(recIdB));
    JmxHelper.verifyMapEquals(settingsA, JmxHelper.getJavaRecording(recIdA).getSettings());
    JmxHelper.verifyMapEquals(settingsB, JmxHelper.getJavaRecording(recIdB).getSettings());

    bean.startRecording(recIdA);
    bean.startRecording(recIdB);
    JmxHelper.verifyMapEquals(settingsA, bean.getRecordingSettings(recIdA));
    JmxHelper.verifyMapEquals(settingsB, bean.getRecordingSettings(recIdB));
    JmxHelper.verifyMapEquals(settingsA, JmxHelper.getJavaRecording(recIdA).getSettings());
    JmxHelper.verifyMapEquals(settingsB, JmxHelper.getJavaRecording(recIdB).getSettings());

    bean.stopRecording(recIdA);
    bean.stopRecording(recIdB);
    JmxHelper.verifyMapEquals(settingsA, bean.getRecordingSettings(recIdA));
    JmxHelper.verifyMapEquals(settingsB, bean.getRecordingSettings(recIdB));
    JmxHelper.verifyMapEquals(settingsA, JmxHelper.getJavaRecording(recIdA).getSettings());
    JmxHelper.verifyMapEquals(settingsB, JmxHelper.getJavaRecording(recIdB).getSettings());

    bean.closeRecording(recIdA);
    bean.closeRecording(recIdB);
}