1 /* 2 * Copyright (c) 2014, 2015, 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.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor; 26 import java.lang.invoke.MethodHandle; 27 import java.lang.management.GarbageCollectorMXBean; 28 import java.lang.management.ManagementFactory; 29 import java.lang.ref.Reference; 30 import java.lang.reflect.Field; 31 import java.lang.reflect.Method; 32 import java.util.Collection; 33 import java.util.List; 34 import java.util.function.Function; 35 import jdk.testlibrary.Utils; 36 import jdk.testlibrary.TimeLimitedRunner; 37 38 /** 39 * Lambda forms caching test case class. Contains all necessary test routines to 40 * test lambda forms caching in method handles returned by methods of 41 * MethodHandles class. 42 * 43 * @author kshefov 44 */ 45 public abstract class LambdaFormTestCase { 46 47 private static final long TIMEOUT = Helper.IS_THOROUGH ? 0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9); 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 static final Method INTERNAL_FORM; 54 protected static final Field KIND_FIELD; 55 protected static final Field REF_FIELD; 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 INTERNAL_FORM = MethodHandle.class.getDeclaredMethod("internalForm"); 65 INTERNAL_FORM.setAccessible(true); 66 67 KIND_FIELD = Class.forName("java.lang.invoke.LambdaForm").getDeclaredField("kind"); 68 KIND_FIELD.setAccessible(true); 69 70 REF_FIELD = Reference.class.getDeclaredField("referent"); 71 REF_FIELD.setAccessible(true); 72 } catch (Exception ex) { 73 throw new Error("Unexpected exception", ex); 74 } 75 76 gcInfo = ManagementFactory.getGarbageCollectorMXBeans(); 77 if (gcInfo.size() == 0) { 78 throw new Error("No GarbageCollectorMXBeans found."); 79 } 80 } 81 82 private final TestMethods testMethod; 83 private long gcCountAtStart; 84 85 private static class TestRun { 86 87 final Function<TestMethods, LambdaFormTestCase> ctor; 88 final Collection<TestMethods> testMethods; 89 final long totalIterations; 90 long doneIterations; 91 long testCounter; 92 long failCounter; 93 boolean passed; 94 95 TestRun(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) { 96 this.ctor = ctor; 97 this.testMethods = testMethods; 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 System.out.printf("Number of iterations is set to %d (%d cases)%n", 103 iterations, iterations * testCaseNum); 104 System.out.flush(); 105 totalIterations = iterations; 106 doneIterations = 0L; 107 testCounter = 0L; 108 failCounter = 0L; 109 passed = true; 110 } 111 112 Boolean doIteration() { 113 if (doneIterations >= totalIterations) { 114 return false; 115 } 116 System.err.println(String.format("Iteration %d:", doneIterations)); 117 for (TestMethods testMethod : testMethods) { 118 LambdaFormTestCase testCase = ctor.apply(testMethod); 119 try { 120 System.err.printf("Tested LF caching feature" 121 + " with MethodHandles.%s method.%n", 122 testCase.getTestMethod().name); 123 Throwable t = CodeCacheOverflowProcessor 124 .runMHTest(testCase::doTest); 125 if (t != null) { 126 return false; 127 } 128 System.err.println("PASSED"); 129 } catch (OutOfMemoryError oome) { 130 // Don't swallow OOME so a heap dump can be created. 131 System.err.println("FAILED"); 132 throw oome; 133 } catch (Throwable t) { 134 t.printStackTrace(); 135 System.err.printf("FAILED. Caused by %s%n", t.getMessage()); 136 passed = false; 137 failCounter++; 138 } 139 testCounter++; 140 } 141 doneIterations++; 142 return true; 143 } 144 } 145 146 /** 147 * Test case constructor. Generates test cases with random method types for 148 * given methods form {@code j.l.i.MethodHandles} class. 149 * 150 * @param testMethod A method from {@code j.l.i.MethodHandles} class which 151 * returns a {@code j.l.i.MethodHandle}. 152 */ 153 protected LambdaFormTestCase(TestMethods testMethod) { 154 this.testMethod = testMethod; 155 this.gcCountAtStart = gcCount(); 156 } 157 158 public TestMethods getTestMethod() { 159 return testMethod; 160 } 161 162 protected boolean noGCHappened() { 163 return gcCount() == gcCountAtStart; 164 } 165 166 /** 167 * Routine that executes a test case. 168 */ 169 public abstract void doTest(); 170 171 /** 172 * Runs a number of test cases defined by the size of testCases list. 173 * 174 * @param ctor constructor of LambdaFormCachingTest or its child classes 175 * object. 176 * @param testMethods list of test methods 177 */ 178 public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) { 179 LambdaFormTestCase.TestRun run 180 = new LambdaFormTestCase.TestRun(ctor, testMethods); 181 TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d, run::doIteration); 182 try { 183 runner.call(); 184 } catch (Exception ex) { 185 System.err.println("FAILED"); 186 throw new Error("Unexpected error!", ex); 187 } 188 if (!run.passed) { 189 throw new Error(String.format("%d of %d test cases FAILED! %n" 190 + "Rerun the test with the same \"-Dseed=\" option as in the log file!", 191 run.failCounter, run.testCounter)); 192 } else { 193 System.err.printf("All %d test cases PASSED!%n", run.testCounter); 194 } 195 } 196 }