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