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