test/java/lang/invoke/LFCaching/LambdaFormTestCase.java

Print this page




  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 }


  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 }