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