1 /*
   2  * Copyright (c) 2019, 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  * @test OSRFailureLevel4Test
  26  * @summary check that not compilable OSR level 4 results in falling back to level 1
  27  * @library /test/lib /
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  * @build sun.hotspot.WhiteBox
  31  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  32  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  34  *                   -XX:+WhiteBoxAPI -XX:+TieredCompilation compiler.whitebox.OSRFailureLevel4Test
  35  */
  36 
  37 package compiler.whitebox;
  38 
  39 import sun.hotspot.WhiteBox;
  40 import java.lang.reflect.Executable;
  41 import java.lang.reflect.Method;
  42 
  43 public class OSRFailureLevel4Test extends Thread {
  44     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  45     private static final long BACKEDGE_THRESHOLD = 150000;
  46     private Method method;
  47 
  48     public static void main(String[] args) throws Exception {
  49         OSRFailureLevel4Test test = new OSRFailureLevel4Test();
  50         test.test();
  51     }
  52 
  53     /**
  54      * Triggers two different OSR compilations for the same method and
  55      * checks if WhiteBox.deoptimizeMethod() deoptimizes both.
  56      *
  57      * @throws Exception
  58      */
  59     public void test() throws Exception {
  60         method = OSRFailureLevel4Test.class.getDeclaredMethod("run");
  61         WHITE_BOX.makeMethodNotCompilable(method, 4, true);
  62 
  63         Thread t = new OSRFailureLevel4Test();
  64         t.setDaemon(true);
  65         t.start();
  66 
  67         int currentLevel = 0;
  68         int loops = 0;
  69         while (true) {
  70             int level = WHITE_BOX.getMethodCompilationLevel(method, true);
  71             if (level == 1) {
  72                 System.err.println("success");
  73                 running = false;
  74                 return;
  75             }
  76             if (level == currentLevel) {
  77                 Thread.sleep(1000);
  78                 loops++;
  79                 if (loops > 100) {
  80                     running = false;
  81                     throw new AssertionError("Never reached level 1");
  82                 }
  83                 continue;
  84             }
  85             currentLevel = level;
  86             System.err.println("Current level = " + currentLevel);
  87         }
  88 
  89     }
  90 
  91     static volatile int counter = 0;
  92     static boolean running = true;
  93 
  94     public void run() {
  95         while (running) {
  96             counter++;
  97         }
  98     }
  99 }