-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add EventPipe EventSource serialization testing #120887
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
noahfalk
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
noahfalk:eventpipe_metadata_tests
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
160 changes: 160 additions & 0 deletions
160
...raries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/Harness/EventPipeListener.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,160 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Tracing; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Diagnostics.NETCore.Client; | ||
| using Microsoft.Diagnostics.Tracing; | ||
| using Xunit; | ||
|
|
||
| namespace BasicEventSourceTests | ||
| { | ||
| /// <summary> | ||
| /// Implementation of Listener for EventPipe (in-process collection using DiagnosticsClient + EventPipeEventSource). | ||
| /// </summary> | ||
| internal sealed class EventPipeListener : Listener | ||
| { | ||
| private readonly List<(string eventSourceName, EventCommand command, FilteringOptions options)> _pendingCommands = new(); | ||
| private readonly Dictionary<string, FilteringOptions> _enabled = new(StringComparer.Ordinal); | ||
| private EventPipeSession _session; | ||
| private Task _processingTask; | ||
| private bool _disposed; | ||
|
|
||
| public override bool IsDynamicConfigChangeSupported => false; | ||
|
|
||
| /// <summary> | ||
| /// EventPipe NetTrace V5 format can't emit the metadata for a Boolean8 HasValue field that self-describing events use. | ||
| /// </summary> | ||
| public override bool IsSelfDescribingNullableSupported => false; | ||
|
|
||
| public override bool IsEventPipe => true; | ||
|
|
||
| public EventPipeListener() { } | ||
|
|
||
| public override void EventSourceCommand(string eventSourceName, EventCommand command, FilteringOptions options = null) | ||
| { | ||
| if (eventSourceName is null) | ||
| throw new ArgumentNullException(nameof(eventSourceName)); | ||
|
|
||
| if (_session != null) | ||
| { | ||
| throw new InvalidOperationException("EventPipeEventListener does not support dynamic configuration changes after Start()."); | ||
| } | ||
| _pendingCommands.Add((eventSourceName, command, options)); | ||
| } | ||
|
|
||
| public override void Start() | ||
| { | ||
| if (_session != null) | ||
| return; // already started | ||
|
|
||
| // Build provider enable list from pending commands | ||
| foreach (var (eventSourceName, command, options) in _pendingCommands) | ||
| { | ||
| if (command == EventCommand.Enable) | ||
| { | ||
| var effective = options ?? new FilteringOptions(); | ||
| _enabled[eventSourceName] = effective; | ||
| } | ||
| else if (command == EventCommand.Disable) | ||
| { | ||
| _enabled.Remove(eventSourceName); | ||
| } | ||
| else | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| var providers = new List<EventPipeProvider>(); | ||
| foreach (var kvp in _enabled) | ||
| { | ||
| var opt = kvp.Value; | ||
| providers.Add(new EventPipeProvider(kvp.Key, (EventLevel)opt.Level, (long)opt.Keywords, opt.Args)); | ||
| } | ||
|
|
||
| var client = new DiagnosticsClient(Environment.ProcessId); | ||
| _session = client.StartEventPipeSession(providers, false); | ||
|
|
||
| _processingTask = Task.Factory.StartNew(() => ProcessEvents(_session), TaskCreationOptions.LongRunning); | ||
| } | ||
|
|
||
| private void ProcessEvents(EventPipeSession session) | ||
| { | ||
| using var source = new EventPipeEventSource(session.EventStream); | ||
| source.Dynamic.All += traceEvent => | ||
| { | ||
| // EventPipe adds extra events we didn't ask for, ignore them. | ||
| if (traceEvent.ProviderName == "Microsoft-DotNETCore-EventPipe") | ||
| return; | ||
|
|
||
| OnEvent?.Invoke(new EventPipeEvent(traceEvent)); | ||
| }; | ||
| source.Process(); | ||
| } | ||
|
|
||
| public override void Dispose() | ||
| { | ||
| if (_disposed) | ||
| return; | ||
| _disposed = true; | ||
| _session?.Stop(); | ||
| _session?.Dispose(); | ||
|
|
||
| if (_processingTask != null && !_processingTask.Wait(TimeSpan.FromSeconds(5))) | ||
| { | ||
| Debug.WriteLine("EventPipeEventListener processing task failed to stop in a timely manner."); | ||
| } | ||
| } | ||
|
|
||
| public override string ToString() => "EventPipeListener"; | ||
|
|
||
| /// <summary> | ||
| /// Wrapper mapping TraceEvent (EventPipe) to harness Event abstraction. | ||
| /// </summary> | ||
| private sealed class EventPipeEvent : Event | ||
| { | ||
| private readonly TraceEvent _data; | ||
| private readonly IList<string> _payloadNames; | ||
| private readonly IList<object> _payloadValues; | ||
|
|
||
| public EventPipeEvent(TraceEvent data) | ||
| { | ||
| _data = data; | ||
| // EventPipe has a discrepancy with ETW for self-describing events - it exposes a single top-level object whereas ETW considers each of the fields within | ||
| // that object as top-level named fields. To workaround that we unwrap any top-level object at payload index 0. | ||
| if(data.PayloadNames.Length > 0 && data.PayloadValue(0) is IDictionary<string,object> d) | ||
| { | ||
| _payloadNames = d.Select(kv => kv.Key).ToList(); | ||
| _payloadValues = d.Select(kv => kv.Value).ToList(); | ||
| } | ||
| else | ||
| { | ||
| _payloadNames = data.PayloadNames; | ||
| _payloadValues = new List<object>(); | ||
| for(int i = 0; i < _payloadNames.Count; i++) | ||
| { | ||
| _payloadValues.Add(data.PayloadValue(i)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override string ProviderName => _data.ProviderName; | ||
| public override string EventName => _data.EventName; | ||
| public override int PayloadCount => _payloadNames.Count; | ||
| public override IList<string> PayloadNames => _payloadNames; | ||
|
|
||
| public override object PayloadValue(int propertyIndex, string propertyName) | ||
| { | ||
| if (propertyName != null) | ||
| Assert.Equal(propertyName, _payloadNames[propertyIndex]); | ||
| return _payloadValues[propertyIndex]; | ||
| } | ||
| } | ||
| } | ||
| } |
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.
This reminded me that I added a modified copy of Microsoft.Diagnostics.NETCore.Client in the runtime to get the EventPipe tests at
src/tests/tracing/eventpipeworking for Android #64358. Were there any changes on the diagnostics side that are missing in runtime's local copy that EventPipeListener needs?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 didn't run into anything but I also didn't run the tests on Android during my local testing. If loopback DiagnosticsClient doesn't work there I'd probably just turn the tests off on that platform. The goal of the new testing was to validate the serialization works and all the serialization code should be identical regardless of platform/OS.
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'm wondering if it's reasonable to still reference the runtime's copy
src/tests/tracing/eventpipe/common/Microsoft.Diagnostics.NETCore.ClientinSystem.Diagnostics.Tracing.Tests.csproj. I wasn't particularly thinking about the test running on Android, more that #64358 removed this property so all tests would use the runtime copy. Reintroducing this property means there's some places that uses the runtimes copy and other places that uses the diagnostics copy.I'm fine if we want to do that, I just recall that one of the previous debts was that the runtime copy would be bumped if there was new logic in the diagnostics copy, so I was just curious if there was something in
0.2.621003that the local copy was missing.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.
If we don't need to run the serialization tests on all platforms and we mainly need the runtime version of the NETCore.Client on mobile platforms using TCP/IP, then maybe we should keep using runtime version for mobile platforms and turn of the serialization tests on those platforms. Alternative is to bring over whats missing into the runtime version of NETCore.Client (and probably turn off the serialization tests on mobile if it depends on spawning additional processes).