Java源码示例:jdk.jfr.internal.JVM

示例1
@SafeVarargs
private static void assertEvents(JVM jvm, boolean inclusion, Class<? extends Event>... targetEvents) {
    final List<Class<? extends Event>> list = jvm.getAllEventClasses();
    for (Class<? extends Event> ev : targetEvents) {
       if (list.contains(ev)) {
           if (inclusion) {
               continue;
           }
           throw new AssertionError(ev.getName() + " in list but should not be!");
       }
       if (!inclusion) {
           continue;
       }
       throw new AssertionError(ev.getName() + " in not in list but should be!");
   }
}
 
示例2
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);
}
 
示例3
public static void main(String... args) throws Exception {
    JVM jvm = JVM.getJVM();
    // Ensure that repeated failures can be handled
    for (int i = 1; i < 4; i++) {
        System.out.println("About to try failed initialization, attempt " + i + " out of 3");
        assertFailedInitialization(jvm);
        System.out.println("As expected, initialization failed.");
    }
    // Ensure that Flight Recorder can be initialized properly after failures
    Configuration defConfig = Configuration.getConfiguration("default");
    Recording r = new Recording(defConfig);
    r.start();
    r.stop();
    r.dump(Paths.get("recording.jfr"));
    r.close();
}
 
示例4
private static void unLoadEventClass() throws Exception {
    long firstCount = jvm.getUnloadedEventClassCount();
    System.out.println("Initial unloaded count: " + firstCount);
    myClassLoader = null;
    System.out.println("Cleared reference to MyClassLoader");
    long newCount = 0;
    do {
        System.out.println("GC triggered");
        System.gc();
        Thread.sleep(1000);
        newCount = jvm.getUnloadedEventClassCount();
        System.out.println("Unloaded count: " + newCount);
    } while (firstCount + 1 != newCount);
    System.out.println("Event class unloaded!");
    System.out.println("Event classes currently on the heap:");
    for (Class<?> eventClass : JVM.getJVM().getAllEventClasses()) {
    }

}
 
示例5
@SafeVarargs
private static void assertEvents(JVM jvm, boolean inclusion, Class<? extends Event>... targetEvents) {
    final List<Class<? extends Event>> list = jvm.getAllEventClasses();
    for (Class<? extends Event> ev : targetEvents) {
       if (list.contains(ev)) {
           if (inclusion) {
               continue;
           }
           throw new AssertionError(ev.getName() + " in list but should not be!");
       }
       if (!inclusion) {
           continue;
       }
       throw new AssertionError(ev.getName() + " in not in list but should be!");
   }
}
 
示例6
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);
}
 
示例7
public static void main(String... args) throws Exception {
    JVM jvm = JVM.getJVM();
    // Ensure that repeated failures can be handled
    for (int i = 1; i < 4; i++) {
        System.out.println("About to try failed initialization, attempt " + i + " out of 3");
        assertFailedInitialization(jvm);
        System.out.println("As expected, initialization failed.");
    }
    // Ensure that Flight Recorder can be initialized properly after failures
    Configuration defConfig = Configuration.getConfiguration("default");
    Recording r = new Recording(defConfig);
    r.start();
    r.stop();
    r.dump(Paths.get("recording.jfr"));
    r.close();
}
 
示例8
private static void checkNoDirectAccess() throws Throwable {
    assertPermission(() -> {
                sm.checkPackageAccess("jdk.jfr.internal");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                Class.forName("jdk.jfr.internal.JVM");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                JVM.getJVM();
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                sm.checkPackageAccess("jdk.jfr.events");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                Class.forName("jdk.jfr.events.AbstractJDKEvent");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                sm.checkPackageAccess("jdk.management.jfr.internal");
            }, RuntimePermission.class, null, false);
}
 
示例9
private static void unLoadEventClass() throws Exception {
    long firstCount = jvm.getUnloadedEventClassCount();
    System.out.println("Initial unloaded count: " + firstCount);
    myClassLoader = null;
    System.out.println("Cleared reference to MyClassLoader");
    long newCount = 0;
    do {
        System.out.println("GC triggered");
        System.gc();
        Thread.sleep(1000);
        newCount = jvm.getUnloadedEventClassCount();
        System.out.println("Unloaded count: " + newCount);
    } while (firstCount + 1 != newCount);
    System.out.println("Event class unloaded!");
    System.out.println("Event classes currently on the heap:");
    for (Class<?> eventClass : JVM.getJVM().getAllEventClasses()) {
        //System.out.println(eventClass + " " + (eventClass.getClassLoader() != null ? eventClass.getClassLoader().getName() : null));
    }

}
 
