Path: blob/master/test/hotspot/jtreg/gc/shenandoah/jni/TestJNIGlobalRefs.java
41153 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/* @test TestJNIGlobalRefs25* @summary Test JNI Global Refs with Shenandoah26* @requires vm.gc.Shenandoah27*28* @run main/othervm/native -Xmx1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions29* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive30* -XX:+ShenandoahVerify31* TestJNIGlobalRefs32*/3334/* @test TestJNIGlobalRefs35* @summary Test JNI Global Refs with Shenandoah36* @requires vm.gc.Shenandoah37*38* @run main/othervm/native -Xmx1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions39* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive40* TestJNIGlobalRefs41*/4243import java.util.Arrays;44import java.util.Random;4546public class TestJNIGlobalRefs {47static {48System.loadLibrary("TestJNIGlobalRefs");49}5051private static final int TIME_MSEC = 120000;52private static final int ARRAY_SIZE = 10000;5354private static native void makeGlobalRef(Object o);55private static native void makeWeakGlobalRef(Object o);56private static native Object readGlobalRef();57private static native Object readWeakGlobalRef();5859public static void main(String[] args) throws Throwable {60seedGlobalRef();61seedWeakGlobalRef();62long start = System.currentTimeMillis();63long current = start;64while (current - start < TIME_MSEC) {65testGlobal();66testWeakGlobal();67Thread.sleep(1);68current = System.currentTimeMillis();69}70}7172private static void seedGlobalRef() {73int[] a = new int[ARRAY_SIZE];74fillArray(a, 1337);75makeGlobalRef(a);76}7778private static void seedWeakGlobalRef() {79int[] a = new int[ARRAY_SIZE];80fillArray(a, 8080);81makeWeakGlobalRef(a);82}8384private static void testGlobal() {85int[] a = (int[]) readGlobalRef();86checkArray(a, 1337);87}8889private static void testWeakGlobal() {90int[] a = (int[]) readWeakGlobalRef();91if (a != null) {92checkArray(a, 8080);93} else {94// weak reference is cleaned, recreate:95seedWeakGlobalRef();96}97}9899private static void fillArray(int[] array, int seed) {100Random r = new Random(seed);101for (int i = 0; i < ARRAY_SIZE; i++) {102array[i] = r.nextInt();103}104}105106private static void checkArray(int[] array, int seed) {107Random r = new Random(seed);108if (array.length != ARRAY_SIZE) {109throw new IllegalStateException("Illegal array length: " + array.length + ", but expected " + ARRAY_SIZE);110}111for (int i = 0; i < ARRAY_SIZE; i++) {112int actual = array[i];113int expected = r.nextInt();114if (actual != expected) {115throw new IllegalStateException("Incorrect array data: " + actual + ", but expected " + expected);116}117}118}119}120121122