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 TestRun {


  81 
  82         final Function<TestMethods, LambdaFormTestCase> ctor;
  83         final Collection<TestMethods> testMethods;
  84         final long totalIterations;
  85         long doneIterations;
  86         long testCounter;
  87         long failCounter;
  88         boolean passed;
  89 
  90         TestRun(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  91             this.ctor = ctor;
  92             this.testMethods = testMethods;









  93             long testCaseNum = testMethods.size();
  94             long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
  95             System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
  96                     iterations, iterations * testCaseNum);
  97             HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
  98             long codeCacheSize = Long.parseLong(
  99                     hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
 100             System.out.printf("Code cache size is %d bytes%n", codeCacheSize);
 101             long iterationsByCodeCacheSize = (long) (codeCacheSize
 102                     * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 103             long nonProfiledCodeCacheSize = Long.parseLong(
 104                     hsDiagBean.getVMOption("NonProfiledCodeHeapSize").getValue());
 105             System.out.printf("Non-profiled code cache size is %d bytes%n", nonProfiledCodeCacheSize);
 106             long iterationsByNonProfiledCodeCacheSize = (long) (nonProfiledCodeCacheSize
 107                     * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
 108             System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
 109                     iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 110             System.out.printf("Number of iterations limited by non-profiled code cache size is %d (%d cases)%n",
 111                     iterationsByNonProfiledCodeCacheSize, iterationsByNonProfiledCodeCacheSize * testCaseNum);
 112             iterations = Math.min(iterationsByCodeCacheSize,
 113                     Math.min(iterations, iterationsByNonProfiledCodeCacheSize));
 114             if (iterations == 0) {
 115                 System.out.println("Warning: code cache size is too small to provide at"
 116                         + " least one iteration! Test will try to do one iteration.");
 117                 iterations = 1;
 118             }
 119             System.out.printf("Number of iterations is set to %d (%d cases)%n",
 120                     iterations, iterations * testCaseNum);
 121             System.out.flush();
 122             totalIterations = iterations;
 123             doneIterations = 0L;
 124             testCounter = 0L;
 125             failCounter = 0L;
 126             passed = true;
 127         }
 128         
 129         Boolean doIteration() {
 130             if (doneIterations >= totalIterations) {
 131                 return false;
 132             }
 133             System.err.println(String.format("Iteration %d:", doneIterations));
 134             for (TestMethods testMethod : testMethods) {
 135                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 136                 try {
 137                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 138                             testCase.getTestMethod().name);
 139                     testCase.doTest();
 140                     System.err.println("PASSED");
 141                 } catch (Throwable t) {
 142                     t.printStackTrace();
 143                     System.err.printf("FAILED. Caused by %s%n", t.getMessage());
 144                     passed = false;
 145                     failCounter++;
 146                 }
 147                     testCounter++;
 148                 }
 149             doneIterations++;
 150             return true;





 151         }
 152     }
 153 
 154     /**
 155      * Test case constructor. Generates test cases with random method types for
 156      * given methods form {@code j.l.i.MethodHandles} class.
 157      *
 158      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
 159      * returns a {@code j.l.i.MethodHandle}.
 160      */
 161     protected LambdaFormTestCase(TestMethods testMethod) {
 162         this.testMethod = testMethod;
 163         this.gcCountAtStart = gcCount();
 164     }
 165 
 166     public TestMethods getTestMethod() {
 167         return testMethod;
 168     }
 169 
 170     protected boolean noGCHappened() {
 171         return gcCount() == gcCountAtStart;
 172     }
 173 
 174     /**
 175      * Routine that executes a test case.
 176      */
 177     public abstract void doTest();
 178 
 179     /**
 180      * Runs a number of test cases defined by the size of testCases list.
 181      *
 182      * @param ctor constructor of LambdaFormCachingTest or its child classes
 183      * object.
 184      * @param testMethods list of test methods
 185      */
 186     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
 187         LambdaFormTestCase.TestRun run =
 188                 new LambdaFormTestCase.TestRun(ctor, testMethods);
 189         TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d, run::doIteration);
 190         try {
 191             runner.call();
 192         } catch (Exception ex) {
 193             System.err.println("FAILED");
 194             throw new Error("Unexpected error!", ex);
 195         }
 196         if (!run.passed) {
 197             throw new Error(String.format("%d of %d test cases FAILED! %n"
 198                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 199                     run.failCounter, run.testCounter));
 200         } else {
 201             System.err.println(String.format("All %d test cases PASSED!", run.testCounter));
 202         }
 203     }
 204 }