1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import com.oracle.testlibrary.jsr292.Helper;
  25 import com.sun.management.HotSpotDiagnosticMXBean;
  26 import java.lang.management.ManagementFactory;
  27 import java.lang.reflect.Method;
  28 import java.util.Collection;
  29 import java.util.concurrent.TimeUnit;
  30 import java.util.function.Function;
  31 import jdk.testlibrary.Utils;
  32 
  33 /**
  34  * Lambda forms caching test case class. Contains all necessary test routines to
  35  * test lambda forms caching in method handles returned by methods of
  36  * MethodHandles class.
  37  *
  38  * @author kshefov
  39  */
  40 public abstract class LambdaFormTestCase {
  41 
  42     private final static String METHOD_HANDLE_CLASS_NAME = "java.lang.invoke.MethodHandle";
  43     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
  44     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
  45             = 45 / (128.0 * 1024 * 1024);
  46     private static final long TIMEOUT = Utils.adjustTimeout(TimeUnit.SECONDS.toMillis(120));
  47 
  48     /**
  49      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  50      * used to get a lambda form from a method handle.
  51      */
  52     protected final static Method INTERNAL_FORM;
  53 
  54     static {
  55         try {
  56             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  57             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  58             INTERNAL_FORM.setAccessible(true);
  59         } catch (Exception ex) {
  60             throw new Error("Unexpected exception: ", ex);
  61         }
  62     }
  63 
  64     private final TestMethods testMethod;
  65 
  66     /**
  67      * Test case constructor. Generates test cases with random method types for
  68      * given methods form {@code j.l.i.MethodHandles} class.
  69      *
  70      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
  71      * returns a {@code j.l.i.MethodHandle}.
  72      */
  73     protected LambdaFormTestCase(TestMethods testMethod) {
  74         this.testMethod = testMethod;
  75     }
  76 
  77     public TestMethods getTestMethod() {
  78         return testMethod;
  79     }
  80 
  81     /**
  82      * Routine that executes a test case.
  83      */
  84     public abstract void doTest();
  85 
  86     /**
  87      * Runs a number of test cases defined by the size of testCases list.
  88      *
  89      * @param ctor constructor of LambdaFormCachingTest or its child classes
  90      * object.
  91      * @param testMethods list of test methods
  92      */
  93     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  94         boolean passed = true;
  95         int testCounter = 0;
  96         int failCounter = 0;
  97         long testCaseNum = testMethods.size();
  98         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
  99         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
 100                 iterations, iterations * testCaseNum);
 101         HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
 102         long codeCacheSize = Long.parseLong(
 103                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 104         System.out.printf("Code cache size is %d bytes%n", codeCacheSize);
 105         long iterationsByCodeCacheSize = (long) (codeCacheSize
 106                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 107         long nonProfiledCodeCacheSize = Long.parseLong(
 108                 hsDiagBean.getVMOption("NonProfiledCodeHeapSize").getValue());
 109         System.out.printf("Non-profiled code cache size is %d bytes%n", nonProfiledCodeCacheSize);
 110         long iterationsByNonProfiledCodeCacheSize = (long) (nonProfiledCodeCacheSize
 111                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 112         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 113                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 114         System.out.printf("Number of iterations limited by non-profiled code cache size is %d (%d cases)%n",
 115                 iterationsByNonProfiledCodeCacheSize, iterationsByNonProfiledCodeCacheSize * testCaseNum);
 116         iterations = Math.min(iterationsByCodeCacheSize,
 117                 Math.min(iterations, iterationsByNonProfiledCodeCacheSize));
 118         if (iterations == 0) {
 119             System.out.println("Warning: code cache size is too small to provide at"
 120                     + " least one iteration! Test will try to do one iteration.");
 121             iterations = 1;
 122         }
 123         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 124                 iterations, iterations * testCaseNum);
 125         System.out.flush();
 126         long startTime = System.currentTimeMillis();
 127         for (long i = 0; i < iterations; i++) {
 128             System.err.println(String.format("Iteration %d:", i));
 129             for (TestMethods testMethod : testMethods) {
 130                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 131                 try {
 132                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 133                             testCase.getTestMethod().name);
 134                     testCase.doTest();
 135                     System.err.println("PASSED");
 136                 } catch (Throwable t) {
 137                     t.printStackTrace();
 138                     System.err.println("FAILED");
 139                     passed = false;
 140                     failCounter++;
 141                 }
 142                 testCounter++;
 143             }
 144             long passedTime = System.currentTimeMillis() - startTime;
 145             long avgIterTime = passedTime / (i + 1);
 146             long remainTime = TIMEOUT - passedTime;
 147             if (avgIterTime > 2 * remainTime) {
 148                 System.err.printf("Stopping iterations because of lack of time.%n"
 149                         + "Increase timeout factor for more iterations.%n");
 150                 break;
 151             }
 152         }
 153         if (!passed) {
 154             throw new Error(String.format("%d of %d test cases FAILED! %n"
 155                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 156                     failCounter, testCounter));
 157         } else {
 158             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 159         }
 160     }
 161 }