示例10
@SafeVarargs
private static void assertEvents(JVM jvm, boolean inclusion, Class<? extends Event>... targetEvents) {
    final List<Class<? extends Event>> list = jvm.getAllEventClasses();
    for (Class<? extends Event> ev : targetEvents) {
       if (list.contains(ev)) {
           if (inclusion) {
               continue;
           }
           throw new AssertionError(ev.getName() + " in list but should not be!");
       }
       if (!inclusion) {
           continue;
       }
       throw new AssertionError(ev.getName() + " in not in list but should be!");
   }
}
 
示例11
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);
}
 
示例12
public static void main(String... args) throws Exception {
    JVM jvm = JVM.getJVM();
    // Ensure that repeated failures can be handled
    for (int i = 1; i < 4; i++) {
        System.out.println("About to try failed initialization, attempt " + i + " out of 3");
        assertFailedInitialization(jvm);
        System.out.println("As expected, initialization failed.");
    }
    // Ensure that Flight Recorder can be initialized properly after failures
    Configuration defConfig = Configuration.getConfiguration("default");
    Recording r = new Recording(defConfig);
    r.start();
    r.stop();
    r.dump(Paths.get("recording.jfr"));
    r.close();
}
 
示例13
private static void checkNoDirectAccess() throws Throwable {
    assertPermission(() -> {
                sm.checkPackageAccess("jdk.jfr.internal");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                Class.forName("jdk.jfr.internal.JVM");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                JVM.getJVM();
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                sm.checkPackageAccess("jdk.jfr.events");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                Class.forName("jdk.jfr.events.AbstractJDKEvent");
            }, RuntimePermission.class, null, false);

    assertPermission(() -> {
                sm.checkPackageAccess("jdk.management.jfr.internal");
            }, RuntimePermission.class, null, false);
}
 
示例14
private static void unLoadEventClass() throws Exception {
    long firstCount = jvm.getUnloadedEventClassCount();
    System.out.println("Initial unloaded count: " + firstCount);
    myClassLoader = null;
    System.out.println("Cleared reference to MyClassLoader");
    long newCount = 0;
    do {
        System.out.println("GC triggered");
        System.gc();
        Thread.sleep(1000);
        newCount = jvm.getUnloadedEventClassCount();
        System.out.println("Unloaded count: " + newCount);
    } while (firstCount + 1 != newCount);
    System.out.println("Event class unloaded!");
    System.out.println("Event classes currently on the heap:");
    for (Class<?> eventClass : JVM.getJVM().getAllEventClasses()) {
        //System.out.println(eventClass + " " + (eventClass.getClassLoader() != null ? eventClass.getClassLoader().getName() : null));
    }

}
 
示例15
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);
    }
}
 
示例16
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);
}
 
示例17
public static void main(String... args) throws ClassNotFoundException {
    JVM jvm = JVM.getJVM();
    // before creating  native
    assertEmptyEventList(jvm);
    jvm.createNativeJFR();
    // after creating native
    assertEmptyEventList(jvm);

    // Test event class load is triggered and only once
    Class<? extends Event> clazz = initialize("jdk.jfr.jvm.HelloWorldEvent1");
    // check that the event class is registered
    assertEventsIncluded(jvm, clazz);
    // second active use of the same event class should not add another class
    // to the list - it would already be loaded
    clazz = initialize(clazz);
    assertEventsIncluded(jvm, clazz);

    // second event class
    Class<? extends Event> clazz2 = initialize("jdk.jfr.jvm.HelloWorldEvent2");
    // the list of event classes should now have two classes registered
    assertEventsIncluded(jvm, clazz, clazz2);

    // verify that an abstract event class is not included
    Class<? extends Event> abstractClass = initialize(MyAbstractEvent.class); // to run <clinit>
    assertEventsExcluded(jvm, abstractClass);

    // verify that a class that is yet to run its <clinit> is not included in the list of event classes
    assertEventsExcluded(jvm, MyUnInitializedEvent.class);

    // ensure old classes are not lost
    assertEventsIncluded(jvm, clazz, clazz2);

    jvm.destroyNativeJFR();
}
 
示例18
public static void main(String... args) {
    JVM jvm = JVM.getJVM();
    jvm.createNativeJFR();
    EventWriter writer = EventWriter.getEventWriter();
    assertNotNull(writer, "EventWriter should not be null");
    jvm.destroyNativeJFR();
}
 
