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

Print this page




   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 static long totalIterations = 0L;
  79     private static long doneIterations = 0L;
  80     private static boolean passed = true;
  81     private static int testCounter = 0;
  82     private static int failCounter = 0;
  83     private long gcCountAtStart;
  84 
  85     /**
  86      * Test case constructor. Generates test cases with random method types for


 129                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 130         if (totalIterations > iterationsByCodeCacheSize) {
 131             totalIterations = iterationsByCodeCacheSize;
 132         }
 133         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 134                 totalIterations, totalIterations * testCaseNum);
 135         System.out.flush();
 136         TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d,
 137                 () -> {
 138                     if (doneIterations >= totalIterations) {
 139                         return false;
 140                     }
 141                     System.err.println(String.format("Iteration %d:", doneIterations));
 142                     for (TestMethods testMethod : testMethods) {
 143                         LambdaFormTestCase testCase = ctor.apply(testMethod);
 144                         try {
 145                             System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 146                                     testCase.getTestMethod().name);
 147                             testCase.doTest();
 148                             System.err.println("PASSED");




 149                         } catch (Throwable t) {
 150                             t.printStackTrace();
 151                             System.err.println("FAILED");
 152                             passed = false;
 153                             failCounter++;
 154                         }
 155                         testCounter++;
 156                     }
 157                     doneIterations++;
 158                     return true;
 159                 });
 160         try {
 161             runner.call();
 162         } catch (Throwable t) {
 163             t.printStackTrace();
 164             System.err.println("FAILED");
 165             throw new Error("Unexpected error!");
 166         }
 167         if (!passed) {
 168             throw new Error(String.format("%d of %d test cases FAILED! %n"


   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.invoke.MethodHandle;
  28 import java.lang.management.GarbageCollectorMXBean;
  29 import java.lang.management.ManagementFactory;
  30 import java.lang.ref.Reference;
  31 import java.lang.reflect.Field;
  32 import java.lang.reflect.Method;
  33 import java.util.Collection;
  34 import java.util.List;
  35 import java.util.function.Function;
  36 import jdk.testlibrary.Utils;
  37 import jdk.testlibrary.TimeLimitedRunner;
  38 
  39 /**
  40  * Lambda forms caching test case class. Contains all necessary test routines to
  41  * test lambda forms caching in method handles returned by methods of
  42  * MethodHandles class.
  43  *
  44  * @author kshefov
  45  */
  46 public abstract class LambdaFormTestCase {
  47 


  48     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
  49             = 45 / (128.0 * 1024 * 1024);
  50     private static final long TIMEOUT = Helper.IS_THOROUGH ? 0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9);
  51 
  52     /**
  53      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  54      * used to get a lambda form from a method handle.
  55      */
  56     protected final static Method INTERNAL_FORM;
  57     protected final static Field DEBUG_NAME;
  58     protected final static Field REF_FIELD;
  59     private static final List<GarbageCollectorMXBean> gcInfo;
  60 
  61     private static long gcCount() {
  62         return gcInfo.stream().mapToLong(GarbageCollectorMXBean::getCollectionCount).sum();
  63     }
  64 
  65     static {
  66         try {
  67             INTERNAL_FORM = MethodHandle.class.getDeclaredMethod("internalForm");

  68             INTERNAL_FORM.setAccessible(true);
  69             
  70             DEBUG_NAME = Class.forName("java.lang.invoke.LambdaForm").getDeclaredField("debugName");
  71             DEBUG_NAME.setAccessible(true);
  72 
  73             REF_FIELD = Reference.class.getDeclaredField("referent");
  74             REF_FIELD.setAccessible(true);
  75         } catch (Exception ex) {
  76             throw new Error("Unexpected exception: ", ex);
  77         }
  78 
  79         gcInfo = ManagementFactory.getGarbageCollectorMXBeans();
  80         if (gcInfo.size() == 0)  {
  81             throw new Error("No GarbageCollectorMXBeans found.");
  82         }
  83     }
  84 
  85     private final TestMethods testMethod;
  86     private static long totalIterations = 0L;
  87     private static long doneIterations = 0L;
  88     private static boolean passed = true;
  89     private static int testCounter = 0;
  90     private static int failCounter = 0;
  91     private long gcCountAtStart;
  92 
  93     /**
  94      * Test case constructor. Generates test cases with random method types for


 137                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
 138         if (totalIterations > iterationsByCodeCacheSize) {
 139             totalIterations = iterationsByCodeCacheSize;
 140         }
 141         System.out.printf("Number of iterations is set to %d (%d cases)%n",
 142                 totalIterations, totalIterations * testCaseNum);
 143         System.out.flush();
 144         TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d,
 145                 () -> {
 146                     if (doneIterations >= totalIterations) {
 147                         return false;
 148                     }
 149                     System.err.println(String.format("Iteration %d:", doneIterations));
 150                     for (TestMethods testMethod : testMethods) {
 151                         LambdaFormTestCase testCase = ctor.apply(testMethod);
 152                         try {
 153                             System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
 154                                     testCase.getTestMethod().name);
 155                             testCase.doTest();
 156                             System.err.println("PASSED");
 157                         } catch (OutOfMemoryError e) {
 158                             // Don't swallow OOME so a heap dump can be created.
 159                             System.err.println("FAILED");
 160                             throw e;
 161                         } catch (Throwable t) {
 162                             t.printStackTrace();
 163                             System.err.println("FAILED");
 164                             passed = false;
 165                             failCounter++;
 166                         }
 167                         testCounter++;
 168                     }
 169                     doneIterations++;
 170                     return true;
 171                 });
 172         try {
 173             runner.call();
 174         } catch (Throwable t) {
 175             t.printStackTrace();
 176             System.err.println("FAILED");
 177             throw new Error("Unexpected error!");
 178         }
 179         if (!passed) {
 180             throw new Error(String.format("%d of %d test cases FAILED! %n"