From 826db3e23768135905ced827862d336c0a02eb4e Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Wed, 21 Oct 2020 19:56:17 -0700 Subject: [PATCH 1/3] Add event for marking the Start/Stop of source generation --- .../DllImportGenerator/Diagnostics/Events.cs | 46 +++++++++++++++++++ .../DllImportGenerator/DllImportGenerator.cs | 3 ++ 2 files changed, 49 insertions(+) create mode 100644 DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs diff --git a/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs b/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs new file mode 100644 index 000000000000..b470631553b5 --- /dev/null +++ b/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs @@ -0,0 +1,46 @@ +using System; +using System.Diagnostics.Tracing; + +namespace Microsoft.Interop.Diagnostics +{ + [EventSource(Name = "Microsoft-Interop-Events")] + internal sealed class Events : EventSource + { + public static class Keywords + { + public const EventKeywords SourceGeneration = (EventKeywords)1; + } + + public static readonly Events Logger = new Events(); + + private const int StartSourceGenerationEventId = 1; + private const int StopSourceGenerationEventId = StartSourceGenerationEventId + 1; + + private Events() + { } + + [NonEvent] + public static IDisposable SourceGenerationStartStop(int methodCount) + { + return new StartStopEvent(methodCount); + } + + [Event(StartSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)] + public void SourceGenerationStart(int methodCount) + { + this.WriteEvent(StartSourceGenerationEventId, methodCount); + } + + [Event(StopSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)] + public void SourceGenerationStop() + { + this.WriteEvent(StopSourceGenerationEventId); + } + + private class StartStopEvent : IDisposable + { + public StartStopEvent(int methodCount) => Logger.SourceGenerationStart(methodCount); + public void Dispose() => Logger.SourceGenerationStop(); + } + } +} diff --git a/DllImportGenerator/DllImportGenerator/DllImportGenerator.cs b/DllImportGenerator/DllImportGenerator/DllImportGenerator.cs index 1b4588f3fc05..141ff64b409b 100644 --- a/DllImportGenerator/DllImportGenerator/DllImportGenerator.cs +++ b/DllImportGenerator/DllImportGenerator/DllImportGenerator.cs @@ -28,6 +28,9 @@ public void Execute(GeneratorExecutionContext context) return; } + // Fire the start/stop pair for source generation + using var _ = Diagnostics.Events.SourceGenerationStartStop(synRec.Methods.Count); + // Store a mapping between SyntaxTree and SemanticModel. // SemanticModels cache results and since we could be looking at // method declarations in the same SyntaxTree we want to benefit from From 48ab77aa34095d10c265a888a0eca9239cb80993 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 23 Oct 2020 12:22:39 -0700 Subject: [PATCH 2/3] Add comment on Start/Stop convention. --- DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs b/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs index b470631553b5..ce1ce5fd12cb 100644 --- a/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs +++ b/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs @@ -25,6 +25,11 @@ public static IDisposable SourceGenerationStartStop(int methodCount) return new StartStopEvent(methodCount); } + // N.B. The 'Start' and 'Stop' suffixes for event names (i.e. "xxxStart" and "xxxStop") + // have special meaning in EventSource. They enable creating 'activities' if they are + // paired and the Stop event's ID is +1 the Start event's ID. + // See https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation-features/ + [Event(StartSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)] public void SourceGenerationStart(int methodCount) { From f50fa25f7e2a82e6d8ff2052adb725dbcca5cc2a Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 23 Oct 2020 12:31:25 -0700 Subject: [PATCH 3/3] Review feedback. --- .../DllImportGenerator/Diagnostics/Events.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs b/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs index ce1ce5fd12cb..ef78d0bc86b4 100644 --- a/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs +++ b/DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs @@ -3,7 +3,7 @@ namespace Microsoft.Interop.Diagnostics { - [EventSource(Name = "Microsoft-Interop-Events")] + [EventSource(Name = "Microsoft-Interop-SourceGeneration-Events")] internal sealed class Events : EventSource { public static class Keywords @@ -19,6 +19,11 @@ public static class Keywords private Events() { } + /// + /// Utility function that wraps emitting start/stop events for the source generation event. + /// + /// The number of methods being generated + /// An instance that will fire the "stop" event when Disposed. [NonEvent] public static IDisposable SourceGenerationStartStop(int methodCount) { @@ -30,12 +35,19 @@ public static IDisposable SourceGenerationStartStop(int methodCount) // paired and the Stop event's ID is +1 the Start event's ID. // See https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation-features/ + /// + /// Indicates the interop's DllImport Roslyn Source Generator has started source generation. + /// + /// The number of methods being generated [Event(StartSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)] public void SourceGenerationStart(int methodCount) { this.WriteEvent(StartSourceGenerationEventId, methodCount); } + /// + /// Indicates the interop's DllImport Roslyn Source Generator has stopped source generation. + /// [Event(StopSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)] public void SourceGenerationStop() {