src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core/src/org/graalvm/compiler/core/CompilationWrapper.java
author dlong
Thu, 14 Nov 2019 12:21:00 -0800
changeset 59095 03fbcd06b4c0
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
8233841: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 57537
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     4
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     8
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    14
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    18
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    24
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    25
package org.graalvm.compiler.core;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    26
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    27
import static org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.ExitVM;
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
    28
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationBailoutAsFailure;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    29
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationFailureAction;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    30
import static org.graalvm.compiler.core.GraalCompilerOptions.ExitVMOnException;
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    31
import static org.graalvm.compiler.core.GraalCompilerOptions.MaxCompilationProblemsPerAction;
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 57537
diff changeset
    32
import static org.graalvm.compiler.core.common.GraalOptions.TrackNodeSourcePosition;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    33
import static org.graalvm.compiler.debug.DebugContext.VERBOSE_LEVEL;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    34
import static org.graalvm.compiler.debug.DebugOptions.Dump;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    35
import static org.graalvm.compiler.debug.DebugOptions.DumpPath;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    36
import static org.graalvm.compiler.debug.DebugOptions.MethodFilter;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    37
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    38
import java.io.ByteArrayOutputStream;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    39
import java.io.File;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    40
import java.io.FileOutputStream;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    41
import java.io.IOException;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    42
import java.io.PrintStream;
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    43
import java.util.Map;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    44
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    45
import org.graalvm.compiler.debug.DebugContext;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    46
import org.graalvm.compiler.debug.DiagnosticsOutputDirectory;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    47
import org.graalvm.compiler.debug.PathUtilities;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    48
import org.graalvm.compiler.debug.TTY;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    49
import org.graalvm.compiler.options.OptionValues;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    50
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    51
import jdk.vm.ci.code.BailoutException;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    52
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    53
/**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    54
 * Wrapper for a compilation that centralizes what action to take based on
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
    55
 * {@link GraalCompilerOptions#CompilationBailoutAsFailure} and
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    56
 * {@link GraalCompilerOptions#CompilationFailureAction} when an uncaught exception occurs during
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    57
 * compilation.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    58
 */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    59
