-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add logging event source * Track timing information in the generator driver - Expose via runrunresult and ETW - Extract out helper from event source - Add extension methods to create a start/stop pair of events the driver can use
- Loading branch information
Showing
8 changed files
with
165 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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,41 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics.Tracing; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis | ||
{ | ||
[EventSource(Name = "Microsoft-CodeAnalysis-General")] | ||
internal sealed class CodeAnalysisEventSource : EventSource | ||
{ | ||
public static readonly CodeAnalysisEventSource Log = new CodeAnalysisEventSource(); | ||
|
||
public static class Keywords | ||
{ | ||
public const EventKeywords Performance = (EventKeywords)1; | ||
} | ||
|
||
public static class Tasks | ||
{ | ||
public const EventTask GeneratorDriverRunTime = (EventTask)1; | ||
public const EventTask SingleGeneratorRunTime = (EventTask)2; | ||
} | ||
|
||
private CodeAnalysisEventSource() { } | ||
|
||
[Event(1, Keywords = Keywords.Performance, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Task = Tasks.GeneratorDriverRunTime)] | ||
internal void StartGeneratorDriverRunTime(string id) => WriteEvent(1, id); | ||
|
||
[Event(2, Message = "Generators ran for {0} ticks", Keywords = Keywords.Performance, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Task = Tasks.GeneratorDriverRunTime)] | ||
internal void StopGeneratorDriverRunTime(long elapsedTicks, string id) => WriteEvent(2, elapsedTicks, id); | ||
|
||
[Event(3, Keywords = Keywords.Performance, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Task = Tasks.SingleGeneratorRunTime)] | ||
internal void StartSingleGeneratorRunTime(string generatorName, string assemblyPath, string id) => WriteEvent(3, generatorName, assemblyPath, id); | ||
|
||
[Event(4, Message = "Generator {0} ran for {2} ticks", Keywords = Keywords.Performance, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Task = Tasks.SingleGeneratorRunTime)] | ||
internal void StopSingleGeneratorRunTime(string generatorName, string assemblyPath, long elapsedTicks, string id) => WriteEvent(4, generatorName, assemblyPath, elapsedTicks, id); | ||
} | ||
} |
This file contains 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 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 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 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
76 changes: 76 additions & 0 deletions
76
src/Compilers/Core/Portable/SourceGeneration/GeneratorTimerExtensions.cs
This file contains 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,76 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
using System.Text; | ||
using Roslyn.Utilities; | ||
using static Microsoft.CodeAnalysis.CodeAnalysisEventSource; | ||
|
||
namespace Microsoft.CodeAnalysis | ||
{ | ||
internal static class GeneratorTimerExtensions | ||
{ | ||
public static RunTimer CreateGeneratorDriverRunTimer(this CodeAnalysisEventSource eventSource) | ||
{ | ||
if (eventSource.IsEnabled(EventLevel.Informational, Keywords.Performance)) | ||
{ | ||
var id = Guid.NewGuid().ToString(); | ||
eventSource.StartGeneratorDriverRunTime(id); | ||
return new RunTimer(t => eventSource.StopGeneratorDriverRunTime(t.Ticks, id)); | ||
} | ||
else | ||
{ | ||
return new RunTimer(); | ||
} | ||
} | ||
|
||
public static RunTimer CreateSingleGeneratorRunTimer(this CodeAnalysisEventSource eventSource, ISourceGenerator generator) | ||
{ | ||
if (eventSource.IsEnabled(EventLevel.Informational, Keywords.Performance)) | ||
{ | ||
var id = Guid.NewGuid().ToString(); | ||
var type = generator.GetGeneratorType(); | ||
eventSource.StartSingleGeneratorRunTime(type.FullName!, type.Assembly.Location, id); | ||
return new RunTimer(t => eventSource.StopSingleGeneratorRunTime(type.FullName!, type.Assembly.Location, t.Ticks, id)); | ||
} | ||
else | ||
{ | ||
return new RunTimer(); | ||
} | ||
} | ||
|
||
internal readonly struct RunTimer : IDisposable | ||
{ | ||
private readonly SharedStopwatch _timer; | ||
private readonly Action<TimeSpan>? _callback; | ||
|
||
public TimeSpan Elapsed => _timer.Elapsed; | ||
|
||
public RunTimer() | ||
{ | ||
// start twice to improve accuracy. See AnalyzerExecutor.ExecuteAndCatchIfThrows for more details | ||
_ = SharedStopwatch.StartNew(); | ||
_timer = SharedStopwatch.StartNew(); | ||
_callback = null; | ||
} | ||
|
||
public RunTimer(Action<TimeSpan> callback) | ||
: this() | ||
{ | ||
_callback = callback; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_callback is not null) | ||
{ | ||
var elapsed = _timer.Elapsed; | ||
_callback(elapsed); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.