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.simpleArch == "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 
  49 import compiler.whitebox.CompilerWhiteBoxTest;
  50 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  51 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  52 import jdk.vm.ci.meta.ProfilingInfo;
  53 import jdk.test.lib.Asserts;
  54 
  55 public class ReprofileTest {
  56 
  57     public static void main(String[] args) {
  58         List<Method> testCases = createTestCases();
  59         testCases.forEach(ReprofileTest::runSanityTest);
  60     }
  61 
  62     private static List<Method> createTestCases() {
  63         List<Method> testCases = new ArrayList<>();
  64         try {
  65 
  66             Class<?> aClass = DummyClass.class;
  67             testCases.add(aClass.getMethod("dummyInstanceFunction"));
  68 
  69             aClass = DummyClass.class;
  70             testCases.add(aClass.getMethod("dummyFunction"));
  71         } catch (NoSuchMethodException e) {
  72             throw new Error("TEST BUG " + e.getMessage(), e);
  73         }
  74         return testCases;
  75     }
  76 
  77     private static void runSanityTest(Method aMethod) {
  78         System.out.println(aMethod);
  79         HotSpotResolvedJavaMethod method = CTVMUtilities
  80                 .getResolvedMethod(aMethod);
  81         ProfilingInfo startProfile = method.getProfilingInfo();
  82         Asserts.assertFalse(startProfile.isMature(), aMethod
  83                 + " : profiling info is mature in the beginning");
  84 
  85         // make interpreter to profile this method
  86         try {
  87             Object obj = aMethod.getDeclaringClass().newInstance();
  88             for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {
  89                 aMethod.invoke(obj);
  90             }
  91         } catch (ReflectiveOperationException e) {
  92             throw new Error("TEST BUG : " + e.getMessage(), e);
  93         }
  94         ProfilingInfo compProfile = method.getProfilingInfo();
  95 
  96         Asserts.assertNE(startProfile.toString(), compProfile.toString(),
  97                 String.format("%s : profiling info wasn't changed after "
  98                                 + "%d invocations",
  99                         aMethod, CompilerWhiteBoxTest.THRESHOLD));
 100         Asserts.assertTrue(compProfile.isMature(),
 101                 String.format("%s is not mature after %d invocations",
 102                         aMethod, CompilerWhiteBoxTest.THRESHOLD));
 103 
 104         CompilerToVMHelper.reprofile(method);
 105         ProfilingInfo reprofiledProfile = method.getProfilingInfo();
 106 
 107         Asserts.assertNE(startProfile.toString(), reprofiledProfile.toString(),
 108                 aMethod + " : profiling info wasn't changed after reprofiling");
 109         Asserts.assertNE(compProfile.toString(), reprofiledProfile.toString(),
 110                 aMethod + " : profiling info didn't change after reprofile");
 111         Asserts.assertFalse(reprofiledProfile.isMature(), aMethod
 112                 + " : profiling info is mature after reprofiling");
 113     }
 114 }