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