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