Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/jni/TestCriticalNativeStress.java
32285 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223import java.util.Random;2425public class TestCriticalNativeStress {26private static Random rand = new Random();2728static {29System.loadLibrary("TestCriticalNative");30}3132static final int CYCLES = 50;33static final int THREAD_PER_CASE = 1;3435static native long sum1(long[] a);3637// More than 6 parameters38static native long sum2(long a1, int[] a2, int[] a3, long[] a4, int[] a5);3940static long sum(long[] a) {41long sum = 0;42for (int index = 0; index < a.length; index++) {43sum += a[index];44}45return sum;46}4748static long sum(int[] a) {49long sum = 0;50for (int index = 0; index < a.length; index++) {51sum += a[index];52}53return sum;54}5556private static volatile String garbage_array[];5758static void create_garbage(int len) {59len = Math.max(len, 1024);60String array[] = new String[len];61for (int index = 0; index < len; index++) {62array[index] = "String " + index;63}64garbage_array = array;65}6667static void run_test_case1() {68int length = rand.nextInt(50) + 1;69long[] arr = new long[length];70for (int index = 0; index < length; index++) {71arr[index] = rand.nextLong() % 10002;72}7374for (int index = 0; index < length; index++) {75create_garbage(index);76}7778long native_sum = sum1(arr);79long java_sum = sum(arr);80if (native_sum != java_sum) {81StringBuffer sb = new StringBuffer("Sums do not match: native = ")82.append(native_sum).append(" java = ").append(java_sum);8384throw new RuntimeException(sb.toString());85}86}8788static void run_test_case2() {89int index;90long a1 = rand.nextLong() % 10245;9192int a2_length = rand.nextInt(50) + 1;93int[] a2 = new int[a2_length];94for (index = 0; index < a2_length; index++) {95a2[index] = rand.nextInt(106);96}9798int a3_length = rand.nextInt(150) + 1;99int[] a3 = new int[a3_length];100for (index = 0; index < a3_length; index++) {101a3[index] = rand.nextInt(3333);102}103104int a4_length = rand.nextInt(200) + 1;105long[] a4 = new long[a4_length];106for (index = 0; index < a4_length; index++) {107a4[index] = rand.nextLong() % 12322;108}109110int a5_length = rand.nextInt(350) + 1;111int[] a5 = new int[a5_length];112for (index = 0; index < a5_length; index++) {113a5[index] = rand.nextInt(3333);114}115116for (index = 0; index < a1; index++) {117create_garbage(index);118}119120long native_sum = sum2(a1, a2, a3, a4, a5);121long java_sum = a1 + sum(a2) + sum(a3) + sum(a4) + sum(a5);122if (native_sum != java_sum) {123StringBuffer sb = new StringBuffer("Sums do not match: native = ")124.append(native_sum).append(" java = ").append(java_sum);125126throw new RuntimeException(sb.toString());127}128}129130static class Case1Runner extends Thread {131public Case1Runner() {132start();133}134135public void run() {136for (int index = 0; index < CYCLES; index++) {137run_test_case1();138}139}140}141142static class Case2Runner extends Thread {143public Case2Runner() {144start();145}146147public void run() {148for (int index = 0; index < CYCLES; index++) {149run_test_case2();150}151}152}153154public static void main(String[] args) {155Thread[] thrs = new Thread[THREAD_PER_CASE * 2];156for (int index = 0; index < thrs.length; index = index + 2) {157thrs[index] = new Case1Runner();158thrs[index + 1] = new Case2Runner();159}160161for (int index = 0; index < thrs.length; index++) {162try {163thrs[index].join();164} catch (Exception e) {165e.printStackTrace();166}167}168}169}170171172