示例19
public static void main(String... args) {
    assertClassIds();
    JVM jvm = JVM.getJVM();
    jvm.createNativeJFR();
    assertClassIds();
    jvm.destroyNativeJFR();
    assertClassIds();
}
 
示例20
public static void main(String... args) {
    JVM jvm = JVM.getJVM();
    jvm.createNativeJFR();
    jvm.setFileNotification(MAX_CHUNK_SIZE);
    jvm.beginRecording();
    jvm.endRecording();
    jvm.destroyNativeJFR();
}
 
示例21
private static void assertFailedInitialization(JVM jvm) throws Exception {
    try {
        jvm.createFailedNativeJFR();
        throw new Exception("Expected failure when creating native JFR");
    } catch (IllegalStateException ise) {
        String message = ise.getMessage();
        if (!message.equals("Unable to start Jfr")) {
            throw new Exception("Expected failure on initialization of native JFR");
        }
    }
}
 
示例22
public static void main(String... args) throws InterruptedException {
    // Not enabled
    assertCounterTime();

    JVM jvm = JVM.getJVM();
    jvm.createNativeJFR();
    assertCounterTime();
    // Enabled
    jvm.destroyNativeJFR();
}
 
示例23
private static void assertCounterTime() throws InterruptedException {
    long time1 = JVM.counterTime();
    assertGreaterThan(time1, 0L, "Counter time can't be negative.");

    Thread.sleep(1);

    long time2 = JVM.counterTime();
    assertGreaterThan(time2, time1, "Counter time must be increasing.");
}
 
示例24
public static void main(String... args) throws InterruptedException {

        JVM jvm = JVM.getJVM();
        String pid = jvm.getPid();

        try {
            String name = ManagementFactory.getRuntimeMXBean().getName();
            String managementPid = name.split("@")[0];
            assertEquals(pid, managementPid, "Pid doesn't match value returned by RuntimeMXBean");
        } catch (NumberFormatException nfe) {
            throw new AssertionError("Pid must be numeric, but was '" + pid + "'");
        }
    }
 
示例25
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);
    }
}
 
示例26
public String getPid() {
    // Invoking ProcessHandle.current().pid() would require loading more
    // classes during startup so instead JVM.getJVM().getPid() is used.
    // The pid will not be exposed to running Java application, only when starting
    // JFR from command line (-XX:StartFlightRecordin) or jcmd (JFR.start and JFR.check)
    return JVM.getJVM().getPid();
}
 
示例27
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);
}
 
示例28
public static void main(String... args) throws ClassNotFoundException {
    JVM jvm = JVM.getJVM();
    // before creating  native
    assertEmptyEventList(jvm);
    jvm.createNativeJFR();
    // after creating native
    assertEmptyEventList(jvm);

    // Test event class load is triggered and only once
    Class<? extends Event> clazz = initialize("jdk.jfr.jvm.HelloWorldEvent1");
    // check that the event class is registered
    assertEventsIncluded(jvm, clazz);
    // second active use of the same event class should not add another class
    // to the list - it would already be loaded
    clazz = initialize(clazz);
    assertEventsIncluded(jvm, clazz);

    // second event class
    Class<? extends Event> clazz2 = initialize("jdk.jfr.jvm.HelloWorldEvent2");
    // the list of event classes should now have two classes registered
    assertEventsIncluded(jvm, clazz, clazz2);

    // verify that an abstract event class is not included
    Class<? extends Event> abstractClass = initialize(MyAbstractEvent.class); // to run <clinit>
    assertEventsExcluded(jvm, abstractClass);

    // verify that a class that is yet to run its <clinit> is not included in the list of event classes
    assertEventsExcluded(jvm, MyUnInitializedEvent.class);

    // ensure old classes are not lost
    assertEventsIncluded(jvm, clazz, clazz2);

    jvm.destroyNativeJFR();
}
 
示例29
private static void assertLogLevelUnderflow() {
    try {
        JVM.log(JFR_LOG_TAG.ordinal(), 0, (String)null);
    } catch (IllegalArgumentException ex) {
        // Expected
    }
}
 
示例30
private static void assertLogLevelOverflow() {
    try {
        JVM.log(JFR_LOG_TAG.ordinal(), LogLevel.ERROR.ordinal() + 1, (String)null);
    } catch (IllegalArgumentException ex) {
        // Expected
    }
}