Skip to content

Commit 8e6bbfd

Browse files
committed
Introduce --enable-monitoring option.
1 parent 432065a commit 8e6bbfd

File tree

17 files changed

+308
-127
lines changed

17 files changed

+308
-127
lines changed

docs/reference-manual/native-image/BuildOptions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The following options are supported across both GraalVM Community and Enterprise
2828
* `--enable-https`: enable HTTPS support in a native executable
2929
* `--enable-preview`: allow classes to depend on preview features of this release
3030
* `--enable-url-protocols`: list comma-separated URL protocols to enable
31+
* `--enable-monitoring`: enable monitoring features that allow the VM to be inspected at run time. Comma-separated list can contain 'heapdump', 'jfr', 'jvmstat', or 'all'. For example: `--enable-monitoring=heapdump,jvmstat`.
3132
* `--features`: a comma-separated list of fully qualified [Feature implementation classes](https://www.graalvm.org/sdk/javadoc/index.html?org/graalvm/nativeimage/hosted/Feature.html)
3233
* `--force-fallback`: force building of a fallback native executable
3334
* `--gc=<value>`: select Native Image garbage collector implementation. Allowed options for `<value>` are: `G1` for G1 garbage collector (**GraalVM Enterprise only**); `epsilon` for Epsilon garbage collector; `serial` for Serial garbage collector (default).

docs/reference-manual/native-image/JFR.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ To record JFR events when running a native executable, JFR support and JFR recor
1717

1818
To build a native executable with the JFR events support, you first need to include JFR at build time, then enable the system, start a recording, and configure logging at native executable run time.
1919

20-
To build a native executable with JFR, use the `-H:+AllowVMInspection` flag:
20+
To build a native executable with JFR, use the `--enable-monitoring=jfr` flag:
2121
```shell
22-
native-image -H:+AllowVMInspection JavaApplication
22+
native-image --enable-monitoring=jfr JavaApplication
2323
```
2424
To enable the system, start a recording, and configure logging at run time, the following options are supported:
2525

docs/reference-manual/native-image/guides/build-and-run-native-executable-with-jfr.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ GraalVM Native Image supports JFR events and users can use [`jdk.jfr.Event`](htt
1212

1313
To record JFR events when running a native executable, JFR support and JFR recording must be enabled, and this guide covers how to do that.
1414

15-
> Note: JFR events recording is not supported on GraalVM distribution for Windows. JFR is only supported with native executables built on GraalVM JDK 11.
15+
> Note: JFR events recording is not supported on GraalVM JDK for Windows. JFR is only supported with native executables built on GraalVM JDK 11.
1616
1717
## Enable JFR Support and Record Events at Run Time
1818

19-
To build a native executable with the JFR events support, you first need to include JFR at build time, then enable the system, start a recording, and configure logging at native executable run time.
20-
The following options are supported:
21-
* `-H:+AllowVMInspection`: enable the VM inspection
22-
* `-XX:+FlightRecorder`: use to enable JFR
19+
To build a native executable with the JFR events support, you first need to add the `--enable-monitoring=jfr` option when invoking the `native-image` tool. Then enable the system, start a recording, and configure logging at native executable run time:
20+
* `-XX:+FlightRecorder`: use to enable JFR at run time
2321
* `-XX:StartFlightRecording`: use to start a recording on application's startup
2422
* `-XX:FlightRecorderLogging`: use to configure the log output for the JFR system
2523

@@ -61,9 +59,9 @@ Follow the steps below to practice building a native executable with JFR support
6159

6260
3. Build a native executable with the VM inspection enabled:
6361
```shell
64-
$JAVA_HOME/bin/native-image -H:+AllowVMInspection JFRDemo
62+
$JAVA_HOME/bin/native-image --enable-monitoring=jfr JFRDemo
6563
```
66-
The `-H:+AllowVMInspection` option enables features such as JFR that can be used to inspect the VM.
64+
The `--enable-monitoring=jfr` option enables features such as JFR that can be used to inspect the VM.
6765

6866
4. Run the executable and start recording:
6967
```shell

docs/reference-manual/native-image/guides/create-heap-dump-from-native-executable.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,42 @@ permalink: /reference-manual/native-image/guides/create-heap-dump/
99

1010
You can create a heap dump of a running executable to monitor its execution. Just like any other Java heap dump, it can be opened with the [VisualVM](../../../tools/visualvm.md) tool.
1111

12-
To enable heap dump support, native executables must be built with the `-H:+AllowVMInspection` option. Heap dumps can then be created in three different ways:
12+
To enable heap dump support, native executables must be built with the `--enable-monitoring=heapdump` option. Heap dumps can then be created in three different ways:
1313

14-
1. Dump the initial heap of a native executable using the `-XX:+DumpHeapAndExit` command-line option.
15-
2. Create heap dumps sending `USR1` (other supported signals are `QUIT/BREAK` for stack dumps and `USR2` to dump runtime compilation information).
16-
3. Create a heap dumps programmatically through the [`org.graalvm.nativeimage.VMRuntime#dumpHeap`](https://github.com/oracle/graal/blob/master/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/VMInspection.java) API.
14+
1. Create heap dumps with VisualVM.
15+
2. Dump the initial heap of a native executable using the `-XX:+DumpHeapAndExit` command-line option.
16+
3. Create heap dumps sending a `SIGUSR1` signal at run time.
17+
4. Create heap dumps programmatically using the [`org.graalvm.nativeimage.VMRuntime#dumpHeap`](https://github.com/oracle/graal/blob/master/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/VMInspection.java) API.
1718

1819
All three approaches are described below.
1920

2021
>Note: Creating heap dumps is not available on the Microsoft Windows platform.
2122
23+
## Create Heap Dumps with VisualVM
24+
25+
A convenient way to create heap dumps is to use [VisualVM](../../../tools/visualvm.md).
26+
For this, you need to add `jvmstat` to the `--enable-monitoring` option (for example, `--enable-monitoring=heapdump,jvmstat`).
27+
This will allow VisualVM to pick up and list running Native Image processes.
28+
You can then request heap dumps in the same way you can request them when your application runs on the JVM (for example, right-click on the process, then select "Heap Dump").
29+
2230
## Dump the Initial Heap of a Native Executable
2331

2432
Use the `-XX:+DumpHeapAndExit` command-line option to dump the initial heap of a native executable.
2533
This can be useful to identify which objects the Native Image build process allocated to the executable's heap.
2634
For a HelloWorld example, use the option as follows:
2735

2836
```shell
29-
$GRAALVM_HOME/bin/native-image HelloWorld -H:+AllowVMInspection
37+
$GRAALVM_HOME/bin/native-image HelloWorld --enable-monitoring=heapdump
3038
./helloworld -XX:+DumpHeapAndExit
3139
Heap dump created at '/path/to/helloworld.hprof'.
3240
```
3341

34-
## Handle USR1 Signals
42+
## Create Heap Dumps with SIGUSR1 (Linux/macOS only)
3543

3644
The following example is a simple multi-threaded Java application that runs for 60 seconds.
37-
This provides you with enough time to send it a `USR1` signal. The application will handle the signal and create a heap dump in the application's working directory. The heap dump will contain the `Collection` of `Person`s referenced by the static variable `CROWD`.
45+
This provides you with enough time to send it a `SIGUSR1` signal. The application will handle the signal and create a heap dump in the application's working directory. The heap dump will contain the `Collection` of `Person`s referenced by the static variable `CROWD`.
3846

39-
Follow these steps to build a native executable that will produce a heap dump when it receives a `USR1` signal.
47+
Follow these steps to build a native executable that will produce a heap dump when it receives a `SIGUSR1` signal.
4048

4149
1. Save the following code in a file named _SVMHeapDump.java_:
4250
```java
@@ -122,11 +130,11 @@ Follow these steps to build a native executable that will produce a heap dump wh
122130
```shell
123131
$JAVA_HOME/bin/javac SVMHeapDump.java
124132
```
125-
Build a native executable using the `-H:+AllowVMInspection` command-line option.
126-
(This causes the resulting native executable to produce a heap dump when it receives a `USR1` signal.)
133+
Build a native executable using the `--enable-monitoring=heapdump` command-line option.
134+
(This causes the resulting native executable to produce a heap dump when it receives a `SIGUSR1` signal.)
127135

128136
```shell
129-
$JAVA_HOME/bin/native-image SVMHeapDump -H:+AllowVMInspection
137+
$JAVA_HOME/bin/native-image SVMHeapDump --enable-monitoring=heapdump
130138
```
131139

132140
(The `native-image` builder creates a native executable from the `SVMHeapDump.class`.

docs/tools/visualvm.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ To capture a heap dump of, for example, a Ruby application for later analysis, s
3636
Then right-click its process in VisualVM and invoke the Heap Dump action.
3737
A new heap viewer for the Ruby process opens.
3838

39-
__Note:__ [Native Image](../reference-manual/native-image/README.md) does not implement the JVMTI agent, so triggering heap dump creation from the Applications area is impossible.
40-
Apply the `-H:+AllowVMInspection` flag with the `native-image` tool for native image processes.
41-
This way your application will handle signals and capture a heap dump when it receives the SIGUSR1 signal.
42-
The guest language REPL process must be started also with the `--jvm` flag to monitor it using VisualVM.
43-
This functionality is available with [GraalVM Enterprise Edition](https://www.oracle.com/downloads/graalvm-downloads.html).
44-
It is not available in GraalVM Community Edition.
39+
__Note:__ Heap dump support must be explicitly enabled when using [Native Image](../reference-manual/native-image/README.md).
40+
Add the `--enable-monitoring=heapdump,jvmstat` option when invoking the `native-image` tool to enable the heap dump feature and allow VisualVM to detect native executables via `jvmstat`.
41+
This way your application will handle signals and capture a heap dump when it receives the `SIGUSR1` signal.
4542
See the [Generating Native Heap Dumps](../reference-manual/native-image/guides/create-heap-dump-from-native-executable.md) page for details on capturing heap dumps from a native image process.
4643

4744
### Analyzing Objects

sdk/mx.sdk/mx_sdk_vm_impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,8 @@ def contents(self):
13411341
if isinstance(image_config, mx_sdk.LauncherConfig) or (isinstance(image_config, mx_sdk.LanguageLibraryConfig) and image_config.launchers):
13421342
build_args += [
13431343
'--install-exit-handlers',
1344-
'-H:+AllowVMInspection',
1344+
'--enable-monitoring=all',
1345+
'-H:+DumpRuntimeCompilationOnSignal',
13451346
]
13461347

13471348
if isinstance(image_config, (mx_sdk.LauncherConfig, mx_sdk.LanguageLibraryConfig)):

substratevm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This changelog summarizes major changes to GraalVM Native Image.
99
* (GR-40170) Add `--silent` option to silence the build output.
1010
* (GR-39475) Add initial support for jvmstat.
1111
* (GR-39563) Add support for JDK 19 and Project Loom Virtual Threads (JEP 425) for high-throughput lightweight concurrency. Enable on JDK 19 with `native-image --enable-preview`.
12+
* (GR-40264) Add `--enable-monitoring=<all,heapdump,jfr,jvmstat>` option to enable fine-grained control over monitoring features enabled in native executables. `-H:±AllowVMInspection` is now deprecated and will be removed in a future release.
1213

1314
## Version 22.2.0
1415
* (GR-20653) Re-enable the usage of all CPU features for JIT compilation on AMD64.

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/jvmstat/PosixPerfMemoryProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
import org.graalvm.nativeimage.Platforms;
6161
import org.graalvm.nativeimage.hosted.Feature;
6262

63-
import com.oracle.svm.core.VMInspection;
63+
import com.oracle.svm.core.VMInspectionOptions;
6464
import com.oracle.svm.core.annotate.Alias;
6565
import com.oracle.svm.core.annotate.AutomaticFeature;
6666
import com.oracle.svm.core.annotate.TargetClass;
@@ -276,7 +276,7 @@ public void teardown() {
276276
class PosixPerfMemoryFeature implements Feature {
277277
@Override
278278
public boolean isInConfiguration(IsInConfigurationAccess access) {
279-
return VMInspection.isEnabled();
279+
return VMInspectionOptions.hasJvmstatSupport();
280280
}
281281

282282
@Override
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core;
26+
27+
import java.io.IOException;
28+
import java.text.DateFormat;
29+
import java.text.SimpleDateFormat;
30+
import java.util.Date;
31+
import java.util.TimeZone;
32+
33+
import org.graalvm.nativeimage.ProcessProperties;
34+
import org.graalvm.nativeimage.VMRuntime;
35+
import org.graalvm.nativeimage.hosted.Feature;
36+
37+
import com.oracle.svm.core.annotate.AutomaticFeature;
38+
import com.oracle.svm.core.jdk.RuntimeSupport;
39+
import com.oracle.svm.core.log.Log;
40+
41+
import sun.misc.Signal;
42+
import sun.misc.SignalHandler;
43+
44+
@AutomaticFeature
45+
public class DumpHeapOnSignalFeature implements Feature {
46+
47+
@Override
48+
public boolean isInConfiguration(IsInConfigurationAccess access) {
49+
return VMInspectionOptions.hasHeapDumpSupport();
50+
}
51+
52+
@Override
53+
public void beforeAnalysis(BeforeAnalysisAccess access) {
54+
RuntimeSupport.getRuntimeSupport().addStartupHook(new DumpHeapStartupHook());
55+
}
56+
}
57+
58+
final class DumpHeapStartupHook implements RuntimeSupport.Hook {
59+
@Override
60+
public void execute(boolean isFirstIsolate) {
61+
if (isFirstIsolate) {
62+
DumpHeapReport.install();
63+
}
64+
}
65+
}
66+
67+
class DumpHeapReport implements SignalHandler {
68+
private static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone("UTC");
69+
70+
static void install() {
71+
Signal.handle(new Signal("USR1"), new DumpHeapReport());
72+
}
73+
74+
@Override
75+
public void handle(Signal arg0) {
76+
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
77+
dateFormat.setTimeZone(UTC_TIMEZONE);
78+
String heapDumpFileName = "svm-heapdump-" + ProcessProperties.getProcessID() + "-" + dateFormat.format(new Date()) + ".hprof";
79+
try {
80+
VMRuntime.dumpHeap(heapDumpFileName, true);
81+
} catch (IOException e) {
82+
Log.log().string("IOException during dumpHeap: ").string(e.getMessage()).newline();
83+
}
84+
}
85+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core;
26+
27+
import org.graalvm.nativeimage.Platform;
28+
import org.graalvm.nativeimage.Platform.WINDOWS;
29+
import org.graalvm.nativeimage.hosted.Feature;
30+
31+
import com.oracle.svm.core.annotate.AutomaticFeature;
32+
import com.oracle.svm.core.deopt.DeoptimizationSupport;
33+
import com.oracle.svm.core.heap.VMOperationInfos;
34+
import com.oracle.svm.core.jdk.RuntimeSupport;
35+
import com.oracle.svm.core.log.Log;
36+
import com.oracle.svm.core.thread.JavaVMOperation;
37+
38+
import sun.misc.Signal;
39+
import sun.misc.SignalHandler;
40+
41+
@AutomaticFeature
42+
public class DumpRuntimeCompilationOnSignalFeature implements Feature {
43+
44+
@Override
45+
public boolean isInConfiguration(IsInConfigurationAccess access) {
46+
return VMInspectionOptions.DumpRuntimeCompilationOnSignal.getValue() && !Platform.includedIn(WINDOWS.class) && DeoptimizationSupport.enabled();
47+
}
48+
49+
@Override
50+
public void beforeAnalysis(BeforeAnalysisAccess access) {
51+
RuntimeSupport.getRuntimeSupport().addStartupHook(new DumpRuntimeCompilationStartupHook());
52+
}
53+
}
54+
55+
final class DumpRuntimeCompilationStartupHook implements RuntimeSupport.Hook {
56+
@Override
57+
public void execute(boolean isFirstIsolate) {
58+
if (isFirstIsolate) {
59+
DumpRuntimeCompilation.install();
60+
}
61+
}
62+
}
63+
64+
class DumpRuntimeCompilation implements SignalHandler {
65+
static void install() {
66+
Signal.handle(new Signal("USR2"), new DumpRuntimeCompilation());
67+
}
68+
69+
@Override
70+
public void handle(Signal arg0) {
71+
DumpRuntimeCompiledMethodsOperation vmOp = new DumpRuntimeCompiledMethodsOperation();
72+
vmOp.enqueue();
73+
}
74+
75+
private static class DumpRuntimeCompiledMethodsOperation extends JavaVMOperation {
76+
DumpRuntimeCompiledMethodsOperation() {
77+
super(VMOperationInfos.get(DumpRuntimeCompiledMethodsOperation.class, "Dump runtime compiled methods", SystemEffect.SAFEPOINT));
78+
}
79+
80+
@Override
81+
protected void operate() {
82+
Log log = Log.log();
83+
SubstrateDiagnostics.dumpRuntimeCompilation(log);
84+
log.flush();
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)