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