oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.4k stars 1.64k forks source link

[GR-55707] Java Agents Support in Native Image #8177

Open alina-yur opened 10 months ago

alina-yur commented 10 months ago

This ticket summarizes the current status and next steps for Java agent support in Native Image.

Current status

At build time, bytecode transformation can be done by passing a Java agent to Native Image with -J-javaagent:agent.jar. Things to note:

Goals

Non-goals

cemo commented 7 months ago

@alina-yur, Is there any coordinated effort from Oracle to resolve this issue? I see there is an open PR but I think it is not addressed by current maintainers of the project. At the end of that effort that PR might be not accepted as well. Is it not a good idea to review that PR and making a better way?

It sounds probably strange but after that PR resolved adoption of the Graal will increase dramatically. Many companies are using agents for APM tools and every single release we are reading release notes to see that this issue resolved or not.

adinn commented 7 months ago

It sounds probably strange but after that PR resolved adoption of the Graal will increase dramatically. Many companies are using agents for APM tools and every single release we are reading release notes to see that this issue resolved or not.

I think you need to reset your expectations of any 'solution'. The qualifications Olga mentioned are not just something to be 'resolved' by a clever redesign. They are intrinsic to what can be achieved here -- because they rule out things that many agents rely on being able to do (there are more issues -- for example, agents that depend on creating service threads under their premain method). In consequence there will be a lot of cases where agents that are used at runtime on the JVM cannot be used at build time to modify behaviour of a generated native image.

As an example, my agent, Byteman, as currently designed, cannot possibly be used to instrument a native image at build time. I am not going to say it is impossible to implement an equivalent for GraalVM Native but I currently don't know how to do it (and, yes, I designed and implemented all the core functionality of Byteman). My expectation is that my (many thousands of) users who rely on using Byteman for testing, tracing and debugging are never going to be able to use it for those purposes with a GraalVM Native app.

Luckily, my users can still benefit from using Byteman on the JVM during development and maintenance of a GraalVM Native app and the tight equivalence in behaviour of a GraalVM Native vs JVM deployment means they can still test, trace and debug most problems that way. However, I do have a significant number of users who rely on Byteman to troubleshoot problems in live deployments and they are out of luck if they choose to deploy using GraalVM Native.

That said, I fully accept that there are agents where build time vs runtime instrumentation makes no difference. So that means a resolution of this issue would be very welcome. Just remember that any such solution will not be universal.

chenzhiguo commented 7 months ago

I am very concerned about when this feature will be implemented?

vjovanov commented 1 month ago

From the GraalVM Workshop: we should prevent the agent from transforming the JDK classes in the host VM as they can affect how the image builder works.

To allow transformation of JDK classes we can take all classes that are not substituted and apply the transformation ourselves, similarly to Native Image annotations. For inspiration we can use the snippet from NetBeans as proposed by @JaroslavTulach.

JaroslavTulach commented 1 month ago

To allow transformation of JDK classes we can take all classes that are not substituted and apply the transformation ourselves, similarly to Native Image annotations. For inspiration we can use the snippet from NetBeans as proposed by @JaroslavTulach.

There may be another use-case for this automatic(?) Agent-Class invocation that would solve an open issue with WASM backend for GraalVM and its integration with HTML/Java API.

When the use of javaagent option was discussed I realised (to my surprise) there already is such an agent for the HTML/Java API. The net.java.html.boot.jar can be used as -javaagent as it has Agent-Class in its manifest.

It transforms the (usually native) methods annotated by @JavaScriptBody annotation into real methods with body that invoke Fn.Presenter's defineFn method. E.g. it is not something that would affect JDK classes, but invoking such Agent-Class declared agent automatically on bytecode by SVM before emitting the native code might help to solve the WASM backend for GraalVM and HTML/Java API issue.

Just FYI. CCing @patrick96