Skip to content

Commit

Permalink
Execute LogManager shutdown hook after all other shutdown hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhaeubl committed Dec 19, 2023
1 parent 1b59f41 commit cfdc207
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ final class Target_java_util_Currency {
@TargetClass(className = "java.util.logging.LogManager", onlyWith = JavaLoggingModule.IsPresent.class)
final class Target_java_util_logging_LogManager {

@Inject @RecomputeFieldValue(kind = Kind.NewInstance, declClass = AtomicBoolean.class) private AtomicBoolean addedShutdownHook = new AtomicBoolean();
@Inject @RecomputeFieldValue(kind = Kind.NewInstance, declClass = AtomicBoolean.class, isFinal = true) private AtomicBoolean addedShutdownHook = new AtomicBoolean();

@Alias static Target_java_util_logging_LogManager manager;

Expand All @@ -222,22 +222,17 @@ final class Target_java_util_logging_LogManager {

@Substitute
public static Target_java_util_logging_LogManager getLogManager() {
/* First performing logic originally in getLogManager. */
/* Logic from original JDK method. */
if (manager == null) {
return manager;
return null;
}
manager.ensureLogManagerInitialized();

/* Logic for adding shutdown hook. */
/* Add a shutdown hook to close the global handlers. */
if (!manager.addedShutdownHook.getAndSet(true)) {
/* Add a shutdown hook to close the global handlers. */
try {
Runtime.getRuntime().addShutdownHook(SubstrateUtil.cast(new Target_java_util_logging_LogManager_Cleaner(manager), Thread.class));
} catch (IllegalStateException e) {
/* If the VM is already shutting down, we do not need to register shutdownHook. */
}
Runnable hook = SubstrateUtil.cast(new Target_java_util_logging_LogManager_Cleaner(manager), Runnable.class);
Util_java_lang_Shutdown.registerLogManagerShutdownHook(hook);
}

return manager;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.util.VMError;

@TargetClass(className = "java.lang.Shutdown")
public final class Target_java_lang_Shutdown {
Expand All @@ -41,12 +42,7 @@ public final class Target_java_lang_Shutdown {

static {
hooks = new Runnable[Util_java_lang_Shutdown.MAX_SYSTEM_HOOKS];
/*
* We use the last system hook slot (index 9), which is currently not used by the JDK, for
* our own shutdown hooks that are registered during image generation. The JDK currently
* uses slots 0, 1, and 2.
*/
hooks[hooks.length - 1] = RuntimeSupport::executeShutdownHooks;
hooks[Util_java_lang_Shutdown.NATIVE_IMAGE_SHUTDOWN_HOOKS_SLOT] = RuntimeSupport::executeShutdownHooks;
}

@Substitute
Expand All @@ -73,7 +69,21 @@ final class Util_java_lang_Shutdown {

/**
* Value *copied* from {@code java.lang.Shutdown.MAX_SYSTEM_HOOKS} so that the value can be used
* during image generation (@Alias values are only visible at run time).
* during image generation (@Alias values are only visible at run time). The JDK currently uses
* slots 0, 1, and 2.
*/
static final int MAX_SYSTEM_HOOKS = 10;

static final int LOG_MANAGER_SHUTDOWN_HOOK_SLOT = MAX_SYSTEM_HOOKS - 1;
static final int NATIVE_IMAGE_SHUTDOWN_HOOKS_SLOT = LOG_MANAGER_SHUTDOWN_HOOK_SLOT - 1;

public static void registerLogManagerShutdownHook(Runnable hook) {
/*
* Execute the LogManager shutdown hook after all other shutdown hooks. This is a workaround
* for GR-39429.
*/
Runnable[] hooks = Target_java_lang_Shutdown.hooks;
VMError.guarantee(hooks[LOG_MANAGER_SHUTDOWN_HOOK_SLOT] == null, "slot must not be used");
hooks[LOG_MANAGER_SHUTDOWN_HOOK_SLOT] = hook;
}
}
3 changes: 1 addition & 2 deletions vm/mx.vm/mx_vm_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ def __init__(self, vm, bm_suite, args):
base_image_build_args += ['-R:+FlightRecorder',
'-R:StartFlightRecording=filename=default.jfr',
'--enable-monitoring=jfr',
# We should enable this flag, but after we fix GR-39429.
# '-R:+JfrBasedExecutionSamplerStatistics'
'-R:+JfrBasedExecutionSamplerStatistics'
]
for stage in ('instrument-image', 'instrument-run'):
if stage in self.stages:
Expand Down

0 comments on commit cfdc207

Please sign in to comment.