46680
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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 |
*/
|
50858
|
23 |
|
|
24 |
|
46680
|
25 |
package org.graalvm.compiler.core;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.ExitVM;
|
|
28 |
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationBailoutAction;
|
|
29 |
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationFailureAction;
|
|
30 |
import static org.graalvm.compiler.core.GraalCompilerOptions.ExitVMOnException;
|
46807
|
31 |
import static org.graalvm.compiler.core.GraalCompilerOptions.MaxCompilationProblemsPerAction;
|
46680
|
32 |
import static org.graalvm.compiler.debug.DebugContext.VERBOSE_LEVEL;
|
|
33 |
import static org.graalvm.compiler.debug.DebugOptions.Dump;
|
|
34 |
import static org.graalvm.compiler.debug.DebugOptions.DumpPath;
|
|
35 |
import static org.graalvm.compiler.debug.DebugOptions.MethodFilter;
|
|
36 |
|
|
37 |
import java.io.ByteArrayOutputStream;
|
|
38 |
import java.io.File;
|
|
39 |
import java.io.FileOutputStream;
|
|
40 |
import java.io.IOException;
|
|
41 |
import java.io.PrintStream;
|
46807
|
42 |
import java.util.Map;
|
46680
|
43 |
|
|
44 |
import org.graalvm.compiler.debug.DebugContext;
|
|
45 |
import org.graalvm.compiler.debug.DiagnosticsOutputDirectory;
|
|
46 |
import org.graalvm.compiler.debug.PathUtilities;
|
|
47 |
import org.graalvm.compiler.debug.TTY;
|
|
48 |
import org.graalvm.compiler.options.EnumOptionKey;
|
|
49 |
import org.graalvm.compiler.options.OptionValues;
|
|
50 |
|
|
51 |
import jdk.vm.ci.code.BailoutException;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Wrapper for a compilation that centralizes what action to take based on
|
|
55 |
* {@link GraalCompilerOptions#CompilationBailoutAction} and
|
|
56 |
* {@link GraalCompilerOptions#CompilationFailureAction} when an uncaught exception occurs during
|
|
57 |
* compilation.
|
|
58 |
*/
|
|
59 |
public abstract class CompilationWrapper<T> {
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Actions to take upon an exception being raised during compilation performed via
|
|
63 |
* {@link CompilationWrapper}. The actions are with respect to what the user sees on the
|
|
64 |
* console. The compilation requester determines what ultimate action is taken in
|
|
65 |
* {@link CompilationWrapper#handleException(Throwable)}.
|
46807
|
66 |
*
|
|
67 |
* The actions are in ascending order of verbosity.
|
46680
|
68 |
*/
|
|
69 |
public enum ExceptionAction {
|
|
70 |
/**
|
|
71 |
* Print nothing to the console.
|
|
72 |
*/
|
|
73 |
Silent,
|
|
74 |
/**
|
|
75 |
* Print a stack trace to the console.
|
|
76 |
*/
|
|
77 |
Print,
|
|
78 |
/**
|
|
79 |
* An exception causes the compilation to be retried with extra diagnostics enabled.
|
|
80 |
*/
|
|
81 |
Diagnose,
|
|
82 |
/**
|
|
83 |
* Same as {@link #Diagnose} except that the VM process is exited after retrying.
|
|
84 |
*/
|
|
85 |
ExitVM;
|
|
86 |
|
51436
|
87 |
private static final ExceptionAction[] VALUES = values();
|
|
88 |
|
46807
|
89 |
/**
|
|
90 |
* Gets the action that is one level less verbose than this action, bottoming out at the
|
|
91 |
* least verbose action.
|
|
92 |
*/
|
|
93 |
ExceptionAction quieter() {
|
|
94 |
assert ExceptionAction.Silent.ordinal() == 0;
|
|
95 |
int index = Math.max(ordinal() - 1, 0);
|
51436
|
96 |
return VALUES[index];
|
46807
|
97 |
}
|
46680
|
98 |
}
|
|
99 |
|
|
100 |
private final DiagnosticsOutputDirectory outputDirectory;
|
|
101 |
|
46807
|
102 |
private final Map<ExceptionAction, Integer> problemsHandledPerAction;
|
|
103 |
|
46680
|
104 |
/**
|
|
105 |
* @param outputDirectory object used to access a directory for dumping if the compilation is
|
|
106 |
* re-executed
|
46807
|
107 |
* @param problemsHandledPerAction map used to count the number of compilation failures or
|
|
108 |
* bailouts handled by each action. This is provided by the caller as it is expected
|
|
109 |
* to be shared between instances of {@link CompilationWrapper}.
|
46680
|
110 |
*/
|
46807
|
111 |
public CompilationWrapper(DiagnosticsOutputDirectory outputDirectory, Map<ExceptionAction, Integer> problemsHandledPerAction) {
|
46680
|
112 |
this.outputDirectory = outputDirectory;
|
46807
|
113 |
this.problemsHandledPerAction = problemsHandledPerAction;
|
46680
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Handles an uncaught exception.
|
|
118 |
*
|
|
119 |
* @param t an exception thrown during {@link #run(DebugContext)}
|
|
120 |
* @return a value representing the result of a failed compilation (may be {@code null})
|
|
121 |
*/
|
|
122 |
protected abstract T handleException(Throwable t);
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Gets the action to take based on the value of {@code actionKey} in {@code options}.
|
|
126 |
*
|
|
127 |
* Subclasses can override this to choose a different action based on factors such as whether
|
|
128 |
* {@code actionKey} has been explicitly set in {@code options} for example.
|
49873
|
129 |
*
|
|
130 |
* @param cause the cause of the bailout or failure
|
46680
|
131 |
*/
|
49873
|
132 |
protected ExceptionAction lookupAction(OptionValues options, EnumOptionKey<ExceptionAction> actionKey, Throwable cause) {
|
46680
|
133 |
if (actionKey == CompilationFailureAction) {
|
|
134 |
if (ExitVMOnException.getValue(options)) {
|
|
135 |
assert CompilationFailureAction.getDefaultValue() != ExceptionAction.ExitVM;
|
|
136 |
assert ExitVMOnException.getDefaultValue() != true;
|
|
137 |
if (CompilationFailureAction.hasBeenSet(options) && CompilationFailureAction.getValue(options) != ExceptionAction.ExitVM) {
|
|
138 |
TTY.printf("WARNING: Ignoring %s=%s since %s=true has been explicitly specified.%n",
|
|
139 |
CompilationFailureAction.getName(), CompilationFailureAction.getValue(options),
|
|
140 |
ExitVMOnException.getName());
|
|
141 |
}
|
|
142 |
return ExceptionAction.ExitVM;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
return actionKey.getValue(options);
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Perform the compilation wrapped by this object.
|
|
150 |
*
|
|
151 |
* @param debug the debug context to use for the compilation
|
|
152 |
*/
|
|
153 |
protected abstract T performCompilation(DebugContext debug);
|
|
154 |
|
|
155 |
/**
|
|
156 |
* Gets a value that represents the input to the compilation.
|
|
157 |
*/
|
|
158 |
@Override
|
|
159 |
public abstract String toString();
|
|
160 |
|
|
161 |
/**
|
|
162 |
* Creates the {@link DebugContext} to use when retrying a compilation.
|
|
163 |
*
|
|
164 |
* @param options the options for configuring the debug context
|
|
165 |
*/
|
|
166 |
protected abstract DebugContext createRetryDebugContext(OptionValues options);
|
|
167 |
|
|
168 |
@SuppressWarnings("try")
|
|
169 |
public final T run(DebugContext initialDebug) {
|
|
170 |
try {
|
|
171 |
return performCompilation(initialDebug);
|
|
172 |
} catch (Throwable cause) {
|
|
173 |
OptionValues initialOptions = initialDebug.getOptions();
|
|
174 |
|
|
175 |
String causeType = "failure";
|
|
176 |
EnumOptionKey<ExceptionAction> actionKey;
|
|
177 |
if (cause instanceof BailoutException) {
|
|
178 |
actionKey = CompilationBailoutAction;
|
|
179 |
causeType = "bailout";
|
|
180 |
} else {
|
|
181 |
actionKey = CompilationFailureAction;
|
|
182 |
causeType = "failure";
|
|
183 |
}
|
49873
|
184 |
ExceptionAction action = lookupAction(initialOptions, actionKey, cause);
|
46680
|
185 |
|
46807
|
186 |
action = adjustAction(initialOptions, actionKey, action);
|
|
187 |
|
46680
|
188 |
if (action == ExceptionAction.Silent) {
|
|
189 |
return handleException(cause);
|
|
190 |
}
|
46807
|
191 |
|
46680
|
192 |
if (action == ExceptionAction.Print) {
|
|
193 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
194 |
try (PrintStream ps = new PrintStream(baos)) {
|
|
195 |
ps.printf("%s: Compilation of %s failed: ", Thread.currentThread(), this);
|
|
196 |
cause.printStackTrace(ps);
|
|
197 |
ps.printf("To disable compilation %s notifications, set %s to %s (e.g., -Dgraal.%s=%s).%n",
|
|
198 |
causeType,
|
|
199 |
actionKey.getName(), ExceptionAction.Silent,
|
|
200 |
actionKey.getName(), ExceptionAction.Silent);
|
|
201 |
ps.printf("To capture more information for diagnosing or reporting a compilation %s, " +
|
|
202 |
"set %s to %s or %s (e.g., -Dgraal.%s=%s).%n",
|
|
203 |
causeType,
|
|
204 |
actionKey.getName(), ExceptionAction.Diagnose,
|
|
205 |
ExceptionAction.ExitVM,
|
|
206 |
actionKey.getName(), ExceptionAction.Diagnose);
|
|
207 |
}
|
|
208 |
synchronized (CompilationFailureAction) {
|
|
209 |
// Synchronize to prevent compilation exception
|
|
210 |
// messages from interleaving.
|
|
211 |
TTY.println(baos.toString());
|
|
212 |
}
|
|
213 |
return handleException(cause);
|
|
214 |
}
|
|
215 |
|
|
216 |
// action is Diagnose or ExitVM
|
|
217 |
|
|
218 |
if (Dump.hasBeenSet(initialOptions)) {
|
|
219 |
// If dumping is explicitly enabled, Graal is being debugged
|
|
220 |
// so don't interfere with what the user is expecting to see.
|
|
221 |
return handleException(cause);
|
|
222 |
}
|
|
223 |
|
|
224 |
String dir = this.outputDirectory.getPath();
|
|
225 |
if (dir == null) {
|
46807
|
226 |
return handleException(cause);
|
46680
|
227 |
}
|
|
228 |
String dumpName = PathUtilities.sanitizeFileName(toString());
|
|
229 |
File dumpPath = new File(dir, dumpName);
|
|
230 |
dumpPath.mkdirs();
|
|
231 |
if (!dumpPath.exists()) {
|
|
232 |
TTY.println("Warning: could not create diagnostics directory " + dumpPath);
|
|
233 |
return handleException(cause);
|
|
234 |
}
|
|
235 |
|
|
236 |
String message;
|
|
237 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
238 |
try (PrintStream ps = new PrintStream(baos)) {
|
|
239 |
ps.printf("%s: Compilation of %s failed: ", Thread.currentThread(), this);
|
|
240 |
cause.printStackTrace(ps);
|
|
241 |
ps.printf("To disable compilation %s notifications, set %s to %s (e.g., -Dgraal.%s=%s).%n",
|
|
242 |
causeType,
|
|
243 |
actionKey.getName(), ExceptionAction.Silent,
|
|
244 |
actionKey.getName(), ExceptionAction.Silent);
|
|
245 |
ps.printf("To print a message for a compilation %s without retrying the compilation, " +
|
|
246 |
"set %s to %s (e.g., -Dgraal.%s=%s).%n",
|
|
247 |
causeType,
|
|
248 |
actionKey.getName(), ExceptionAction.Print,
|
|
249 |
actionKey.getName(), ExceptionAction.Print);
|
|
250 |
ps.println("Retrying compilation of " + this);
|
|
251 |
message = baos.toString();
|
|
252 |
}
|
|
253 |
|
|
254 |
synchronized (CompilationFailureAction) {
|
|
255 |
// Synchronize here to serialize retry compilations. This
|
|
256 |
// mitigates retry compilation storms.
|
|
257 |
TTY.println(message);
|
|
258 |
File retryLogFile = new File(dumpPath, "retry.log");
|
|
259 |
try (PrintStream ps = new PrintStream(new FileOutputStream(retryLogFile))) {
|
|
260 |
ps.print(message);
|
|
261 |
} catch (IOException ioe) {
|
|
262 |
TTY.printf("Error writing to %s: %s%n", retryLogFile, ioe);
|
|
263 |
}
|
|
264 |
|
|
265 |
OptionValues retryOptions = new OptionValues(initialOptions,
|
|
266 |
Dump, ":" + VERBOSE_LEVEL,
|
|
267 |
MethodFilter, null,
|
|
268 |
DumpPath, dumpPath.getPath());
|
|
269 |
|
|
270 |
try (DebugContext retryDebug = createRetryDebugContext(retryOptions)) {
|
49873
|
271 |
T res = performCompilation(retryDebug);
|
|
272 |
maybeExitVM(action);
|
|
273 |
return res;
|
46680
|
274 |
} catch (Throwable ignore) {
|
|
275 |
// Failures during retry are silent
|
49873
|
276 |
T res = handleException(cause);
|
|
277 |
maybeExitVM(action);
|
|
278 |
return res;
|
46680
|
279 |
}
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
46807
|
283 |
|
49873
|
284 |
private void maybeExitVM(ExceptionAction action) {
|
|
285 |
if (action == ExitVM) {
|
|
286 |
synchronized (ExceptionAction.class) {
|
|
287 |
try {
|
|
288 |
// Give other compiler threads a chance to flush
|
|
289 |
// error handling output.
|
|
290 |
ExceptionAction.class.wait(2000);
|
|
291 |
} catch (InterruptedException e) {
|
|
292 |
}
|
|
293 |
TTY.println("Exiting VM after retry compilation of " + this);
|
|
294 |
System.exit(-1);
|
|
295 |
}
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
46807
|
299 |
/**
|
|
300 |
* Adjusts {@code initialAction} if necessary based on
|
|
301 |
* {@link GraalCompilerOptions#MaxCompilationProblemsPerAction}.
|
|
302 |
*/
|
|
303 |
private ExceptionAction adjustAction(OptionValues initialOptions, EnumOptionKey<ExceptionAction> actionKey, ExceptionAction initialAction) {
|
|
304 |
ExceptionAction action = initialAction;
|
|
305 |
int maxProblems = MaxCompilationProblemsPerAction.getValue(initialOptions);
|
|
306 |
synchronized (problemsHandledPerAction) {
|
|
307 |
while (action != ExceptionAction.Silent) {
|
|
308 |
int problems = problemsHandledPerAction.getOrDefault(action, 0);
|
|
309 |
if (problems >= maxProblems) {
|
|
310 |
if (problems == maxProblems) {
|
|
311 |
TTY.printf("Warning: adjusting %s from %s to %s after %s (%d) failed compilations%n", actionKey, action, action.quieter(),
|
|
312 |
MaxCompilationProblemsPerAction, maxProblems);
|
|
313 |
// Ensure that the message above is only printed once
|
|
314 |
problemsHandledPerAction.put(action, problems + 1);
|
|
315 |
}
|
|
316 |
action = action.quieter();
|
|
317 |
} else {
|
|
318 |
break;
|
|
319 |
}
|
|
320 |
}
|
|
321 |
problemsHandledPerAction.put(action, problemsHandledPerAction.getOrDefault(action, 0) + 1);
|
|
322 |
}
|
|
323 |
return action;
|
|
324 |
}
|
46680
|
325 |
}
|