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