-
Notifications
You must be signed in to change notification settings - Fork 578
Add EventPipe interop instrumentation for wrapper lifecycle and GC-bridge reachability #12258
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
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/add-eventpipe-instrumentation-dotnet-java-interop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
447c870
Add interop EventSource and instrumentation hooks
Copilot e9eee27
Add interop EventPipe tests and fixups
Copilot 51b45bd
Address EventPipe review feedback
Copilot b76fa19
Fix EventSource keyword metadata
Copilot 7c20bea
Defer bridge reachability events
Copilot 2a5b83e
Merge origin/main into copilot/add-eventpipe-instrumentation-dotnet-j…
Copilot 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
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
external/Java.Interop/Documentation/EventPipeInteropEvents.md
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,42 @@ | ||
| # Java.Interop EventPipe interop events | ||
|
|
||
| The .NET ↔ Java interop layer emits EventPipe events through a single provider: | ||
|
|
||
| - **Provider name:** `Java.Interop` | ||
|
|
||
| ## Event catalog | ||
|
|
||
| | Event ID | Event name | Meaning | | ||
| |---|---|---| | ||
| | 1 | `DotNetWrapperCreated` | A managed wrapper for a Java object was created. | | ||
| | 2 | `JavaWrapperCreated` | A Java wrapper for a managed object was created. | | ||
| | 3 | `DotNetWrapperReleasedJavaReference` | A managed wrapper released its Java reference. | | ||
| | 4 | `JavaWrapperReleasedDotNetReference` | A Java wrapper released its managed reference. | | ||
| | 5 | `DotNetObjectOnlyReachableFromJava` | A managed object is only reachable from Java during bridge processing. | | ||
| | 6 | `JavaObjectOnlyReachableFromDotNet` | A Java object is only reachable from .NET during bridge processing. | | ||
|
|
||
| ## Payload schema | ||
|
|
||
| All events contain: | ||
|
|
||
| - `managedType` (`string`) | ||
| - `javaType` (`string`) | ||
| - `jniIdentityHashCode` (`int`) | ||
| - `managedObjectHashCode` (`int`) | ||
| - `runtimeMode` (`string`) | ||
|
|
||
| Reachability events (`5`, `6`) additionally contain: | ||
|
|
||
| - `componentIndex` (`int`) | ||
| - `contextIndex` (`int`) | ||
| - `contextPointer` (`long`) | ||
|
|
||
| ## Collecting events | ||
|
|
||
| Use `dotnet-trace` to capture the provider: | ||
|
|
||
| ```bash | ||
| dotnet-trace collect --process-id <pid> --providers Java.Interop:0x3:4 | ||
| ``` | ||
|
|
||
| `0x3` enables both wrapper lifecycle and reachability keywords, and `4` enables informational-level events. | ||
172 changes: 172 additions & 0 deletions
172
external/Java.Interop/src/Java.Interop/Java.Interop/InteropEventSource.cs
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,172 @@ | ||
| #nullable enable | ||
|
|
||
| using System.Diagnostics.Tracing; | ||
|
|
||
| namespace Java.Interop | ||
| { | ||
| public static class InteropEventSource | ||
|
jkoritzinsky marked this conversation as resolved.
Outdated
|
||
| { | ||
| internal const string ProviderName = "Java.Interop"; | ||
| const string UnknownValue = "Unknown"; | ||
|
|
||
| static readonly InteropEventSourceImplementation source = new InteropEventSourceImplementation (); | ||
|
|
||
| public static bool IsEnabled () | ||
| { | ||
| return source.IsEnabled (); | ||
| } | ||
|
jkoritzinsky marked this conversation as resolved.
Outdated
|
||
|
|
||
| public static void DotNetWrapperCreated ( | ||
| string? managedType, | ||
| string? javaType, | ||
| int jniIdentityHashCode, | ||
| int managedObjectHashCode, | ||
| string? runtimeMode) | ||
| { | ||
|
jkoritzinsky marked this conversation as resolved.
Outdated
|
||
| source.DotNetWrapperCreated ( | ||
| GetPayloadValue (managedType), | ||
| GetPayloadValue (javaType), | ||
| jniIdentityHashCode, | ||
| managedObjectHashCode, | ||
| GetPayloadValue (runtimeMode)); | ||
| } | ||
|
|
||
| public static void JavaWrapperCreated ( | ||
| string? managedType, | ||
| string? javaType, | ||
| int jniIdentityHashCode, | ||
| int managedObjectHashCode, | ||
| string? runtimeMode) | ||
| { | ||
| source.JavaWrapperCreated ( | ||
| GetPayloadValue (managedType), | ||
| GetPayloadValue (javaType), | ||
| jniIdentityHashCode, | ||
| managedObjectHashCode, | ||
| GetPayloadValue (runtimeMode)); | ||
| } | ||
|
|
||
| public static void DotNetWrapperReleasedJavaReference ( | ||
| string? managedType, | ||
| string? javaType, | ||
| int jniIdentityHashCode, | ||
| int managedObjectHashCode, | ||
| string? runtimeMode) | ||
| { | ||
| source.DotNetWrapperReleasedJavaReference ( | ||
| GetPayloadValue (managedType), | ||
| GetPayloadValue (javaType), | ||
| jniIdentityHashCode, | ||
| managedObjectHashCode, | ||
| GetPayloadValue (runtimeMode)); | ||
| } | ||
|
|
||
| public static void JavaWrapperReleasedDotNetReference ( | ||
| string? managedType, | ||
| string? javaType, | ||
| int jniIdentityHashCode, | ||
| int managedObjectHashCode, | ||
| string? runtimeMode) | ||
| { | ||
| source.JavaWrapperReleasedDotNetReference ( | ||
| GetPayloadValue (managedType), | ||
| GetPayloadValue (javaType), | ||
| jniIdentityHashCode, | ||
| managedObjectHashCode, | ||
| GetPayloadValue (runtimeMode)); | ||
| } | ||
|
|
||
| public static void DotNetObjectOnlyReachableFromJava ( | ||
| string? managedType, | ||
| string? javaType, | ||
| int jniIdentityHashCode, | ||
| int managedObjectHashCode, | ||
| string? runtimeMode, | ||
| int componentIndex, | ||
| int contextIndex, | ||
| long contextPointer) | ||
| { | ||
| source.DotNetObjectOnlyReachableFromJava ( | ||
| GetPayloadValue (managedType), | ||
| GetPayloadValue (javaType), | ||
| jniIdentityHashCode, | ||
| managedObjectHashCode, | ||
| GetPayloadValue (runtimeMode), | ||
| componentIndex, | ||
| contextIndex, | ||
| contextPointer); | ||
| } | ||
|
|
||
| public static void JavaObjectOnlyReachableFromDotNet ( | ||
| string? managedType, | ||
| string? javaType, | ||
| int jniIdentityHashCode, | ||
| int managedObjectHashCode, | ||
| string? runtimeMode, | ||
| int componentIndex, | ||
| int contextIndex, | ||
| long contextPointer) | ||
| { | ||
| source.JavaObjectOnlyReachableFromDotNet ( | ||
| GetPayloadValue (managedType), | ||
| GetPayloadValue (javaType), | ||
| jniIdentityHashCode, | ||
| managedObjectHashCode, | ||
| GetPayloadValue (runtimeMode), | ||
| componentIndex, | ||
| contextIndex, | ||
| contextPointer); | ||
| } | ||
|
|
||
| static string GetPayloadValue (string? value) | ||
| { | ||
| return value ?? UnknownValue; | ||
| } | ||
|
|
||
| [EventSource (Name = ProviderName)] | ||
| sealed class InteropEventSourceImplementation : EventSource | ||
| { | ||
| public static class Keywords | ||
| { | ||
| public const EventKeywords WrapperLifecycle = (EventKeywords) 0x1; | ||
| public const EventKeywords Reachability = (EventKeywords) 0x2; | ||
| } | ||
|
|
||
| [Event (1, Level = EventLevel.Informational, Keywords = Keywords.WrapperLifecycle)] | ||
| public void DotNetWrapperCreated (string managedType, string javaType, int jniIdentityHashCode, int managedObjectHashCode, string runtimeMode) | ||
| { | ||
| WriteEvent (1, managedType, javaType, jniIdentityHashCode, managedObjectHashCode, runtimeMode); | ||
| } | ||
|
|
||
| [Event (2, Level = EventLevel.Informational, Keywords = Keywords.WrapperLifecycle)] | ||
| public void JavaWrapperCreated (string managedType, string javaType, int jniIdentityHashCode, int managedObjectHashCode, string runtimeMode) | ||
| { | ||
| WriteEvent (2, managedType, javaType, jniIdentityHashCode, managedObjectHashCode, runtimeMode); | ||
| } | ||
|
|
||
| [Event (3, Level = EventLevel.Informational, Keywords = Keywords.WrapperLifecycle)] | ||
| public void DotNetWrapperReleasedJavaReference (string managedType, string javaType, int jniIdentityHashCode, int managedObjectHashCode, string runtimeMode) | ||
| { | ||
| WriteEvent (3, managedType, javaType, jniIdentityHashCode, managedObjectHashCode, runtimeMode); | ||
| } | ||
|
|
||
| [Event (4, Level = EventLevel.Informational, Keywords = Keywords.WrapperLifecycle)] | ||
| public void JavaWrapperReleasedDotNetReference (string managedType, string javaType, int jniIdentityHashCode, int managedObjectHashCode, string runtimeMode) | ||
| { | ||
| WriteEvent (4, managedType, javaType, jniIdentityHashCode, managedObjectHashCode, runtimeMode); | ||
| } | ||
|
|
||
| [Event (5, Level = EventLevel.Informational, Keywords = Keywords.Reachability)] | ||
| public void DotNetObjectOnlyReachableFromJava (string managedType, string javaType, int jniIdentityHashCode, int managedObjectHashCode, string runtimeMode, int componentIndex, int contextIndex, long contextPointer) | ||
| { | ||
| WriteEvent (5, managedType, javaType, jniIdentityHashCode, managedObjectHashCode, runtimeMode, componentIndex, contextIndex, contextPointer); | ||
| } | ||
|
|
||
| [Event (6, Level = EventLevel.Informational, Keywords = Keywords.Reachability)] | ||
| public void JavaObjectOnlyReachableFromDotNet (string managedType, string javaType, int jniIdentityHashCode, int managedObjectHashCode, string runtimeMode, int componentIndex, int contextIndex, long contextPointer) | ||
| { | ||
| WriteEvent (6, managedType, javaType, jniIdentityHashCode, managedObjectHashCode, runtimeMode, componentIndex, contextIndex, contextPointer); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
There is some existing code in here for writing gref/lref logs. It is old and text-based. Does this replace or supplement this?