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