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 * @build BlockingCompilation 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 * -XX:TieredCompileTaskTimeout=60000 41 * BlockingCompilation 42 */ 43 44 import java.lang.reflect.Method; 45 import java.util.Random; 46 47 import sun.hotspot.WhiteBox; 48 import compiler.testlibrary.CompilerUtils; 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: \"BlockingCompilation.foo\", BackgroundCompilation: false }]"; 100 if (WB.addCompilerDirective(directive) != 1) { 101 throw new Exception("Failed to add compiler directive"); 102 } 103 104 try { 105 for (int l : levels) { 106 // Make uncompiled 107 WB.deoptimizeMethod(m); 108 109 // Verify that it's not compiled 110 if (WB.isMethodCompiled(m)) { 111 throw new Exception("Should not be compiled after deoptimization"); 112 } 113 if (WB.isMethodQueuedForCompilation(m)) { 114 throw new Exception("Should not be enqueued on any level"); 115 } 116 117 // Add to queue and verify that it went well 118 if (!WB.enqueueMethodForCompilation(m, l)) { 119 throw new Exception("Could not be enqueued for compilation"); 120 } 121 122 // Verify that it is compiled 123 if (!WB.isMethodCompiled(m)) { 124 throw new Exception("Must be compiled here"); 125 } 126 // And verify the level 127 if (WB.getMethodCompilationLevel(m) != l) { 128 String msg = m + " should be compiled at level " + l + 129 "(but is actually compiled at level " + 130 WB.getMethodCompilationLevel(m) + ")"; 131 System.out.println("==> " + msg); 132 throw new Exception(msg); 133 } 134 } 135 } finally { 136 WB.removeCompilerDirective(1); 137 } 138 139 // Clean up 140 WB.deoptimizeMethod(m); 141 WB.clearMethodState(m); 142 143 // Make sure no compilations can progress, blocking compiles will hang 144 WB.lockCompilation(); 145 146 // Verify method state before test 147 if (WB.isMethodCompiled(m)) { 148 throw new Exception("Should not be compiled after deoptimization"); 149 } 150 if (WB.isMethodQueuedForCompilation(m)) { 151 throw new Exception("Should not be enqueued on any level"); 152 } 153 154 // Try compiling on highest available comp level. 155 // If the compiles are blocking, this call will block until the test time out, 156 // Progress == success 157 // (Don't run with -Xcomp since that can cause long timeouts due to many compiles) 158 WB.enqueueMethodForCompilation(m, highest_level); 159 160 // restore state 161 WB.unlockCompilation(); 162 } 163 }