1 /*
   2  * Copyright (c) 2013, 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 import java.util.function.Function;
  25 
  26 import compiler.whitebox.CompilerWhiteBoxTest;
  27 
  28 /*
  29  * @test ClearMethodStateTest
  30  * @bug 8006683 8007288 8022832
  31  * @library /testlibrary /test/lib /
  32  * @modules java.base/jdk.internal.misc
  33  * @modules java.management
  34  * @build ClearMethodStateTest
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  37  * @run main/othervm -Xbootclasspath/a:. -Xmixed -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+PrintCompilation -XX:-UseCounterDecay ClearMethodStateTest
  38  * @summary testing of WB::clearMethodState()
  39  * @author igor.ignatyev@oracle.com
  40  */
  41 public class ClearMethodStateTest extends CompilerWhiteBoxTest {
  42 
  43     public static void main(String[] args) throws Exception {
  44         String directive =
  45                 "[{ match:\"*SimpleTestCaseHelper.*\", BackgroundCompilation: false }, " +
  46                 " { match:\"*.*\", inline:\"-*SimpleTestCaseHelper.*\"}]";
  47         if (WHITE_BOX.addCompilerDirective(directive) != 2) {
  48             throw new RuntimeException("Could not add directive");
  49         }
  50         try {
  51             CompilerWhiteBoxTest.main(ClearMethodStateTest::new, args);
  52         } finally {
  53             WHITE_BOX.removeCompilerDirective(2);
  54         }
  55     }
  56 
  57     private ClearMethodStateTest(TestCase testCase) {
  58         super(testCase);
  59         // to prevent inlining of #method
  60         WHITE_BOX.testSetDontInlineMethod(method, true);
  61     }
  62 
  63 
  64     /**
  65      * Tests {@code WB::clearMethodState()} by calling it before/after
  66      * compilation. For non-tiered, checks that counters will be rested after
  67      * clearing of method state.
  68      *
  69      * @throws Exception if one of the checks fails.
  70      */
  71     @Override
  72     protected void test() throws Exception {
  73 
  74         checkNotCompiled();
  75         compile();
  76         WHITE_BOX.clearMethodState(method);
  77         checkCompiled();
  78         WHITE_BOX.clearMethodState(method);
  79         deoptimize();
  80         checkNotCompiled();
  81 
  82         if (testCase.isOsr()) {
  83             // part test isn't applicable for OSR test case
  84             return;
  85         }
  86         if (!TIERED_COMPILATION) {
  87             WHITE_BOX.clearMethodState(method);
  88             compile(COMPILE_THRESHOLD);
  89             checkCompiled();
  90 
  91             deoptimize();
  92             checkNotCompiled();
  93             WHITE_BOX.clearMethodState(method);
  94 
  95             // invoke method one less time than needed to compile
  96             if (COMPILE_THRESHOLD > 1) {
  97                 compile(COMPILE_THRESHOLD - 1);
  98                 checkNotCompiled();
  99             } else {
 100                 System.err.println("Warning: 'CompileThreshold' <= 1");
 101             }
 102 
 103             compile(1);
 104             checkCompiled();
 105         } else {
 106             System.err.println(
 107                     "Warning: part of test is not applicable in Tiered");
 108         }
 109     }
 110 }