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 package compiler.compilercontrol.share.actions;
  25 
  26 import compiler.compilercontrol.share.scenario.State;
  27 import compiler.testlibrary.CompilerUtils;
  28 import jdk.test.lib.Asserts;
  29 import jdk.test.lib.Pair;
  30 import jdk.test.lib.Utils;
  31 import pool.PoolHelper;
  32 import sun.hotspot.WhiteBox;
  33 
  34 import java.lang.reflect.Executable;
  35 import java.util.List;
  36 import java.util.concurrent.Callable;
  37 
  38 public class CompileAction {
  39     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  40     private static final int[] COMP_LEVELS;
  41     private static final List<Pair<Executable, Callable<?>>> METHODS
  42             = new PoolHelper().getAllMethods();
  43     private static final int EXEC_AMOUNT = 100;
  44 
  45     static {
  46         COMP_LEVELS = CompilerUtils.getAvailableCompilationLevels();
  47         if (COMP_LEVELS.length == 0) {
  48             throw new Error("TESTBUG: test requires JIT " +
  49                     "compiler to be available");
  50         }
  51     }
  52 
  53     /**
  54      * Checks executable if it could be compiled
  55      *
  56      * @param executable given executable to check
  57      * @param state method compilation state
  58      */
  59     public static void checkCompiled(Executable executable,
  60                                      State state) {
  61         int first = COMP_LEVELS[0];
  62         if (first < 4) {
  63             checkCompilation(executable, first, state.isC1Compilable());
  64         }
  65         int last = COMP_LEVELS[COMP_LEVELS.length - 1];
  66         if (last == 4) {
  67             checkCompilation(executable, last, state.isC2Compilable());
  68         }
  69     }
  70 
  71     private static void checkCompilation(Executable executable,
  72                                          int level,
  73                                          boolean expectedCompiled) {
  74         execute(executable);
  75         WHITE_BOX.enqueueMethodForCompilation(executable, level);
  76         Utils.waitForCondition(
  77                 () -> {
  78                     execute(executable);
  79                     return !WHITE_BOX.isMethodQueuedForCompilation(executable);
  80                 }, 100L);
  81         execute(executable);
  82         boolean isCompilable = WHITE_BOX.isMethodCompilable(executable, level);
  83         Asserts.assertEQ(isCompilable, expectedCompiled,
  84                 String.format("FAILED: method %s compilable: %b, but should: %b"
  85                         + " on required level: %d", executable, isCompilable,
  86                         expectedCompiled, level));
  87     }
  88 
  89     private static void execute(Executable executable) {
  90         Callable<?> callable = getCallableFor(executable);
  91         try {
  92             for (int i = 0; i < EXEC_AMOUNT; i++) {
  93                 callable.call();
  94             }
  95         } catch (Exception e) {
  96             throw new Error("Got exception during execution", e);
  97         }
  98     }
  99 
 100     private static Callable<?> getCallableFor(Executable executable) {
 101         for (Pair<Executable, Callable<?>> pair : METHODS) {
 102             if (pair.first == executable) {
 103                 return pair.second;
 104             }
 105         }
 106         throw new Error("TESTBUG: wrong executable: " + executable);
 107     }
 108 }