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 84 for (level = 1; level <= 4; level++) { 85 WB.deoptimizeMethod(m); 86 WB.enqueueMethodForCompilation(m, level, true); 87 88 if (!WB.isMethodCompiled(m) || WB.getMethodCompilationLevel(m) != level) { 89 String msg = m + " should be compiled at level " + level + 90 "(but is actually compiled at level " + 91 WB.getMethodCompilationLevel(m) + ")"; 92 System.out.println("==> " + msg); 93 throw new Exception(msg); 94 } 95 } 96 97 WB.deoptimizeMethod(m); 98 WB.clearMethodState(m); 99 level = 0; 100 enqued = false; 101 int iteration = 0; 102 103 // Normal, non-blocking compilation 104 for (int i = 0; i < 500_000; i++) { 105 sum += foo(); 106 if (!enqued && WB.isMethodQueuedForCompilation(m)) { 107 System.out.println("==> " + m + " enqued for compilation in iteration " + i); 108 iteration = i; 109 enqued = true; 110 } 111 if (WB.isMethodCompiled(m)) { 112 if (WB.getMethodCompilationLevel(m) != level) { 113 level = WB.getMethodCompilationLevel(m); 114 System.out.println("==> " + m + " compiled at level " + level + " in iteration " + i); 115 if (level == 4 && iteration == i) { 116 throw new Exception("This seems to be a blocking compilation although it shouldn't."); 117 } 118 enqued = false; 119 if (level == 4) break; 120 } 121 } 122 } 123 } 124 }