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 /* @test TestPinnedGarbage
  25  * @summary Test that garbage in the pinned region does not crash VM
  26  * @key gc
  27  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  28  *
  29  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  30  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  31  *      -XX:+ShenandoahVerify -XX:+ShenandoahDegeneratedGC
  32  *      TestPinnedGarbage
  33  *
  34  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  35  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  36  *      -XX:+ShenandoahVerify -XX:-ShenandoahDegeneratedGC
  37  *      TestPinnedGarbage
  38  */
  39 
  40 /* @test TestPinnedGarbage
  41  * @summary Test that garbage in the pinned region does not crash VM
  42  * @key gc
  43  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  44  *
  45  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  46  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  47  *      TestPinnedGarbage
  48  *
  49  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
  50  *      -XX:+UseShenandoahGC
  51  *      -XX:+ShenandoahVerify
  52  *      TestPinnedGarbage
  53  */
  54 
  55 import java.util.Arrays;
  56 import java.util.concurrent.*;
  57 
  58 public class TestPinnedGarbage {
  59     static {
  60         System.loadLibrary("TestPinnedGarbage");
  61     }
  62 
  63     private static final int NUM_RUNS      = 1_000;
  64     private static final int OBJS_COUNT    = 1_000;
  65     private static final int GARBAGE_COUNT = 1_000_000;
  66 
  67     private static native void pin(int[] a);
  68     private static native void unpin(int[] a);
  69 
  70     public static void main(String[] args) {
  71         for (int i = 0; i < NUM_RUNS; i++) {
  72             test();
  73         }
  74     }
  75 
  76     private static void test() {
  77         Object[] objs = new Object[OBJS_COUNT];
  78         for (int i = 0; i < OBJS_COUNT; i++) {
  79             objs[i] = new MyClass();
  80         }
  81 
  82         int[] cog = new int[10];
  83         int cogIdx = ThreadLocalRandom.current().nextInt(OBJS_COUNT);
  84         objs[cogIdx] = cog;
  85         pin(cog);
  86 
  87         for (int i = 0; i < GARBAGE_COUNT; i++) {
  88             int rIdx = ThreadLocalRandom.current().nextInt(OBJS_COUNT);
  89             if (rIdx != cogIdx) {
  90                 objs[rIdx] = new MyClass();
  91             }
  92         }
  93 
  94         unpin(cog);
  95     }
  96 
  97     public static class MyClass {
  98         public Object ref = new Object();
  99     }
 100 
 101 }