Skip to content

Commit 702be07

Browse files
committed
Track the project name for a generator driver and report it via ETW
1 parent 01d9867 commit 702be07

File tree

12 files changed

+46
-17
lines changed

12 files changed

+46
-17
lines changed

src/Compilers/Core/Portable/CodeAnalysisEventSource.Common.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,23 @@ private CodeAnalysisEventSource() { }
3737
internal void StartSingleGeneratorRunTime(string generatorName, string assemblyPath, string id) => WriteEvent(3, generatorName, assemblyPath, id);
3838

3939
[Event(4, Message = "Generator {0} ran for {2} ticks", Keywords = Keywords.Performance, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Task = Tasks.SingleGeneratorRunTime)]
40-
internal unsafe void StopSingleGeneratorRunTime(string generatorName, string assemblyPath, long elapsedTicks, string id)
40+
internal unsafe void StopSingleGeneratorRunTime(string generatorName, string projectName, string assemblyPath, long elapsedTicks, string id)
4141
{
4242
if (IsEnabled())
4343
{
4444
fixed (char* generatorNameBytes = generatorName)
45+
fixed (char* projectNameBytes = projectName)
4546
fixed (char* assemblyPathBytes = assemblyPath)
4647
fixed (char* idBytes = id)
4748
{
48-
Span<EventData> data = stackalloc EventData[]
49-
{
49+
Span<EventData> data =
50+
[
5051
GetEventDataForString(generatorName, generatorNameBytes),
52+
GetEventDataForString(projectName, projectNameBytes),
5153
GetEventDataForString(assemblyPath, assemblyPathBytes),
5254
GetEventDataForInt64(&elapsedTicks),
5355
GetEventDataForString(id, idBytes),
54-
};
56+
];
5557

5658
fixed (EventSource.EventData* dataPtr = data)
5759
{

src/Compilers/Core/Portable/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Microsoft.CodeAnalysis.CommandLineArguments.ManifestResourceArguments.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CommandLineResource>
1+
Microsoft.CodeAnalysis.CommandLineArguments.ManifestResourceArguments.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CommandLineResource>
22
Microsoft.CodeAnalysis.CommandLineResource
33
Microsoft.CodeAnalysis.CommandLineResource.CommandLineResource() -> void
44
Microsoft.CodeAnalysis.CommandLineResource.FullPath.get -> string!
@@ -13,6 +13,9 @@ Microsoft.CodeAnalysis.Emit.EmitDifferenceOptions
1313
Microsoft.CodeAnalysis.Emit.EmitDifferenceOptions.EmitDifferenceOptions() -> void
1414
Microsoft.CodeAnalysis.Emit.EmitDifferenceOptions.EmitFieldRva.get -> bool
1515
Microsoft.CodeAnalysis.Emit.EmitDifferenceOptions.EmitFieldRva.init -> void
16+
Microsoft.CodeAnalysis.GeneratorDriverOptions.GeneratorDriverOptions(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind disabledOutputs = Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false, string? baseDirectory = null, string? projectName = null) -> void
17+
Microsoft.CodeAnalysis.GeneratorDriverOptions.GeneratorDriverOptions(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind disabledOutputs, bool trackIncrementalGeneratorSteps, string? baseDirectory) -> void
18+
Microsoft.CodeAnalysis.GeneratorDriverOptions.ProjectName.get -> string?
1619
Microsoft.CodeAnalysis.IMethodSymbol.AssociatedExtensionImplementation.get -> Microsoft.CodeAnalysis.IMethodSymbol?
1720
Microsoft.CodeAnalysis.IMethodSymbol.IsIterator.get -> bool
1821
Microsoft.CodeAnalysis.IMethodSymbol.ReduceExtensionMember(Microsoft.CodeAnalysis.ITypeSymbol! receiverType) -> Microsoft.CodeAnalysis.IMethodSymbol?

src/Compilers/Core/Portable/SourceGeneration/GeneratorDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ internal GeneratorDriverState RunGeneratorsCore(Compilation compilation, Diagnos
317317
continue;
318318
}
319319

320-
using var generatorTimer = CodeAnalysisEventSource.Log.CreateSingleGeneratorRunTimer(state.Generators[i], (t) => t.Add(syntaxStoreBuilder.GetRuntimeAdjustment(stateBuilder[i].InputNodes)));
320+
using var generatorTimer = CodeAnalysisEventSource.Log.CreateSingleGeneratorRunTimer(state.Generators[i], state.ProjectName, (t) => t.Add(syntaxStoreBuilder.GetRuntimeAdjustment(stateBuilder[i].InputNodes)));
321321
try
322322
{
323323
// We do not support incremental step tracking for v1 generators, as the pipeline is implicitly defined.

src/Compilers/Core/Portable/SourceGeneration/GeneratorDriverOptions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public readonly struct GeneratorDriverOptions
2222
/// </summary>
2323
public string? BaseDirectory { get; }
2424

25+
/// <summary>
26+
/// The name of the project this generator driver is for.
27+
/// </summary>
28+
/// <remarks>
29+
/// Only used for telemetry purposes.
30+
/// </remarks>
31+
public string? ProjectName { get; }
32+
2533
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs)
2634
: this(disabledOutputs, false)
2735
{
@@ -40,7 +48,7 @@ public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs, bo
4048
/// <param name="trackIncrementalGeneratorSteps"></param>
4149
/// <param name="baseDirectory">Absolute path to the base directory used for file paths of generated files.</param>
4250
/// <exception cref="ArgumentException"><paramref name="baseDirectory"/> is not an absolute path.</exception>
43-
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs = IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false, string? baseDirectory = null)
51+
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs = IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false, string? baseDirectory = null, string? projectName = null)
4452
{
4553
if (baseDirectory != null && !PathUtilities.IsAbsolute(baseDirectory))
4654
{
@@ -50,6 +58,13 @@ public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs = I
5058
DisabledOutputs = disabledOutputs;
5159
TrackIncrementalGeneratorSteps = trackIncrementalGeneratorSteps;
5260
BaseDirectory = baseDirectory;
61+
ProjectName = projectName;
62+
}
63+
64+
// 5.0 BACKCOMPAT OVERLOAD -- DO NOT TOUCH
65+
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs, bool trackIncrementalGeneratorSteps, string? baseDirectory)
66+
: this(disabledOutputs, trackIncrementalGeneratorSteps, baseDirectory, projectName: null)
67+
{
5368
}
5469
}
5570
}

src/Compilers/Core/Portable/SourceGeneration/GeneratorDriverState.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ internal GeneratorDriverState(ParseOptions parseOptions,
8080
/// </summary>
8181
internal string? BaseDirectory => _driverOptions.BaseDirectory;
8282

83+
/// <summary>
84+
/// The name of the project this driver is associated with.
85+
/// </summary>
86+
internal string? ProjectName => _driverOptions.ProjectName;
87+
8388
/// <summary>
8489
/// ParseOptions to use when parsing generator provided source.
8590
/// </summary>

src/Compilers/Core/Portable/SourceGeneration/GeneratorTimerExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public static RunTimer CreateGeneratorDriverRunTimer(this CodeAnalysisEventSourc
2727
}
2828
}
2929

30-
public static RunTimer CreateSingleGeneratorRunTimer(this CodeAnalysisEventSource eventSource, ISourceGenerator generator, Func<TimeSpan, TimeSpan> adjustRunTime)
30+
public static RunTimer CreateSingleGeneratorRunTimer(this CodeAnalysisEventSource eventSource, ISourceGenerator generator, string? projectName, Func<TimeSpan, TimeSpan> adjustRunTime)
3131
{
3232
if (eventSource.IsEnabled(EventLevel.Informational, Keywords.Performance))
3333
{
3434
var id = Guid.NewGuid().ToString();
3535
var type = generator.GetGeneratorType();
3636
eventSource.StartSingleGeneratorRunTime(type.FullName!, type.Assembly.Location, id);
37-
return new RunTimer(t => eventSource.StopSingleGeneratorRunTime(type.FullName!, type.Assembly.Location, t.Ticks, id), adjustRunTime);
37+
return new RunTimer(t => eventSource.StopSingleGeneratorRunTime(type.FullName!, projectName ?? "<Unknown Project>", type.Assembly.Location, t.Ticks, id), adjustRunTime);
3838
}
3939
else
4040
{

src/Tools/SemanticSearch/ReferenceAssemblies/Apis/Microsoft.CodeAnalysis.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,11 @@ Microsoft.CodeAnalysis.GeneratorDriverOptions
10761076
Microsoft.CodeAnalysis.GeneratorDriverOptions.#ctor(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind)
10771077
Microsoft.CodeAnalysis.GeneratorDriverOptions.#ctor(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind,System.Boolean)
10781078
Microsoft.CodeAnalysis.GeneratorDriverOptions.#ctor(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind,System.Boolean,System.String)
1079+
Microsoft.CodeAnalysis.GeneratorDriverOptions.#ctor(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind,System.Boolean,System.String,System.String)
10791080
Microsoft.CodeAnalysis.GeneratorDriverOptions.DisabledOutputs
10801081
Microsoft.CodeAnalysis.GeneratorDriverOptions.TrackIncrementalGeneratorSteps
10811082
Microsoft.CodeAnalysis.GeneratorDriverOptions.get_BaseDirectory
1083+
Microsoft.CodeAnalysis.GeneratorDriverOptions.get_ProjectName
10821084
Microsoft.CodeAnalysis.GeneratorDriverRunResult
10831085
Microsoft.CodeAnalysis.GeneratorDriverRunResult.get_Diagnostics
10841086
Microsoft.CodeAnalysis.GeneratorDriverRunResult.get_GeneratedTrees

src/Workspaces/CSharp/Portable/Workspace/LanguageServices/CSharpCompilationFactoryService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ CompilationOptions ICompilationFactoryService.GetDefaultCompilationOptions()
5353
return new CSharpCompilationOptions(outputKind: outputKind);
5454
}
5555

56-
GeneratorDriver ICompilationFactoryService.CreateGeneratorDriver(ParseOptions parseOptions, ImmutableArray<ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray<AdditionalText> additionalTexts, string? generatedFilesBaseDirectory)
57-
=> CSharpGeneratorDriver.Create(generators, additionalTexts, (CSharpParseOptions)parseOptions, optionsProvider, new GeneratorDriverOptions(baseDirectory: generatedFilesBaseDirectory));
56+
GeneratorDriver ICompilationFactoryService.CreateGeneratorDriver(ParseOptions parseOptions, ImmutableArray<ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray<AdditionalText> additionalTexts, string? generatedFilesBaseDirectory, string? projectName)
57+
=> CSharpGeneratorDriver.Create(generators, additionalTexts, (CSharpParseOptions)parseOptions, optionsProvider, new GeneratorDriverOptions(baseDirectory: generatedFilesBaseDirectory, projectName: projectName));
5858
}

src/Workspaces/Core/Portable/Workspace/Host/CompilationFactory/ICompilationFactoryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ internal interface ICompilationFactoryService : ILanguageService
1515
Compilation CreateSubmissionCompilation(string assemblyName, CompilationOptions options, Type? hostObjectType);
1616
CompilationOptions GetDefaultCompilationOptions();
1717
CompilationOptions? TryParsePdbCompilationOptions(IReadOnlyDictionary<string, string> compilationOptionsMetadata);
18-
GeneratorDriver CreateGeneratorDriver(ParseOptions parseOptions, ImmutableArray<ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray<AdditionalText> additionalTexts, string? generatedFilesBaseDirectory);
18+
GeneratorDriver CreateGeneratorDriver(ParseOptions parseOptions, ImmutableArray<ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray<AdditionalText> additionalTexts, string? generatedFilesBaseDirectory, string? projectName);
1919
}

src/Workspaces/Core/Portable/Workspace/Solution/SolutionCompilationState.RegularCompilationTracker_Generators.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ static GeneratorDriver CreateGeneratorDriver(ProjectState projectState)
437437
GetSourceGenerators(projectState),
438438
projectState.ProjectAnalyzerOptions.AnalyzerConfigOptionsProvider,
439439
additionalTexts,
440-
generatedFilesBaseDirectory);
440+
generatedFilesBaseDirectory,
441+
$"{projectState.Name} ({projectState.Id})");
441442
}
442443

443444
[Conditional("DEBUG")]

0 commit comments

Comments
 (0)