author | lkorinth |
Wed, 13 Nov 2019 11:37:29 +0100 | |
changeset 59053 | ba6c248cae19 |
parent 51214 | 67736b4846a0 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
59053
ba6c248cae19
8232365: Implementation for JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
lkorinth
parents:
51214
diff
changeset
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 |
package jdk.jfr.event.oldobject; |
|
26 |
||
27 |
import java.time.Instant; |
|
28 |
import java.util.List; |
|
29 |
||
30 |
import jdk.jfr.Event; |
|
31 |
import jdk.jfr.Recording; |
|
32 |
import jdk.jfr.consumer.RecordedClass; |
|
33 |
import jdk.jfr.consumer.RecordedClassLoader; |
|
34 |
import jdk.jfr.consumer.RecordedEvent; |
|
35 |
import jdk.jfr.consumer.RecordedFrame; |
|
36 |
import jdk.jfr.consumer.RecordedStackTrace; |
|
37 |
import jdk.jfr.consumer.RecordedThread; |
|
38 |
import jdk.jfr.internal.test.WhiteBox; |
|
39 |
import jdk.test.lib.Asserts; |
|
40 |
import jdk.test.lib.jfr.EventNames; |
|
41 |
import jdk.test.lib.jfr.Events; |
|
42 |
import jdk.test.lib.jfr.TestClassLoader; |
|
43 |
||
51214 | 44 |
/** |
50113 | 45 |
* @test |
46 |
* @summary The test verifies that an old object sample maintains references to "stale" metadata |
|
51214 | 47 |
* @requires vm.hasJFR |
50113 | 48 |
* @requires vm.gc == "null" |
49 |
* @key jfr |
|
50 |
* @modules jdk.jfr/jdk.jfr.internal.test |
|
51 |
* @library /test/lib /test/jdk |
|
52 |
* @build jdk.jfr.event.oldobject.TestMetadataObject |
|
53 |
* @run main/othervm -XX:TLABSize=2k -Xmx16m jdk.jfr.event.oldobject.TestMetadataRetention |
|
54 |
*/ |
|
55 |
public final class TestMetadataRetention { |
|
56 |
private final static String TEST_PACKAGE = TestMetadataRetention.class.getPackage().getName(); |
|
57 |
private final static String TEST_CLASS_LOADER_NAME = "JFR TestClassLoader"; |
|
58 |
private final static String TEST_CLASS_NAME = TEST_PACKAGE + ".TestMetadataObject"; |
|
59 |
private final static String ALLOCATOR_THREAD_NAME = "TestAllocationThread"; |
|
60 |
||
61 |
public static ClassLoader testClassLoader; |
|
62 |
public static Object leakObject; |
|
63 |
public static Thread allocatorThread; |
|
64 |
public static Class<?> testClass; |
|
65 |
||
66 |
static class ChunkRotation extends Event { |
|
67 |
} |
|
68 |
||
69 |
public static void main(String[] args) throws Throwable { |
|
70 |
WhiteBox.setWriteAllObjectSamples(true); |
|
71 |
while (true) { |
|
72 |
int failedAttempts = 0; |
|
73 |
try (Recording recording = new Recording()) { |
|
74 |
recording.enable(EventNames.OldObjectSample).withStackTrace(); |
|
75 |
recording.enable(EventNames.ClassUnload); |
|
76 |
recording.start(); |
|
77 |
||
78 |
// Setup metadata on the Java heap (class, stack trace and thread) |
|
79 |
testClassLoader = new TestClassLoader(); |
|
80 |
testClass = testClassLoader.loadClass(TEST_CLASS_NAME); |
|
81 |
allocatorThread = new Thread(TestMetadataRetention::allocateLeak, ALLOCATOR_THREAD_NAME); |
|
82 |
allocatorThread.start(); |
|
83 |
allocatorThread.join(); |
|
84 |
||
85 |
// Clear out metadata on the heap |
|
86 |
testClassLoader = null; |
|
87 |
testClass = null; |
|
88 |
allocatorThread = null; |
|
89 |
||
90 |
// System.gc() will trigger class unloading if -XX:+ExplicitGCInvokesConcurrent |
|
59053
ba6c248cae19
8232365: Implementation for JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
lkorinth
parents:
51214
diff
changeset
|
91 |
// is NOT set. If this flag is set G1 will never unload classes on System.gc(). |
ba6c248cae19
8232365: Implementation for JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
lkorinth
parents:
51214
diff
changeset
|
92 |
// As far as the "jfr" key guarantees no VM flags are set from the |
50113 | 93 |
// outside it should be enough with System.gc(). |
94 |
System.gc(); |
|
95 |
||
96 |
// Provoke a chunk rotation, which will flush out ordinary metadata. |
|
97 |
provokeChunkRotation(); |
|
98 |
ChunkRotation cr = new ChunkRotation(); |
|
99 |
cr.commit(); |
|
100 |
recording.stop(); |
|
101 |
||
102 |
List<RecordedEvent> events = Events.fromRecording(recording); |
|
103 |
RecordedEvent chunkRotation = findChunkRotationEvent(events); |
|
104 |
try { |
|
105 |
// Sanity check that class was unloaded |
|
106 |
Events.hasEvent(recording, EventNames.ClassUnload); |
|
107 |
validateClassUnloadEvent(events); |
|
108 |
// Validate that metadata for old object event has survived chunk rotation |
|
109 |
Events.hasEvent(recording, EventNames.OldObjectSample); |
|
110 |
validateOldObjectEvent(events, chunkRotation.getStartTime()); |
|
111 |
} catch (Throwable t) { |
|
112 |
t.printStackTrace(); |
|
113 |
System.out.println("Number of failed attempts " + ++failedAttempts); |
|
114 |
continue; |
|
115 |
} |
|
116 |
break; |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
private static RecordedEvent findChunkRotationEvent(List<RecordedEvent> events) { |
|
122 |
for (RecordedEvent e : events) { |
|
123 |
if (e.getEventType().getName().equals(ChunkRotation.class.getName())) { |
|
124 |
return e; |
|
125 |
} |
|
126 |
} |
|
127 |
Asserts.fail("Could not find chunk rotation event"); |
|
128 |
return null; // Can't happen; |
|
129 |
} |
|
130 |
||
131 |
private static void allocateLeak() { |
|
132 |
try { |
|
133 |
leakObject = testClass.getMethod("startRecurse").invoke(null); |
|
134 |
} catch (Exception e) { |
|
135 |
System.out.println("Could not allocate memory leak!"); |
|
136 |
e.printStackTrace(); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
private static void provokeChunkRotation() { |
|
141 |
try (Recording r = new Recording()) { |
|
142 |
r.start(); |
|
143 |
r.stop(); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
private static void validateClassUnloadEvent(List<RecordedEvent> events) throws Throwable { |
|
148 |
for (RecordedEvent event : events) { |
|
149 |
if (event.getEventType().getName().equals(EventNames.ClassUnload)) { |
|
150 |
RecordedClass unloadedClass = event.getValue("unloadedClass"); |
|
151 |
if (TEST_CLASS_NAME.equals(unloadedClass.getName())) { |
|
152 |
RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader(); |
|
153 |
Asserts.assertEquals(TEST_CLASS_LOADER_NAME, definingClassLoader.getName(), "Expected " + TEST_CLASS_LOADER_NAME + ", got " + definingClassLoader.getType().getName()); |
|
154 |
return; |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
private static void validateOldObjectEvent(List<RecordedEvent> events, Instant chunkRotation) throws Throwable { |
|
161 |
for (RecordedEvent event : events) { |
|
162 |
if (event.getEventType().getName().equals(EventNames.OldObjectSample)) { |
|
163 |
// Only check event after the rotation |
|
164 |
if (!event.getStartTime().isBefore(chunkRotation)) { |
|
165 |
System.out.println(event); |
|
166 |
RecordedThread rt = event.getThread(); |
|
167 |
if (rt.getJavaName().equals(ALLOCATOR_THREAD_NAME)) { |
|
168 |
RecordedStackTrace s = event.getStackTrace(); |
|
169 |
assertStackTrace(s, "startRecurse"); |
|
170 |
assertStackTrace(s, "recurse"); |
|
171 |
return; |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
Asserts.fail("Did not find an old object event with thread " + ALLOCATOR_THREAD_NAME); |
|
178 |
} |
|
179 |
||
180 |
private static void assertStackTrace(RecordedStackTrace stacktrace, final String methodName) { |
|
181 |
for (RecordedFrame f : stacktrace.getFrames()) { |
|
182 |
if (f.getMethod().getName().equals(methodName)) { |
|
183 |
return; |
|
184 |
} |
|
185 |
} |
|
186 |
Asserts.fail("Could not find class " + methodName + " in stack trace " + stacktrace); |
|
187 |
} |
|
188 |
} |