1 /*
   2  * Copyright (c) 2015, 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.
   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 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @build sun.hotspot.WhiteBox
  32  * @run main ClassFileInstaller
  33  *      sun.hotspot.WhiteBox
  34  *      sun.hotspot.WhiteBox$WhiteBoxPermission
  35  *      jdk.vm.ci.hotspot.CompilerToVMHelper
  36  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  37  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  38  *      -Xmixed
  39  *      compiler.jvmci.compilerToVM.ReprofileTest
  40  */
  41 
  42 package compiler.jvmci.compilerToVM;
  43 
  44 import compiler.jvmci.common.CTVMUtilities;
  45 import java.lang.reflect.Method;
  46 import java.util.ArrayList;
  47 import java.util.List;
  48 import java.util.Random;
  49 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  50 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  51 import jdk.vm.ci.meta.ProfilingInfo;
  52 import jdk.test.lib.Asserts;
  53 import jdk.test.lib.Utils;
  54 import sun.hotspot.WhiteBox;
  55 
  56 public class ReprofileTest {
  57 
  58     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  59 
  60     public static void main(String[] args) {
  61         List<Method> testCases = createTestCases();
  62         testCases.forEach(ReprofileTest::runSanityTest);
  63     }
  64 
  65     private static List<Method> createTestCases() {
  66         List<Method> testCases = new ArrayList<>();
  67         try {
  68 
  69             Class<?> aClass = DummyClass.class;
  70             testCases.add(aClass.getMethod("withLoop"));
  71 
  72             aClass = DummyClass.class;
  73             testCases.add(aClass.getDeclaredMethod("dummyFunction"));
  74         } catch (NoSuchMethodException e) {
  75             throw new Error("TEST BUG " + e.getMessage(), e);
  76         }
  77         return testCases;
  78     }
  79 
  80     private static void runSanityTest(Method aMethod) {
  81         HotSpotResolvedJavaMethodImpl method = CTVMUtilities
  82                 .getResolvedMethod(aMethod);
  83         ProfilingInfo startProfile = method.getProfilingInfo();
  84         Asserts.assertFalse(startProfile.isMature(), aMethod
  85                 + " : profiling info is mature in the begging");
  86 
  87         long compileThreshold = (Long) WB.getVMFlag("CompileThreshold");
  88         // make interpreter to profile this method
  89         try {
  90             Object obj = aMethod.getDeclaringClass().newInstance();
  91             for (long i = 0; i < compileThreshold; i++) {
  92                 aMethod.invoke(obj);
  93             }
  94         } catch (ReflectiveOperationException e) {
  95             throw new Error("TEST BUG : " + e.getMessage(), e);
  96         }
  97         ProfilingInfo compProfile = method.getProfilingInfo();
  98 
  99         Asserts.assertNE(startProfile.toString(), compProfile.toString(),
 100                 String.format("%s : profiling info wasn't changed after "
 101                                 + "%d invocations",
 102                         aMethod, compileThreshold));
 103         Asserts.assertTrue(compProfile.isMature(),
 104                 String.format("%s is not mature after %d invocations",
 105                         aMethod, compileThreshold));
 106 
 107         CompilerToVMHelper.reprofile(method);
 108         ProfilingInfo reprofiledProfile = method.getProfilingInfo();
 109 
 110         Asserts.assertNE(startProfile.toString(), reprofiledProfile.toString(),
 111                 aMethod + " : profiling info wasn't changed after reprofiling");
 112         Asserts.assertNE(compProfile.toString(), reprofiledProfile.toString(),
 113                 aMethod + " : profiling info didn't change after reprofile");
 114         Asserts.assertFalse(reprofiledProfile.isMature(), aMethod
 115                 + " : profiling info is mature after reprofiling");
 116     }
 117 }