test/java/lang/invoke/LFCaching/LambdaFormTestCase.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Sdiff test/java/lang/invoke/LFCaching

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

Print this page
rev 11011 : 8057020: LambdaForm caches should support eviction
Reviewed-by: psandoz, ?


   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 import java.lang.management.ManagementFactory;
  27 import java.lang.reflect.Method;
  28 import java.util.Collection;

  29 import java.util.function.Function;
  30 import jdk.testlibrary.Utils;
  31 
  32 /**
  33  * Lambda forms caching test case class. Contains all necessary test routines to
  34  * test lambda forms caching in method handles returned by methods of
  35  * MethodHandles class.
  36  *
  37  * @author kshefov
  38  */
  39 public abstract class LambdaFormTestCase {
  40 
  41     private final static String METHOD_HANDLE_CLASS_NAME = "java.lang.invoke.MethodHandle";
  42     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
  43     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
  44             = 45 / (128.0 * 1024 * 1024);
  45     private static final long TIMEOUT = Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT);
  46 
  47     /**
  48      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  49      * used to get a lambda form from a method handle.
  50      */
  51     protected final static Method INTERNAL_FORM;





  52 
  53     static {
  54         try {
  55             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  56             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  57             INTERNAL_FORM.setAccessible(true);
  58         } catch (Exception ex) {
  59             throw new Error("Unexpected exception: ", ex);
  60         }





  61     }
  62 
  63     private final TestMethods testMethod;
  64 
  65     /**
  66      * Test case constructor. Generates test cases with random method types for
  67      * given methods form {@code j.l.i.MethodHandles} class.
  68      *
  69      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
  70      * returns a {@code j.l.i.MethodHandle}.
  71      */
  72     protected LambdaFormTestCase(TestMethods testMethod) {
  73         this.testMethod = testMethod;

  74     }
  75 
  76     public TestMethods getTestMethod() {
  77         return testMethod;
  78     }
  79 




  80     /**
  81      * Routine that executes a test case.
  82      */
  83     public abstract void doTest();
  84 
  85     /**
  86      * Runs a number of test cases defined by the size of testCases list.
  87      *
  88      * @param ctor constructor of LambdaFormCachingTest or its child classes
  89      * object.
  90      * @param testMethods list of test methods
  91      */
  92     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  93         boolean passed = true;
  94         int testCounter = 0;
  95         int failCounter = 0;
  96         long testCaseNum = testMethods.size();
  97         long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
  98         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
  99                 iterations, iterations * testCaseNum);




   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);


test/java/lang/invoke/LFCaching/LambdaFormTestCase.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File