Skip to content

Commit aeed0d5

Browse files
committed
fix hotspot issue
1 parent 48799d7 commit aeed0d5

File tree

9 files changed

+62
-45
lines changed

9 files changed

+62
-45
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/CompilationWatchDogTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void onStuckCompilation(CompilationWatchDog watchDog, Thread watched, Com
137137

138138
};
139139

140-
CompilationWatchDog watch = CompilationWatchDog.watch(compilation, options, false, longCompilationHandler);
140+
CompilationWatchDog watch = CompilationWatchDog.watch(compilation, options, false, longCompilationHandler, null);
141141
try (CompilationWatchDog watchScope = watch) {
142142
event("start compiling");
143143
try (TTY.Filter f = new TTY.Filter()) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/CompilationWatchDog.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,21 @@ public static class Options {
181181

182182
private final ScheduledExecutorService singleShotExecutor;
183183

184-
CompilationWatchDog(CompilationIdentifier compilation, Thread watchedThread, int delay, int vmExitDelay,
185-
boolean singleShotExecutor, EventHandler eventHandler) {
184+
CompilationWatchDog(CompilationIdentifier compilation,
185+
Thread watchedThread, int delay, int vmExitDelay,
186+
boolean singleShotExecutor, EventHandler eventHandler,
187+
ThreadFactory factory) {
186188
this.compilation = compilation;
187189
this.watchedThread = watchedThread;
188190
this.vmExitDelayNS = TimeUnit.SECONDS.toNanos(vmExitDelay);
189191
this.eventHandler = eventHandler == null ? EventHandler.DEFAULT : eventHandler;
190192
trace("started compiling %s", compilation);
191193
if (singleShotExecutor) {
192-
this.singleShotExecutor = createExecutor();
194+
this.singleShotExecutor = createExecutor(factory);
193195
this.task = this.singleShotExecutor.schedule(this, delay, TimeUnit.SECONDS);
194196
} else {
195197
this.singleShotExecutor = null;
196-
this.task = schedule(this, delay);
198+
this.task = schedule(this, delay, factory);
197199
}
198200
}
199201

@@ -293,27 +295,24 @@ public void run() {
293295

294296
private static ScheduledExecutorService watchDogService;
295297

296-
private static synchronized ScheduledFuture<?> schedule(CompilationWatchDog watchdog, int delay) {
298+
private static synchronized ScheduledFuture<?> schedule(CompilationWatchDog watchdog, int delay, ThreadFactory factory) {
297299
if (watchDogService == null) {
298-
watchDogService = createExecutor();
300+
watchDogService = createExecutor(factory);
299301
}
300302
return watchDogService.schedule(watchdog, delay, TimeUnit.SECONDS);
301303
}
302304

303-
private static ScheduledExecutorService createExecutor() {
304-
ThreadFactory threadFactory = new ThreadFactory() {
305-
@Override
306-
public Thread newThread(Runnable r) {
307-
Thread thread = new GraalServiceThread(CompilationWatchDog.class.getSimpleName(), r);
308-
thread.setName("WatchDog-" + GraalServices.getThreadId(thread));
309-
thread.setPriority(Thread.MAX_PRIORITY);
310-
thread.setDaemon(true);
311-
return thread;
312-
}
305+
private static ScheduledExecutorService createExecutor(ThreadFactory factory) {
306+
ThreadFactory watchDogThreadFactory = r -> {
307+
Thread thread = factory == null ? new Thread(r) : factory.newThread(r);
308+
thread.setName("WatchDog-" + GraalServices.getThreadId(thread));
309+
thread.setPriority(Thread.MAX_PRIORITY);
310+
thread.setDaemon(true);
311+
return thread;
313312
};
314313

315314
int poolSize = 1;
316-
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(poolSize, threadFactory);
315+
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(poolSize, watchDogThreadFactory);
317316
executor.setRemoveOnCancelPolicy(true);
318317
executor.allowCoreThreadTimeOut(true);
319318
return executor;
@@ -323,16 +322,19 @@ public Thread newThread(Runnable r) {
323322
* Opens a scope for watching a compilation.
324323
*
325324
* @param compilation identifies the compilation being watched
326-
* @param singleShotExecutor if true, then a dedicated executor is created for this task and it
325+
* @param singleShotExecutor if true, then a dedicated executor is created for this task, and it
327326
* is shutdown once the compilation ends
328-
* @param eventHandler notified of events like a compilation running long running or getting
329-
* stuck. If {@code null}, {@link EventHandler#DEFAULT} is used.
327+
* @param eventHandler notified of events like a compilation running long or getting stuck. If
328+
* {@code null}, {@link EventHandler#DEFAULT} is used.
329+
* @param factory factory to use for creating the watcher thread. If null, a default Thread
330+
* object is used.
330331
* @return {@code null} if the compilation watch dog is disabled otherwise a new
331332
* {@link CompilationWatchDog} object. The returned value should be used in a
332333
* {@code try}-with-resources statement whose scope is the whole compilation so that
333334
* leaving the scope will cause {@link #close()} to be called.
334335
*/
335-
public static CompilationWatchDog watch(CompilationIdentifier compilation, OptionValues options, boolean singleShotExecutor, EventHandler eventHandler) {
336+
public static CompilationWatchDog watch(CompilationIdentifier compilation, OptionValues options,
337+
boolean singleShotExecutor, EventHandler eventHandler, ThreadFactory factory) {
336338
int delay = Options.CompilationWatchDogStartDelay.getValue(options);
337339
if (ImageInfo.inImageBuildtimeCode() && !Options.CompilationWatchDogStartDelay.hasBeenSet(options)) {
338340
// Disable watch dog by default when building a native image
@@ -341,9 +343,8 @@ public static CompilationWatchDog watch(CompilationIdentifier compilation, Optio
341343
if (delay > 0) {
342344
Thread watchedThread = Thread.currentThread();
343345
int vmExitDelay = Options.CompilationWatchDogVMExitDelay.getValue(options);
344-
CompilationWatchDog watchDog = new CompilationWatchDog(compilation, watchedThread, delay,
345-
vmExitDelay, singleShotExecutor, eventHandler);
346-
return watchDog;
346+
return new CompilationWatchDog(compilation, watchedThread, delay,
347+
vmExitDelay, singleShotExecutor, eventHandler, factory);
347348
}
348349
return null;
349350
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/debug/DebugContext.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public final class DebugContext implements AutoCloseable {
8686
*/
8787
public static final String DUMP_FILE_MESSAGE_REGEXP = "Dumping debug output to '(?<filename>[^']+)'";
8888

89+
private static final Description DISABLED_DESCRIPTION = new Description(null, "DISABLED_DESCRIPTION");
8990
public static final Description NO_DESCRIPTION = new Description(null, "NO_DESCRIPTION");
9091
public static final GlobalMetrics NO_GLOBAL_METRIC_VALUES = null;
9192
public static final Iterable<DebugHandlersFactory> NO_CONFIG_CUSTOMIZERS = Collections.emptyList();
@@ -349,7 +350,7 @@ public Activation activate() {
349350
/**
350351
* Singleton used to represent a disabled debug context.
351352
*/
352-
private static final DebugContext DISABLED = new DebugContext(NO_DESCRIPTION, null, NO_GLOBAL_METRIC_VALUES, getDefaultLogStream(), new Immutable(), NO_CONFIG_CUSTOMIZERS);
353+
private static final DebugContext DISABLED = new DebugContext(DISABLED_DESCRIPTION, null, NO_GLOBAL_METRIC_VALUES, getDefaultLogStream(), new Immutable(), NO_CONFIG_CUSTOMIZERS);
353354

354355
/**
355356
* Create a DebugContext with debugging disabled.
@@ -638,6 +639,7 @@ private DebugContext(Description description,
638639
Immutable immutable,
639640
Iterable<DebugHandlersFactory> factories, boolean disableConfig) {
640641
this.immutable = immutable;
642+
this.invariants = Assertions.assertionsEnabled() && description != DISABLED_DESCRIPTION ? new Invariants() : null;
641643
this.description = description;
642644
this.globalMetrics = globalMetrics;
643645
this.compilationListener = compilationListener;
@@ -873,11 +875,7 @@ public DebugContext.Scope scope(Object name) {
873875
}
874876
}
875877

876-
/**
877-
* Arbitrary threads cannot be in the image so null out {@code DebugContext.invariants} which
878-
* holds onto a thread and is only used for assertions.
879-
*/
880-
private final Invariants invariants = Assertions.assertionsEnabled() ? new Invariants() : null;
878+
private final Invariants invariants;
881879

882880
static StackTraceElement[] getStackTrace(Thread thread) {
883881
return thread.getStackTrace();

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/HotSpotGraalCompiler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collections;
3131
import java.util.List;
3232
import java.util.Objects;
33+
import java.util.concurrent.ThreadFactory;
3334

3435
import jdk.graal.compiler.api.runtime.GraalJVMCICompiler;
3536
import jdk.graal.compiler.code.CompilationResult;
@@ -207,7 +208,8 @@ public CompilationRequestResult compileMethod(CompilationRequest request, boolea
207208
boolean oneIsolatePerCompilation = ImageInfo.inImageRuntimeCode() &&
208209
config.getFlag("JVMCIThreadsPerNativeLibraryRuntime", Integer.class, 0) == 1 &&
209210
config.getFlag("JVMCICompilerIdleDelay", Integer.class, 1000) == 0;
210-
try (CompilationWatchDog w1 = CompilationWatchDog.watch(task.getCompilationIdentifier(), options, oneIsolatePerCompilation, task);
211+
ThreadFactory factory = ImageInfo.inImageRuntimeCode() ? HotSpotGraalServiceThread::new : null;
212+
try (CompilationWatchDog w1 = CompilationWatchDog.watch(task.getCompilationIdentifier(), options, oneIsolatePerCompilation, task, factory);
211213
BootstrapWatchDog.Watch w2 = bootstrapWatchDog == null ? null : bootstrapWatchDog.watch(request);
212214
CompilationAlarm alarm = CompilationAlarm.trackCompilationPeriod(options);) {
213215
if (compilationCounters != null) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/GraalServiceThread.java renamed to compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/HotSpotGraalServiceThread.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25-
package jdk.graal.compiler.core;
25+
package jdk.graal.compiler.hotspot;
2626

2727
import jdk.graal.compiler.serviceprovider.GraalServices;
2828
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
2929

3030
/**
31-
* This is a utility class for Threads started by the compiler itself. In certain execution
32-
* environments extra work must be done for these threads to execute correctly and this class
33-
* provides hooks for this work.
31+
* This is a utility class for Threads started by the compiler itself. In libgraal, such threads
32+
* must be attached and detached to the host JVM process.
3433
*/
35-
public class GraalServiceThread extends Thread {
34+
public class HotSpotGraalServiceThread extends Thread {
3635
private final Runnable runnable;
3736

38-
public GraalServiceThread(String name, Runnable runnable) {
39-
super(name);
37+
public HotSpotGraalServiceThread(Runnable runnable) {
4038
this.runnable = runnable;
4139
}
4240

@@ -67,12 +65,12 @@ public final void run() {
6765
* Notifies of an error on attaching this thread to the libgraal peer runtime.
6866
*
6967
* The default implementation of this method is to print the stack trace for {@code error} if
70-
* the {@code GraalServiceThread.verbose} system property is {@code "true"}.
68+
* the {@code HotSpotGraalServiceThread.verbose} system property is {@code "true"}.
7169
*
7270
* @param error the error
7371
*/
7472
protected void onAttachError(InternalError error) {
75-
if (Boolean.parseBoolean(GraalServices.getSavedProperty("GraalServiceThread.verbose", "false"))) {
73+
if (Boolean.parseBoolean(GraalServices.getSavedProperty("HotSpotGraalServiceThread.verbose", "false"))) {
7674
error.printStackTrace();
7775
}
7876
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/debug/BenchmarkCounters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.concurrent.ConcurrentHashMap;
4040
import java.util.concurrent.atomic.AtomicLong;
4141

42-
import jdk.graal.compiler.core.GraalServiceThread;
4342
import jdk.graal.compiler.core.common.SuppressFBWarnings;
4443
import jdk.graal.compiler.debug.CSVUtil;
4544
import jdk.graal.compiler.debug.GraalError;
@@ -468,7 +467,7 @@ protected void patternFound(int index) {
468467
if (ImageInfo.inImageRuntimeCode()) {
469468
throw new GraalError("Use of %s is only supported in jargraal", Options.TimedDynamicCounters.getName());
470469
}
471-
Thread thread = new GraalServiceThread(BenchmarkCounters.class.getSimpleName(), new Runnable() {
470+
Thread thread = new Thread(new Runnable() {
472471
long lastTime = System.nanoTime();
473472

474473
@Override
@@ -486,6 +485,7 @@ public void run() {
486485
}
487486
}
488487
});
488+
thread.setName(BenchmarkCounters.class.getSimpleName());
489489
thread.setDaemon(true);
490490
thread.setPriority(Thread.MAX_PRIORITY);
491491
thread.start();

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/TruffleCompilerImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.LinkedHashMap;
4040
import java.util.List;
4141
import java.util.Map;
42+
import java.util.concurrent.ThreadFactory;
4243
import java.util.function.Consumer;
4344

4445
import org.graalvm.collections.EconomicMap;
@@ -471,6 +472,15 @@ public void compileAST(
471472
compileAST(new TruffleCompilationWrapper(getOrCreateCompilerOptions(compilable), getDebugOutputDirectory(), Collections.emptyMap(), compilable, task, compilationId, listener), debug);
472473
}
473474

475+
/**
476+
* Gets a factory to use for creating the {@link CompilationWatchDog} watcher thread.
477+
*
478+
* @see CompilationWatchDog#watch
479+
*/
480+
protected ThreadFactory getWatchDogThreadFactory() {
481+
return null;
482+
}
483+
474484
/**
475485
* Compiles a Truffle AST. If compilation succeeds, the AST will have compiled code associated
476486
* with it that can be executed instead of interpreting the AST.
@@ -760,7 +770,8 @@ protected Void handleException(Throwable t) {
760770
@SuppressWarnings("try")
761771
@Override
762772
protected Void performCompilation(DebugContext debug) {
763-
try (CompilationWatchDog watch = CompilationWatchDog.watch(compilationId, debug.getOptions(), false, TruffleCompilerImpl.this)) {
773+
ThreadFactory factory = getWatchDogThreadFactory();
774+
try (CompilationWatchDog watch = CompilationWatchDog.watch(compilationId, debug.getOptions(), false, TruffleCompilerImpl.this, factory)) {
764775
compileAST(this, debug);
765776
return null;
766777
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/hotspot/HotSpotTruffleCompilerImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import java.util.List;
3434
import java.util.ListIterator;
3535
import java.util.Map;
36+
import java.util.concurrent.ThreadFactory;
3637
import java.util.function.Supplier;
3738

3839
import jdk.graal.compiler.core.GraalCompiler;
40+
import jdk.graal.compiler.hotspot.HotSpotGraalServiceThread;
3941
import jdk.graal.compiler.truffle.host.TruffleHostEnvironment.TruffleRuntimeScope;
4042
import org.graalvm.collections.EconomicMap;
4143

@@ -141,6 +143,11 @@ protected void parseGraalOptions(String[] options, EconomicMap<OptionKey<?>, Obj
141143
OptionsParser.parseOptions(options, values, OptionsParser.getOptionsLoader());
142144
}
143145

146+
@Override
147+
protected ThreadFactory getWatchDogThreadFactory() {
148+
return HotSpotGraalServiceThread::new;
149+
}
150+
144151
public static HotSpotTruffleCompilerImpl create(final TruffleCompilerRuntime runtime, Supplier<TruffleRuntimeScope> openCanCallTruffleRuntimeScope) {
145152
OptionValues options = HotSpotGraalOptionValues.defaultOptions();
146153
/*

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/SubstrateGraalUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected void parseRetryOptions(String[] options, EconomicMap<OptionKey<?>, Obj
121121
@SuppressWarnings("try")
122122
@Override
123123
protected CompilationResult performCompilation(DebugContext debug) {
124-
try (CompilationWatchDog watchdog = CompilationWatchDog.watch(compilationId, debug.getOptions(), false, COMPILATION_WATCH_DOG_EVENT_HANDLER)) {
124+
try (CompilationWatchDog watchdog = CompilationWatchDog.watch(compilationId, debug.getOptions(), false, COMPILATION_WATCH_DOG_EVENT_HANDLER, null)) {
125125
StructuredGraph graph = TruffleRuntimeCompilationSupport.decodeGraph(debug, null, compilationId, method, null);
126126
return compileGraph(runtimeConfig, TruffleRuntimeCompilationSupport.getMatchingSuitesForGraph(graph), lirSuites, method, graph);
127127
}

0 commit comments

Comments
 (0)