1 /*
  2  * Copyright (c) 2018, Red Hat, Inc. 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 
 25 /*
 26  * @test id=default
 27  * @summary Test that null references/referents work fine
 28  * @requires vm.gc.Shenandoah
 29  *
 30  * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 31  *      -XX:+UseShenandoahGC
 32  *      -XX:+ShenandoahVerify
 33  *      TestRefprocSanity
 34  *
 35  * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 36  *      -XX:+UseShenandoahGC
 37  *      TestRefprocSanity
 38  *
 39  * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 40  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 41  *      TestRefprocSanity
 42  */
 43 
 44 /*
 45  * @test id=iu
 46  * @summary Test that null references/referents work fine
 47  * @requires vm.gc.Shenandoah
 48  *
 49  * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 50  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 51  *      -XX:+ShenandoahVerify
 52  *      TestRefprocSanity
 53  *
 54  * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 55  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 56  *      TestRefprocSanity
 57  *
 58  * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 59  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 60  *      TestRefprocSanity
 61  */
 62 
 63 import java.lang.ref.*;
 64 
 65 public class TestRefprocSanity {
 66 
 67     static final long TARGET_MB = Long.getLong("target", 1_000); // 1 Gb allocation
 68     static final int WINDOW = 1_000;
 69 
 70     static final Reference<MyObject>[] refs = new Reference[WINDOW];
 71 
 72     static Object sink;
 73 
 74     public static void main(String[] args) throws Exception {
 75         long count = TARGET_MB * 1024 * 1024 / 128;
 76         int rIdx = 0;
 77 
 78         ReferenceQueue rq = new ReferenceQueue();
 79 
 80         for (int c = 0; c < WINDOW; c++) {
 81             refs[c] = select(c, new MyObject(c), rq);
 82         }
 83 
 84         for (int c = 0; c < count; c++) {
 85             verifyRefAt(rIdx);
 86             refs[rIdx] = select(c, new MyObject(rIdx), rq);
 87 
 88             rIdx++;
 89             if (rIdx >= WINDOW) {
 90                 rIdx = 0;
 91             }
 92 
 93             // Do allocations to force GCs
 94             sink = new byte[100];
 95 
 96             // Drain the refqueue
 97             while (rq.poll() != null);
 98         }
 99     }
100 
101     static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) {
102         switch (v % 10) {
103             case 0:  return new SoftReference<MyObject>(null);
104             case 1:  return new SoftReference<MyObject>(null, rq);
105             case 2:  return new SoftReference<MyObject>(ext);
106             case 3:  return new SoftReference<MyObject>(ext, rq);
107             case 4:  return new WeakReference<MyObject>(null);
108             case 5:  return new WeakReference<MyObject>(null, rq);
109             case 6:  return new WeakReference<MyObject>(ext);
110             case 7:  return new WeakReference<MyObject>(ext, rq);
111             case 8:  return new PhantomReference<MyObject>(null, rq);
112             case 9:  return new PhantomReference<MyObject>(ext, rq);
113             default: throw new IllegalStateException();
114         }
115     }
116 
117     static void verifyRefAt(int idx) {
118         Reference<MyObject> ref = refs[idx];
119         MyObject mo = ref.get();
120         if (mo != null && mo.x != idx) {
121             throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx);
122         }
123     }
124 
125     static class MyObject {
126         final int x;
127 
128         public MyObject(int x) {
129             this.x = x;
130         }
131     }
132 
133 }