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 TestJNIGlobalRefs
  25  * @summary Test JNI Global Refs with Shenandoah
  26  * @key gc
  27  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  28  *
  29  * @run main/othervm/native -Xmx1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  30  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  31  *      -XX:+ShenandoahVerify
  32  *      TestJNIGlobalRefs
  33  */
  34 
  35 /* @test TestJNIGlobalRefs
  36  * @summary Test JNI Global Refs with Shenandoah
  37  * @key gc
  38  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  39  *
  40  * @run main/othervm/native -Xmx1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  41  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  42  *      TestJNIGlobalRefs
  43  */
  44 
  45 import java.util.Arrays;
  46 import java.util.Random;
  47 
  48 public class TestJNIGlobalRefs {
  49     static {
  50         System.loadLibrary("TestJNIGlobalRefs");
  51     }
  52 
  53     private static final int TIME_MSEC = 120000;
  54     private static final int ARRAY_SIZE = 10000;
  55 
  56     private static native void makeGlobalRef(Object o);
  57     private static native void makeWeakGlobalRef(Object o);
  58     private static native Object readGlobalRef();
  59     private static native Object readWeakGlobalRef();
  60 
  61     public static void main(String[] args) throws Throwable {
  62         seedGlobalRef();
  63         seedWeakGlobalRef();
  64         long start = System.currentTimeMillis();
  65         long current = start;
  66         while (current - start < TIME_MSEC) {
  67             testGlobal();
  68             testWeakGlobal();
  69             Thread.sleep(1);
  70             current = System.currentTimeMillis();
  71         }
  72     }
  73 
  74     private static void seedGlobalRef() {
  75         int[] a = new int[ARRAY_SIZE];
  76         fillArray(a, 1337);
  77         makeGlobalRef(a);
  78     }
  79 
  80     private static void seedWeakGlobalRef() {
  81         int[] a = new int[ARRAY_SIZE];
  82         fillArray(a, 8080);
  83         makeWeakGlobalRef(a);
  84     }
  85 
  86     private static void testGlobal() {
  87         int[] a = (int[]) readGlobalRef();
  88         checkArray(a, 1337);
  89     }
  90 
  91     private static void testWeakGlobal() {
  92         int[] a = (int[]) readWeakGlobalRef();
  93         if (a != null) {
  94             checkArray(a, 8080);
  95         } else {
  96             // weak reference is cleaned, recreate:
  97             seedWeakGlobalRef();
  98         }
  99     }
 100 
 101     private static void fillArray(int[] array, int seed) {
 102         Random r = new Random(seed);
 103         for (int i = 0; i < ARRAY_SIZE; i++) {
 104             array[i] = r.nextInt();
 105         }
 106     }
 107 
 108     private static void checkArray(int[] array, int seed) {
 109         Random r = new Random(seed);
 110         if (array.length != ARRAY_SIZE) {
 111             throw new IllegalStateException("Illegal array length: " + array.length + ", but expected " + ARRAY_SIZE);
 112         }
 113         for (int i = 0; i < ARRAY_SIZE; i++) {
 114             int actual = array[i];
 115             int expected = r.nextInt();
 116             if (actual != expected) {
 117                 throw new IllegalStateException("Incorrect array data: " + actual + ", but expected " + expected);
 118             }
 119         }
 120     }
 121 }