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

The following examples show how to use jdk.management.jfr.FlightRecorderMXBean#stopRecording() . 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: TestCloneRepeat.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 orgId = bean.newRecording();
    bean.startRecording(orgId);

    List<Integer> ids = new ArrayList<>();
    for (int i=0; i<5; i++) {
        long cloneId = bean.cloneRecording(orgId, false);
        SimpleEventHelper.createEvent(i);
        bean.stopRecording(cloneId);
        Path path = Paths.get(".", i + "-org.jfr");
        bean.copyTo(cloneId, path.toString());
        bean.closeRecording(cloneId);
        ids.add(i);
        verifyEvents(path, ids);
    }

    bean.closeRecording(orgId);
}
 
Example 3
Source File: TestEnoughPermission.java    From TencentKona-8 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 4
Source File: TestNotificationListenerPermission.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 {
    try {
        System.getProperty("user.name");
        Asserts.fail("Didn't get security exception. Test not configured propertly?");
    } catch (SecurityException se) {
        // as expected
    }
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    TestListener testListener = new TestListener();
    ManagementFactory.getPlatformMBeanServer().addNotificationListener(new ObjectName(FlightRecorderMXBean.MXBEAN_NAME), testListener, null, null);
    long id = bean.newRecording();
    bean.startRecording(id);
    testListener.awaitNotication();
    Asserts.assertTrue(gotSecurityException, "Should not get elevated privileges in notification handler!");
    bean.stopRecording(id);
    bean.closeRecording(id);
}
 
Example 5
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 6
Source File: TestRecordingState.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();

    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 7
Source File: TestStreamClosed.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

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

    long streamId = bean.openStream(recId, null);
    bean.closeStream(streamId);
    try {
        bean.readStream(streamId);
        Asserts.fail("No exception whean reading closed stream");
    } catch (IOException e) {
        // Expected exception.
        String msg = e.getMessage().toLowerCase();
        Asserts.assertTrue(msg.contains("stream") && msg.contains("closed"), "No 'stream closed' in " + msg);
    }
    bean.closeRecording(recId);
}
 
Example 8
Source File: TestCopyToRunning.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

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

    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);
    }

    Recording recording = getRecording(recId);
    Asserts.assertEquals(recording.getState(), RecordingState.RUNNING, "Recording not in state running");
    bean.stopRecording(recId);
    Asserts.assertEquals(recording.getState(), RecordingState.STOPPED, "Recording not in state stopped");
    bean.closeRecording(recId);
    Asserts.assertEquals(recording.getState(), RecordingState.CLOSED, "Recording not in state closed");
}
 
Example 9
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 10
Source File: TestCloneRepeat.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 {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long orgId = bean.newRecording();
    bean.startRecording(orgId);

    List<Integer> ids = new ArrayList<>();
    for (int i=0; i<5; i++) {
        long cloneId = bean.cloneRecording(orgId, false);
        SimpleEventHelper.createEvent(i);
        bean.stopRecording(cloneId);
        Path path = Paths.get(".", i + "-org.jfr");
        bean.copyTo(cloneId, path.toString());
        bean.closeRecording(cloneId);
        ids.add(i);
        verifyEvents(path, ids);
    }

    bean.closeRecording(orgId);
}
 
Example 11
Source File: TestStreamClosed.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);

    long streamId = bean.openStream(recId, null);
    bean.closeStream(streamId);
    try {
        bean.readStream(streamId);
        Asserts.fail("No exception whean reading closed stream");
    } catch (IOException e) {
        // Expected exception.
        String msg = e.getMessage().toLowerCase();
        Asserts.assertTrue(msg.contains("stream") && msg.contains("closed"), "No 'stream closed' in " + msg);
    }
    bean.closeRecording(recId);
}
 
Example 12
Source File: TestCopyToInvalidPath.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();

    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 13
Source File: TestStreamClosed.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);

    long streamId = bean.openStream(recId, null);
    bean.closeStream(streamId);
    try {
        bean.readStream(streamId);
        Asserts.fail("No exception whean reading closed stream");
    } catch (IOException e) {
        // Expected exception.
        String msg = e.getMessage().toLowerCase();
        Asserts.assertTrue(msg.contains("stream") && msg.contains("closed"), "No 'stream closed' in " + msg);
    }
    bean.closeRecording(recId);
}
 
Example 14
Source File: TestEnoughPermission.java    From dragonwell8_jdk 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(".", String.format("rec%d.jfr", recId));
    bean.copyTo(recId, path.toString());
    //EventSet events = EventSet.fromFile(path);
    return recId;
}
 
Example 15
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 16
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 17
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 18
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 19
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);
}
 
Example 20
Source File: TestMultipleRecordings.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void stopRecording(long recId, FlightRecorderMXBean bean) throws Exception {
    JmxHelper.verifyState(recId, RecordingState.RUNNING, bean);
    bean.stopRecording(recId);
    JmxHelper.verifyState(recId, RecordingState.STOPPED, bean);
}