1 /*
   2  * Copyright (c) 2014, 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 
  25 import java.lang.reflect.Method;
  26 import java.util.stream.IntStream;
  27 
  28 import com.oracle.java.testlibrary.Platform;
  29 
  30 /*
  31  * @test OverloadCompileQueueTest
  32  * @library /testlibrary /../../test/lib
  33  * @build OverloadCompileQueueTest
  34  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  35  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  37  *                   -XX:CompileCommand=dontinline,Helper$TestCase::method
  38  *                   -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache OverloadCompileQueueTest
  39  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  40  *                   -XX:CompileCommand=dontinline,Helper$TestCase::method
  41  *                   -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache OverloadCompileQueueTest
  42  * @summary stressing code cache by overloading compile queues
  43  */
  44 public class OverloadCompileQueueTest implements Runnable {
  45     private static final int MAX_SLEEP = 10000;
  46     private static final String METHOD_TO_ENQUEUE = "method";
  47     private static final int LEVEL_SIMPLE = 1;
  48     private static final int LEVEL_FULL_OPTIMIZATION = 4;
  49     private static final boolean INTERPRETED
  50             = System.getProperty("java.vm.info").startsWith("interpreted ");
  51     private static final boolean TIERED_COMPILATION
  52             = Helper.WHITE_BOX.getBooleanVMFlag("TieredCompilation");
  53     private static final int TIERED_STOP_AT_LEVEL
  54             = Helper.WHITE_BOX.getIntxVMFlag("TieredStopAtLevel").intValue();
  55     private static final int[] AVAILABLE_LEVELS;
  56     static {
  57         if (TIERED_COMPILATION) {
  58             AVAILABLE_LEVELS = IntStream
  59                     .rangeClosed(LEVEL_SIMPLE, TIERED_STOP_AT_LEVEL)
  60                     .toArray();
  61         } else if (Platform.isServer()) {
  62             AVAILABLE_LEVELS = new int[] { LEVEL_FULL_OPTIMIZATION };
  63         } else if (Platform.isClient() || Platform.isMinimal()) {
  64             AVAILABLE_LEVELS = new int[] { LEVEL_SIMPLE };
  65         } else {
  66             throw new Error(String.format(
  67                     "TESTBUG: unknown VM: %s", System.getProperty("java.vm.name")));
  68         }
  69     }
  70 
  71     public static void main(String[] args) {
  72         if (INTERPRETED) {
  73             System.err.println("Test isn't applicable for interpreter. Skip test.");
  74             return;
  75         }
  76         new CodeCacheStressRunner(new OverloadCompileQueueTest()).runTest();
  77     }
  78 
  79     public OverloadCompileQueueTest() {
  80         Helper.startInfiniteLoopThread(this::lockUnlock);
  81     }
  82 
  83     @Override
  84     public void run() {
  85         Helper.TestCase obj = Helper.TestCase.get();
  86         Class clazz = obj.getClass();
  87         Method mEnqueue;
  88         try {
  89             mEnqueue = clazz.getMethod(METHOD_TO_ENQUEUE);
  90         } catch (NoSuchMethodException | SecurityException e) {
  91             throw new Error(String.format(
  92                     "TESTBUG: cannot get method '%s' of class %s",
  93                     METHOD_TO_ENQUEUE, clazz.getName()), e);
  94         }
  95         for (int compLevel : AVAILABLE_LEVELS) {
  96             Helper.WHITE_BOX.enqueueMethodForCompilation(mEnqueue, compLevel);
  97         }
  98     }
  99 
 100     private void lockUnlock() {
 101         try {
 102             Helper.WHITE_BOX.lockCompilation();
 103             Thread.sleep(Helper.RNG.nextInt(MAX_SLEEP));
 104         } catch (InterruptedException e) {
 105             throw new Error("TESTBUG: lockUnlocker thread was unexpectedly interrupted", e);
 106         } finally {
 107             Helper.WHITE_BOX.unlockCompilation();
 108         }
 109     }
 110 
 111 }