hotspot/test/compiler/whitebox/CompilerWhiteBoxTest.java
author iignatyev
Mon, 18 Mar 2013 04:29:08 -0700
changeset 16367 9cd60b8380a1
parent 15621 b094c56bba84
child 16689 efce070b8d42
permissions -rw-r--r--
8008211: Some of WB tests on compiler fail Reviewed-by: kvn, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     1
/*
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     4
 *
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     8
 *
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    14
 *
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    18
 *
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    21
 * questions.
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    22
 */
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    23
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    24
import sun.hotspot.WhiteBox;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    25
import sun.management.ManagementFactoryHelper;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    26
import com.sun.management.HotSpotDiagnosticMXBean;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    27
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    28
import java.lang.reflect.Method;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    29
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    30
/*
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    31
 * @author igor.ignatyev@oracle.com
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    32
 */
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    33
public abstract class CompilerWhiteBoxTest {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    34
    protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    35
    protected static final Method METHOD = getMethod("method");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    36
    protected static final int COMPILE_THRESHOLD
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    37
            = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    38
    protected static final boolean BACKGROUND_COMPILATION
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    39
            = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    40
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    41
    protected static Method getMethod(String name) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    42
        try {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    43
            return CompilerWhiteBoxTest.class.getDeclaredMethod(name);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    44
        } catch (NoSuchMethodException | SecurityException e) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    45
            throw new RuntimeException(
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    46
                    "exception on getting method " + name, e);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    47
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    48
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    49
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    50
    protected static String getVMOption(String name) {
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    51
        String result;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    52
        HotSpotDiagnosticMXBean diagnostic
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    53
                = ManagementFactoryHelper.getDiagnosticMXBean();
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    54
        result = diagnostic.getVMOption(name).getValue();
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    55
        return result;
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    56
    }
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    57
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    58
    protected static String getVMOption(String name, String defaultValue) {
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    59
        String result = getVMOption(name);
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    60
        return result == null ? defaultValue : result;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    61
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    62
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    63
    protected final void runTest() throws RuntimeException {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    64
        if (ManagementFactoryHelper.getCompilationMXBean() == null) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    65
            System.err.println(
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    66
                    "Warning: test is not applicable in interpreted mode");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    67
            return;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    68
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    69
        System.out.println("at test's start:");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    70
        printInfo(METHOD);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    71
        try {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    72
            test();
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    73
        } catch (Exception e) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    74
            System.out.printf("on exception '%s':", e.getMessage());
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    75
            printInfo(METHOD);
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
    76
            e.printStackTrace();
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    77
            throw new RuntimeException(e);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    78
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    79
        System.out.println("at test's end:");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    80
        printInfo(METHOD);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    81
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    82
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    83
    protected static void checkNotCompiled(Method method) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    84
        if (WHITE_BOX.isMethodCompiled(method)) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    85
            throw new RuntimeException(method + " must be not compiled");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    86
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    87
        if (WHITE_BOX.getMethodCompilationLevel(method) != 0) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    88
            throw new RuntimeException(method + " comp_level must be == 0");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    89
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    90
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    91
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    92
    protected static void checkCompiled(Method method)
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    93
            throws InterruptedException {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    94
        final long start = System.currentTimeMillis();
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    95
        waitBackgroundCompilation(method);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    96
        if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    97
            System.err.printf("Warning: %s is still in queue after %dms%n",
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    98
                    method, System.currentTimeMillis() - start);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
    99
            return;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   100
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   101
        if (!WHITE_BOX.isMethodCompiled(method)) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   102
            throw new RuntimeException(method + " must be compiled");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   103
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   104
        if (WHITE_BOX.getMethodCompilationLevel(method) == 0) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   105
            throw new RuntimeException(method + " comp_level must be != 0");
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   106
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   107
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   108
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   109
    protected static void waitBackgroundCompilation(Method method)
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   110
            throws InterruptedException {
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
   111
        if (!BACKGROUND_COMPILATION) {
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
   112
            return;
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
   113
        }
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   114
        final Object obj = new Object();
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   115
        synchronized (obj) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   116
            for (int i = 0; i < 10; ++i) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   117
                if (!WHITE_BOX.isMethodQueuedForCompilation(method)) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   118
                    break;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   119
                }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   120
                obj.wait(1000);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   121
            }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   122
        }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   123
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   124
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   125
    protected static void printInfo(Method method) {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   126
        System.out.printf("%n%s:%n", method);
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   127
        System.out.printf("\tcompilable:\t%b%n",
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   128
                WHITE_BOX.isMethodCompilable(method));
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   129
        System.out.printf("\tcompiled:\t%b%n",
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   130
                WHITE_BOX.isMethodCompiled(method));
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   131
        System.out.printf("\tcomp_level:\t%d%n",
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   132
                WHITE_BOX.getMethodCompilationLevel(method));
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   133
        System.out.printf("\tin_queue:\t%b%n",
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   134
                WHITE_BOX.isMethodQueuedForCompilation(method));
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   135
        System.out.printf("compile_queues_size:\t%d%n%n",
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   136
                WHITE_BOX.getCompileQueuesSize());
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   137
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   138
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   139
    protected abstract void test() throws Exception;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   140
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   141
    protected final int compile() {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   142
        int result = 0;
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
   143
        int count = Math.max(COMPILE_THRESHOLD, 150000);
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
   144
        for (int i = 0; i < count; ++i) {
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   145
            result += method();
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   146
        }
16367
9cd60b8380a1 8008211: Some of WB tests on compiler fail
iignatyev
parents: 15621
diff changeset
   147
        System.out.println("method was invoked " + count + " times");
15621
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   148
        return result;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   149
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   150
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   151
    protected int method() {
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   152
        return 42;
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   153
    }
b094c56bba84 8006683: Add WhiteBox API to testing of compiler
iignatyev
parents:
diff changeset
   154
}