1 /*
   2  * Copyright (c) 2014, 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 jdk.test.lib.Utils;
  25 import java.lang.reflect.Executable;
  26 import java.util.ArrayList;
  27 import java.util.Arrays;
  28 import java.util.Collections;
  29 import java.util.List;
  30 import sun.hotspot.WhiteBox;
  31 
  32 public class SegmentedCodeCacheDtraceTestWorker {
  33 
  34     private static final String METHOD1_NAME = "foo";
  35     private static final String METHOD2_NAME = "bar";
  36     private static final String METHOD3_NAME = "baz";
  37     public static final List<Executable> TESTED_METHODS_LIST;
  38     private final WhiteBox wb;
  39     private final int compLevels[];
  40 
  41     static {
  42         List<Executable> methods = new ArrayList<>();
  43         try {
  44             // method order is important. Need to place methods in call order,
  45             // to be able to verify results later
  46             methods.add(SegmentedCodeCacheDtraceTestWorker.class.getMethod(METHOD1_NAME));
  47             methods.add(SegmentedCodeCacheDtraceTestWorker.class.getMethod(METHOD2_NAME));
  48             methods.add(SegmentedCodeCacheDtraceTestWorker.class.getMethod(METHOD3_NAME));
  49         } catch (NoSuchMethodException e) {
  50             throw new Error("TESTBUG: no expected method found", e);
  51         }
  52         TESTED_METHODS_LIST = Collections.unmodifiableList(methods);
  53     }
  54 
  55     protected static final boolean BACKGROUND_COMPILATION
  56             = WhiteBox.getWhiteBox().getBooleanVMFlag("BackgroundCompilation");
  57 
  58     public static void main(String[] args) {
  59         if (args.length != 2 * TESTED_METHODS_LIST.size()) {
  60             throw new Error("Usage: java <thisClass> <fooCompLevel> <fooInlined>"
  61                     + "<barCompLevel> <barInlined> "
  62                     + "<bazCompLevel> <bazInlined>");
  63         } else {
  64             int compLevels[] = new int[TESTED_METHODS_LIST.size()];
  65             boolean inlines[] = new boolean[TESTED_METHODS_LIST.size()];
  66             for (int i = 0; i < TESTED_METHODS_LIST.size(); i++) {
  67                 compLevels[i] = Integer.parseInt(args[2 * i]);
  68                 inlines[i] = Boolean.parseBoolean(args[2 * i + 1]);
  69             }
  70             new SegmentedCodeCacheDtraceTestWorker(compLevels, inlines).test();
  71         }
  72     }
  73 
  74     public SegmentedCodeCacheDtraceTestWorker(int compLevels[], boolean inlines[]) {
  75         wb = WhiteBox.getWhiteBox();
  76         this.compLevels = Arrays.copyOf(compLevels, compLevels.length);
  77         for (int i = 0; i < compLevels.length; i++) {
  78             if (inlines[i]) {
  79                 wb.testSetForceInlineMethod(TESTED_METHODS_LIST.get(i), true);
  80             } else {
  81                 wb.testSetDontInlineMethod(TESTED_METHODS_LIST.get(i), true);
  82             }
  83         }
  84     }
  85 
  86     private void waitForCompilation(Executable executable, int compLevel) {
  87         if (compLevel > 0) {
  88             Utils.waitForCondition(() -> wb.isMethodCompiled(executable));
  89         }
  90     }
  91 
  92     protected void test() {
  93         for (int i = 0; i < TESTED_METHODS_LIST.size(); i++) {
  94             Executable method = TESTED_METHODS_LIST.get(i);
  95             int compLevel = compLevels[i];
  96             wb.enqueueMethodForCompilation(method, compLevel);
  97             waitForCompilation(method, compLevel);
  98         }
  99         foo();
 100     }
 101 
 102     public static void foo() {
 103         bar();
 104     }
 105 
 106     public static void bar() {
 107         baz();
 108     }
 109 
 110     public static void baz() {
 111         System.out.println("Reached baz method");
 112     }
 113 }