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.io.IOException;
  27 import java.lang.management.ManagementFactory;
  28 import java.lang.reflect.Method;
  29 import java.util.Collection;
  30 import java.util.function.Function;
  31 import javax.management.MBeanServer;
  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 String HOTSPOT_BEAN_NAME
  45             = "com.sun.management:type=HotSpotDiagnostic";
  46     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
  47             = 45 / (128.0 * 1024 * 1024);
  48 
  49     /**
  50      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  51      * used to get a lambda form from a method handle.
  52      */
  53     protected final static Method INTERNAL_FORM;
  54 
  55     static {
  56         try {
  57             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  58             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  59             INTERNAL_FORM.setAccessible(true);
  60         } catch (Exception ex) {
  61             throw new Error("Unexpected exception: ", ex);
  62         }
  63     }
  64 
  65     private final TestMethods testMethod;
  66 
  67     /**
  68      * Test case constructor. Generates test cases with random method types for
  69      * given methods form {@code j.l.i.MethodHandles} class.
  70      *
  71      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
  72      * returns a {@code j.l.i.MethodHandle}.
  73      */
  74     protected LambdaFormTestCase(TestMethods testMethod) {
  75         this.testMethod = testMethod;
  76     }
  77 
  78     public TestMethods getTestMethod() {
  79         return testMethod;
  80     }
  81 
  82     /**
  83      * Routine that executes a test case.
  84      */
  85     public abstract void doTest();
  86 
  87     /**
  88      * Runs a number of test cases defined by the size of testCases list.
  89      *
  90      * @param ctor constructor of LambdaFormCachingTest or its child classes
  91      * object.
  92      * @param testMethods list of test methods
  93      */
  94     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  95         boolean passed = true;
  96         int testCounter = 0;
  97         int failCounter = 0;
  98         long testCaseNum = testMethods.size();
  99         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
 100         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
 101                 iterations, iterations * testCaseNum);
 102         MBeanServer server = ManagementFactory.getPlatformMBeanServer();
 103         HotSpotDiagnosticMXBean hsDiagBean;
 104         try {
 105             hsDiagBean = ManagementFactory.newPlatformMXBeanProxy(server,
 106                     HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
 107         } catch (IOException ex) {
 108             throw new Error("TESTBUG: ", ex);
 109         }
 110         long codeCacheSize = Long.parseLong(
 111                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 112         System.out.printf("Code Cache Size is %d bytes%n", codeCacheSize);
 113         long iterationsByCodeCacheSize = (long) (codeCacheSize
 114                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 115         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 116                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 117         if (iterations > iterationsByCodeCacheSize) {
 118             iterations = iterationsByCodeCacheSize;
 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 }
 150