Skip to content

Commit

Permalink
Add JVMTI VirtualThreadDestroy Event
Browse files Browse the repository at this point in the history
Currently destroyThreadData() is hooked onto the VirtualThreadEnd event
which is called before the event callback. In JDWP, the agent uses TLS
to associate nodes in the running thread list. Clearing TLS before
the event callback makes the agent lose track of the node thus failed to
remove existed virtual threads from the list. This commit adds JVMTI
VirtualThreadDestroy event which is triggered after VirtualThreadEnd and
jvmtiHookVirtualThreadDestroy() is hooked onto VirtualThreadDestroy
event.

Fixes:
ibmruntimes/Semeru-Runtimes#80
eclipse-openj9#19759

Signed-off-by: Gengchen Tuo <[email protected]>
  • Loading branch information
thallium committed Jul 17, 2024
1 parent 91e4073 commit 4771211
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions runtime/include/ibmjvmti.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@

#define COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_MOUNT "com.sun.hotspot.events.VirtualThreadMount"
#define COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_UNMOUNT "com.sun.hotspot.events.VirtualThreadUnmount"
#define COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY "com.sun.hotspot.events.VirtualThreadDestroy"

#define OPENJ9_EVENT_VM_CHECKPOINT "openj9.event.VMCheckpoint"
#define OPENJ9_EVENT_VM_RESTORE "openj9.event.VMRestore"
Expand Down
1 change: 1 addition & 0 deletions runtime/j9vm/javanextvmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ JVM_VirtualThreadEnd(JNIEnv *env, jobject vthread)
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_END(vm->hookInterface, currentThread);
setContinuationStateToLastUnmount((J9VMThread *)env, vthread);
virtualThreadUnmountBegin(env, vthread);
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_DESTROY(vm->hookInterface, currentThread);

vmFuncs->internalExitVMToJNI(currentThread);

