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