1 /* 2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.compiler; 27 28 import static jdk.test.lib.Asserts.assertFalse; 29 30 import java.lang.reflect.Method; 31 import java.util.HashSet; 32 import java.util.List; 33 import java.util.Set; 34 35 import jdk.jfr.Recording; 36 import jdk.jfr.consumer.RecordedEvent; 37 import jdk.test.lib.Utils; 38 import jdk.test.lib.jfr.EventNames; 39 import jdk.test.lib.jfr.Events; 40 import sun.hotspot.WhiteBox; 41 42 /** 43 * @test 44 * @key jfr 45 * 46 * 47 * @library /lib / 48 * @build sun.hotspot.WhiteBox 49 * @run main ClassFileInstaller sun.hotspot.WhiteBox 50 * sun.hotspot.WhiteBox$WhiteBoxPermission 51 * @run main/othervm -Xbootclasspath/a:. 52 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 53 * jdk.jfr.event.compiler.TestCompilerCompile 54 */ 55 public class TestCompilerCompile { 56 private final static String EVENT_NAME = EventNames.Compilation; 57 private final static String METHOD_NAME = "dummyMethod"; 58 private boolean foundKnownMethod = false; 59 private boolean foundOsrMethod = false; 60 61 public static void main(String[] args) throws Throwable { 62 TestCompilerCompile test = new TestCompilerCompile(); 63 test.doTest(); 64 } 65 66 static void dummyMethod() { 67 System.out.println("hello!"); 68 } 69 70 public void doTest() throws Throwable { 71 Recording recording = new Recording(); 72 recording.enable(EVENT_NAME); 73 74 recording.start(); 75 long start = System.currentTimeMillis(); 76 // provoke OSR compilation 77 for (int i = 0; i < Integer.MAX_VALUE; i++) { 78 } 79 // compile dummyMethod() 80 Method mtd = TestCompilerCompile.class.getDeclaredMethod(METHOD_NAME, new Class[0]); 81 WhiteBox WB = WhiteBox.getWhiteBox(); 82 String directive = "[{ match: \"" + TestCompilerCompile.class.getName().replace('.', '/') 83 + "." + METHOD_NAME + "\", " + "BackgroundCompilation: false }]"; 84 WB.addCompilerDirective(directive); 85 if (!WB.enqueueMethodForCompilation(mtd, 4 /* CompLevel_full_optimization */)) { 86 WB.enqueueMethodForCompilation(mtd, 1 /* CompLevel_simple */); 87 } 88 Utils.waitForCondition(() -> WB.isMethodCompiled(mtd)); 89 dummyMethod(); 90 91 System.out.println("time:" + (System.currentTimeMillis() - start)); 92 recording.stop(); 93 94 Set<Integer> compileIds = new HashSet<Integer>(); 95 List<RecordedEvent> events = Events.fromRecording(recording); 96 Events.hasEvents(events); 97 for (RecordedEvent event : events) { 98 System.out.println("Event:" + event); 99 verifyEvent(event); 100 Integer compileId = Events.assertField(event, "compileId").getValue(); 101 assertFalse(compileIds.contains(compileId), "compile id not unique: " + compileId); 102 compileIds.add(compileId); 103 } 104 105 // Verify that we actually encountered our expected method 106 if (!foundKnownMethod) { 107 throw new Exception("Couldn't find method jdk/jfr/event/compiler/TestCompilerCompile.dummyMethod()V among compilation events"); 108 } 109 110 // Verify that doTest() function has been replaced on stack. 111 if (!foundOsrMethod) { 112 throw new Exception("No On Stack Replacement of function doTest()"); 113 } 114 } 115 116 private void verifyEvent(RecordedEvent event) throws Throwable { 117 Events.assertJavaMethod(event); 118 Events.assertEventThread(event); 119 120 String methodName = Events.assertField(event, "method.name").notEmpty().getValue(); 121 String methodDescriptor = Events.assertField(event, "method.descriptor").notEmpty().getValue(); 122 String methodType = Events.assertField(event, "method.type.name").notEmpty().getValue(); 123 124 // Compare with a known candidate 125 if ("jdk/jfr/event/compiler/TestCompilerCompile".equals(methodType) && "dummyMethod".equals(methodName) && "()V".equals(methodDescriptor)) { 126 foundKnownMethod = true; 127 } 128 129 // The doTest() function is live almost the entire time the test runs. 130 // We should get at least 1 "on stack replacement" for that method. 131 if (TestCompilerCompile.class.getName().replace('.', '/').equals(methodType) && "doTest".equals(methodName)) { 132 boolean isOsr = Events.assertField(event, "isOsr").getValue(); 133 if (isOsr) { 134 foundOsrMethod = true; 135 } 136 } 137 138 Events.assertField(event, "compileId").atLeast(0); 139 Events.assertField(event, "compileLevel").atLeast((short) 0).atMost((short) 4); 140 Events.assertField(event, "inlinedBytes").atLeast(0L); 141 Events.assertField(event, "codeSize").atLeast(0L); 142 Events.assertField(event, "isOsr"); 143 } 144 }