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         if (!WB.enqueueMethodForCompilation(mtd, 4 /* CompLevel_full_optimization */)) {
  83             WB.enqueueMethodForCompilation(mtd, 1 /* CompLevel_simple */);
  84         }
  85         Utils.waitForCondition(() -> WB.isMethodCompiled(mtd));
  86         dummyMethod();
  87 
  88         System.out.println("time:" + (System.currentTimeMillis() - start));
  89         recording.stop();
  90 
  91         Set<Integer> compileIds = new HashSet<Integer>();
  92         List<RecordedEvent> events = Events.fromRecording(recording);
  93         Events.hasEvents(events);
  94         for (RecordedEvent event : events) {
  95             System.out.println("Event:" + event);
  96             verifyEvent(event);
  97             Integer compileId = Events.assertField(event, "compileId").getValue();
  98             assertFalse(compileIds.contains(compileId), "compile id not unique: " + compileId);
  99             compileIds.add(compileId);
 100         }
 101 
 102         // Verify that we actually encountered our expected method
 103         if (!foundKnownMethod) {
 104             throw new Exception("Couldn't find method jdk/jfr/event/compiler/TestCompilerCompile.dummyMethod()V among compilation events");
 105         }
 106 
 107         // Verify that doTest() function has been replaced on stack.
 108         if (!foundOsrMethod) {
 109             throw new Exception("No On Stack Replacement of function doTest()");
 110         }
 111     }
 112 
 113     private void verifyEvent(RecordedEvent event) throws Throwable {
 114         Events.assertJavaMethod(event);
 115         Events.assertEventThread(event);
 116 
 117         String methodName = Events.assertField(event, "method.name").notEmpty().getValue();
 118         String methodDescriptor = Events.assertField(event, "method.descriptor").notEmpty().getValue();
 119         String methodType = Events.assertField(event, "method.type.name").notEmpty().getValue();
 120 
 121         // Compare with a known candidate
 122         if ("jdk/jfr/event/compiler/TestCompilerCompile".equals(methodType) && "dummyMethod".equals(methodName) && "()V".equals(methodDescriptor)) {
 123             foundKnownMethod = true;
 124         }
 125 
 126         // The doTest() function is live almost the entire time the test runs.
 127         // We should get at least 1 "on stack replacement" for that method.
 128         if (TestCompilerCompile.class.getName().replace('.', '/').equals(methodType) && "doTest".equals(methodName)) {
 129             boolean isOsr = Events.assertField(event, "isOsr").getValue();
 130             if (isOsr) {
 131                 foundOsrMethod = true;
 132             }
 133         }
 134 
 135         Events.assertField(event, "compileId").atLeast(0);
 136         Events.assertField(event, "compileLevel").atLeast((short) 0).atMost((short) 4);
 137         Events.assertField(event, "inlinedBytes").atLeast(0L);
 138         Events.assertField(event, "codeSize").atLeast(0L);
 139         Events.assertField(event, "isOsr");
 140     }
 141 }