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