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 package gc.stress;
  24 
  25 import java.util.Random;
  26 
  27 import gc.CriticalNative;
  28 
  29 /*
  30  * @test CriticalNativeStressEpsilon
  31  * @key gc
  32  * @bug 8199868
  33  * @library /
  34  * @requires  vm.bits == "64" & (os.arch =="x86_64" | os.arch == "amd64")  & vm.gc.Epsilon & !vm.graal.enabled
  35  * @summary test argument pinning by nmethod wrapper of critical native method
  36  * @run main/othervm/native -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xcomp -Xmx1G -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  37  */
  38 
  39 /*
  40  * @test CriticalNativeStressShenandoah
  41  * @key gc
  42  * @bug 8199868
  43  * @library /
  44  * @requires  vm.bits == "64" & (os.arch =="x86_64" | os.arch == "amd64") & vm.gc.Shenandoah & !vm.graal.enabled
  45  * @summary test argument pinning by nmethod wrapper of critical native method
  46  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive    -XX:-ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  47  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive    -XX:+ShenandoahDegeneratedGC -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  48  *
  49  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  50  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC                                       -Xcomp -Xmx256M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  51  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=traversal        -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  52  * @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=traversal -XX:ShenandoahGCHeuristics=aggressive -Xcomp -Xmx512M -XX:+CriticalJNINatives gc.stress.CriticalNativeStress
  53  */
  54 public class CriticalNativeStress {
  55     private static Random rand = new Random();
  56 
  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() {
  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() {
 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         public Case1Runner() {
 171             start();
 172         }
 173 
 174         public void run() {
 175             for (int index = 0; index < CYCLES; index ++) {
 176                 run_test_case1();
 177             }
 178         }
 179     }
 180 
 181     static class Case2Runner extends Thread {
 182         public Case2Runner() {
 183             start();
 184         }
 185 
 186         public void run() {
 187             for (int index = 0; index < CYCLES; index ++) {
 188                 run_test_case2();
 189             }
 190         }
 191     }
 192 
 193     public static void main(String[] args) {
 194         Thread[] thrs = new Thread[THREAD_PER_CASE * 2];
 195         for (int index = 0; index < thrs.length; index = index + 2) {
 196             thrs[index] = new Case1Runner();
 197             thrs[index + 1] = new Case2Runner();
 198         }
 199 
 200         for (int index = 0; index < thrs.length; index ++) {
 201             try {
 202                 thrs[index].join();
 203             } catch (Exception e) {
 204                 e.printStackTrace();
 205             }
 206         }
 207     }
 208 }