1 /* 2 * Copyright (c) 2018, 2019, Red Hat, Inc. and/or its affiliates. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 package gc.stress; 25 26 import java.util.Random; 27 28 import gc.CriticalNative; 29 import jdk.test.lib.Utils; 30 31 /* 32 * @test CriticalNativeStressEpsilon 33 * @key randomness 34 * @bug 8199868 35 * @library / /test/lib 36 * @requires (os.arch =="x86_64" | os.arch == "amd64" | os.arch=="x86" | os.arch=="i386") & vm.gc.Epsilon & !vm.graal.enabled 37 * @summary test argument pinning by nmethod wrapper of critical native method 38 * @run main/othervm/native -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xcomp -Xmx1G -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 39 */ 40 41 /* 42 * @test CriticalNativeStressShenandoah 43 * @key randomness 44 * @bug 8199868 45 * @library / /test/lib 46 * @requires (os.arch =="x86_64" | os.arch == "amd64" | os.arch=="x86" | os.arch=="i386") & vm.gc.Shenandoah & !vm.graal.enabled 47 * @summary test argument pinning by nmethod wrapper of critical native method 48 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:-ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 49 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:+ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 50 * 51 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 52 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xcomp -Xmx256M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 53 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 54 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 55 */ 56 public class CriticalNativeStress { 57 // CYCLES and THREAD_PER_CASE are used to tune the tests for different GC settings, 58 // so that they can execrise enough GC cycles and not OOM 59 private static int CYCLES = Integer.getInteger("cycles", 3); 60 private static int THREAD_PER_CASE = Integer.getInteger("threadPerCase", 1); 61 62 static long sum(long[] a) { 63 long sum = 0; 64 for (int index = 0; index < a.length; index ++) { 65 sum += a[index]; 66 } 67 return sum; 68 } 69 70 static long sum(int[] a) { 71 long sum = 0; 72 for (int index = 0; index < a.length; index ++) { 73 sum += a[index]; 74 } 75 return sum; 76 } 77 78 private static volatile String garbage_array[]; 79 80 // GC potentially moves arrays passed to critical native methods 81 // if they are not pinned correctly. 82 // Create enough garbages to exercise GC cycles, verify 83 // the arrays are pinned correctly. 84 static void create_garbage(int len) { 85 len = Math.max(len, 1024); 86 String array[] = new String[len]; 87 for (int index = 0; index < len; index ++) { 88 array[index] = "String " + index; 89 } 90 garbage_array = array; 91 } 92 93 // Two test cases with different method signatures: 94 // Tests generate arbitrary length of arrays with 95 // arbitrary values, then calcuate sum of the array 96 // elements with critical native JNI methods and java 97 // methods, and compare the results for correctness. 98 static void run_test_case1(Random rand) { 99 // Create testing arary with arbitrary length and 100 // values 101 int length = rand.nextInt(50) + 1; 102 long[] arr = new long[length]; 103 for (int index = 0; index < length; index ++) { 104 arr[index] = rand.nextLong() % 1002; 105 } 106 107 // Generate garbages to trigger GCs 108 for (int index = 0; index < length; index ++) { 109 create_garbage(index); 110 } 111 112 // Compare results for correctness. 113 long native_sum = CriticalNative.sum1(arr); 114 long java_sum = sum(arr); 115 if (native_sum != java_sum) { 116 StringBuffer sb = new StringBuffer("Sums do not match: native = ") 117 .append(native_sum).append(" java = ").append(java_sum); 118 119 throw new RuntimeException(sb.toString()); 120 } 121 } 122 123 static void run_test_case2(Random rand) { 124 // Create testing arary with arbitrary length and 125 // values 126 int index; 127 long a1 = rand.nextLong() % 1025; 128 129 int a2_length = rand.nextInt(50) + 1; 130 int[] a2 = new int[a2_length]; 131 for (index = 0; index < a2_length; index ++) { 132 a2[index] = rand.nextInt(106); 133 } 134 135 int a3_length = rand.nextInt(150) + 1; 136 int[] a3 = new int[a3_length]; 137 for (index = 0; index < a3_length; index ++) { 138 a3[index] = rand.nextInt(3333); 139 } 140 141 int a4_length = rand.nextInt(200) + 1; 142 long[] a4 = new long[a4_length]; 143 for (index = 0; index < a4_length; index ++) { 144 a4[index] = rand.nextLong() % 122; 145 } 146 147 int a5_length = rand.nextInt(350) + 1; 148 int[] a5 = new int[a5_length]; 149 for (index = 0; index < a5_length; index ++) { 150 a5[index] = rand.nextInt(333); 151 } 152 153 // Generate garbages to trigger GCs 154 for (index = 0; index < a1; index ++) { 155 create_garbage(index); 156 } 157 158 // Compare results for correctness. 159 long native_sum = CriticalNative.sum2(a1, a2, a3, a4, a5); 160 long java_sum = a1 + sum(a2) + sum(a3) + sum(a4) + sum(a5); 161 if (native_sum != java_sum) { 162 StringBuffer sb = new StringBuffer("Sums do not match: native = ") 163 .append(native_sum).append(" java = ").append(java_sum); 164 165 throw new RuntimeException(sb.toString()); 166 } 167 } 168 169 static class Case1Runner extends Thread { 170 private final Random rand; 171 public Case1Runner() { 172 rand = new Random(Utils.getRandomInstance().nextLong()); 173 start(); 174 } 175 176 public void run() { 177 for (int index = 0; index < CYCLES; index ++) { 178 run_test_case1(rand); 179 } 180 } 181 } 182 183 static class Case2Runner extends Thread { 184 private final Random rand; 185 public Case2Runner() { 186 rand = new Random(Utils.getRandomInstance().nextLong()); 187 start(); 188 } 189 190 public void run() { 191 for (int index = 0; index < CYCLES; index ++) { 192 run_test_case2(rand); 193 } 194 } 195 } 196 197 public static void main(String[] args) { 198 Thread[] thrs = new Thread[THREAD_PER_CASE * 2]; 199 for (int index = 0; index < thrs.length; index = index + 2) { 200 thrs[index] = new Case1Runner(); 201 thrs[index + 1] = new Case2Runner(); 202 } 203 204 for (int index = 0; index < thrs.length; index ++) { 205 try { 206 thrs[index].join(); 207 } catch (Exception e) { 208 e.printStackTrace(); 209 } 210 } 211 } 212 }