public abstract class CompilationWrapper<T> {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    60
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    61
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    62
     * Actions to take upon an exception being raised during compilation performed via
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    63
     * {@link CompilationWrapper}. The actions are with respect to what the user sees on the
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    64
     * console. The compilation requester determines what ultimate action is taken in
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    65
     * {@link CompilationWrapper#handleException(Throwable)}.
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    66
     *
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    67
     * The actions are in ascending order of verbosity.
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    68
     */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    69
    public enum ExceptionAction {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    70
        /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    71
         * Print nothing to the console.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    72
         */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    73
        Silent,
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
    74
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    75
        /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    76
         * Print a stack trace to the console.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    77
         */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    78
        Print,
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
    79
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    80
        /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    81
         * An exception causes the compilation to be retried with extra diagnostics enabled.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    82
         */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    83
        Diagnose,
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
    84
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    85
        /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    86
         * Same as {@link #Diagnose} except that the VM process is exited after retrying.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    87
         */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    88
        ExitVM;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    89
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    90
        private static final ExceptionAction[] VALUES = values();
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    91
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    92
        /**
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    93
         * Gets the action that is one level less verbose than this action, bottoming out at the
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    94
         * least verbose action.
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    95
         */
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    96
        ExceptionAction quieter() {
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    97
            assert ExceptionAction.Silent.ordinal() == 0;
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
    98
            int index = Math.max(ordinal() - 1, 0);
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    99
            return VALUES[index];
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   100
        }
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   101
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   102
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   103
    private final DiagnosticsOutputDirectory outputDirectory;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   104
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   105
    private final Map<ExceptionAction, Integer> problemsHandledPerAction;
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   106
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   107
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   108
     * @param outputDirectory object used to access a directory for dumping if the compilation is
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   109
     *            re-executed
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   110
     * @param problemsHandledPerAction map used to count the number of compilation failures or
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   111
     *            bailouts handled by each action. This is provided by the caller as it is expected
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   112
     *            to be shared between instances of {@link CompilationWrapper}.
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   113
     */
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   114
    public CompilationWrapper(DiagnosticsOutputDirectory outputDirectory, Map<ExceptionAction, Integer> problemsHandledPerAction) {
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   115
        this.outputDirectory = outputDirectory;
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   116
        this.problemsHandledPerAction = problemsHandledPerAction;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   117
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   118
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   119
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   120
     * Handles an uncaught exception.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   121
     *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   122
     * @param t an exception thrown during {@link #run(DebugContext)}
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   123
     * @return a value representing the result of a failed compilation (may be {@code null})
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   124
     */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   125
    protected abstract T handleException(Throwable t);
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   126
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   127
    /**
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   128
     * Gets the action to take based on the value of
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   129
     * {@link GraalCompilerOptions#CompilationBailoutAsFailure},
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   130
     * {@link GraalCompilerOptions#CompilationFailureAction} and
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   131
     * {@link GraalCompilerOptions#ExitVMOnException} in {@code options}.
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   132
     *
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   133
     * Subclasses can override this to choose a different action.
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   134
     *
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   135
     * @param cause the cause of the bailout or failure
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   136
     */
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   137
    protected ExceptionAction lookupAction(OptionValues options, Throwable cause) {
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   138
        if (cause instanceof BailoutException && !CompilationBailoutAsFailure.getValue(options)) {
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   139
            return ExceptionAction.Silent;
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   140
        }
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   141
        if (ExitVMOnException.getValue(options)) {
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   142
            assert CompilationFailureAction.getDefaultValue() != ExceptionAction.ExitVM;
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   143
            assert ExitVMOnException.getDefaultValue() != true;
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   144
            if (CompilationFailureAction.hasBeenSet(options) && CompilationFailureAction.getValue(options) != ExceptionAction.ExitVM) {
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   145
                TTY.printf("WARNING: Ignoring %s=%s since %s=true has been explicitly specified.%n",
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   146
                                CompilationFailureAction.getName(), CompilationFailureAction.getValue(options),
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   147
                                ExitVMOnException.getName());
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   148
            }
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   149
            return ExceptionAction.ExitVM;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   150
        }
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   151
        return CompilationFailureAction.getValue(options);
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   152
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   153
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   154
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   155
     * Perform the compilation wrapped by this object.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   156
     *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   157
     * @param debug the debug context to use for the compilation
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   158
     */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   159
    protected abstract T performCompilation(DebugContext debug);
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   160
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   161
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   162
     * Gets a value that represents the input to the compilation.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   163
     */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   164
    @Override
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   165
    public abstract String toString();
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   166
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   167
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   168
     * Creates the {@link DebugContext} to use when retrying a compilation.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   169
     *
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54204
diff changeset
   170
     * @param initialDebug the debug context used in the failing compilation
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   171
     * @param options the options for configuring the debug context
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   172
     * @param logStream the log stream to use in the debug context
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   173
     */
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54204
diff changeset
   174
    protected abstract DebugContext createRetryDebugContext(DebugContext initialDebug, OptionValues options, PrintStream logStream);
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   175
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   176
    @SuppressWarnings("try")
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   177
    public final T run(DebugContext initialDebug) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   178
        try {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   179
            return performCompilation(initialDebug);
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   180
        } catch (Throwable cause) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   181
            OptionValues initialOptions = initialDebug.getOptions();
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   182
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   183
            synchronized (CompilationFailureAction) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   184
                // Serialize all compilation failure handling.
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   185
                // This prevents retry compilation storms and interleaving
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   186
                // of compilation exception messages.
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   187
                // It also allows for reliable testing of CompilationWrapper
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   188
                // by avoiding a race whereby retry compilation output from a
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   189
                // forced crash (i.e., use of GraalCompilerOptions.CrashAt)
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   190
                // is truncated.
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   191
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   192
                ExceptionAction action = lookupAction(initialOptions, cause);
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   193
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   194
                action = adjustAction(initialOptions, action);
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   195
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   196
                if (action == ExceptionAction.Silent) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   197
                    return handleException(cause);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   198
                }
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   199
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   200
                if (action == ExceptionAction.Print) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   201
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   202
                    try (PrintStream ps = new PrintStream(baos)) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   203
                        ps.printf("%s: Compilation of %s failed: ", Thread.currentThread(), this);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   204
                        cause.printStackTrace(ps);
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   205
                        ps.printf("To disable compilation failure notifications, set %s to %s (e.g., -Dgraal.%s=%s).%n",
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   206
                                        CompilationFailureAction.getName(), ExceptionAction.Silent,
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   207
                                        CompilationFailureAction.getName(), ExceptionAction.Silent);
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   208
                        ps.printf("To capture more information for diagnosing or reporting a compilation failure, " +
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   209
                                        "set %s to %s or %s (e.g., -Dgraal.%s=%s).%n",
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   210
                                        CompilationFailureAction.getName(), ExceptionAction.Diagnose,
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   211
                                        ExceptionAction.ExitVM,
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   212
                                        CompilationFailureAction.getName(), ExceptionAction.Diagnose);
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   213
                    }
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   214
                    TTY.print(baos.toString());
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   215
                    return handleException(cause);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   216
                }
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   217
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   218
                // action is Diagnose or ExitVM
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   219
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   220
                if (Dump.hasBeenSet(initialOptions)) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   221
                    // If dumping is explicitly enabled, Graal is being debugged
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   222
                    // so don't interfere with what the user is expecting to see.
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   223
                    return handleException(cause);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   224
                }
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   225
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   226
                File dumpPath = null;
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   227
                try {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   228
                    String dir = this.outputDirectory.getPath();
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   229
                    if (dir != null) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   230
                        String dumpName = PathUtilities.sanitizeFileName(toString());
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   231
                        dumpPath = new File(dir, dumpName);
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   232
                        dumpPath.mkdirs();
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   233
                        if (!dumpPath.exists()) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   234
                            TTY.println("Warning: could not create diagnostics directory " + dumpPath);
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   235
                            dumpPath = null;
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   236
                        }
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   237
                    }
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   238
                } catch (Throwable t) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   239
                    TTY.println("Warning: could not create Graal diagnostic directory");
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   240
                    t.printStackTrace(TTY.out);
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   241
                }
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   242
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   243
                String message;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   244
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   245
                try (PrintStream ps = new PrintStream(baos)) {
59095
03fbcd06b4c0 8233841: Update Graal
dlong
parents: 58299
diff changeset
   246
                    // This output is used by external tools to detect compilation failures.
03fbcd06b4c0 8233841: Update Graal
dlong
parents: 58299
diff changeset
   247
                    ps.println("[[[Graal compilation failure]]]");
03fbcd06b4c0 8233841: Update Graal
dlong
parents: 58299
diff changeset
   248
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   249
                    ps.printf("%s: Compilation of %s failed:%n", Thread.currentThread(), this);
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   250
                    cause.printStackTrace(ps);
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   251
                    ps.printf("To disable compilation failure notifications, set %s to %s (e.g., -Dgraal.%s=%s).%n",
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   252
                                    CompilationFailureAction.getName(), ExceptionAction.Silent,
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   253
                                    CompilationFailureAction.getName(), ExceptionAction.Silent);
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   254
                    ps.printf("To print a message for a compilation failure without retrying the compilation, " +
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   255
                                    "set %s to %s (e.g., -Dgraal.%s=%s).%n",
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   256
                                    CompilationFailureAction.getName(), ExceptionAction.Print,
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   257
                                    CompilationFailureAction.getName(), ExceptionAction.Print);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   258
                    if (dumpPath != null) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   259
                        ps.println("Retrying compilation of " + this);
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   260
                    } else {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   261
                        ps.println("Not retrying compilation of " + this + " as the dump path could not be created.");
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   262
                    }
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   263
                    message = baos.toString();
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   264
                }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   265
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   266
                TTY.print(message);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   267
                if (dumpPath == null) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   268
                    return handleException(cause);
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   269
                }
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   270
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   271
                File retryLogFile = new File(dumpPath, "retry.log");
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   272
                try (PrintStream ps = new PrintStream(new FileOutputStream(retryLogFile))) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   273
                    ps.print(message);
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   274
                } catch (IOException ioe) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   275
                    TTY.printf("Error writing to %s: %s%n", retryLogFile, ioe);
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   276
                }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   277
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   278
                OptionValues retryOptions = new OptionValues(initialOptions,
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   279
                                Dump, ":" + VERBOSE_LEVEL,
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   280
                                MethodFilter, null,
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 57537
diff changeset
   281
                                DumpPath, dumpPath.getPath(),
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 57537
diff changeset
   282
                                TrackNodeSourcePosition, true);
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   283
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   284
                ByteArrayOutputStream logBaos = new ByteArrayOutputStream();
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   285
                PrintStream ps = new PrintStream(logBaos);
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54204
diff changeset
   286
                try (DebugContext retryDebug = createRetryDebugContext(initialDebug, retryOptions, ps)) {
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   287
                    T res = performCompilation(retryDebug);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   288
                    ps.println("There was no exception during retry.");
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   289
                    maybeExitVM(action);
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   290
                    return res;
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   291
                } catch (Throwable e) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   292
                    ps.println("Exception during retry:");
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   293
                    e.printStackTrace(ps);
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   294
                    // Failures during retry are silent
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   295
                    T res = handleException(cause);
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   296
                    maybeExitVM(action);
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   297
                    return res;
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   298
                } finally {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   299
                    ps.close();
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   300
                    try (FileOutputStream fos = new FileOutputStream(retryLogFile, true)) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   301
                        fos.write(logBaos.toByteArray());
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   302
                    } catch (Throwable e) {
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   303
                        TTY.printf("Error writing to %s: %s%n", retryLogFile, e);
7dd81e82d083 8210777: Update Graal
dlong
parents: 51736
diff changeset
   304
                    }
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   305
                }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   306
            }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   307
        }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   308
    }
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   309
57537
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   310
    /**
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   311
     * Calls {@link System#exit(int)} in the runtime embedding the Graal compiler. This will be a
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   312
     * different runtime than Graal's runtime in the case of libgraal.
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   313
     */
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   314
    protected abstract void exitHostVM(int status);
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   315
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   316
    private void maybeExitVM(ExceptionAction action) {
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   317
        if (action == ExitVM) {
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   318
            TTY.println("Exiting VM after retry compilation of " + this);
57537
ecc6e394475f 8226771: Update Graal
dlong
parents: 55509
diff changeset
   319
            exitHostVM(-1);
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   320
        }
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   321
    }
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   322
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   323
    /**
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   324
     * Adjusts {@code initialAction} if necessary based on
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   325
     * {@link GraalCompilerOptions#MaxCompilationProblemsPerAction}.
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   326
     */
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   327
    private ExceptionAction adjustAction(OptionValues initialOptions, ExceptionAction initialAction) {
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   328
        ExceptionAction action = initialAction;
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   329
        int maxProblems = MaxCompilationProblemsPerAction.getValue(initialOptions);
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   330
        if (action != ExceptionAction.ExitVM) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   331
            synchronized (problemsHandledPerAction) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   332
                while (action != ExceptionAction.Silent) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   333
                    int problems = problemsHandledPerAction.getOrDefault(action, 0);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   334
                    if (problems >= maxProblems) {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   335
                        if (problems == maxProblems) {
54204
55c262f4f5a1 8220389: Update Graal
jwilhelm
parents: 52910
diff changeset
   336
                            TTY.printf("Warning: adjusting %s from %s to %s after %s (%d) failed compilations%n", CompilationFailureAction, action, action.quieter(),
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   337
                                            MaxCompilationProblemsPerAction, maxProblems);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   338
                            // Ensure that the message above is only printed once
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   339
                            problemsHandledPerAction.put(action, problems + 1);
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   340
                        }
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   341
                        action = action.quieter();
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   342
                    } else {
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   343
                        break;
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   344
                    }
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   345
                }
51736
42d99cb7f50f 8210478: Update Graal
iveresov
parents: 51436
diff changeset
   346
                problemsHandledPerAction.put(action, problemsHandledPerAction.getOrDefault(action, 0) + 1);
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   347
            }
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   348
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   349
        return action;
8b2c620d7092 8186158: Update Graal
iveresov
parents: 46680
diff changeset
   350
    }
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   351
}