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