test/jdk/jdk/jfr/api/metadata/eventtype/TestUnloadingEventClass.java
changeset 50113 caf115bb98ad
child 50225 2942ae532175
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jfr.api.metadata.eventtype;
       
    27 
       
    28 import java.io.ByteArrayOutputStream;
       
    29 import java.io.IOException;
       
    30 import java.io.InputStream;
       
    31 import java.nio.file.Files;
       
    32 import java.nio.file.Path;
       
    33 
       
    34 import jdk.jfr.Event;
       
    35 import jdk.jfr.EventType;
       
    36 import jdk.jfr.FlightRecorder;
       
    37 import jdk.jfr.Recording;
       
    38 import jdk.jfr.consumer.RecordingFile;
       
    39 import jdk.jfr.internal.JVM;
       
    40 
       
    41 /*
       
    42  * @test
       
    43  * @key jfr
       
    44  * @summary Test that verifies event metadata is removed when an event class is unloaded.
       
    45  *
       
    46  * @library /test/lib
       
    47  * @modules jdk.jfr/jdk.jfr.internal
       
    48  *          java.base/jdk.internal.misc
       
    49  *
       
    50  * @run main/othervm -Xlog:class+unload -Xlog:gc jdk.jfr.api.metadata.eventtype.TestUnloadingEventClass
       
    51  */
       
    52 public class TestUnloadingEventClass {
       
    53 
       
    54     private static final String EVENT_NAME = "jdk.jfr.api.metadata.eventtype.TestUnloadingEventClass$ToBeUnloaded";
       
    55 
       
    56     public static class ToBeUnloaded extends Event {
       
    57     }
       
    58 
       
    59     static public class MyClassLoader extends ClassLoader {
       
    60         public MyClassLoader() {
       
    61             super("MyClassLoader", null);
       
    62         }
       
    63 
       
    64         public final Class<?> defineClass(String name, byte[] b) {
       
    65             return super.defineClass(name, b, 0, b.length);
       
    66         }
       
    67     }
       
    68 
       
    69     private static final JVM jvm = JVM.getJVM();
       
    70     public static ClassLoader myClassLoader;
       
    71 
       
    72     public static void main(String[] args) throws Throwable {
       
    73         assertEventTypeNotAvailable();
       
    74         myClassLoader = createClassLoaderWithEventClass();
       
    75 
       
    76         try (Recording r0 = new Recording()) {
       
    77             r0.start();
       
    78             r0.stop();
       
    79             if (getEventType(r0, 0, EVENT_NAME) == null) {
       
    80                 throw new Exception("Expected event class to have corresponding event type");
       
    81             }
       
    82         }
       
    83 
       
    84         try (Recording r1 = new Recording(); Recording r2 = new Recording(); Recording r3 = new Recording()) {
       
    85             r1.start();
       
    86             r2.start();
       
    87             System.out.println("Class loader with name " + myClassLoader.getName() + " is on the heap");
       
    88             unLoadEventClass();
       
    89             r3.start();
       
    90 
       
    91             assertEventTypeNotAvailable();
       
    92             r3.stop();
       
    93             r2.stop();
       
    94             r1.stop();
       
    95 
       
    96             if (getEventType(r1, 1, EVENT_NAME) == null) {
       
    97                 throw new Exception("Expected event class to have corresponding event type in recording with all chunks");
       
    98             }
       
    99             if (getEventType(r2, 2, EVENT_NAME) == null) {
       
   100                 throw new Exception("Expected event class to have corresponding event type in recording where event class was unloaded");
       
   101             }
       
   102             if (getEventType(r3, 3, EVENT_NAME) != null) {
       
   103                 throw new Exception("Unexpected metadata found for event class tha has been unloaded.");
       
   104             }
       
   105         }
       
   106     }
       
   107 
       
   108     private static MyClassLoader createClassLoaderWithEventClass() throws Exception {
       
   109         String resourceName = EVENT_NAME.replace('.', '/') + ".class";
       
   110         try (InputStream is = TestUnloadingEventClass.class.getClassLoader().getResourceAsStream(resourceName)) {
       
   111             ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
   112             byte[] buffer = new byte[4096];
       
   113             int byteValue = 0;
       
   114             while ((byteValue = is.read(buffer, 0, buffer.length)) != -1) {
       
   115                 baos.write(buffer, 0, byteValue);
       
   116             }
       
   117             baos.flush();
       
   118             MyClassLoader myClassLoader = new MyClassLoader();
       
   119             Class<?> eventClass = myClassLoader.defineClass(EVENT_NAME, baos.toByteArray());
       
   120             if (eventClass == null) {
       
   121                 throw new Exception("Could not define test class");
       
   122             }
       
   123             if (eventClass.getSuperclass() != Event.class) {
       
   124                 throw new Exception("Superclass should be jdk.jfr.Event");
       
   125             }
       
   126             if (eventClass.getSuperclass().getClassLoader() != null) {
       
   127                 throw new Exception("Class loader of jdk.jfr.Event should be null");
       
   128             }
       
   129             if (eventClass.getClassLoader() != myClassLoader) {
       
   130                 throw new Exception("Incorrect class loader for event class");
       
   131             }
       
   132             eventClass.newInstance(); // force <clinit>
       
   133             return myClassLoader;
       
   134         }
       
   135     }
       
   136 
       
   137     private static void unLoadEventClass() throws Exception {
       
   138         long firstCount = jvm.getUnloadedEventClassCount();
       
   139         System.out.println("Initial unloaded count: " + firstCount);
       
   140         myClassLoader = null;
       
   141         System.out.println("Cleared reference to MyClassLoader");
       
   142         long newCount = 0;
       
   143         do {
       
   144             System.out.println("GC triggered");
       
   145             System.gc();
       
   146             Thread.sleep(1000);
       
   147             newCount = jvm.getUnloadedEventClassCount();
       
   148             System.out.println("Unloaded count: " + newCount);
       
   149         } while (firstCount + 1 != newCount);
       
   150         System.out.println("Event class unloaded!");
       
   151         System.out.println("Event classes currently on the heap:");
       
   152         for (Class<?> eventClass : JVM.getJVM().getAllEventClasses()) {
       
   153             System.out.println(eventClass + " " + (eventClass.getClassLoader() != null ? eventClass.getClassLoader().getName() : null));
       
   154         }
       
   155 
       
   156     }
       
   157 
       
   158     private static void assertEventTypeNotAvailable() throws Exception {
       
   159         for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
       
   160             if (type.getName().equals(EVENT_NAME)) {
       
   161                 throw new Exception("Event type should not be available");
       
   162             }
       
   163         }
       
   164     }
       
   165 
       
   166     private static Object getEventType(Recording r, long id, String eventName) throws IOException {
       
   167         Path p = Files.createTempFile("recording-" + id + "_", ".jfr");
       
   168         r.dump(p);
       
   169         try (RecordingFile rf = new RecordingFile(p)) {
       
   170             for (EventType et : rf.readEventTypes()) {
       
   171                 if (et.getName().equals(eventName)) {
       
   172                     return et;
       
   173                 }
       
   174             }
       
   175 
       
   176         }
       
   177         return null;
       
   178     }
       
   179 }
       
   180