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