gluonhq / substrate

Create native Java(FX) apps for desktop, mobile and embedded
GNU General Public License v2.0
387 stars 52 forks source link

Support of visualvm monitoring on native image #1268

Open cnico opened 1 month ago

cnico commented 1 month ago

I tried to use the monitoring of native image build for my javafx application with gluonfx maven plugin. I am on windows and my build and native app following gluon docs works correctly :)

I followed : https://www.graalvm.org/latest/tools/visualvm/ to add --enable-monitoring=all to the native image build.

I did this configuration in the pom.xml :

         <properties>
        <gluonfx.plugin.version>1.0.23</gluonfx.plugin.version>
        <gluonfx.target>host</gluonfx.target>
        <gluonfx.nativeImageArgs>--enable-monitoring=all</gluonfx.nativeImageArgs>
                  ...
    </properties>
...

    <plugin>
                <groupId>com.gluonhq</groupId>
                <artifactId>gluonfx-maven-plugin</artifactId>
                <version>${gluonfx.plugin.version}</version>
                <configuration>
                    <target>${gluonfx.target}</target>
                    <mainClass>${main.class}</mainClass>
                    <verbose>true</verbose>
                    <runtimeArgs>${runtime.arguments}</runtimeArgs>
                    ...

The build failed with the logs : [mer. août 07 10:03:10 CEST 2024][INFOS] [SUB] Error: Unrecognized option: --enable-monitoring=all [mer. août 07 10:03:10 CEST 2024][PRÉCIS] Result for compile: 1 [mer. août 07 10:03:10 CEST 2024][GRAVE] Process compile failed with result: 1

Expected Behavior

I expect the option of monitoring is supported and I can then connect with visualvm to my native application in order to profile it.

Current Behavior

Build fail.

Context

I try to analyze ressource usage (cpu, memory) of the native image and optimize it because cpu usage is excessive by now.

jperedadnr commented 1 month ago

The option --enable-monitoring was added to this commit https://github.com/oracle/graal/commit/8e6bbfd54e5df0aec2fcc8fe8cd68bbb5dcc4d4b

If you are using Gluon build of GraalVM (22.1.0.1-Final), that commit didn't make it to the build.

If you check the changes in that commit:

- To build a native executable with JFR, use the `-H:+AllowVMInspection` flag:
+ To build a native executable with JFR, use the `--enable-monitoring=jfr` flag:

the "old" way to enable VM inspection works with GluonFX and GraalVM 22.1.0.1-Final:

<nativeImageArgs>-H:+AllowVMInspection</nativeImageArgs>

Note also that if you want to test on desktop, you can try a more recent GraalVM official build with the GluonFX plugin, and the new flag.

cnico commented 1 month ago

Hi, Thanks you for your feedback. I tried various options and did have no success with neither of them.

Here are my attemps : 1/ With the "old" way, I added the option in the pom.xml <nativeImageArgs>-H:+AllowVMInspection</nativeImageArgs>

It fails with the following error :

Exception in thread "main" java.lang.IllegalArgumentException: Installing signal handlers is not enabled at jdk.internal.misc.Signal.handle0(Signal.java:63) at jdk.internal.misc.Signal.handle(Signal.java:170) at sun.misc.Signal.handle(Signal.java:157) at com.oracle.svm.core.DumpAllStacks.install(VMInspection.java:101) at com.oracle.svm.core.VMInspectionStartupHook.execute(VMInspection.java:88) at com.oracle.svm.core.jdk.RuntimeSupport.executeHooks(RuntimeSupport.java:151) at com.oracle.svm.core.jdk.RuntimeSupport.initialize(RuntimeSupport.java:96) at org.graalvm.nativeimage.VMRuntime.initialize(VMRuntime.java:65)

As my app is only a Desktop one, with a recent version of graalVM set in the PATH (version 22.0.1 of april), I tried various syntax for the options :

2/ With this maven conf : <nativeImageArgs>--enable-monitoring=heapdump,jvmstat</nativeImageArgs> Maven build fails because it converts the , as a separator for args

3/ With :

<nativeImageArgs>
  <nativeImageArg>--enable-monitoring=heapdump,jvmstat</nativeImageArg>
</nativeImageArgs>

Build is OK but native exe crashes at startup with a :

Exception in thread "main" java.lang.IllegalArgumentException: Signal handlers can't be installed if signal handling is disabled, see option 'EnableSignalHandling'. at java.base@22.0.1/jdk.internal.misc.Signal.handle0(Signal.java:63) at java.base@22.0.1/jdk.internal.misc.Signal.handle(Signal.java:170) at org.graalvm.nativeimage.builder/com.oracle.svm.core.DumpAllStacks.install(DumpThreadStacksOnSignalFeature.java:71) at org.graalvm.nativeimage.builder/com.oracle.svm.core.DumpThreadStacksOnSignalStartupHook.execute(DumpThreadStacksOnSignalFeature.java:64) at org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.RuntimeSupport.executeHooks(RuntimeSupport.java:168) at org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.RuntimeSupport.initialize(RuntimeSupport.java:99) at org.graalvm.nativeimage/org.graalvm.nativeimage.VMRuntime.initialize(VMRuntime.java:65)

4/ With an extensive search, I found other options and after many tries :

<nativeImageArgs>
  <nativeImageArg>--install-exit-handlers</nativeImageArg>
  <nativeImageArg>--enable-monitoring=all</nativeImageArg>
  <nativeImageArg>-R:+EnableSignalHandling</nativeImageArg>
</nativeImageArgs>

Build is OK and native exe starts correctly. But in visualVM, my native image is not detected :(

If you have any idea on how to make it work, I really would appreciate it.

cnico commented 2 weeks ago

For information, I isolated and reproduced the problem with the latest version of graalvm and reported a bug here : https://github.com/oracle/graal/issues/9563 Waiting for it.