On Fri, 27 Nov 2026 12:52:37 GMT, Markus Grönlund <[email protected]> wrote:
>> Greetings, >> >> Please review this change to improve JFR’s reporting of JVMTI agents and, >> for better separation of concerns, the responsibilities and lifetimes of >> JVMTI agents. >> >> ## Problem >> >> Tools commonly reuse `Agent_OnAttach` as a control channel—for example, to >> start and stop profiling. Previously, each successful startup invocation was >> counted as a new agent instance, producing duplicate and misleading JFR >> reporting. >> >> This change associates reporting with JVMTI environment lifetimes >> established during agent startup, while separating those lifetimes from >> library lookup and `Agent_OnUnload` notifications. >> >> For further details and a before-and-after profiling demonstration, see >> [JDK-8392584](https://bugs.openjdk.org/browse/JDK-8392584). >> >> ## Model and responsibilities >> >> This design keeps each agent’s state separate from shared library details, >> startup tracking, and entry-point calls. >> >> - `JvmtiAgent` stores the agent’s identity, options, a reference to the >> `JvmtiAgentLibrary`, and a link to the JVMTI environment. Startup, JVMTI >> environment association, and timing have separate state models. >> - `JvmtiAgentManager` creates new `JvmtiAgent` objects, manages agent >> startup and selects agents for retention or discarding. It handles JVMTI >> environment disposal and `VMInit` timing, agent iteration, and diagnostic >> info. >> - `JvmtiAgentLibrary` represents a loaded OS library. It includes the >> library’s handle, static-agent identity, file path, and resolution of JVMTI >> entry points. More than one agent can use the same library. >> - `JvmtiAgentLibraryManager` loads and tracks `JvmtiAgentLibrary` objects >> and manages native lookups and `Agent_OnUnload` eligibility. The identity >> and lifetime of a library are separate from any `JvmtiAgent`. >> - `JvmtiAgentInvoker` resolves and invokes exported JVMTI entry points like >> `Agent_OnLoad` and `Agent_OnAttach`, handling thread-state changes and >> results. It also handles legacy `XRUN` conversion and runtime agent >> attachment timing. >> >> ## JVMTI environment association and retention >> >> The first successfully created JVMTI environment on the startup thread is >> associated with the innermost executing startup invocation. This environment >> becomes the `JvmtiAgent`'s identity over its lifetime. >> >> The association has some constraints: >> >> - Disposing the associated environment does not make the `JvmtiAgent` >> eligible for reassociation. >> - Additional environments created by an inner startup invocation do not ... > > Markus Grönlund has updated the pull request with a new target base due to a > merge or a rebase. The pull request now contains seven commits: > > - Merge upstream master into 8392584 > - streamlining > - Decomposition > - Remove unrelated JFR tests > - start() instead of startup() and updated test > - line break > - 8392584 This PR aims to improve tracking of JVMTI agents, but honestly, I find it very counterintuitive. I suggest you start with defining what a JVMTI agent is (for the purpose of JFR reporting). Since `jdk.NativeAgent` event is a user-facing API, it would be useful to have a clear definition when the event is emitted and what it means - ideally, something a user can easily recognize. E.g., previously, each `JVMTI.agent_load` invocation produced a new event - this caused certain problems, but at least, it was obvious and logical, and had practical application as every profiler command was recorded. This PR obscures the logic. It now depends on subtle implementation details of each particular agent, whether it is reported in JFR or not. For example, the event is not reported if an agent does not create `jvmtiEnv`, or if it creates `jvmtiEnv` outside `Agent_OnAttach`, or if it creates `jvmtiEnv` in a separate thread. I see two meaningful ways to identify an agent: - by library path (with special handling for libinstrument); - by `jvmtiEnv`, regardless of when an where the environment was created. There are pros and cons for both, but either way is more consistent to me than the proposed PR. Trying to mix both approaches becomes problematic, since an agent may create one environment, multiple environments (even in the same invocation), or no environments at all. JVMTI environment can also be created from a regular JNI library. As an exercise, consider three versions of an agent: 1. One allocates `jvmtiEnv` on the first invocation and reuses the environment on subsequent invocations. 2. Another one disposes the previous environment and allocates a new one on each invocation. 3. The third agent spawns a new thread from `Agent_OnAttach` and allocates the environment from there. Otherwise, all agents do the same thing. For the user, there is no observable difference between three. But JFR will record one, many, or zero `jdk.NativeAgent` events in these scenarios. ------------- PR Review: https://git.openjdk.org/jdk/pull/33700#pullrequestreview-6101217624
