[Agent] Avoiding need for Unsafe on Java 11+
Gunnar Morling
gunnar at hibernate.org
Sat Oct 31 22:29:10 UTC 2020
Hey all,
I've been looking into ways for circumventing the need for using Unsafe for
creating the event types emitted by the agent, which requires the
--add-opens switch on Java 11+.
I first was hoping that Lookup#defineClass() could be used (see WIP PR
[1]), but this means event types would have to live under the same package
as the agent itself, as Lookup doesn't allow to create classes in another
package without support by code in that target package (which would have to
expose a lookup with "private access"). As Marcus pointed out to me, that'd
mean event types couldn't be GC-ed after unloading instrumented classes in
a dynamic environment.
Next option I considered is Java 15 hidden classes, but I'm having doubts
how feasible this actually is, as I can't find a good way for making the
generated hidden classes known to the instrumented methods (they cannot be
accessed symbolically). Plus, this would only work on Java 15+.
So what I'm wondering now: has it been considered to use dynamic JFR event
types [2] for the JMC agent? This should avoid the need for Unsafe in the
agent. IIUC, JFR generates event classes for these types internally. Would
that be an option to avoid the --add-opens switch?
Thanks,
--Gunnar
[1] https://github.com/openjdk/jmc/pull/148
[2]
https://docs.oracle.com/en/java/javase/11/docs/api/jdk.jfr/jdk/jfr/EventFactory.html
More information about the jmc-dev
mailing list