1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   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 public class TestCriticalNativeStress {
  27     private static Random rand = new Random();
  28 
  29     static {
  30         System.loadLibrary("TestCriticalNative");
  31     }
  32 
  33     static final int CYCLES = 50;
  34     static final int THREAD_PER_CASE = 1;
  35 
  36     static native long sum1(long[] a);
  37 
  38     // More than 6 parameters
  39     static native long sum2(long a1, int[] a2, int[] a3, long[] a4, int[] a5);
  40 
  41     static long sum(long[] a) {
  42         long sum = 0;
  43         for (int index = 0; index < a.length; index++) {
  44             sum += a[index];
  45         }
  46         return sum;
  47     }
  48 
  49     static long sum(int[] a) {
  50         long sum = 0;
  51         for (int index = 0; index < a.length; index++) {
  52             sum += a[index];
  53         }
  54         return sum;
  55     }
  56 
  57     private static volatile String garbage_array[];
  58 
  59     static void create_garbage(int len) {
  60         len = Math.max(len, 1024);
  61         String array[] = new String[len];
  62         for (int index = 0; index < len; index++) {
  63             array[index] = "String " + index;
  64         }
  65         garbage_array = array;
  66     }
  67 
  68     static void run_test_case1() {
  69         int length = rand.nextInt(50) + 1;
  70         long[] arr = new long[length];
  71         for (int index = 0; index < length; index++) {
  72             arr[index] = rand.nextLong() % 10002;
  73         }
  74 
  75         for (int index = 0; index < length; index++) {
  76             create_garbage(index);
  77         }
  78 
  79         long native_sum = sum1(arr);
  80         long java_sum = sum(arr);
  81         if (native_sum != java_sum) {
  82             StringBuffer sb = new StringBuffer("Sums do not match: native = ")
  83                     .append(native_sum).append(" java = ").append(java_sum);
  84 
  85             throw new RuntimeException(sb.toString());
  86         }
  87     }
  88 
  89     static void run_test_case2() {
  90         int index;
  91         long a1 = rand.nextLong() % 10245;
  92 
  93         int a2_length = rand.nextInt(50) + 1;
  94         int[] a2 = new int[a2_length];
  95         for (index = 0; index < a2_length; index++) {
  96             a2[index] = rand.nextInt(106);
  97         }
  98 
  99         int a3_length = rand.nextInt(150) + 1;
 100         int[] a3 = new int[a3_length];
 101         for (index = 0; index < a3_length; index++) {
 102             a3[index] = rand.nextInt(3333);
 103         }
 104 
 105         int a4_length = rand.nextInt(200) + 1;
 106         long[] a4 = new long[a4_length];
 107         for (index = 0; index < a4_length; index++) {
 108             a4[index] = rand.nextLong() % 12322;
 109         }
 110 
 111         int a5_length = rand.nextInt(350) + 1;
 112         int[] a5 = new int[a5_length];
 113         for (index = 0; index < a5_length; index++) {
 114             a5[index] = rand.nextInt(3333);
 115         }
 116 
 117         for (index = 0; index < a1; index++) {
 118             create_garbage(index);
 119         }
 120 
 121         long native_sum = sum2(a1, a2, a3, a4, a5);
 122         long java_sum = a1 + sum(a2) + sum(a3) + sum(a4) + sum(a5);
 123         if (native_sum != java_sum) {
 124             StringBuffer sb = new StringBuffer("Sums do not match: native = ")
 125                     .append(native_sum).append(" java = ").append(java_sum);
 126 
 127             throw new RuntimeException(sb.toString());
 128         }
 129     }
 130 
 131     static class Case1Runner extends Thread {
 132         public Case1Runner() {
 133             start();
 134         }
 135 
 136         public void run() {
 137             for (int index = 0; index < CYCLES; index++) {
 138                 run_test_case1();
 139             }
 140         }
 141     }
 142 
 143     static class Case2Runner extends Thread {
 144         public Case2Runner() {
 145             start();
 146         }
 147 
 148         public void run() {
 149             for (int index = 0; index < CYCLES; index++) {
 150                 run_test_case2();
 151             }
 152         }
 153     }
 154 
 155     public static void main(String[] args) {
 156         Thread[] thrs = new Thread[THREAD_PER_CASE * 2];
 157         for (int index = 0; index < thrs.length; index = index + 2) {
 158             thrs[index] = new Case1Runner();
 159             thrs[index + 1] = new Case2Runner();
 160         }
 161 
 162         for (int index = 0; index < thrs.length; index++) {
 163             try {
 164                 thrs[index].join();
 165             } catch (Exception e) {
 166                 e.printStackTrace();
 167             }
 168         }
 169     }
 170 }