提交 92d723ff 编写于 作者: M Manish Vasani

Address review feedback

上级 f18d016a
......@@ -294,48 +294,35 @@ public async Task<Document> ApplyCodeFixesForSpecificDiagnosticIdAsync(Document
}
var extensionManager = document.Project.Solution.Workspace.Services.GetService<IExtensionManager>();
var isPerProviderLoggingEnabled = RoslynEventSource.Instance.IsEnabled(EventLevel.Informational, EventKeywords.None);
// run each CodeFixProvider to gather individual CodeFixes for reported diagnostics
foreach (var fixer in allFixers.Distinct())
{
cancellationToken.ThrowIfCancellationRequested();
if (isPerProviderLoggingEnabled)
using (RoslynEventSource.LogInformationalBlock(FunctionId.CodeFixes_GetCodeFixesAsync, fixer, cancellationToken))
{
using (RoslynEventSource.LogInformationalBlock(FunctionId.CodeFixes_GetCodeFixesAsync, fixer.ToString(), cancellationToken))
{
await ProcessFixerAsync(fixer).ConfigureAwait(false);
}
}
else
{
await ProcessFixerAsync(fixer).ConfigureAwait(false);
await AppendFixesOrConfigurationsAsync(
document, span, diagnostics, fixAllForInSpan, result, fixer,
hasFix: d => this.GetFixableDiagnosticIds(fixer, extensionManager).Contains(d.Id),
getFixes: dxs =>
{
if (fixAllForInSpan)
{
var primaryDiagnostic = dxs.First();
return GetCodeFixesAsync(document, primaryDiagnostic.Location.SourceSpan, fixer, isBlocking, ImmutableArray.Create(primaryDiagnostic), cancellationToken);
}
else
{
return GetCodeFixesAsync(document, span, fixer, isBlocking, dxs, cancellationToken);
}
},
cancellationToken: cancellationToken).ConfigureAwait(false);
}
// Just need the first result if we are doing fix all in span
if (fixAllForInSpan && result.Any()) return;
}
async Task ProcessFixerAsync(CodeFixProvider fixer)
{
await AppendFixesOrConfigurationsAsync(
document, span, diagnostics, fixAllForInSpan, result, fixer,
hasFix: d => this.GetFixableDiagnosticIds(fixer, extensionManager).Contains(d.Id),
getFixes: dxs =>
{
if (fixAllForInSpan)
{
var primaryDiagnostic = dxs.First();
return GetCodeFixesAsync(document, primaryDiagnostic.Location.SourceSpan, fixer, isBlocking, ImmutableArray.Create(primaryDiagnostic), cancellationToken);
}
else
{
return GetCodeFixesAsync(document, span, fixer, isBlocking, dxs, cancellationToken);
}
},
cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
private async Task<ImmutableArray<CodeFix>> GetCodeFixesAsync(
......@@ -371,33 +358,19 @@ async Task ProcessFixerAsync(CodeFixProvider fixer)
return;
}
var isPerProviderLoggingEnabled = RoslynEventSource.Instance.IsEnabled(EventLevel.Informational, EventKeywords.None);
// append CodeFixCollection for each CodeFixProvider
foreach (var provider in lazyConfigurationProviders.Value)
{
if (isPerProviderLoggingEnabled)
{
using (RoslynEventSource.LogInformationalBlock(FunctionId.CodeFixes_GetCodeFixesAsync, provider.ToString(), cancellationToken))
{
await AppendConfigurationsAsync(provider).ConfigureAwait(false);
}
}
else
using (RoslynEventSource.LogInformationalBlock(FunctionId.CodeFixes_GetCodeFixesAsync, provider, cancellationToken))
{
await AppendConfigurationsAsync(provider).ConfigureAwait(false);
await AppendFixesOrConfigurationsAsync(
document, diagnosticsSpan, diagnostics, fixAllForInSpan: false, result, provider,
hasFix: d => provider.IsFixableDiagnostic(d),
getFixes: dxs => provider.GetFixesAsync(
document, diagnosticsSpan, dxs, cancellationToken),
cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
async Task AppendConfigurationsAsync(IConfigurationFixProvider provider)
{
await AppendFixesOrConfigurationsAsync(
document, diagnosticsSpan, diagnostics, fixAllForInSpan: false, result, provider,
hasFix: d => provider.IsFixableDiagnostic(d),
getFixes: dxs => provider.GetFixesAsync(
document, diagnosticsSpan, dxs, cancellationToken),
cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
private async Task AppendFixesOrConfigurationsAsync<TCodeFixProvider>(
......
......@@ -114,27 +114,19 @@ private IEnumerable<CodeRefactoringProvider> GetProviders(Document document)
foreach (var provider in GetProviders(document))
{
tasks.Add(Task.Run(
() => GetRefactoringsAsync(provider), cancellationToken));
() =>
{
using (RoslynEventSource.LogInformationalBlock(FunctionId.Refactoring_CodeRefactoringService_GetRefactoringsAsync, provider, cancellationToken))
{
return GetRefactoringFromProviderAsync(document, state, provider, extensionManager, isBlocking, cancellationToken);
}
},
cancellationToken));
}
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
return results.WhereNotNull().ToImmutableArray();
}
Task<CodeRefactoring> GetRefactoringsAsync(CodeRefactoringProvider provider)
{
if (isPerProviderLoggingEnabled)
{
using (RoslynEventSource.LogInformationalBlock(FunctionId.Refactoring_CodeRefactoringService_GetRefactoringsAsync, provider.ToString(), cancellationToken))
{
return GetRefactoringFromProviderAsync(document, state, provider, extensionManager, isBlocking, cancellationToken);
}
}
else
{
return GetRefactoringFromProviderAsync(document, state, provider, extensionManager, isBlocking, cancellationToken);
}
}
}
private async Task<CodeRefactoring> GetRefactoringFromProviderAsync(
......
......@@ -108,23 +108,39 @@ private class LatestDiagnosticsForSpanGetter
public async Task<bool> TryGetAsync(List<DiagnosticData> list, CancellationToken cancellationToken)
{
var containsFullResult = true;
var loggingEnabled = RoslynEventSource.Instance.IsEnabled(EventLevel.Informational, EventKeywords.None);
try
{
var containsFullResult = true;
foreach (var stateSet in _stateSets)
{
if (loggingEnabled)
using (RoslynEventSource.LogInformationalBlock(FunctionId.DiagnosticAnalyzerService_GetDiagnosticsForSpanAsync, stateSet.Analyzer, cancellationToken))
{
using (RoslynEventSource.LogInformationalBlock(FunctionId.DiagnosticAnalyzerService_GetDiagnosticsForSpanAsync, stateSet.Analyzer.GetAnalyzerId(), cancellationToken))
cancellationToken.ThrowIfCancellationRequested();
containsFullResult &= await TryGetSyntaxAndSemanticDiagnosticsAsync(stateSet, list, cancellationToken).ConfigureAwait(false);
// check whether compilation end code fix is enabled
if (!_document.Project.Solution.Workspace.Options.GetOption(InternalDiagnosticsOptions.CompilationEndCodeFix))
{
await ProcessStateSetAsync(stateSet).ConfigureAwait(false);
continue;
}
}
else
{
await ProcessStateSetAsync(stateSet).ConfigureAwait(false);
// check whether heuristic is enabled
if (_blockForData && _document.Project.Solution.Workspace.Options.GetOption(InternalDiagnosticsOptions.UseCompilationEndCodeFixHeuristic))
{
var avoidLoadingData = true;
var state = stateSet.GetProjectState(_project.Id);
var result = await state.GetAnalysisDataAsync(_document, avoidLoadingData, cancellationToken).ConfigureAwait(false);
// no previous compilation end diagnostics in this file.
var version = await GetDiagnosticVersionAsync(_project, cancellationToken).ConfigureAwait(false);
if (state.IsEmpty(_document.Id) || result.Version != version)
{
continue;
}
}
containsFullResult &= await TryGetProjectDiagnosticsAsync(stateSet, GetProjectDiagnosticsAsync, list, cancellationToken).ConfigureAwait(false);
}
}
......@@ -136,36 +152,6 @@ public async Task<bool> TryGetAsync(List<DiagnosticData> list, CancellationToken
{
throw ExceptionUtilities.Unreachable;
}
async Task ProcessStateSetAsync(StateSet stateSet)
{
cancellationToken.ThrowIfCancellationRequested();
containsFullResult &= await TryGetSyntaxAndSemanticDiagnosticsAsync(stateSet, list, cancellationToken).ConfigureAwait(false);
// check whether compilation end code fix is enabled
if (!_document.Project.Solution.Workspace.Options.GetOption(InternalDiagnosticsOptions.CompilationEndCodeFix))
{
return;
}
// check whether heuristic is enabled
if (_blockForData && _document.Project.Solution.Workspace.Options.GetOption(InternalDiagnosticsOptions.UseCompilationEndCodeFixHeuristic))
{
var avoidLoadingData = true;
var state = stateSet.GetProjectState(_project.Id);
var result = await state.GetAnalysisDataAsync(_document, avoidLoadingData, cancellationToken).ConfigureAwait(false);
// no previous compilation end diagnostics in this file.
var version = await GetDiagnosticVersionAsync(_project, cancellationToken).ConfigureAwait(false);
if (state.IsEmpty(_document.Id) || result.Version != version)
{
return;
}
}
containsFullResult &= await TryGetProjectDiagnosticsAsync(stateSet, GetProjectDiagnosticsAsync, list, cancellationToken).ConfigureAwait(false);
}
}
private async Task<bool> TryGetSyntaxAndSemanticDiagnosticsAsync(StateSet stateSet, List<DiagnosticData> list, CancellationToken cancellationToken)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Threading;
using Microsoft.CodeAnalysis.PooledObjects;
namespace Microsoft.CodeAnalysis.Internal.Log
{
internal partial class RoslynEventSource
{
// Regardless of how many tasks we can run in parallel on the machine, we likely won't need more than 256
// instrumentation points in flight at a given time.
// Use an object pool since we may be logging up to 1-10k events/second
private static readonly ObjectPool<RoslynLogBlock> s_pool = new ObjectPool<RoslynLogBlock>(() => new RoslynLogBlock(s_pool), Math.Min(Environment.ProcessorCount * 8, 256));
/// <summary>
/// next unique block id that will be given to each LogBlock
/// Logs an informational block with given <paramref name="entity"/>'s <see cref="object.ToString"/> representation as the message
/// and specified <paramref name="functionId"/>.
/// On dispose of the returned disposable object, it logs the 'tick' count between the start and end of the block.
/// Unlike other logging methods on <see cref="RoslynEventSource"/>, this method does not check
/// if the specified <paramref name="functionId"/> was explicitly enabled.
/// Instead it checks if the <see cref="RoslynEventSource"/> was enabled at <see cref="EventLevel.Informational"/> level.
/// </summary>
private static int s_lastUniqueBlockId;
public static IDisposable LogInformationalBlock(FunctionId functionId, object entity, CancellationToken cancellationToken)
=> LogBlock.Create(functionId, entity, EventLevel.Informational, cancellationToken);
/// <summary>
/// Logs a block with the given <paramref name="message"/> and specified <paramref name="functionId"/>.
/// Logs an informational message block with the given <paramref name="message"/>> and specified <paramref name="functionId"/>.
/// On dispose of the returned disposable object, it logs the 'tick' count between the start and end of the block.
/// Unlike other logging methods on <see cref="RoslynEventSource"/>, this method does not check
/// if the specified <paramref name="functionId"/> was explicitly enabled.
/// Instead it checks if the <see cref="RoslynEventSource"/> was enabled at <see cref="EventLevel.Informational"/> level.
/// </summary>
public static IDisposable LogInformationalBlock(FunctionId functionId, string message, CancellationToken cancellationToken)
=> LogBlock(functionId, message, EventLevel.Informational, cancellationToken);
private static IDisposable LogBlock(FunctionId functionId, string message, EventLevel requiredEventLevel, CancellationToken cancellationToken)
{
if (!Instance.IsEnabled(requiredEventLevel, EventKeywords.None))
{
return EmptyLogBlock.Instance;
}
return CreateLogBlock(functionId, message, cancellationToken);
}
/// <summary>
/// return next unique pair id
/// </summary>
private static int GetNextUniqueBlockId()
{
return Interlocked.Increment(ref s_lastUniqueBlockId);
}
private static IDisposable CreateLogBlock(FunctionId functionId, string message, CancellationToken cancellationToken)
{
var block = s_pool.Allocate();
var blockId = GetNextUniqueBlockId();
block.Construct(functionId, message, blockId, cancellationToken);
return block;
}
=> LogBlock.Create(functionId, message, EventLevel.Informational, cancellationToken);
/// <summary>
/// This tracks the logged message. On instantiation, it logs 'Started block' with other event data.
/// On dispose, it logs 'Ended block' with the same event data so we can track which block started and ended when looking at logs.
/// </summary>
private class RoslynLogBlock : IDisposable
private struct LogBlock : IDisposable
{
private readonly ObjectPool<RoslynLogBlock> _pool;
private CancellationToken _cancellationToken;
private readonly FunctionId _functionId;
private readonly object? _entityForMessage;
private readonly EventLevel _eventLevel;
private readonly int _blockId;
private readonly CancellationToken _cancellationToken;
private FunctionId _functionId;
private int _tick;
private int _blockId;
public RoslynLogBlock(ObjectPool<RoslynLogBlock> pool)
private bool _startLogged;
private string? _message;
/// <summary>
/// next unique block id that will be given to each LogBlock
/// </summary>
private static int s_lastUniqueBlockId;
private LogBlock(
FunctionId functionId,
string? message,
object? entityForMessage,
EventLevel eventLevel,
int blockId,
CancellationToken cancellationToken)
{
_pool = pool;
}
Debug.Assert(message != null || entityForMessage != null);
public void Construct(FunctionId functionId, string message, int blockId, CancellationToken cancellationToken)
{
_functionId = functionId;
_tick = Environment.TickCount;
_message = message;
_entityForMessage = entityForMessage;
_eventLevel = eventLevel;
_blockId = blockId;
_cancellationToken = cancellationToken;
_tick = Environment.TickCount;
_startLogged = false;
}
Instance.BlockStart(message, functionId, blockId);
public static LogBlock Create(
FunctionId functionId,
object entityForMessage,
EventLevel eventLevel,
CancellationToken cancellationToken)
{
var blockId = GetNextUniqueBlockId();
var logBlock = new LogBlock(functionId, message: null, entityForMessage, eventLevel, blockId, cancellationToken);
logBlock.OnStart();
return logBlock;
}
public static LogBlock Create(
FunctionId functionId,
string message,
EventLevel eventLevel,
CancellationToken cancellationToken)
{
var blockId = GetNextUniqueBlockId();
var logBlock = new LogBlock(functionId, message, entityForMessage: null, eventLevel, blockId, cancellationToken);
logBlock.OnStart();
return logBlock;
}
/// <summary>
/// return next unique pair id
/// </summary>
private static int GetNextUniqueBlockId()
{
return Interlocked.Increment(ref s_lastUniqueBlockId);
}
private void OnStart()
{
if (EnsureMessageIfLoggingEnabled())
{
Debug.Assert(_message != null);
Debug.Assert(!_startLogged);
Instance.BlockStart(_message, _functionId, _blockId);
_startLogged = true;
}
}
private bool EnsureMessageIfLoggingEnabled()
{
if (Instance.IsEnabled(_eventLevel, EventKeywords.None))
{
_message ??= (_entityForMessage?.ToString() ?? string.Empty);
return true;
}
return false;
}
public void Dispose()
{
if (!EnsureMessageIfLoggingEnabled())
{
return;
}
if (!_startLogged)
{
// User enabled logging after the block start.
// We log a block start to log the message along with the block ID.
Instance.BlockStart(_message, _functionId, _blockId);
_startLogged = true;
}
Debug.Assert(_message != null);
// This delta is valid for durations of < 25 days
var delta = Environment.TickCount - _tick;
......@@ -96,9 +156,6 @@ public void Dispose()
{
Instance.BlockStop(_functionId, delta, _blockId);
}
// Free this block back to the pool
_pool.Free(this);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册