On Fri, 18 Nov 2022 02:11:39 GMT, Serguei Spitsyn <sspit...@openjdk.org> wrote:
>> src/hotspot/share/prims/jvmtiExport.cpp line 390: >> >>> 388: ThreadInVMfromNative tiv(JavaThread::current()); >>> 389: java_lang_VirtualThread::init_static_notify_jvmti_events(); >>> 390: } >> >> Doesn't this logic mean that if the first pass through this code is made >> with an unattached thread, then that will prevent subsequent passes from >> calling `init_static_notify_jvmti_events`, even if the thread is attached. >> The reason is because >> `java_lang_VirtualThread::set_notify_jvmti_events(true);` will already have >> been done, so you won't pass the `if (!java_lang_VirtualThread >> ::notify_jvmti_events())` check. > > Enabling the `notify_jvmti_events` is an optimization to avoid having this > notification overhead with JVMTI virtual thread mount state transitions when > it is not needed. > We need to enable it only once and never disable it if enabled. > The first attempt to enable it is at startup if there was any agent loaded > with command line options. > In such a case, the get_jvmti_interface() is called in a context of > `AgentOnLoad()` in unattached thread. > It only sets the `java_lang_VirtualThread::set_notify_jvmti_events(true)` The > `init_static_notify_jvmti_events()` is called from the `javaClasses_init()`. > The agents that are loaded into running VM are initialized with the > `AgentOnAttach()`. > In this case, we can't rely on the `javaClasses_init()` and so, have to > explicitly call the `init_static_notify_jvmti_events()`. > I feels like this can be simplified. I keep thinking about the best way to do > it. > Probably, the pair <set_notify_jvmti_events, > init_static_notify_jvmti_events> can be replaced with just function. The > problem is that we can't use the `ThreadInVMfromNative` helper for unattached > thread. If `notify_jvmti_events()` is false, then you call `set_notify_jvmti_events(true)`, which means you will never enter the `if` block again. However, if the thread is not attached, you do not call `init_static_notify_jvmti_events()`. What happens if later there is an attached thread that triggers this code? Is seem when that happens you should call `init_static_notify_jvmti_events()`, but won't because `notify_jvmti_events()` is true. ------------- PR: https://git.openjdk.org/jdk/pull/11204