1 /*
   2  * Copyright (c) 2016 SAP SE. 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
  26  * @bug 8150646
  27  * @summary Add support for blocking compiles through whitebox API
  28  * @library /testlibrary /test/lib /
  29  * @build sun.hotspot.WhiteBox
  30  *        compiler.testlibrary.CompilerUtils
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  *
  34  * @run main/othervm/timeout=60
  35  *        -Xbootclasspath/a:.
  36  *        -Xmixed
  37  *        -XX:+UnlockDiagnosticVMOptions
  38  *        -XX:+WhiteBoxAPI
  39  *        -XX:+PrintCompilation
  40  *        BlockingCompilation
  41  */
  42 
  43 import java.lang.reflect.Method;
  44 import java.util.Random;
  45 
  46 import sun.hotspot.WhiteBox;
  47 import compiler.testlibrary.CompilerUtils;
  48 
  49 public class BlockingCompilation {
  50     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  51     private static final Random RANDOM = new Random();
  52 
  53     public static int foo() {
  54         return RANDOM.nextInt();
  55     }
  56 
  57     public static void main(String[] args) throws Exception {
  58         Method m = BlockingCompilation.class.getMethod("foo");
  59         int[] levels = CompilerUtils.getAvailableCompilationLevels();
  60         int highest_level = levels[levels.length-1];
  61 
  62         // If there are no compilers available these tests don't make any sense.
  63         if (levels.length == 0) return;
  64 
  65         // Make sure no compilations can progress, blocking compiles will hang
  66         WB.lockCompilation();
  67 
  68         // Verify method state before test
  69         if (WB.isMethodCompiled(m)) {
  70             throw new Exception("Should not be compiled after deoptimization");
  71         }
  72         if (WB.isMethodQueuedForCompilation(m)) {
  73             throw new Exception("Should not be enqueued on any level");
  74         }
  75 
  76         // Try compiling on highest available comp level.
  77         // If the compiles are blocking, this call will block until the test time out,
  78         // Progress == success
  79         // (Don't run with -Xcomp since that can cause long timeouts due to many compiles)
  80         WB.enqueueMethodForCompilation(m, highest_level);
  81 
  82         // restore state
  83         WB.unlockCompilation();
  84         while (!WB.isMethodCompiled(m)) {
  85           Thread.sleep(100);
  86         }
  87         WB.deoptimizeMethod(m);
  88         WB.clearMethodState(m);
  89 
  90         // Blocking compilations on all levels, using the default versions of
  91         // WB.enqueueMethodForCompilation() and manually setting compiler directives.
  92         String directive = "[{ match: \"BlockingCompilation.foo\", BackgroundCompilation: false }]";
  93         if (WB.addCompilerDirective(directive) != 1) {
  94             throw new Exception("Failed to add compiler directive");
  95         }
  96 
  97         try {
  98             for (int l : levels) {
  99                 // Make uncompiled
 100                 WB.deoptimizeMethod(m);
 101 
 102                 // Verify that it's not compiled
 103                 if (WB.isMethodCompiled(m)) {
 104                     throw new Exception("Should not be compiled after deoptimization");
 105                 }
 106                 if (WB.isMethodQueuedForCompilation(m)) {
 107                     throw new Exception("Should not be enqueued on any level");
 108                 }
 109 
 110                 // Add to queue and verify that it went well
 111                 if (!WB.enqueueMethodForCompilation(m, l)) {
 112                     throw new Exception("Could not be enqueued for compilation");
 113                 }
 114 
 115                 // Verify that it is compiled
 116                 if (!WB.isMethodCompiled(m)) {
 117                     throw new Exception("Must be compiled here");
 118                 }
 119                 // And verify the level
 120                 if (WB.getMethodCompilationLevel(m) != l) {
 121                     String msg = m + " should be compiled at level " + l +
 122                                  "(but is actually compiled at level " +
 123                                  WB.getMethodCompilationLevel(m) + ")";
 124                     System.out.println("==> " + msg);
 125                     throw new Exception(msg);
 126                 }
 127             }
 128         } finally {
 129             WB.removeCompilerDirective(1);
 130         }
 131 
 132         // Clean up
 133         WB.deoptimizeMethod(m);
 134         WB.clearMethodState(m);
 135 
 136         // Make sure no compilations can progress, blocking compiles will hang
 137         WB.lockCompilation();
 138 
 139         // Verify method state before test
 140         if (WB.isMethodCompiled(m)) {
 141             throw new Exception("Should not be compiled after deoptimization");
 142         }
 143         if (WB.isMethodQueuedForCompilation(m)) {
 144             throw new Exception("Should not be enqueued on any level");
 145         }
 146 
 147         // Try compiling on highest available comp level.
 148         // If the compiles are blocking, this call will block until the test time out,
 149         // Progress == success
 150         // (Don't run with -Xcomp since that can cause long timeouts due to many compiles)
 151         WB.enqueueMethodForCompilation(m, highest_level);
 152 
 153         // restore state
 154         WB.unlockCompilation();
 155     }
 156 }