Skip to content

Commit 1a18dea

Browse files
Copilotbrianrob
andauthored
Extend PredefinedDynamicTraceEventParser to Support Dynamic Events from EventPipeEventSource (#2232)
* Initial plan for issue * Add EventPipe support to PredefinedDynamicTraceEventParser Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com> * Update UniversalEventsTraceEventParser to inherit from PredefinedDynamicTraceEventParser Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com> * Update cpu and cswitch events to use registered dynamic events Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com> * Fix parser implementation. * Handle the case when events don't match any known template in PredefinedDynamicTraceEventParser. Ensure we don't spend lots of time decoding metadata over and over. * Fix implementation to work with instances of TraceLog. Metadata is pulled from the serialized dynamic event information. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com> Co-authored-by: Brian Robbins <brianrob@microsoft.com>
1 parent cd2b0fb commit 1a18dea

4 files changed

Lines changed: 173 additions & 86 deletions

File tree

src/PerfView/PerfViewData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Diagnostics.Tracing.Etlx;
88
using Microsoft.Diagnostics.Tracing.EventPipe;
99
using Microsoft.Diagnostics.Tracing.Parsers;
10+
using Microsoft.Diagnostics.Tracing.Parsers.Universal.Events;
1011
using Microsoft.Diagnostics.Tracing.Parsers.AspNet;
1112
using Microsoft.Diagnostics.Tracing.Parsers.Clr;
1213
using Microsoft.Diagnostics.Tracing.Parsers.ClrPrivate;
@@ -42,7 +43,6 @@
4243
using Utilities;
4344
using Address = System.UInt64;
4445
using EventSource = EventSources.EventSource;
45-
using Microsoft.Diagnostics.Tracing.Parsers.Universal.Events;
4646

4747
namespace PerfView
4848
{

src/TraceEvent/Parsers/UniversalEventsTraceEventParser.cs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.Diagnostics.Tracing.Parsers
99
{
1010
using Microsoft.Diagnostics.Tracing.Parsers.Universal.Events;
1111

12-
public sealed class UniversalEventsTraceEventParser: TraceEventParser
12+
public sealed class UniversalEventsTraceEventParser: PredefinedDynamicTraceEventParser
1313
{
1414
public static string ProviderName = "Universal.Events";
1515
public static Guid ProviderGuid = new Guid("bc5e5d63-9799-5873-33d9-fba8316cef71");
@@ -18,84 +18,60 @@ public enum Keywords : long
1818
None = 0x0,
1919
};
2020

21-
public UniversalEventsTraceEventParser(TraceEventSource source) : base(source) { }
21+
public UniversalEventsTraceEventParser(TraceEventSource source) : base(source)
22+
{
23+
// Register templates for the universal events
24+
RegisterTemplate(new SampleTraceData("cpu"));
25+
RegisterTemplate(new SampleTraceData("cswitch"));
26+
}
2227

2328
public event Action<SampleTraceData> cpu
2429
{
2530
add
2631
{
27-
source.RegisterEventTemplate(new SampleTraceData(value, 1, (int)TraceEventTask.Default, "cpu", Guid.Empty, 0, "Default", ProviderGuid, ProviderName));
32+
AddCallbackForEvent(
33+
"cpu",
34+
(TraceEvent data) => value((SampleTraceData)data)
35+
);
2836
}
2937
remove
3038
{
31-
source.UnregisterEventTemplate(value, 1, Guid.Empty);
39+
throw new NotImplementedException();
3240
}
3341
}
3442

3543
public event Action<SampleTraceData> cswitch
3644
{
3745
add
3846
{
39-
source.RegisterEventTemplate(new SampleTraceData(value, 2, (int)TraceEventTask.Default, "cswitch", Guid.Empty, 0, "Default", ProviderGuid, ProviderName));
47+
AddCallbackForEvent(
48+
"cswitch",
49+
(TraceEvent data) => value((SampleTraceData)data)
50+
);
4051
}
4152
remove
4253
{
43-
source.UnregisterEventTemplate(value, 2, Guid.Empty);
54+
throw new NotImplementedException();
4455
}
4556
}
4657

4758
#region private
4859
protected override string GetProviderName() { return ProviderName; }
4960

50-
static private volatile TraceEvent[] s_templates;
51-
52-
protected internal override void EnumerateTemplates(Func<string, string, EventFilterResponse> eventsToObserve, Action<TraceEvent> callback)
53-
{
54-
if (s_templates == null)
55-
{
56-
var templates = new TraceEvent[2];
57-
templates[0] = new SampleTraceData(null, 1, (int)TraceEventTask.Default, "cpu", Guid.Empty, 0, "Default", ProviderGuid, ProviderName);
58-
templates[1] = new SampleTraceData(null, 2, (int)TraceEventTask.Default, "cswitch", Guid.Empty, 0, "Default", ProviderGuid, ProviderName);
59-
s_templates = templates;
60-
}
61-
foreach (var template in s_templates)
62-
if (eventsToObserve == null || eventsToObserve(template.ProviderName, template.EventName) == EventFilterResponse.AcceptEvent)
63-
callback(template);
64-
}
65-
6661
#endregion
6762
}
6863
}
6964

7065
namespace Microsoft.Diagnostics.Tracing.Parsers.Universal.Events
7166
{
72-
public sealed class SampleTraceData : TraceEvent
67+
public sealed class SampleTraceData : PredefinedDynamicEvent
7368
{
74-
public Address Value { get { return GetVarUIntAt(0); } }
75-
76-
#region Private
77-
internal SampleTraceData(Action<SampleTraceData> action, int eventID, int task, string taskName, Guid taskGuid, int opcode, string opcodeName, Guid providerGuid, string providerName)
78-
: base(eventID, task, taskName, taskGuid, opcode, opcodeName, providerGuid, providerName)
79-
{
80-
Action = action;
81-
}
82-
protected internal override void Dispatch()
69+
public SampleTraceData(string eventName)
70+
: base(eventName, UniversalEventsTraceEventParser.ProviderGuid, UniversalEventsTraceEventParser.ProviderName)
8371
{
84-
Action(this);
8572
}
8673

87-
protected internal override Delegate Target
88-
{
89-
get { return Action; }
90-
set { Action = (Action<SampleTraceData>)value; }
91-
}
92-
public override StringBuilder ToXml(StringBuilder sb)
93-
{
94-
Prefix(sb);
95-
XmlAttrib(sb, "Value", Value);
96-
sb.Append("/>");
97-
return sb;
98-
}
74+
public Address Value { get { return GetVarUIntAt(0); } }
9975

10076
public override string[] PayloadNames
10177
{
@@ -119,10 +95,16 @@ public override object PayloadValue(int index)
11995
}
12096
}
12197

98+
public override StringBuilder ToXml(StringBuilder sb)
99+
{
100+
Prefix(sb);
101+
XmlAttrib(sb, "Value", Value);
102+
sb.Append("/>");
103+
return sb;
104+
}
105+
122106
public static ulong GetKeywords() { return 0; }
123107
public static string GetProviderName() { return UniversalEventsTraceEventParser.ProviderName; }
124108
public static Guid GetProviderGuid() { return UniversalEventsTraceEventParser.ProviderGuid; }
125-
private event Action<SampleTraceData> Action;
126-
#endregion
127109
}
128110
}

src/TraceEvent/PredefinedDynamicEvent.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
namespace Microsoft.Diagnostics.Tracing.Parsers
77
{
88
/// <summary>
9-
/// PredefinedDynamicEvent is used to create strongly typed representations of TraceLogging events.
10-
/// This allows TraceLogging events (self-describing events) to have the same level of strongly typed
9+
/// PredefinedDynamicEvent is used to create strongly typed representations of self-describing events.
10+
/// This allows self-describing events to have the same level of strongly typed
1111
/// support as manifested events.
1212
/// </summary>
1313
public abstract class PredefinedDynamicEvent : TraceEvent
1414
{
1515
/// <summary>
16-
/// Creates a template for a TraceLogging event.
16+
/// Creates a template for a self-describing event.
1717
/// </summary>
1818
/// <param name="eventName">The name of the event</param>
1919
/// <param name="providerGuid">The GUID of the provider</param>
@@ -57,8 +57,6 @@ protected internal override Delegate Target
5757
set { m_action = (Action<TraceEvent>)value; }
5858
}
5959

60-
61-
6260
/// <summary>
6361
/// Creates a clone of this template.
6462
/// </summary>

0 commit comments

Comments
 (0)