Expand Down
12 changes: 12 additions & 0 deletions runtime/jvmti/jvmtiExtensionMechanism.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ static const jvmtiParamInfo jvmtiVirtualThreadUnmount_params[] = {
{ "jni_env", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
{ "thread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE }
};

/* (jvmtiEnv *jvmti_env, jthread thread) */
static const jvmtiParamInfo jvmtiVirtualThreadDestroy_params[] = {
{ "jni_env", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
{ "thread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE }
};
#endif /* JAVA_SPEC_VERSION >= 19 */

#if JAVA_SPEC_VERSION >= 19
Expand Down Expand Up @@ -906,6 +912,12 @@ static const J9JVMTIExtensionEventInfo J9JVMTIExtensionEventInfoTable[] = {
J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_UNMOUNT,
SIZE_AND_TABLE(jvmtiVirtualThreadUnmount_params),
},
{
J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY,
COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY,
J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY,
SIZE_AND_TABLE(jvmtiVirtualThreadDestroy_params),
},
#endif /* JAVA_SPEC_VERSION >= 19 */
#if defined(J9VM_OPT_CRIU_SUPPORT)
{
Expand Down
1 change: 1 addition & 0 deletions runtime/jvmti/jvmtiHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ static const UDATA reasonCodeFromJVMTIEvent[] = {
J9_JNI_OFFLOAD_SWITCH_J9JVMTI_GC_CYCLE_FINISH, /* J9JVMTI_EVENT_COM_IBM_GARBAGE_COLLECTION_CYCLE_FINISH */
J9_JNI_OFFLOAD_SWITCH_J9JVMTI_VIRTUAL_THREAD_MOUNT, /* J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_MOUNT */
J9_JNI_OFFLOAD_SWITCH_J9JVMTI_VIRTUAL_THREAD_UNMOUNT, /* J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_UNMOUNT */
J9_JNI_OFFLOAD_SWITCH_J9JVMTI_VIRTUAL_THREAD_DESTROY, /* J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY */
J9_JNI_OFFLOAD_SWITCH_J9JVMTI_EVENT_OPENJ9_VM_CHECKPOINT, /* J9JVMTI_EVENT_OPENJ9_VM_CHECKPOINT */
J9_JNI_OFFLOAD_SWITCH_J9JVMTI_EVENT_OPENJ9_VM_RESTORE, /* J9JVMTI_EVENT_OPENJ9_VM_RESTORE */
};
Expand Down
4 changes: 2 additions & 2 deletions runtime/jvmti/jvmtiHook.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ unhookAllEvents(J9JVMTIEnv * j9env)

hookUnregister(vmHook, J9HOOK_VM_THREAD_DESTROY, jvmtiHookThreadDestroy, NULL, j9env);
#if JAVA_SPEC_VERSION >= 19
hookUnregister(vmHook, J9HOOK_VM_VIRTUAL_THREAD_END, jvmtiHookVirtualThreadDestroy, NULL, j9env);
hookUnregister(vmHook, J9HOOK_VM_VIRTUAL_THREAD_DESTROY, jvmtiHookVirtualThreadDestroy, NULL, j9env);
#endif /* JAVA_SPEC_VERSION >= 19 */
hookUnregister(vmHook, J9HOOK_VM_POP_FRAMES_INTERRUPT, jvmtiHookPopFramesInterrupt, NULL, j9env);

Expand All @@ -987,7 +987,7 @@ hookRequiredEvents(J9JVMTIEnv * j9env)
}

#if JAVA_SPEC_VERSION >= 19
if (hookRegister(vmHook, J9HOOK_VM_VIRTUAL_THREAD_END, jvmtiHookVirtualThreadDestroy, OMR_GET_CALLSITE(), j9env)) {
if (hookRegister(vmHook, J9HOOK_VM_VIRTUAL_THREAD_DESTROY, jvmtiHookVirtualThreadDestroy, OMR_GET_CALLSITE(), j9env)) {
return 1;
}
#endif /* JAVA_SPEC_VERSION >= 19 */
Expand Down
7 changes: 7 additions & 0 deletions runtime/nls/j9ti/jvmti.nls
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,13 @@ J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_UNMOUNT.system_action=None
J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_UNMOUNT.user_response=None
# END NON-TRANSLATABLE

J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY=Free native resources after a virtual thread ends.
# START NON-TRANSLATABLE
J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY.explanation=Internationalized description of a JVMTI extension
J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY.system_action=None
J9NLS_JVMTI_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY.user_response=None
# END NON-TRANSLATABLE

J9NLS_J9JVMTI_EVENT_OPENJ9_VM_CHECKPOINT=At VM pre-checkpoint phase.
# START NON-TRANSLATABLE
J9NLS_J9JVMTI_EVENT_OPENJ9_VM_CHECKPOINT.explanation=VM is preparing to take a checkpoint.
Expand Down
2 changes: 1 addition & 1 deletion runtime/oti/j9consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ extern "C" {
#define J9_JNI_OFFLOAD_SWITCH_JVMTI_MONITOR_CONTENDED_ENTERED 0x30
#define J9_JNI_OFFLOAD_SWITCH_J9JVMTI_VIRTUAL_THREAD_MOUNT 0x31
#define J9_JNI_OFFLOAD_SWITCH_J9JVMTI_VIRTUAL_THREAD_UNMOUNT 0x32
/* 0x33 Reserved for JVMTI event */
#define J9_JNI_OFFLOAD_SWITCH_J9JVMTI_VIRTUAL_THREAD_DESTROY 0x33
#define J9_JNI_OFFLOAD_SWITCH_JVMTI_RESOURCE_EXHAUSTED 0x34
#define J9_JNI_OFFLOAD_SWITCH_JVMTI_GC_START 0x35
#define J9_JNI_OFFLOAD_SWITCH_JVMTI_GC_FINISH 0x36
Expand Down
10 changes: 10 additions & 0 deletions runtime/oti/j9vm.hdf
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,16 @@ typedef UDATA (* lookupNativeAddressCallback)(struct J9VMThread *currentThread,
<data type="struct J9VMThread*" name="currentThread" description="current thread" />
</event>

<event>
<name>J9HOOK_VM_VIRTUAL_THREAD_DESTROY</name>
<description>
Triggered after J9HOOK_VM_VIRTUAL_THREAD_END to free native resources.
currentThread->threadObject is the virtual thread at the point of trigger.
</description>
<struct>J9VMVirtualThreadDestroyEvent</struct>
<data type="struct J9VMThread*" name="currentThread" description="current thread" />
</event>

<event>
<name>J9HOOK_VM_CRIU_CHECKPOINT</name>
<description>Triggered at the end of CRIU checkpoint, just before CRIU dump</description>
Expand Down
1 change: 1 addition & 0 deletions runtime/oti/jvmtiInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef enum {
J9JVMTI_EVENT_COM_IBM_GARBAGE_COLLECTION_CYCLE_FINISH,
J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_MOUNT,
J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_UNMOUNT,
J9JVMTI_EVENT_COM_SUN_HOTSPOT_EVENTS_VIRTUAL_THREAD_DESTROY,
J9JVMTI_EVENT_OPENJ9_VM_CHECKPOINT,
J9JVMTI_EVENT_OPENJ9_VM_RESTORE,
J9JVMTI_AFTER_LAST_EXTENSION_EVENT
Expand Down

0 comments on commit 4771211

Please sign in to comment.