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

Don't get me wrong - as for improving agent tracking, I'm fully with you, 
willing to help. But what I'd like to avoid is replacing one inconsistent 
behavior with another inconsistent approach. I mentioned several scenarios 
where the proposed approach ends up in missing or misleading information.

> Environments created outside that explicit scope are deliberately not 
> automatically attributed

Note that environments created _within_ that explicit scope can also be 
misattributed in this PR, e.g. when an agent creates/disposes multiple 
environments during `Agent_OnAttach`.

> Take a look at the examples and the output in JDK-8392584

The proposed change does not fully fix this issue either. Instead, it yields 
control over how agents are reported to the user code. For instance, if I want 
to hide my agent from JFR reporting, I will create `jvmtiEnv` in a separate 
thread. Or, if want JFR to keep reporting every profiler command, I will create 
a temporary `jvmtiEnv` in `Agent_OnAttach` every time, just for reporting 
purpose. I believe it's against the spirit of JFR to rely on integrity of 
third-party code for reporting JVM-level events.

The more I think about it, the more I come to the conclusion that tracking 
`jvmtiEnv` is not the best approach for the existing `jdk.NativeAgent` event 
which has `name`, `path`, and `options` properties - because of the above 
problems and the inherent difficulty of associating `jvmtiEnv` with the native 
context. At the same time, it's not a user-friendly solution either, since 
specifying an `-agentpath:` argument or calling `jcmd JVMTI.agent_load` for the 
first time is exactly loading an agent from user's perspective, no matter if 
the loaded library obtains `jvmtiEnv` or not.

A simple way to fix the original problem of event flood would be to track the 
first attachment only, regardless of JVMTI environments. The main exception 
when one native library may create multiple logical agents is 
`libinstrument.so`, but it's already handled separately. What do you think?

-------------

PR Comment: https://git.openjdk.org/jdk/pull/33700#issuecomment-7316098468

Reply via email to