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 sun.hotspot.WhiteBox;
  25 import java.lang.reflect.Executable;
  26 import java.lang.reflect.Method;
  27 import compiler.whitebox.CompilerWhiteBoxTest;
  28 
  29 /*
  30  * @test DeoptimizeMultipleOSRTest
  31  * @bug 8061817
  32  * @library /testlibrary /../../test/lib /
  33  * @modules java.management
  34  * @build DeoptimizeMultipleOSRTest
  35  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  36  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  37  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,DeoptimizeMultipleOSRTest::triggerOSR DeoptimizeMultipleOSRTest
  38  * @summary testing of WB::deoptimizeMethod()
  39  */
  40 public class DeoptimizeMultipleOSRTest {
  41     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  42     private static final long BACKEDGE_THRESHOLD = 150000;
  43     private Method method;
  44     private int counter = 0;
  45 
  46     public static void main(String[] args) throws Exception {
  47         DeoptimizeMultipleOSRTest test = new DeoptimizeMultipleOSRTest();
  48         test.test();
  49     }
  50 
  51     /**
  52      * Triggers two different OSR compilations for the same method and
  53      * checks if WhiteBox.deoptimizeMethod() deoptimizes both.
  54      *
  55      * @throws Exception
  56      */
  57     public void test() throws Exception {
  58         method = DeoptimizeMultipleOSRTest.class.getDeclaredMethod("triggerOSR", boolean.class, long.class);
  59         // Trigger two OSR compiled versions
  60         triggerOSR(true, BACKEDGE_THRESHOLD);
  61         triggerOSR(false, BACKEDGE_THRESHOLD);
  62         // Wait for compilation
  63         CompilerWhiteBoxTest.waitBackgroundCompilation(method);
  64         // Deoptimize
  65         WHITE_BOX.deoptimizeMethod(method, true);
  66         if (WHITE_BOX.isMethodCompiled(method, true)) {
  67             throw new AssertionError("Not all OSR compiled versions were deoptimized");
  68         }
  69     }
  70 
  71     /**
  72      * Triggers OSR compilations by executing loops.
  73      *
  74      * @param first Determines which loop to execute
  75      * @param limit The number of loop iterations
  76      */
  77     public void triggerOSR(boolean first, long limit) {
  78         if (limit != 1) {
  79             // Warmup method to avoid uncommon traps
  80             for (int i = 0; i < limit; ++i) {
  81                 triggerOSR(first, 1);
  82             }
  83             CompilerWhiteBoxTest.waitBackgroundCompilation(method);
  84         }
  85         if (first) {
  86             // Trigger OSR compilation 1
  87             for (int i = 0; i < limit; ++i) {
  88                 counter++;
  89             }
  90         } else {
  91             // Trigger OSR compilation 2
  92             for (int i = 0; i < limit; ++i) {
  93                 counter++;
  94             }
  95         }
  96     }
  97 }