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 TestRefprocSanity
  27  * @summary Test that null references/referents work fine
  28  * @key gc
  29  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  30  *
  31  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  32  *      -XX:+UseShenandoahGC
  33  *      -XX:+ShenandoahVerify
  34  *      TestRefprocSanity
  35  *
  36  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  37  *      -XX:+UseShenandoahGC
  38  *      TestRefprocSanity
  39  *
  40  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  41  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  42  *      TestRefprocSanity
  43  */
  44 
  45 import java.lang.ref.*;
  46 
  47 public class TestRefprocSanity {
  48 
  49     static final long TARGET_MB = Long.getLong("target", 10_000); // 10 Gb allocation
  50     static final int WINDOW = 10_000;
  51 
  52     static final Reference<MyObject>[] refs = new Reference[WINDOW];
  53 
  54     public static void main(String[] args) throws Exception {
  55         long count = TARGET_MB * 1024 * 1024 / 32;
  56         int rIdx = 0;
  57 
  58         ReferenceQueue rq = new ReferenceQueue();
  59 
  60         for (int c = 0; c < WINDOW; c++) {
  61             refs[c] = select(c, new MyObject(c), rq);
  62         }
  63 
  64         for (int c = 0; c < count; c++) {
  65             verifyRefAt(rIdx);
  66             refs[rIdx] = select(c, new MyObject(rIdx), rq);
  67 
  68             rIdx++;
  69             if (rIdx >= WINDOW) {
  70                 rIdx = 0;
  71             }
  72             while (rq.poll() != null); // drain
  73         }
  74     }
  75 
  76     static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) {
  77         switch (v % 10) {
  78             case 0:  return new SoftReference<MyObject>(null);
  79             case 1:  return new SoftReference<MyObject>(null, rq);
  80             case 2:  return new SoftReference<MyObject>(ext);
  81             case 3:  return new SoftReference<MyObject>(ext, rq);
  82             case 4:  return new WeakReference<MyObject>(null);
  83             case 5:  return new WeakReference<MyObject>(null, rq);
  84             case 6:  return new WeakReference<MyObject>(ext);
  85             case 7:  return new WeakReference<MyObject>(ext, rq);
  86             case 8:  return new PhantomReference<MyObject>(null, rq);
  87             case 9:  return new PhantomReference<MyObject>(ext, rq);
  88             default: throw new IllegalStateException();
  89         }
  90     }
  91 
  92     static void verifyRefAt(int idx) {
  93         Reference<MyObject> ref = refs[idx];
  94         MyObject mo = ref.get();
  95         if (mo != null && mo.x != idx) {
  96             throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx);
  97         }
  98     }
  99 
 100     static class MyObject {
 101         final int x;
 102 
 103         public MyObject(int x) {
 104             this.x = x;
 105         }
 106     }
 107 
 108 }