1 /* 2 * Copyright (c) 2018, 2019, Red Hat, Inc. and/or its affiliates. 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 import java.util.Random; 25 26 /* @test 27 * @requires (os.arch =="x86_64" | os.arch == "amd64" | os.arch=="x86" | os.arch=="i386") & !vm.graal.enabled & vm.gc.Shenandoah 28 * 29 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:+ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 30 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:-ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 31 * 32 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 33 * 34 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xcomp -Xmx256M -XX:+CriticalJNINatives CriticalNativeStress 35 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 36 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 37 */ 38 public class CriticalNativeStress { 39 private static Random rand = new Random(); 40 41 static { 42 System.loadLibrary("CriticalNative"); 43 } 44 45 static final int CYCLES = 50; 46 static final int THREAD_PER_CASE = 1; 47 48 static native long sum1(long[] a); 49 50 // More than 6 parameters 51 static native long sum2(long a1, int[] a2, int[] a3, long[] a4, int[] a5); 52 53 static long sum(long[] a) { 54 long sum = 0; 55 for (int index = 0; index < a.length; index++) { 56 sum += a[index]; 57 } 58 return sum; 59 } 60 61 static long sum(int[] a) { 62 long sum = 0; 63 for (int index = 0; index < a.length; index++) { 64 sum += a[index]; 65 } 66 return sum; 67 } 68 69 private static volatile String garbage_array[]; 70 71 static void create_garbage(int len) { 72 len = Math.max(len, 1024); 73 String array[] = new String[len]; 74 for (int index = 0; index < len; index++) { 75 array[index] = "String " + index; 76 } 77 garbage_array = array; 78 } 79 80 static void run_test_case1() { 81 int length = rand.nextInt(50) + 1; 82 long[] arr = new long[length]; 83 for (int index = 0; index < length; index++) { 84 arr[index] = rand.nextLong() % 10002; 85 } 86 87 for (int index = 0; index < length; index++) { 88 create_garbage(index); 89 } 90 91 long native_sum = sum1(arr); 92 long java_sum = sum(arr); 93 if (native_sum != java_sum) { 94 StringBuffer sb = new StringBuffer("Sums do not match: native = ") 95 .append(native_sum).append(" java = ").append(java_sum); 96 97 throw new RuntimeException(sb.toString()); 98 } 99 } 100 101 static void run_test_case2() { 102 int index; 103 long a1 = rand.nextLong() % 10245; 104 105 int a2_length = rand.nextInt(50) + 1; 106 int[] a2 = new int[a2_length]; 107 for (index = 0; index < a2_length; index++) { 108 a2[index] = rand.nextInt(106); 109 } 110 111 int a3_length = rand.nextInt(150) + 1; 112 int[] a3 = new int[a3_length]; 113 for (index = 0; index < a3_length; index++) { 114 a3[index] = rand.nextInt(3333); 115 } 116 117 int a4_length = rand.nextInt(200) + 1; 118 long[] a4 = new long[a4_length]; 119 for (index = 0; index < a4_length; index++) { 120 a4[index] = rand.nextLong() % 12322; 121 } 122 123 int a5_length = rand.nextInt(350) + 1; 124 int[] a5 = new int[a5_length]; 125 for (index = 0; index < a5_length; index++) { 126 a5[index] = rand.nextInt(3333); 127 } 128 129 for (index = 0; index < a1; index++) { 130 create_garbage(index); 131 } 132 133 long native_sum = sum2(a1, a2, a3, a4, a5); 134 long java_sum = a1 + sum(a2) + sum(a3) + sum(a4) + sum(a5); 135 if (native_sum != java_sum) { 136 StringBuffer sb = new StringBuffer("Sums do not match: native = ") 137 .append(native_sum).append(" java = ").append(java_sum); 138 139 throw new RuntimeException(sb.toString()); 140 } 141 } 142 143 static class Case1Runner extends Thread { 144 public Case1Runner() { 145 start(); 146 } 147 148 public void run() { 149 for (int index = 0; index < CYCLES; index++) { 150 run_test_case1(); 151 } 152 } 153 } 154 155 static class Case2Runner extends Thread { 156 public Case2Runner() { 157 start(); 158 } 159 160 public void run() { 161 for (int index = 0; index < CYCLES; index++) { 162 run_test_case2(); 163 } 164 } 165 } 166 167 public static void main(String[] args) { 168 Thread[] thrs = new Thread[THREAD_PER_CASE * 2]; 169 for (int index = 0; index < thrs.length; index = index + 2) { 170 thrs[index] = new Case1Runner(); 171 thrs[index + 1] = new Case2Runner(); 172 } 173 174 for (int index = 0; index < thrs.length; index++) { 175 try { 176 thrs[index].join(); 177 } catch (Exception e) { 178 e.printStackTrace(); 179 } 180 } 181 } 182 }