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