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 * @run main ClassFileInstaller sun.hotspot.WhiteBox 31 * sun.hotspot.WhiteBox$WhiteBoxPermission 32 * 33 * @run main/othervm 34 * -Xbootclasspath/a:. 35 * -Xmixed 36 * -XX:+UnlockDiagnosticVMOptions 37 * -XX:+WhiteBoxAPI 38 * -XX:+PrintCompilation 39 * -XX:CompileCommand=option,BlockingCompilation::foo,PrintInlining 40 * BlockingCompilation 41 */ 42 43 import java.lang.reflect.Method; 44 import java.util.Random; 45 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 long sum = 0; 58 int level = 0; 59 boolean enqued = false; 60 Method m = BlockingCompilation.class.getMethod("foo"); 61 62 // Normal, non-blocking compilation 63 for (int i = 0; i < 500_000; i++) { 64 sum += foo(); 65 if (!enqued && WB.isMethodQueuedForCompilation(m)) { 66 System.out.println("==> " + m + " enqued for compilation in iteration " + i); 67 enqued = true; 68 } 69 if (WB.isMethodCompiled(m)) { 70 if (WB.getMethodCompilationLevel(m) != level) { 71 level = WB.getMethodCompilationLevel(m); 72 System.out.println("==> " + m + " compiled at level " + level + " in iteration " + i); 73 enqued = false; 74 if (level == 4) break; 75 } 76 } 77 } 78 79 // This is necessarry because WB.deoptimizeMethod doesn't clear the methods 80 // MDO and therefore level 3 compilations will be downgraded to level 2. 81 WB.clearMethodState(m); 82 83 // Blocking compilations on all levels, using the 84 // blocking versions of WB.enqueueMethodForCompilation() 85 for (level = 1; level <= 4; level++) { 86 WB.deoptimizeMethod(m); 87 WB.enqueueMethodForCompilation(m, level, true); 88 89 if (!WB.isMethodCompiled(m) || WB.getMethodCompilationLevel(m) != level) { 90 String msg = m + " should be compiled at level " + level + 91 "(but is actually compiled at level " + 92 WB.getMethodCompilationLevel(m) + ")"; 93 System.out.println("==> " + msg); 94 throw new Exception(msg); 95 } 96 } 97 98 WB.deoptimizeMethod(m); 99 WB.clearMethodState(m); 100 101 // Blocking compilations on all levels, using the default versions of 102 // WB.enqueueMethodForCompilation() and manually setting compiler directives. 103 String directive = "[{ match: \"BlockingCompilation.foo\", BackgroundCompilation: false }]"; 104 WB.addCompilerDirective(directive); 105 106 for (level = 1; level <= 4; level++) { 107 WB.deoptimizeMethod(m); 108 WB.enqueueMethodForCompilation(m, level); 109 110 if (!WB.isMethodCompiled(m) || WB.getMethodCompilationLevel(m) != level) { 111 String msg = m + " should be compiled at level " + level + 112 "(but is actually compiled at level " + 113 WB.getMethodCompilationLevel(m) + ")"; 114 System.out.println("==> " + msg); 115 throw new Exception(msg); 116 } 117 } 118 119 WB.removeCompilerDirective(1); 120 121 WB.deoptimizeMethod(m); 122 WB.clearMethodState(m); 123 level = 0; 124 enqued = false; 125 int iteration = 0; 126 127 // Normal, non-blocking compilation 128 for (int i = 0; i < 500_000; i++) { 129 sum += foo(); 130 if (!enqued && WB.isMethodQueuedForCompilation(m)) { 131 System.out.println("==> " + m + " enqued for compilation in iteration " + i); 132 iteration = i; 133 enqued = true; 134 } 135 if (WB.isMethodCompiled(m)) { 136 if (WB.getMethodCompilationLevel(m) != level) { 137 level = WB.getMethodCompilationLevel(m); 138 System.out.println("==> " + m + " compiled at level " + level + " in iteration " + i); 139 if (level == 4 && iteration == i) { 140 throw new Exception("This seems to be a blocking compilation although it shouldn't."); 141 } 142 enqued = false; 143 if (level == 4) break; 144 } 145 } 146 } 147 } 148 }