35254
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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.
|
|
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 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 8133348
|
|
26 |
* @summary Tests if reachabilityFence is working
|
|
27 |
*
|
|
28 |
* @run main/othervm -Xint -Dpremature=false ReachabilityFenceTest
|
|
29 |
* @run main/othervm -XX:TieredStopAtLevel=1 -Dpremature=true ReachabilityFenceTest
|
|
30 |
* @run main/othervm -XX:TieredStopAtLevel=2 -Dpremature=true ReachabilityFenceTest
|
|
31 |
* @run main/othervm -XX:TieredStopAtLevel=3 -Dpremature=true ReachabilityFenceTest
|
|
32 |
* @run main/othervm -XX:TieredStopAtLevel=4 -Dpremature=true ReachabilityFenceTest
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.lang.ref.Reference;
|
|
36 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
37 |
|
|
38 |
public class ReachabilityFenceTest {
|
|
39 |
|
|
40 |
/*
|
|
41 |
* Implementation notes:
|
|
42 |
*
|
|
43 |
* This test has positive and negative parts.
|
|
44 |
*
|
|
45 |
* Negative test is "nonFenced", and it tests that absent of reachabilityFence, the object can
|
|
46 |
* be prematurely finalized -- this validates the test itself. Not every VM mode is expected to
|
|
47 |
* prematurely finalize the objects, and -Dpremature option communicates that to test. If a VM mode
|
|
48 |
* passes the negative test, then our understanding of what could happen is correct, and we can
|
|
49 |
* go forward.
|
|
50 |
*
|
|
51 |
* Positive test is "fenced", and it checks that given the reachabilityFence at the end of the block,
|
|
52 |
* the object cannot be finalized. There is no sense running a positive test when premature finalization
|
|
53 |
* is not expected. It is a job for negative test to verify that invariant.
|
|
54 |
*
|
|
55 |
* The test methods should be appropriately compiled, therefore we do several iterations.
|
|
56 |
*/
|
|
57 |
|
|
58 |
// Enough to OSR and compile
|
|
59 |
static final int LOOP_ITERS = Integer.getInteger("LOOP_ITERS", 50000);
|
|
60 |
|
|
61 |
// Enough after which to start triggering GC and finalization
|
|
62 |
static final int WARMUP_LOOP_ITERS = LOOP_ITERS - Integer.getInteger("GC_ITERS", 100);
|
|
63 |
|
|
64 |
// Enough to switch from an OSR'ed method to compiled method
|
|
65 |
static final int MAIN_ITERS = 3;
|
|
66 |
|
|
67 |
static final boolean PREMATURE_FINALIZATION = Boolean.getBoolean("premature");
|
|
68 |
|
|
69 |
public static void main(String... args) {
|
|
70 |
// Negative test
|
|
71 |
boolean finalized = false;
|
|
72 |
for (int c = 0; !finalized && c < MAIN_ITERS; c++) {
|
|
73 |
finalized |= nonFenced();
|
|
74 |
}
|
|
75 |
|
|
76 |
if (PREMATURE_FINALIZATION && !finalized) {
|
|
77 |
throw new IllegalStateException("The object had never been finalized before timeout reached.");
|
|
78 |
}
|
|
79 |
|
|
80 |
if (!PREMATURE_FINALIZATION && finalized) {
|
|
81 |
throw new IllegalStateException("The object had been finalized without a fence, even though we don't expect it.");
|
|
82 |
}
|
|
83 |
|
|
84 |
if (!PREMATURE_FINALIZATION)
|
|
85 |
return;
|
|
86 |
|
|
87 |
// Positive test
|
|
88 |
finalized = false;
|
|
89 |
for (int c = 0; !finalized && c < MAIN_ITERS; c++) {
|
|
90 |
finalized |= fenced();
|
|
91 |
}
|
|
92 |
|
|
93 |
if (finalized) {
|
|
94 |
throw new IllegalStateException("The object had been prematurely finalized.");
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
public static boolean nonFenced() {
|
|
99 |
AtomicBoolean finalized = new AtomicBoolean();
|
|
100 |
MyFinalizeable o = new MyFinalizeable(finalized);
|
|
101 |
|
|
102 |
for (int i = 0; i < LOOP_ITERS; i++) {
|
|
103 |
if (finalized.get()) break;
|
|
104 |
if (i > WARMUP_LOOP_ITERS) {
|
|
105 |
System.gc();
|
|
106 |
System.runFinalization();
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
return finalized.get();
|
|
111 |
}
|
|
112 |
|
|
113 |
public static boolean fenced() {
|
|
114 |
AtomicBoolean finalized = new AtomicBoolean();
|
|
115 |
MyFinalizeable o = new MyFinalizeable(finalized);
|
|
116 |
|
|
117 |
for (int i = 0; i < LOOP_ITERS; i++) {
|
|
118 |
if (finalized.get()) break;
|
|
119 |
if (i > WARMUP_LOOP_ITERS) {
|
|
120 |
System.gc();
|
|
121 |
System.runFinalization();
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
Reference.reachabilityFence(o);
|
|
126 |
|
|
127 |
return finalized.get();
|
|
128 |
}
|
|
129 |
|
|
130 |
private static class MyFinalizeable {
|
|
131 |
private final AtomicBoolean finalized;
|
|
132 |
|
|
133 |
public MyFinalizeable(AtomicBoolean b) {
|
|
134 |
this.finalized = b;
|
|
135 |
}
|
|
136 |
|
|
137 |
@Override
|
|
138 |
protected void finalize() throws Throwable {
|
|
139 |
super.finalize();
|
|
140 |
finalized.set(true);
|
|
141 |
}
|
|
142 |
}
|
|
143 |
} |