Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for -XX:+EnableDynamicAgentLoading #17085

Closed
tajila opened this issue Mar 31, 2023 · 11 comments
Closed

Add support for -XX:+EnableDynamicAgentLoading #17085

tajila opened this issue Mar 31, 2023 · 11 comments

Comments

@tajila
Copy link
Contributor

tajila commented Mar 31, 2023

Currently, j9 supports dynamic loading agents by default.

The capabilities required for agents are:

J9_EXTENDED_RUNTIME_OSR_SAFE_POINT
J9_EXTENDED_RUNTIME_ENABLE_HCR

These are currently enabled all the time.

We need to put default enablement of these capabilities under the EnableDynamicAgentLoading flag. This will make it possible to turn off EnableDynamicAgentLoading in a future release if it is required in a future release. For now EnableDynamicAgentLoading can remain enabled by default. This will change likely in JDK21.

Additionally, we need to detect jvm agents attached are startup. We can look at createAgentLibrary( and lazily add the OSR/HCR flags when this occurs.

We should add a runtime flag, for dynamic agent loading, this can be used to check if an agent is allowed to be loading at runtime.

The end result is:

  1. When EnableDynamicAgentLoading is enabled, OSR/HCR flags are enable and dynamic agent loading is also enabled.
  2. When EnableDynamicAgentLoading is disabled, OSR/HCR is only enabled when agents are attached at startup. Dynamic loading of agents is disabled.
@tajila tajila added the comp:vm label Mar 31, 2023
@tajila tajila added this to the Java 21 milestone Mar 31, 2023
@JasonFengJ9
Copy link
Member

JEP 261: Module System proposed -XX:[+|-]EnableDynamicAgentLoading
The dynamic loading of JVM TI agents will be disabled by default in a future release. To prepare for that change we recommend that applications that allow dynamic agents start using the option -XX:+EnableDynamicAgentLoading to enable that loading explicitly. The option -XX:-EnableDynamicAgentLoading disables dynamic agent loading.

This change prevents the AttachAPI from loading an agent into a JVM after startup

/* openj9.internal.tools.attach.target.Attachment: private native int loadAgentLibraryImpl(ClassLoader loader, String agentLibrary, String options, boolean decorate); */
VMINLINE VM_BytecodeAction
inlAttachmentLoadAgentLibraryImpl(REGISTER_ARGS_LIST)

which invokes
* Load a JVMTI agent at run time and call the Agent_OnAttach_L/Agent_OnAttach function
* @param vm Java VM
* @param library library name. Must be non-null.
* @param options options, if any. May be null.
* @return JNI_ERR, JNI_OK
*/
I_32 JNICALL
loadAgentLibraryOnAttach(struct J9JavaVM * vm, const char * library, const char *options, UDATA decorate)

@dnakamura
Copy link
Contributor

Prototype for command line option is at #17215. @JasonFengJ9 are you saying we should also error out when attempting to load an agent? If so is it sufficient to check the J9_EXTENDED_RUNTIME_OSR_SAFE_POINT and J9_EXTENDED_RUNTIME_ENABLE_HCR? Or should we be adding a new runtime flag? (since its possible to disable either of those flags individually without -XX:-EnableDynamicAgentLoading)

@JasonFengJ9
Copy link
Member

Or should we be adding a new runtime flag? (since its possible to disable either of those flags individually without -XX:-EnableDynamicAgentLoading)

I think a new runtime flag is required.

@fengxue-IS
Copy link
Contributor

-XX:[+/-]EnableDynamicAgentLoading should not affect how HCR and OSR work and will need a new runtime flag or internal java property. Looking at the purposed JEP draft: Prepare to Restrict The Dynamic Loading of Agents this option is simply a way of disabling the Attach API.

As OpenJ9's agentload code only have a single entry point in JCL, could just add new check for the flag/property at

String loadAgentLibrary(String agentLibrary, String options,
boolean decorate) {
IPC.logMessage("loadAgentLibrary " + agentLibrary + ':' + options + " decorate=" + decorate); //$NON-NLS-1$ //$NON-NLS-2$
ClassLoader loader = java.lang.ClassLoader.getSystemClassLoader();
int status = loadAgentLibraryImpl(loader ,agentLibrary, options, decorate);
if (0 != status) {
if (-1 == status) {
return Response.EXCEPTION_AGENT_LOAD_EXCEPTION + ' '
+ agentLibrary + ':' + options;
} else {
return Response.EXCEPTION_AGENT_INITIALIZATION_EXCEPTION
+ status;
}
}
return null;

This would be a requirement if we want enable optimization to allow VirtualThread to skip notifyJvmti* calls when no JVMTI agent requires such capabilities.

@tajila
Copy link
Contributor Author

tajila commented May 8, 2023

From a functional perspective, all we need to do is disable dynamic loading of agents. However, given that in J9 an agent can always be dynamically loaded we need to support OSR/HCR all the time. The removal of this capability lets us disable HCR/OSR as default JVM capabilities and only apply them when they are needed which is important from a performance perspective. This issue was created as an attempt to do some early refactoring and combine default enablement of HCR/OSR under EnableDynamicAgentLoading

@tajila
Copy link
Contributor Author

tajila commented May 8, 2023

I've updated the description to be more clear

@keithc-ca
Copy link
Contributor

given that in J9 an agent can always be dynamically loaded

Please explain how this is not in fundamental conflict with the intent of -XX:-EnableDynamicAgentLoading?

@tajila
Copy link
Contributor Author

tajila commented Jun 14, 2023

Please explain how this is not in fundamental conflict with the intent of -XX:-EnableDynamicAgentLoading?

The fact that J9 always allows dynamic agent loading is not spec behaviour. In fact, it hasn't always been J9 behaviour. In the early releases of JDK8 this was not the case.

The new proposed options standardizes this behaviour.

@keithc-ca
Copy link
Contributor

Just to be crystal clear, so using -XX:-EnableDynamicAgentLoading would disallow dynamic agent loading (period)?

@tajila
Copy link
Contributor Author

tajila commented Jun 14, 2023

Just to be crystal clear, so using -XX:-EnableDynamicAgentLoading would disallow dynamic agent loading (period)?

yes, thats correct

dnakamura added a commit to dnakamura/openj9 that referenced this issue Jul 11, 2023
Flag controls runtime loading of dynamic agents

- When EnableDynamicAgentLoading is enabled, OSR/HCR flags are
enabled and dynamic agent loading is also enabled.
- When EnableDynamicAgentLoading is disabled, OSR/HCR  flags default
to off, and are enabled if/when agents are attached at startup.
Dynamic loading of agents is disabled.

Issue eclipse-openj9#17085

Signed-off-by: Devin Nakamura <[email protected]>
@tajila
Copy link
Contributor Author

tajila commented Jul 12, 2023

Closing as initial PR is merged

@tajila tajila closed this as completed Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants