43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.hotspot.test;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.referentOffset;
|
|
28 |
|
|
29 |
import java.lang.ref.WeakReference;
|
|
30 |
|
58299
|
31 |
import org.graalvm.compiler.api.test.Graal;
|
46640
|
32 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
33 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
58299
|
34 |
import org.graalvm.compiler.hotspot.HotSpotBackend;
|
43972
|
35 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
36 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
55509
|
37 |
import org.graalvm.compiler.nodes.gc.G1PostWriteBarrier;
|
|
38 |
import org.graalvm.compiler.nodes.gc.G1PreWriteBarrier;
|
|
39 |
import org.graalvm.compiler.nodes.gc.G1ReferentFieldReadBarrier;
|
|
40 |
import org.graalvm.compiler.nodes.gc.SerialWriteBarrier;
|
43972
|
41 |
import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
|
|
42 |
import org.graalvm.compiler.nodes.memory.ReadNode;
|
|
43 |
import org.graalvm.compiler.nodes.memory.WriteNode;
|
|
44 |
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
|
|
45 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
46 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
47 |
import org.graalvm.compiler.phases.common.GuardLoweringPhase;
|
|
48 |
import org.graalvm.compiler.phases.common.LoweringPhase;
|
55509
|
49 |
import org.graalvm.compiler.phases.common.WriteBarrierAdditionPhase;
|
43972
|
50 |
import org.graalvm.compiler.phases.common.inlining.InliningPhase;
|
|
51 |
import org.graalvm.compiler.phases.common.inlining.policy.InlineEverythingPolicy;
|
|
52 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
53 |
import org.graalvm.compiler.phases.tiers.MidTierContext;
|
58299
|
54 |
import org.graalvm.compiler.runtime.RuntimeProvider;
|
46640
|
55 |
import org.junit.Assert;
|
|
56 |
import org.junit.Test;
|
43972
|
57 |
|
|
58 |
import jdk.vm.ci.hotspot.HotSpotInstalledCode;
|
|
59 |
import jdk.vm.ci.meta.JavaConstant;
|
|
60 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
61 |
import sun.misc.Unsafe;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* The following unit tests assert the presence of write barriers for both Serial and G1 GCs.
|
|
65 |
* Normally, the tests check for compile time inserted barriers. However, there are the cases of
|
|
66 |
* unsafe loads of the java.lang.ref.Reference.referent field where runtime checks have to be
|
|
67 |
* performed also. For those cases, the unit tests check the presence of the compile-time inserted
|
|
68 |
* barriers. Concerning the runtime checks, the results of variable inputs (object types and
|
|
69 |
* offsets) passed as input parameters can be checked against printed output from the G1 write
|
|
70 |
* barrier snippets. The runtime checks have been validated offline.
|
|
71 |
*/
|
|
72 |
public class WriteBarrierAdditionTest extends HotSpotGraalCompilerTest {
|
|
73 |
|
|
74 |
private final GraalHotSpotVMConfig config = runtime().getVMConfig();
|
|
75 |
|
|
76 |
public static class Container {
|
|
77 |
|
|
78 |
public Container a;
|
|
79 |
public Container b;
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Expected 2 barriers for the Serial GC and 4 for G1 (2 pre + 2 post).
|
|
84 |
*/
|
|
85 |
@Test
|
|
86 |
public void test1() throws Exception {
|
|
87 |
testHelper("test1Snippet", (config.useG1GC) ? 4 : 2);
|
|
88 |
}
|
|
89 |
|
|
90 |
public static void test1Snippet() {
|
|
91 |
Container main = new Container();
|
|
92 |
Container temp1 = new Container();
|
|
93 |
Container temp2 = new Container();
|
|
94 |
main.a = temp1;
|
|
95 |
main.b = temp2;
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Expected 4 barriers for the Serial GC and 8 for G1 (4 pre + 4 post).
|
|
100 |
*/
|
|
101 |
@Test
|
|
102 |
public void test2() throws Exception {
|
|
103 |
testHelper("test2Snippet", config.useG1GC ? 8 : 4);
|
|
104 |
}
|
|
105 |
|
|
106 |
public static void test2Snippet(boolean test) {
|
|
107 |
Container main = new Container();
|
|
108 |
Container temp1 = new Container();
|
|
109 |
Container temp2 = new Container();
|
|
110 |
for (int i = 0; i < 10; i++) {
|
|
111 |
if (test) {
|
|
112 |
main.a = temp1;
|
|
113 |
main.b = temp2;
|
|
114 |
} else {
|
|
115 |
main.a = temp2;
|
|
116 |
main.b = temp1;
|
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
* Expected 4 barriers for the Serial GC and 8 for G1 (4 pre + 4 post).
|
|
123 |
*/
|
|
124 |
@Test
|
|
125 |
public void test3() throws Exception {
|
|
126 |
testHelper("test3Snippet", config.useG1GC ? 8 : 4);
|
|
127 |
}
|
|
128 |
|
|
129 |
public static void test3Snippet() {
|
|
130 |
Container[] main = new Container[10];
|
|
131 |
Container temp1 = new Container();
|
|
132 |
Container temp2 = new Container();
|
|
133 |
for (int i = 0; i < 10; i++) {
|
|
134 |
main[i].a = main[i].b = temp1;
|
|
135 |
}
|
|
136 |
|
|
137 |
for (int i = 0; i < 10; i++) {
|
|
138 |
main[i].a = main[i].b = temp2;
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* Expected 2 barriers for the Serial GC and 5 for G1 (3 pre + 2 post) The (2 or 4) barriers are
|
|
144 |
* emitted while initializing the fields of the WeakReference instance. The extra pre barrier of
|
|
145 |
* G1 concerns the read of the referent field.
|
|
146 |
*/
|
|
147 |
@Test
|
|
148 |
public void test4() throws Exception {
|
|
149 |
testHelper("test4Snippet", config.useG1GC ? 5 : 2);
|
|
150 |
}
|
|
151 |
|
|
152 |
public static Object test4Snippet() {
|
|
153 |
WeakReference<Object> weakRef = new WeakReference<>(new Object());
|
|
154 |
return weakRef.get();
|
|
155 |
}
|
|
156 |
|
|
157 |
static WeakReference<Object> wr = new WeakReference<>(new Object());
|
|
158 |
static Container con = new Container();
|
|
159 |
|
|
160 |
/**
|
58299
|
161 |
* Expected 0 barrier for the Serial GC and 1 for G1. In this test, we load the correct offset
|
|
162 |
* of the WeakReference object so naturally we assert the presence of the pre barrier.
|
43972
|
163 |
*/
|
|
164 |
@Test
|
|
165 |
public void test5() throws Exception {
|
|
166 |
testHelper("test5Snippet", config.useG1GC ? 1 : 0);
|
|
167 |
}
|
|
168 |
|
58299
|
169 |
private static final boolean useCompressedOops = ((HotSpotBackend) Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend()).getRuntime().getVMConfig().useCompressedOops;
|
52956
|
170 |
|
58299
|
171 |
public Object test5Snippet() {
|
|
172 |
return UNSAFE.getObject(wr, useCompressedOops ? 12L : 16L);
|
43972
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely
|
|
177 |
* load the java.lang.ref.Reference.referent field so the pre barier has to be executed.
|
|
178 |
*/
|
|
179 |
@Test
|
|
180 |
public void test6() throws Exception {
|
52956
|
181 |
test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(referentOffset(getMetaAccess())), null);
|
43972
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely
|
|
186 |
* load a matching offset of a wrong object so the pre barier must not be executed.
|
|
187 |
*/
|
|
188 |
@Test
|
|
189 |
public void test7() throws Exception {
|
52956
|
190 |
test2("testUnsafeLoad", UNSAFE, con, Long.valueOf(referentOffset(getMetaAccess())), null);
|
43972
|
191 |
}
|
|
192 |
|
|
193 |
/**
|
|
194 |
* The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely
|
|
195 |
* load a non-matching offset field of the java.lang.ref.Reference object so the pre barier must
|
|
196 |
* not be executed.
|
|
197 |
*/
|
|
198 |
@Test
|
|
199 |
public void test8() throws Exception {
|
51436
|
200 |
test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(config.useCompressedOops ? 20 : 32), null);
|
43972
|
201 |
}
|
|
202 |
|
|
203 |
/**
|
|
204 |
* The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely
|
|
205 |
* load a matching offset+disp field of the java.lang.ref.Reference object so the pre barier
|
|
206 |
* must be executed.
|
|
207 |
*/
|
|
208 |
@Test
|
|
209 |
public void test10() throws Exception {
|
51436
|
210 |
test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(config.useCompressedOops ? 6 : 8), Integer.valueOf(config.useCompressedOops ? 6 : 8));
|
43972
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* The following test concerns the runtime checks of the unsafe loads. In this test, we unsafely
|
|
215 |
* load a non-matching offset+disp field of the java.lang.ref.Reference object so the pre barier
|
|
216 |
* must not be executed.
|
|
217 |
*/
|
|
218 |
@Test
|
|
219 |
public void test9() throws Exception {
|
51436
|
220 |
test2("testUnsafeLoad", UNSAFE, wr, Long.valueOf(config.useCompressedOops ? 10 : 16), Integer.valueOf(config.useCompressedOops ? 10 : 16));
|
43972
|
221 |
}
|
|
222 |
|
|
223 |
static Object[] src = new Object[1];
|
|
224 |
static Object[] dst = new Object[1];
|
|
225 |
|
|
226 |
static {
|
|
227 |
for (int i = 0; i < src.length; i++) {
|
|
228 |
src[i] = new Object();
|
|
229 |
}
|
|
230 |
for (int i = 0; i < dst.length; i++) {
|
|
231 |
dst[i] = new Object();
|
|
232 |
}
|
|
233 |
}
|
|
234 |
|
|
235 |
public static void testArrayCopy(Object a, Object b, Object c) throws Exception {
|
|
236 |
System.arraycopy(a, 0, b, 0, (int) c);
|
|
237 |
}
|
|
238 |
|
|
239 |
@Test
|
|
240 |
public void test11() throws Exception {
|
|
241 |
test2("testArrayCopy", src, dst, dst.length);
|
|
242 |
}
|
|
243 |
|
|
244 |
public static Object testUnsafeLoad(Unsafe theUnsafe, Object a, Object b, Object c) throws Exception {
|
|
245 |
final int offset = (c == null ? 0 : ((Integer) c).intValue());
|
|
246 |
final long displacement = (b == null ? 0 : ((Long) b).longValue());
|
|
247 |
return theUnsafe.getObject(a, offset + displacement);
|
|
248 |
}
|
|
249 |
|
|
250 |
private HotSpotInstalledCode getInstalledCode(String name, boolean withUnsafePrefix) throws Exception {
|
|
251 |
final ResolvedJavaMethod javaMethod = withUnsafePrefix ? getResolvedJavaMethod(WriteBarrierAdditionTest.class, name, Unsafe.class, Object.class, Object.class, Object.class)
|
|
252 |
: getResolvedJavaMethod(WriteBarrierAdditionTest.class, name, Object.class, Object.class, Object.class);
|
|
253 |
final HotSpotInstalledCode installedCode = (HotSpotInstalledCode) getCode(javaMethod);
|
|
254 |
return installedCode;
|
|
255 |
}
|
|
256 |
|
|
257 |
@SuppressWarnings("try")
|
|
258 |
private void testHelper(final String snippetName, final int expectedBarriers) throws Exception, SecurityException {
|
|
259 |
ResolvedJavaMethod snippet = getResolvedJavaMethod(snippetName);
|
46640
|
260 |
DebugContext debug = getDebugContext();
|
|
261 |
try (DebugContext.Scope s = debug.scope("WriteBarrierAdditionTest", snippet)) {
|
|
262 |
StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO, debug);
|
43972
|
263 |
HighTierContext highContext = getDefaultHighTierContext();
|
|
264 |
MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
|
58877
|
265 |
new InliningPhase(new InlineEverythingPolicy(), createCanonicalizerPhase()).apply(graph, highContext);
|
|
266 |
this.createCanonicalizerPhase().apply(graph, highContext);
|
|
267 |
new LoweringPhase(this.createCanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, highContext);
|
43972
|
268 |
new GuardLoweringPhase().apply(graph, midContext);
|
58877
|
269 |
new LoweringPhase(this.createCanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, midContext);
|
55509
|
270 |
new WriteBarrierAdditionPhase().apply(graph, midContext);
|
46640
|
271 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "After Write Barrier Addition");
|
43972
|
272 |
|
|
273 |
int barriers = 0;
|
|
274 |
if (config.useG1GC) {
|
|
275 |
barriers = graph.getNodes().filter(G1ReferentFieldReadBarrier.class).count() + graph.getNodes().filter(G1PreWriteBarrier.class).count() +
|
|
276 |
graph.getNodes().filter(G1PostWriteBarrier.class).count();
|
|
277 |
} else {
|
|
278 |
barriers = graph.getNodes().filter(SerialWriteBarrier.class).count();
|
|
279 |
}
|
|
280 |
if (expectedBarriers != barriers) {
|
|
281 |
Assert.assertEquals(getScheduledGraphString(graph), expectedBarriers, barriers);
|
|
282 |
}
|
|
283 |
for (WriteNode write : graph.getNodes().filter(WriteNode.class)) {
|
|
284 |
if (config.useG1GC) {
|
|
285 |
if (write.getBarrierType() != BarrierType.NONE) {
|
|
286 |
Assert.assertEquals(1, write.successors().count());
|
|
287 |
Assert.assertTrue(write.next() instanceof G1PostWriteBarrier);
|
|
288 |
Assert.assertTrue(write.predecessor() instanceof G1PreWriteBarrier);
|
|
289 |
}
|
|
290 |
} else {
|
|
291 |
if (write.getBarrierType() != BarrierType.NONE) {
|
|
292 |
Assert.assertEquals(1, write.successors().count());
|
|
293 |
Assert.assertTrue(write.next() instanceof SerialWriteBarrier);
|
|
294 |
}
|
|
295 |
}
|
|
296 |
}
|
|
297 |
|
|
298 |
for (ReadNode read : graph.getNodes().filter(ReadNode.class)) {
|
|
299 |
if (read.getBarrierType() != BarrierType.NONE) {
|
|
300 |
Assert.assertTrue(read.getAddress() instanceof OffsetAddressNode);
|
|
301 |
JavaConstant constDisp = ((OffsetAddressNode) read.getAddress()).getOffset().asJavaConstant();
|
|
302 |
Assert.assertNotNull(constDisp);
|
52956
|
303 |
Assert.assertEquals(referentOffset(getMetaAccess()), constDisp.asLong());
|
54914
|
304 |
Assert.assertEquals(BarrierType.WEAK_FIELD, read.getBarrierType());
|
|
305 |
if (config.useG1GC) {
|
|
306 |
Assert.assertTrue(read.next() instanceof G1ReferentFieldReadBarrier);
|
|
307 |
}
|
43972
|
308 |
}
|
|
309 |
}
|
|
310 |
} catch (Throwable e) {
|
46640
|
311 |
throw debug.handle(e);
|
43972
|
312 |
}
|
|
313 |
}
|
|
314 |
|
|
315 |
private void test2(final String snippet, Object... args) throws Exception {
|
|
316 |
HotSpotInstalledCode code = getInstalledCode(snippet, args[0] instanceof Unsafe);
|
|
317 |
code.executeVarargs(args);
|
|
318 |
}
|
|
319 |
}
|