Skip to content

Commit 831599a

Browse files
author
Aleksandar Gradinac
committed
[GR-37572] Introduce agent experimental options documentation.
PullRequest: graal/11434
2 parents dad33e0 + 35a7cb8 commit 831599a

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ It is advisable to manually review the generated configuration files. Because th
3939

4040
The generated configuration files can be supplied to the `native-image` tool by placing them in a `META-INF/native-image/` directory on the class path, for example, in a JAR file used in the image build. This directory (or any of its subdirectories) is searched for files with the names `jni-config.json`, `reflect-config.json`, `proxy-config.json` and `resource-config.json`, which are then automatically included in the build. Not all of those files must be present. When multiple files with the same name are found, all of them are included.
4141

42-
## Building Native Image with Java Reflection Example
42+
## Build a Native Executable with Java Reflection Example
4343

4444
For demonstration purposes, save the following code as _ReflectionExample.java_ file:
4545

@@ -155,6 +155,10 @@ Filter files have the following structure:
155155
{"excludeClasses": "com.oracle.svm.**"},
156156
{"includeClasses": "com.oracle.svm.tutorial.*"},
157157
{"excludeClasses": "com.oracle.svm.tutorial.HostedHelper"}
158+
],
159+
"regexRules": [
160+
{"includeClasses": ".*"},
161+
{"excludeClasses": ".*\\$\\$Generated[0-9]+"}
158162
]
159163
}
160164
```
@@ -171,6 +175,12 @@ In the next rule however, lookups from those classes that are directly in packag
171175
Finally, lookups from the `HostedHelper` class is excluded again. Each of these rules partially overrides the previous ones.
172176
For example, if the rules were in the reverse order, the exclusion of `com.oracle.svm.**` would be the last rule and would override all other rules.
173177

178+
The `regexRules` section also contains a sequence of rules.
179+
Its structure is the same as that of the `rules` section, but rules are specified as regular expression patterns which are matched against the entire fully qualified class identifier.
180+
The `regexRules` section is optional.
181+
If a `regexRules` section is specified, a class will be considered included if (and only if) both `rules` and `regexRules` include the class and neither of them exclude it.
182+
With no `regexRules` section, only the `rules` section determines whether a class is included or excluded.
183+
174184
For testing purposes, the built-in filter for Java class library lookups can be disabled by adding the `no-builtin-caller-filter` option, but the resulting configuration files are generally unsuitable for a native image build.
175185
Similarly, the built-in filter for Java VM-internal accesses based on heuristics can be disabled with `no-builtin-heuristic-filter` and will also generally lead to less usable configuration files.
176186
For example: `-agentlib:native-image-agent=no-builtin-caller-filter,no-builtin-heuristic-filter,config-output-dir=...`
@@ -185,7 +195,7 @@ Using the `access-filter-file` option, a custom filter file that follows the fil
185195
The option can be specified more than once to add multiple filter files and can be combined with the other filter options.
186196
For example: `-agentlib:access-filter-file=/path/to/access-filter-file,caller-filter-file=/path/to/caller-filter-file,config-output-dir=...`
187197

188-
### Specifying Configuration Files as Native Image Arguments
198+
### Specify Configuration Files as Native Image Arguments
189199

190200
A directory containing configuration files that is not part of the class path can be specified to `native-image` via `-H:ConfigurationFileDirectories=/path/to/config-dir/`.
191201
This directory must directly contain all four files: `jni-config.json`, `reflect-config.json`, `proxy-config.json` and `resource-config.json`.
@@ -243,3 +253,8 @@ native-image-configure generate --input-dir=/path/to/config-dir-0/ --input-dir=/
243253

244254
This command reads one set of configuration files from `/path/to/config-dir-0/` and another from `/path/to/config-dir-1/` and then writes a set of configuration files that contains both of their information to `/path/to/merged-config-dir/`.
245255
An arbitrary number of `--input-dir` arguments with sets of configuration files can be specified. See `native-image-configure help` for all options.
256+
257+
### Experimental Options
258+
259+
The native-image-agent has options which are currently experimental and might be enabled in future releases, but can also be changed or removed entirely.
260+
See the [ExperimentalAgentOptions.md](ExperimentalAgentOptions.md) guide.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
layout: docs
3+
toc_group: native-image
4+
link_title: Experimental Agent Options
5+
permalink: /reference-manual/native-image/ExperimentalAgentOptions/
6+
---
7+
8+
# Experimental Agent Options
9+
10+
The `native-image-agent` tool has options which are currently experimental and might be enabled in future releases, but can also be changed or removed entirely.
11+
These options are described here.
12+
13+
## Support For Predefined Classes
14+
15+
Native-image needs all classes to be known at image build time (a "closed-world assumption").
16+
However, Java has support for loading new classes at runtime.
17+
To emulate class loading, the agent can be told to trace dynamically loaded classes and save their bytecode for later use by the image builder.
18+
This functionality can be enabled by adding `experimental-class-define-support` to the agent option string, e.g.: `-agentlib:native-image-agent=config-output-dir=config,experimental-class-define-support`
19+
Apart from the standard configuration files, the agent will create an `agent-extracted-predefined-classes` directory in the configuration output directory and write bytecode of newly loaded classes on the go.
20+
The configuration directory can then be used by image builder without additional tweaks,.
21+
The classes will be loaded during the image build, but will not be initialized or made available to the application.
22+
At runtime, if there is an attempt to load a class with the same name and bytecodes as one of the classes encountered during tracing, the predefined class will be supplied to the application.
23+
24+
### Known Limitations
25+
26+
- Native images support "loading" a predefined class only once per execution, by just a single class loader.
27+
- Predefined classes are initialized when they are "loaded" at runtime and cannot be initialized at build time.
28+
- The agent collects all classes which are not loaded by one of the Java VM's built-in class loaders (with some exceptions), that is, from the class path or module path. This includes classes loaded by any custom class loaders.
29+
- Classes that are generated with varying data in their name or bytecodes, such as sequential or random numbers or timestamps, can generally not be matched to predefined classes at runtime. In these cases, the way such classes are generated needs to be adjusted.
30+
31+
## Printing Configuration With Origins
32+
33+
For debugging, it may be useful to know the origin of certain configuration entries.
34+
By supplying `experimental-configuration-with-origins` to the agent option string, the agent will output configuration files with configuration entries broken down to the calling context (stack trace) they originate from in tree form.
35+
This option should be used in conjunction with `config-output-dir=<path>` to tell the agent where to output the configuration files.
36+
An example agent option string: `-agentlib:native-image-agent=config-output-dir=config-with-origins/,experimental-configuration-with-origins`
37+
38+
## Omitting Configuration From The Agent's Output
39+
40+
The agent can omit traced configuration entries present in existing configuration files.
41+
There are two ways to specify these existing configuration files:
42+
- By using configuration files from the class path or module path. When `experimental-omit-config-from-classpath` is added to the agent option string, the class path and module path of the running application are scanned for `META-INF/native-image/**/*.json` configuration files.
43+
- By explicitly pointing the agent to an existing configuration file directory using `config-to-omit=<path>`.
44+
45+
## Generating Conditional Configuration Using The Agent
46+
47+
The agent can, using a heuristic, generate configuration with reachability conditions on user specified classes.
48+
The agent will track configuration origins and try to deduce the conditions automatically.
49+
User classes are specified via an agent filter file (for more information on the format, see [Agent.md](Agent.md)).
50+
Additionally, the resulting configuration can further be filtered using another filter file.
51+
52+
Currently, this feature supports two modes:
53+
1. Generating conditional configuration in a single run with the agent.
54+
2. Generating conditional configuration from multiple runs with the agent and finally merging the collected data.
55+
56+
### Generating Conditional Configuration During An Agent Run
57+
58+
To enable this mode, add `experimental-conditional-config-filter-file=<path>` to the agent's command line, where `<path>` points to an agent filter file.
59+
Classes that are considered included by this filter will be designated as user code classes.
60+
To further filter the generated configuration, you can use `conditional-config-class-filter-file=<path>`, where `<path>` is a path to an agent filter file.
61+
62+
### Generating Conditional Configuration From Multiple Agent Runs
63+
64+
Conditional configuration can be generated from multiple agent runs that reach different code paths in the application.
65+
Each agent run produces configuration with metadata. `native-image-configure` is then used to merge the collected data and produce a conditional configuration.
66+
To run the agent in this mode, add `experimental-conditional-config-part` to the agent's command line.
67+
Once all the agent runs have finished, you can generate a conditional configuration by invoking:
68+
```shell
69+
native-image-configure generate-conditional --user-code-filter=<path-to-filter-file> --class-name-filter=<path-to-filter-file> --input-dir=<path-to-agent-run-output-1> --input-dir=<path-to-agent-run-ouput-2> ... --output-dir=<path-to-resulting-conditional-config>
70+
```
71+
where:
72+
- `--user-code-filter=<path-to-filter-file>`: path to an agent filter file that specifies user classes
73+
- (optional) `--class-name-filter=<path-to-filter-file>`: path to an agent filter file that further filters the generated config
74+
75+
### The Underlying Heuristics
76+
77+
Conditions are generated using the call tree of the application. The heuristics work as follows:
78+
1. For each unique method, create a list of all nodes in the call tree that correspond to the method
79+
2. For each unique method, if the method has more than one call node in the tree:
80+
- Find common configuration across all call nodes of that method
81+
- For each call node of the method, propagate configuration that isn't common across these calls to the caller node
82+
3. Repeat 2. until an iteration produced no changes in the call tree.
83+
4. For each node that contains configuration, generate a conditional configuration entry with the method's class as the condition.
84+
85+
The primary goal of this heuristic is to attempt to find where a method creates different configuration entries depending on the caller (for example, a method that wraps `Class.forName` calls.)
86+
This implies that the heuristic will not work well for code that generates configuration through a different dependency (for example, same method returns calls `Class.forName` with different class parameters depending on a system property).

substratevm/src/com.oracle.svm.configure/resources/Help.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This tool can be used to prepare a configuration of JNI, reflection and
55
resources for a native-image build.
66

77
Usage: native-image-configure generate [options]
8+
native-image-configure generate-conditional [options]
89
native-image-configure generate-filters [options]
910
native-image-configure command-file <command-file-path>
1011
native-image-configure help
@@ -83,6 +84,25 @@ generate generates configuration file(s) from all inputs.
8384
This option disables builtin heuristics that identify
8485
further internal JNI, reflection and resource usages.
8586

87+
generate-conditional generates conditional configuration from data
88+
collected by previous agent runs.
89+
--input-dir=<path>
90+
reads configuration and metadata from a directory that
91+
was previously populated by a run with the agent in
92+
the partial configuration mode.
93+
--output-dir=<path>
94+
writes a set of conditional configuration files to
95+
the given path.
96+
--user-code-filter=<path>
97+
specifies a filter file used to classify classes as
98+
user application classes. Generated conditions will
99+
only reference these classes.
100+
--class-name-filter=<path>
101+
specifies a filter file used to exclude classes from
102+
the computed configuration. Both the configuration
103+
and the conditions in the configuration will be
104+
tested against this filter.
105+
86106
generate-filters builds a class filter according to the parameters.
87107
Filter rules are created according to the order of
88108
these parameters, and filter rules are applied in

0 commit comments

Comments
 (0)