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