test/hotspot/jtreg/gc/epsilon/CriticalNativeStress.java
changeset 52925 9c18c9d839d3
parent 52924 420ff459906f
child 52926 38bee05fb0e4
equal deleted inserted replaced
52924:420ff459906f 52925:9c18c9d839d3
     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   static void run_test_case1() {
       
    87     // Create testing arary with arbitrary length and
       
    88     // values
       
    89     int length = rand.nextInt(50) + 1;
       
    90     long[] arr = new long[length];
       
    91     for (int index = 0; index < length; index ++) {
       
    92       arr[index] = rand.nextLong() % 1002;
       
    93     }
       
    94 
       
    95     // Generate garbages to trigger GCs
       
    96     for (int index = 0; index < length; index ++) {
       
    97       create_garbage(index);
       
    98     }
       
    99 
       
   100     // Compare results for correctness.
       
   101     long native_sum = sum1(arr);
       
   102     long java_sum = sum(arr);
       
   103     if (native_sum != java_sum) {
       
   104       StringBuffer sb = new StringBuffer("Sums do not match: native = ")
       
   105         .append(native_sum).append(" java = ").append(java_sum);
       
   106 
       
   107       throw new RuntimeException(sb.toString());
       
   108     }
       
   109   }
       
   110 
       
   111   static void run_test_case2() {
       
   112     // Create testing arary with arbitrary length and
       
   113     // values
       
   114     int index;
       
   115     long a1 = rand.nextLong() % 1025;
       
   116 
       
   117     int a2_length = rand.nextInt(50) + 1;
       
   118     int[] a2 = new int[a2_length];
       
   119     for (index = 0; index < a2_length; index ++) {
       
   120       a2[index] = rand.nextInt(106);
       
   121     }
       
   122 
       
   123     int a3_length = rand.nextInt(150) + 1;
       
   124     int[] a3 = new int[a3_length];
       
   125     for (index = 0; index < a3_length; index ++) {
       
   126       a3[index] = rand.nextInt(3333);
       
   127     }
       
   128 
       
   129     int a4_length = rand.nextInt(200) + 1;
       
   130     long[] a4 = new long[a4_length];
       
   131     for (index = 0; index < a4_length; index ++) {
       
   132       a4[index] = rand.nextLong() % 122;
       
   133     }
       
   134 
       
   135     int a5_length = rand.nextInt(350) + 1;
       
   136     int[] a5 = new int[a5_length];
       
   137     for (index = 0; index < a5_length; index ++) {
       
   138       a5[index] = rand.nextInt(333);
       
   139     }
       
   140 
       
   141     // Generate garbages to trigger GCs
       
   142     for (index = 0; index < a1; index ++) {
       
   143       create_garbage(index);
       
   144     }
       
   145 
       
   146     // Compare results for correctness.
       
   147     long native_sum = sum2(a1, a2, a3, a4, a5);
       
   148     long java_sum = a1 + sum(a2) + sum(a3) + sum(a4) + sum(a5);
       
   149     if (native_sum != java_sum) {
       
   150       StringBuffer sb = new StringBuffer("Sums do not match: native = ")
       
   151         .append(native_sum).append(" java = ").append(java_sum);
       
   152 
       
   153       throw new RuntimeException(sb.toString());
       
   154     }
       
   155   }
       
   156 
       
   157   static class Case1Runner extends Thread {
       
   158     public Case1Runner() {
       
   159       start();
       
   160     }
       
   161 
       
   162     public void run() {
       
   163       for (int index = 0; index < CYCLES; index ++) {
       
   164         run_test_case1();
       
   165       }
       
   166     }
       
   167   }
       
   168 
       
   169   static class Case2Runner extends Thread {
       
   170     public Case2Runner() {
       
   171       start();
       
   172     }
       
   173 
       
   174     public void run() {
       
   175       for (int index = 0; index < CYCLES; index ++) {
       
   176         run_test_case2();
       
   177       }
       
   178     }
       
   179   }
       
   180 
       
   181   public static void main(String[] args) {
       
   182     Thread[] thrs = new Thread[THREAD_PER_CASE * 2];
       
   183     for (int index = 0; index < thrs.length; index = index + 2) {
       
   184       thrs[index] = new Case1Runner();
       
   185       thrs[index + 1] = new Case2Runner();
       
   186     }
       
   187 
       
   188     for (int index = 0; index < thrs.length; index ++) {
       
   189       try {
       
   190         thrs[index].join();
       
   191       } catch (Exception e) {
       
   192         e.printStackTrace();
       
   193       }
       
   194     }
       
   195   }
       
   196 }
       
   197