1 /*
2 * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 /*
27 * @test id=default
28 * @summary Test that null references/referents work fine
29 * @requires vm.gc.Shenandoah
30 *
31 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
32 * -XX:+UseShenandoahGC
33 * -XX:+ShenandoahVerify
34 * TestRefprocSanity
35 *
36 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
37 * -XX:+UseShenandoahGC
38 * TestRefprocSanity
39 *
40 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
41 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
42 * TestRefprocSanity
43 */
44
45 /*
46 * @test id=generational
47 * @summary Test that null references/referents work fine
48 * @requires vm.gc.Shenandoah
49 *
50 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
51 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
52 * -XX:+ShenandoahVerify
53 * TestRefprocSanity
54 *
55 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
56 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
57 * TestRefprocSanity
58 */
59
60 import java.lang.ref.*;
61
62 public class TestRefprocSanity {
63
64 static final long TARGET_MB = Long.getLong("target", 1_000); // 1 Gb allocation
65 static final int WINDOW = 1_000;
66
67 static final Reference<MyObject>[] refs = new Reference[WINDOW];
68
69 static Object sink;
70
71 public static void main(String[] args) throws Exception {
72 long count = TARGET_MB * 1024 * 1024 / 128;
73 int rIdx = 0;
74
75 ReferenceQueue rq = new ReferenceQueue();
76
77 for (int c = 0; c < WINDOW; c++) {
78 refs[c] = select(c, new MyObject(c), rq);
79 }
80
81 for (int c = 0; c < count; c++) {
82 verifyRefAt(rIdx);
83 refs[rIdx] = select(c, new MyObject(rIdx), rq);
84
85 rIdx++;
86 if (rIdx >= WINDOW) {
87 rIdx = 0;
88 }
89
90 // Do allocations to force GCs
91 sink = new byte[100];
92
93 // Drain the refqueue
94 while (rq.poll() != null);
95 }
96 }
97
98 static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) {
99 switch (v % 10) {
100 case 0: return new SoftReference<MyObject>(null);
101 case 1: return new SoftReference<MyObject>(null, rq);
102 case 2: return new SoftReference<MyObject>(ext);
103 case 3: return new SoftReference<MyObject>(ext, rq);
104 case 4: return new WeakReference<MyObject>(null);
105 case 5: return new WeakReference<MyObject>(null, rq);
106 case 6: return new WeakReference<MyObject>(ext);
107 case 7: return new WeakReference<MyObject>(ext, rq);
108 case 8: return new PhantomReference<MyObject>(null, rq);
109 case 9: return new PhantomReference<MyObject>(ext, rq);
110 default: throw new IllegalStateException();
111 }
112 }
113
114 static void verifyRefAt(int idx) {
115 Reference<MyObject> ref = refs[idx];
116 MyObject mo = ref.get();
117 if (mo != null && mo.x != idx) {
118 throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx);
119 }
120 }
121
122 static class MyObject {
123 final int x;
124
125 public MyObject(int x) {
126 this.x = x;
127 }
128 }
129
130 }