-
Notifications
You must be signed in to change notification settings - Fork 14
JVM Flags access API #149
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
Merged
Merged
JVM Flags access API #149
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a59fc26
Introduce an API to access JVM flags directly
jbachorik 5559c50
Try increasing retry limit for ObjectSampleDumpSmokeTest
jbachorik c71a738
Add more javadocs
jbachorik 82fa8a1
Fix assumption
jbachorik 619c5b7
Fix condition
jbachorik 340884a
Zing does not support vmstructs, too
jbachorik 6dd9ae7
Deal with the JVM flag implementation differences
jbachorik 1ceb055
Decouple library initialization from profiler initialization
jbachorik bb4e9ea
success -> succeeded
jbachorik a5e686e
Update ddprof-test/src/test/java/com/datadoghq/profiler/JVMAccessTest…
jbachorik 4c214f3
Update ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerT…
jbachorik 078b5f7
Use shared Libraries instance
jbachorik ae50b2a
Simplify JVMFlags API
jbachorik 24a142f
Fix flag C++ calls
jbachorik 4102eb3
More fixes
jbachorik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Introduce an API to access JVM flags directly
- Loading branch information
commit a59fc2689eb6b26560201fbd1261fa025342a3f6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
ddprof-lib/src/main/java/com/datadoghq/profiler/JVMAccess.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package com.datadoghq.profiler; | ||
|
|
||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| * An internal JVM access support. | ||
| * <p> | ||
| * We are using vmstructs and dynamic symbol lookups to provide access to some JVM internals. | ||
| * There will be dragons here. We are touching and possibly mutating JVM internals. Do not use | ||
| * unless you know what you are doing. | ||
| * </p> | ||
| */ | ||
| public final class JVMAccess { | ||
| private static final class SingletonHolder { | ||
| static final JVMAccess INSTANCE = new JVMAccess(); | ||
| } | ||
|
|
||
| /** | ||
| * Flags interface to access JVM flags. | ||
| * In general, the flags are read-only. However, some flags can be modified at runtime. | ||
| * Currently, only string and boolean flags can be modified. Allowing modification of numeric | ||
| * flags would require exact specification of the flag type (int, long, float, double) such | ||
| * that the correct number of bytes would be written to the flag and not overwrite the surrounding | ||
| * memory. | ||
| */ | ||
| public interface Flags { | ||
| Flags NONE = new Flags() { | ||
| @Override | ||
| public String getStringFlag(String name) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void setStringFlag(String name, String value) { | ||
| } | ||
|
|
||
| @Override | ||
| public boolean getBooleanFlag(String name) { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void setBooleanFlag(String name, boolean value) { | ||
| } | ||
|
|
||
| @Override | ||
| public long getIntFlag(String name) { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public double getFloatFlag(String name) { | ||
| return 0; | ||
| } | ||
| }; | ||
|
|
||
| String getStringFlag(String name); | ||
| void setStringFlag(String name, String value); | ||
| boolean getBooleanFlag(String name); | ||
| void setBooleanFlag(String name, boolean value); | ||
| long getIntFlag(String name); | ||
| double getFloatFlag(String name); | ||
| } | ||
|
|
||
| private class FlagsImpl implements Flags { | ||
| public String getStringFlag(String name) { | ||
| return findStringJVMFlag0(name); | ||
| } | ||
|
|
||
| public void setStringFlag(String name, String value) { | ||
| setStringJVMFlag0(name, value); | ||
| } | ||
|
|
||
| public boolean getBooleanFlag(String name) { | ||
|
jbachorik marked this conversation as resolved.
|
||
| return findBooleanJVMFlag0(name); | ||
| } | ||
|
|
||
| public void setBooleanFlag(String name, boolean value) { | ||
| setBooleanJVMFlag0(name, value); | ||
| } | ||
|
|
||
| public long getIntFlag(String name) { | ||
| return findIntJVMFlag0(name); | ||
| } | ||
|
|
||
| public double getFloatFlag(String name) { | ||
| return findFloatJVMFlag0(name); | ||
| } | ||
| } | ||
|
|
||
| public static JVMAccess getInstance() { | ||
| return SingletonHolder.INSTANCE; | ||
| } | ||
|
|
||
| private final LibraryLoader.Result libraryLoadResult; | ||
| private final Flags flags; | ||
|
|
||
| private JVMAccess() { | ||
| LibraryLoader.Result result = LibraryLoader.builder().load();; | ||
| if (result.success) { | ||
| // library loaded successfully, check if we can access JVM | ||
| try { | ||
| healthCheck0(); | ||
| } catch (Throwable t) { | ||
| // failed to access JVM; update the result | ||
| result = new LibraryLoader.Result(false, t); | ||
| } | ||
|
|
||
| } | ||
| if (!result.success && result.error != null) { | ||
| System.out.println("[WARNING] Failed to obtain JVM access.\n" + result.error); | ||
|
MattAlp marked this conversation as resolved.
|
||
| } | ||
| flags = result.success ? new FlagsImpl() : Flags.NONE; | ||
| libraryLoadResult = result; | ||
| } | ||
|
|
||
| public JVMAccess(String libLocation, String scratchDir, Consumer<Throwable> errorHandler) { | ||
| LibraryLoader.Result result = LibraryLoader.builder().withLibraryLocation(libLocation).withScratchDir(scratchDir).load(); | ||
| if (result.success) { | ||
| // library loaded successfully, check if we can access JVM | ||
| try { | ||
| healthCheck0(); | ||
| } catch (Throwable t) { | ||
| // failed to access JVM; update the result | ||
| result = new LibraryLoader.Result(false, t); | ||
| } | ||
|
|
||
| } | ||
| if (!result.success && result.error != null) { | ||
| if (errorHandler != null) { | ||
| errorHandler.accept(result.error); | ||
| } else { | ||
| System.out.println("[WARNING] Failed to obtain JVM access.\n" + result.error); | ||
|
MattAlp marked this conversation as resolved.
|
||
| } | ||
| } | ||
| flags = result.success ? new FlagsImpl() : Flags.NONE; | ||
| libraryLoadResult = result; | ||
| } | ||
|
|
||
| public Flags flags() { | ||
| return flags; | ||
| } | ||
|
|
||
| public boolean isActive() { | ||
| return libraryLoadResult.success; | ||
| } | ||
|
|
||
| // a dummy method to check if the library has loaded properly | ||
| private native boolean healthCheck0(); | ||
|
|
||
| private native String findStringJVMFlag0(String name); | ||
| private native void setStringJVMFlag0(String name, String value); | ||
| private native boolean findBooleanJVMFlag0(String name); | ||
| private native void setBooleanJVMFlag0(String name, boolean value); | ||
| private native long findIntJVMFlag0(String name); | ||
| private native double findFloatJVMFlag0(String name); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would specify as-of details here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand - what should I specify here?