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" 37 * @requires vm.gc.Epsilon 38 * @summary test argument pinning by nmethod wrapper of critical native method 39 * @run main/othervm/native -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xcomp -Xmx1G -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 40 */ 41 42 /* 43 * @test CriticalNativeStressShenandoah 44 * @key randomness 45 * @bug 8199868 46 * @library / /test/lib 47 * @requires os.arch =="x86_64" | os.arch == "amd64" | os.arch=="x86" | os.arch=="i386" 48 * @requires vm.gc.Shenandoah 49 * @summary test argument pinning by nmethod wrapper of critical native method 50 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:-ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 51 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:+ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 52 * 53 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 54 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xcomp -Xmx256M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 55 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 56 * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress 57 */ 58 public class CriticalNativeStress { 59 // CYCLES and THREAD_PER_CASE are used to tune the tests for different GC settings, 60 // so that they can execrise enough GC cycles and not OOM 61 private static int CYCLES = Integer.getInteger("cycles", 3); 62 private static int THREAD_PER_CASE = Integer.getInteger("threadPerCase", 1); 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(Random rand) { 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 = CriticalNative.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(Random rand) { 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 = CriticalNative.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 private final Random rand; 173 public Case1Runner() { 174 rand = new Random(Utils.getRandomInstance().nextLong()); 175 start(); 176 } 177 178 public void run() { 179 for (int index = 0; index < CYCLES; index ++) { 180 run_test_case1(rand); 181 } 182 } 183 } 184 185 static class Case2Runner extends Thread { 186 private final Random rand; 187 public Case2Runner() { 188 rand = new Random(Utils.getRandomInstance().nextLong()); 189 start(); 190 } 191 192 public void run() { 193 for (int index = 0; index < CYCLES; index ++) { 194 run_test_case2(rand); 195 } 196 } 197 } 198 199 public static void main(String[] args) { 200 Thread[] thrs = new Thread[THREAD_PER_CASE * 2]; 201 for (int index = 0; index < thrs.length; index = index + 2) { 202 thrs[index] = new Case1Runner(); 203 thrs[index + 1] = new Case2Runner(); 204 } 205 206 for (int index = 0; index < thrs.length; index ++) { 207 try { 208 thrs[index].join(); 209 } catch (Exception e) { 210 e.printStackTrace(); 211 } 212 } 213 } 214 }