Skip to content

Commit

Permalink
Add event for marking the Start/Stop of source generation (#258)
Browse files Browse the repository at this point in the history
* Add event for marking the Start/Stop of source generation
  • Loading branch information
AaronRobinsonMSFT authored Oct 23, 2020
1 parent 915905c commit 1e3819b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
63 changes: 63 additions & 0 deletions DllImportGenerator/DllImportGenerator/Diagnostics/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Diagnostics.Tracing;

namespace Microsoft.Interop.Diagnostics
{
[EventSource(Name = "Microsoft-Interop-SourceGeneration-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()
{ }

/// <summary>
/// Utility function that wraps emitting start/stop events for the source generation event.
/// </summary>
/// <param name="methodCount">The number of methods being generated</param>
/// <returns>An <see cref="IDisposable"/> instance that will fire the "stop" event when Disposed.</returns>
[NonEvent]
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/

/// <summary>
/// Indicates the interop's DllImport Roslyn Source Generator has started source generation.
/// </summary>
/// <param name="methodCount">The number of methods being generated</param>
[Event(StartSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)]
public void SourceGenerationStart(int methodCount)
{
this.WriteEvent(StartSourceGenerationEventId, methodCount);
}

/// <summary>
/// Indicates the interop's DllImport Roslyn Source Generator has stopped source generation.
/// </summary>
[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();
}
}
}
3 changes: 3 additions & 0 deletions DllImportGenerator/DllImportGenerator/DllImportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1e3819b

Please sign in to comment.