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.function.Function;
  30 
  31 /**
  32  * Lambda forms caching test case class. Contains all necessary test routines to
  33  * test lambda forms caching in method handles returned by methods of
  34  * MethodHandles class.
  35  *
  36  * @author kshefov
  37  */
  38 public abstract class LambdaFormTestCase {
  39 
  40     private final static String METHOD_HANDLE_CLASS_NAME = "java.lang.invoke.MethodHandle";
  41     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
  42     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
  43             = 45 / (128.0 * 1024 * 1024);
  44 
  45     /**
  46      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  47      * used to get a lambda form from a method handle.
  48      */
  49     protected final static Method INTERNAL_FORM;
  50 
  51     static {
  52         try {
  53             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  54             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  55             INTERNAL_FORM.setAccessible(true);
  56         } catch (Exception ex) {
  57             throw new Error("Unexpected exception: ", ex);
  58         }
  59     }
  60 
  61     private final TestMethods testMethod;
  62 
  63     /**
  64      * Test case constructor. Generates test cases with random method types for
  65      * given methods form {@code j.l.i.MethodHandles} class.
  66      *
  67      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
  68      * returns a {@code j.l.i.MethodHandle}.
  69      */
  70     protected LambdaFormTestCase(TestMethods testMethod) {
  71         this.testMethod = testMethod;
  72     }
  73 
  74     public TestMethods getTestMethod() {
  75         return testMethod;
  76     }
  77 
  78     /**
  79      * Routine that executes a test case.
  80      */
  81     public abstract void doTest();
  82 
  83     /**
  84      * Runs a number of test cases defined by the size of testCases list.
  85      *
  86      * @param ctor constructor of LambdaFormCachingTest or its child classes
  87      * object.
  88      * @param testMethods list of test methods
  89      */
  90     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  91         boolean passed = true;
  92         int testCounter = 0;
  93         int failCounter = 0;
  94         long testCaseNum = testMethods.size();
  95         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
  96         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
  97                 iterations, iterations * testCaseNum);
  98         HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
  99         long codeCacheSize = Long.parseLong(
 100                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 101         System.out.printf("Code cache size is %d bytes%n", codeCacheSize);
 102         long iterationsByCodeCacheSize = (long) (codeCacheSize
 103                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 104         long nonProfiledCodeCacheSize = Long.parseLong(
 105                 hsDiagBean.getVMOption("NonProfiledCodeHeapSize").getValue());
 106         System.out.printf("Non-profiled code cache size is %d bytes%n", nonProfiledCodeCacheSize);
 107         long iterationsByNonProfiledCodeCacheSize = (long) (nonProfiledCodeCacheSize
 108                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 109         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 110                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 111         System.out.printf("Number of iterations limited by non-profiled code cache size is %d (%d cases)%n",
 112                 iterationsByNonProfiledCodeCacheSize, iterationsByNonProfiledCodeCacheSize * testCaseNum);
 113         iterations = Math.min(iterationsByCodeCacheSize,
 114                 Math.min(iterations, iterationsByNonProfiledCodeCacheSize));
 115         if (iterations == 0) {
 116             System.out.println("Warning: code cache size is too small to provide at"
 117                     + " least one iteration! Test will try to do one iteration.");
 118             iterations = 1;
 119         }
 120         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 121                 iterations, iterations * testCaseNum);
 122         System.out.flush();
 123         for (long i = 0; i < iterations; i++) {
 124             System.err.println(String.format("Iteration %d:", i));
 125             for (TestMethods testMethod : testMethods) {
 126                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 127                 try {
 128                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 129                             testCase.getTestMethod().name);
 130                     testCase.doTest();
 131                     System.err.println("PASSED");
 132                 } catch (Throwable t) {
 133                     t.printStackTrace();
 134                     System.err.println("FAILED");
 135                     passed = false;
 136                     failCounter++;
 137                 }
 138                 testCounter++;
 139             }
 140         }
 141         if (!passed) {
 142             throw new Error(String.format("%d of %d test cases FAILED! %n"
 143                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 144                     failCounter, testCounter));
 145         } else {
 146             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 147         }
 148     }
 149 }