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