author | goetz |
Fri, 20 Jul 2018 09:46:57 +0200 | |
changeset 51214 | 67736b4846a0 |
parent 50225 | 2942ae532175 |
permissions | -rw-r--r-- |
50113 | 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.Path; |
|
32 |
||
33 |
import jdk.jfr.Event; |
|
34 |
import jdk.jfr.EventType; |
|
35 |
import jdk.jfr.FlightRecorder; |
|
36 |
import jdk.jfr.Recording; |
|
37 |
import jdk.jfr.consumer.RecordingFile; |
|
38 |
import jdk.jfr.internal.JVM; |
|
50225
2942ae532175
8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents:
50113
diff
changeset
|
39 |
import jdk.test.lib.Utils; |
50113 | 40 |
|
51214 | 41 |
/** |
50113 | 42 |
* @test |
43 |
* @key jfr |
|
44 |
* @summary Test that verifies event metadata is removed when an event class is unloaded. |
|
51214 | 45 |
* @requires vm.hasJFR |
50113 | 46 |
* |
47 |
* @library /test/lib |
|
48 |
* @modules jdk.jfr/jdk.jfr.internal |
|
49 |
* java.base/jdk.internal.misc |
|
50 |
* |
|
51 |
* @run main/othervm -Xlog:class+unload -Xlog:gc jdk.jfr.api.metadata.eventtype.TestUnloadingEventClass |
|
52 |
*/ |
|
53 |
public class TestUnloadingEventClass { |
|
54 |
||
55 |
private static final String EVENT_NAME = "jdk.jfr.api.metadata.eventtype.TestUnloadingEventClass$ToBeUnloaded"; |
|
56 |
||
57 |
public static class ToBeUnloaded extends Event { |
|
58 |
} |
|
59 |
||
60 |
static public class MyClassLoader extends ClassLoader { |
|
61 |
public MyClassLoader() { |
|
62 |
super("MyClassLoader", null); |
|
63 |
} |
|
64 |
||
65 |
public final Class<?> defineClass(String name, byte[] b) { |
|
66 |
return super.defineClass(name, b, 0, b.length); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
private static final JVM jvm = JVM.getJVM(); |
|
71 |
public static ClassLoader myClassLoader; |
|
72 |
||
73 |
public static void main(String[] args) throws Throwable { |
|
74 |
assertEventTypeNotAvailable(); |
|
75 |
myClassLoader = createClassLoaderWithEventClass(); |
|
76 |
||
77 |
try (Recording r0 = new Recording()) { |
|
78 |
r0.start(); |
|
79 |
r0.stop(); |
|
80 |
if (getEventType(r0, 0, EVENT_NAME) == null) { |
|
81 |
throw new Exception("Expected event class to have corresponding event type"); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
try (Recording r1 = new Recording(); Recording r2 = new Recording(); Recording r3 = new Recording()) { |
|
86 |
r1.start(); |
|
87 |
r2.start(); |
|
88 |
System.out.println("Class loader with name " + myClassLoader.getName() + " is on the heap"); |
|
89 |
unLoadEventClass(); |
|
90 |
r3.start(); |
|
91 |
||
92 |
assertEventTypeNotAvailable(); |
|
93 |
r3.stop(); |
|
94 |
r2.stop(); |
|
95 |
r1.stop(); |
|
96 |
||
97 |
if (getEventType(r1, 1, EVENT_NAME) == null) { |
|
98 |
throw new Exception("Expected event class to have corresponding event type in recording with all chunks"); |
|
99 |
} |
|
100 |
if (getEventType(r2, 2, EVENT_NAME) == null) { |
|
101 |
throw new Exception("Expected event class to have corresponding event type in recording where event class was unloaded"); |
|
102 |
} |
|
103 |
if (getEventType(r3, 3, EVENT_NAME) != null) { |
|
104 |
throw new Exception("Unexpected metadata found for event class tha has been unloaded."); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private static MyClassLoader createClassLoaderWithEventClass() throws Exception { |
|
110 |
String resourceName = EVENT_NAME.replace('.', '/') + ".class"; |
|
111 |
try (InputStream is = TestUnloadingEventClass.class.getClassLoader().getResourceAsStream(resourceName)) { |
|
112 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
113 |
byte[] buffer = new byte[4096]; |
|
114 |
int byteValue = 0; |
|
115 |
while ((byteValue = is.read(buffer, 0, buffer.length)) != -1) { |
|
116 |
baos.write(buffer, 0, byteValue); |
|
117 |
} |
|
118 |
baos.flush(); |
|
119 |
MyClassLoader myClassLoader = new MyClassLoader(); |
|
120 |
Class<?> eventClass = myClassLoader.defineClass(EVENT_NAME, baos.toByteArray()); |
|
121 |
if (eventClass == null) { |
|
122 |
throw new Exception("Could not define test class"); |
|
123 |
} |
|
124 |
if (eventClass.getSuperclass() != Event.class) { |
|
125 |
throw new Exception("Superclass should be jdk.jfr.Event"); |
|
126 |
} |
|
127 |
if (eventClass.getSuperclass().getClassLoader() != null) { |
|
128 |
throw new Exception("Class loader of jdk.jfr.Event should be null"); |
|
129 |
} |
|
130 |
if (eventClass.getClassLoader() != myClassLoader) { |
|
131 |
throw new Exception("Incorrect class loader for event class"); |
|
132 |
} |
|
133 |
eventClass.newInstance(); // force <clinit> |
|
134 |
return myClassLoader; |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
private static void unLoadEventClass() throws Exception { |
|
139 |
long firstCount = jvm.getUnloadedEventClassCount(); |
|
140 |
System.out.println("Initial unloaded count: " + firstCount); |
|
141 |
myClassLoader = null; |
|
142 |
System.out.println("Cleared reference to MyClassLoader"); |
|
143 |
long newCount = 0; |
|
144 |
do { |
|
145 |
System.out.println("GC triggered"); |
|
146 |
System.gc(); |
|
147 |
Thread.sleep(1000); |
|
148 |
newCount = jvm.getUnloadedEventClassCount(); |
|
149 |
System.out.println("Unloaded count: " + newCount); |
|
150 |
} while (firstCount + 1 != newCount); |
|
151 |
System.out.println("Event class unloaded!"); |
|
152 |
System.out.println("Event classes currently on the heap:"); |
|
153 |
for (Class<?> eventClass : JVM.getJVM().getAllEventClasses()) { |
|
154 |
System.out.println(eventClass + " " + (eventClass.getClassLoader() != null ? eventClass.getClassLoader().getName() : null)); |
|
155 |
} |
|
156 |
||
157 |
} |
|
158 |
||
159 |
private static void assertEventTypeNotAvailable() throws Exception { |
|
160 |
for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) { |
|
161 |
if (type.getName().equals(EVENT_NAME)) { |
|
162 |
throw new Exception("Event type should not be available"); |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
private static Object getEventType(Recording r, long id, String eventName) throws IOException { |
|
50225
2942ae532175
8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents:
50113
diff
changeset
|
168 |
Path p = Utils.createTempFile("unloading-event-class-recording-" + id + "_", ".jfr"); |
50113 | 169 |
r.dump(p); |
170 |
try (RecordingFile rf = new RecordingFile(p)) { |
|
171 |
for (EventType et : rf.readEventTypes()) { |
|
172 |
if (et.getName().equals(eventName)) { |
|
173 |
return et; |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
} |
|
178 |
return null; |
|
179 |
} |
|
180 |
} |
|
181 |