1 /* 2 * Copyright (c) 2018, 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 /* 27 * @test CriticalNativeStressEpsilon 28 * @key gc 29 * @bug 8199868 30 * @requires (os.arch =="x86_64" | os.arch == "amd64") & vm.gc.Epsilon & !vm.graal.enabled 31 * @summary test argument pinning by nmethod wrapper of critical native method 32 * @run main/othervm/native -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xcomp -Xmx1G -XX:+CriticalJNINatives CriticalNativeStress 33 */ 34 35 /* 36 * @test CriticalNativeStressShenandoah 37 * @key gc 38 * @bug 8199868 39 * @requires (os.arch =="x86_64" | os.arch == "amd64") & vm.gc.Shenandoah & !vm.graal.enabled 40 * @summary test argument pinning by nmethod wrapper of critical native method 41 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=passive -XX:-ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 42 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=passive -XX:+ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 43 * 44 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 45 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xcomp -Xmx256M -XX:+CriticalJNINatives CriticalNativeStress 46 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=traversal -Xcomp -Xmx512M -XX:+CriticalJNINatives CriticalNativeStress 47 */ 48 public class CriticalNativeStress { 49 private static Random rand = new Random(); 50 static { 51 System.loadLibrary("CriticalNative"); 52 } 53 54 // CYCLES and THREAD_PER_CASE are used to tune the tests for different GC settings, 55 // so that they can execrise enough GC cycles and not OOM 56 private static int CYCLES = Integer.getInteger("cycles", 3); 57 private static int THREAD_PER_CASE = Integer.getInteger("threadPerCase", 1); 58 59 static native long sum1(long[] a); 60 61 // More than 6 parameters 62 static native long sum2(long a1, int[] a2, int[] a3, long[] a4, int[] a5); 63 64 static long sum(long[] a) { 65 long sum = 0; 66 for (int index = 0; index < a.length; index ++) { 67 sum += a[index]; 68 } 69 return sum; 70 } 71 72 static long sum(int[] a) { 73 long sum = 0; 74 for (int index = 0; index < a.length; index ++) { 75 sum += a[index]; 76 } 77 return sum; 78 } 79 80 private static volatile String garbage_array[]; 81 82 // GC potentially moves arrays passed to critical native methods 83 // if they are not pinned correctly. 84 // Create enough garbages to exercise GC cycles, verify 85 // the arrays are pinned correctly. 86 static void create_garbage(int len) { 87 len = Math.max(len, 1024); 88 String array[] = new String[len]; 89 for (int index = 0; index < len; index ++) { 90 array[index] = "String " + index; 91 } 92 garbage_array = array; 93 } 94 95 // Two test cases with different method signatures: 96 // Tests generate arbitrary length of arrays with 97 // arbitrary values, then calcuate sum of the array 98 // elements with critical native JNI methods and java 99 // methods, and compare the results for correctness. 100 static void run_test_case1() { 101 // Create testing arary with arbitrary length and 102 // values 103 int length = rand.nextInt(50) + 1; 104 long[] arr = new long[length]; 105 for (int index = 0; index < length; index ++) { 106 arr[index] = rand.nextLong() % 1002; 107 } 108 109 // Generate garbages to trigger GCs 110 for (int index = 0; index < length; index ++) { 111 create_garbage(index); 112 } 113 114 // Compare results for correctness. 115 long native_sum = sum1(arr); 116 long java_sum = sum(arr); 117 if (native_sum != java_sum) { 118 StringBuffer sb = new StringBuffer("Sums do not match: native = ") 119 .append(native_sum).append(" java = ").append(java_sum); 120 121 throw new RuntimeException(sb.toString()); 122 } 123 } 124 125 static void run_test_case2() { 126 // Create testing arary with arbitrary length and 127 // values 128 int index; 129 long a1 = rand.nextLong() % 1025; 130 131 int a2_length = rand.nextInt(50) + 1; 132 int[] a2 = new int[a2_length]; 133 for (index = 0; index < a2_length; index ++) { 134 a2[index] = rand.nextInt(106); 135 } 136 137 int a3_length = rand.nextInt(150) + 1; 138 int[] a3 = new int[a3_length]; 139 for (index = 0; index < a3_length; index ++) { 140 a3[index] = rand.nextInt(3333); 141 } 142 143 int a4_length = rand.nextInt(200) + 1; 144 long[] a4 = new long[a4_length]; 145 for (index = 0; index < a4_length; index ++) { 146 a4[index] = rand.nextLong() % 122; 147 } 148 149 int a5_length = rand.nextInt(350) + 1; 150 int[] a5 = new int[a5_length]; 151 for (index = 0; index < a5_length; index ++) { 152 a5[index] = rand.nextInt(333); 153 } 154 155 // Generate garbages to trigger GCs 156 for (index = 0; index < a1; index ++) { 157 create_garbage(index); 158 } 159 160 // Compare results for correctness. 161 long native_sum = sum2(a1, a2, a3, a4, a5); 162 long java_sum = a1 + sum(a2) + sum(a3) + sum(a4) + sum(a5); 163 if (native_sum != java_sum) { 164 StringBuffer sb = new StringBuffer("Sums do not match: native = ") 165 .append(native_sum).append(" java = ").append(java_sum); 166 167 throw new RuntimeException(sb.toString()); 168 } 169 } 170 171 static class Case1Runner extends Thread { 172 public Case1Runner() { 173 start(); 174 } 175 176 public void run() { 177 for (int index = 0; index < CYCLES; index ++) { 178 run_test_case1(); 179 } 180 } 181 } 182 183 static class Case2Runner extends Thread { 184 public Case2Runner() { 185 start(); 186 } 187 188 public void run() { 189 for (int index = 0; index < CYCLES; index ++) { 190 run_test_case2(); 191 } 192 } 193 } 194 195 public static void main(String[] args) { 196 Thread[] thrs = new Thread[THREAD_PER_CASE * 2]; 197 for (int index = 0; index < thrs.length; index = index + 2) { 198 thrs[index] = new Case1Runner(); 199 thrs[index + 1] = new Case2Runner(); 200 } 201 202 for (int index = 0; index < thrs.length; index ++) { 203 try { 204 thrs[index].join(); 205 } catch (Exception e) { 206 e.printStackTrace(); 207 } 208 } 209 } 210 }