提交 775c4ebc 编写于 作者: S Sam Harwell

Replace PooledStopwatch with SharedStopwatch

上级 6cb854a5
......@@ -1448,10 +1448,10 @@ private void ExecuteAndCatchIfThrows_NoLock<TArg>(DiagnosticAnalyzer analyzer, A
{
_cancellationToken.ThrowIfCancellationRequested();
PooledStopwatch timer = null;
SharedStopwatch timer = default;
if (_analyzerExecutionTimeMapOpt != null)
{
timer = PooledStopwatch.StartInstance();
timer = SharedStopwatch.StartNew();
// This call to Restart isn't required by the API, but is included to avoid measurement errors which
// can occur during periods of high allocation activity. In some cases, calls to Stopwatch
......@@ -1468,12 +1468,11 @@ private void ExecuteAndCatchIfThrows_NoLock<TArg>(DiagnosticAnalyzer analyzer, A
analyze(argument);
if (timer != null)
if (_analyzerExecutionTimeMapOpt != null)
{
timer.Stop();
var elapsed = timer.Elapsed.Ticks;
StrongBox<long> totalTicks = _analyzerExecutionTimeMapOpt.GetOrAdd(analyzer, _ => new StrongBox<long>(0));
Interlocked.Add(ref totalTicks.Value, timer.Elapsed.Ticks);
timer.Free();
Interlocked.Add(ref totalTicks.Value, elapsed);
}
}
catch (Exception e) when (ExceptionFilter(e))
......
// 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;
namespace Roslyn.Utilities
{
internal struct SharedStopwatch
{
private static readonly Stopwatch s_stopwatch = Stopwatch.StartNew();
private TimeSpan _accumulated;
private TimeSpan _started;
public readonly TimeSpan Elapsed
{
get
{
if (IsRunning)
return _accumulated + s_stopwatch.Elapsed - _started;
return _accumulated;
}
}
public readonly long ElapsedMilliseconds => (long)Elapsed.TotalMilliseconds;
public readonly bool IsRunning => _started != TimeSpan.Zero;
public static SharedStopwatch StartNew()
{
var result = new SharedStopwatch();
result.Start();
return result;
}
public void Reset()
{
Stop();
_accumulated = TimeSpan.Zero;
}
public void Restart()
{
Reset();
Start();
}
public void Start()
{
if (!IsRunning)
{
_started = s_stopwatch.Elapsed;
}
}
public void Stop()
{
if (IsRunning)
{
_accumulated += s_stopwatch.Elapsed - _started;
_started = TimeSpan.Zero;
}
}
}
}
......@@ -15,7 +15,6 @@
<Compile Include="$(MSBuildThisFileDirectory)ObjectPool`1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledDictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledHashSet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledStopwatch.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledStringBuilder.cs" />
</ItemGroup>
</Project>
\ No newline at end of file
// 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;
namespace Microsoft.CodeAnalysis.PooledObjects
{
internal class PooledStopwatch : Stopwatch
{
private static readonly ObjectPool<PooledStopwatch> s_poolInstance = CreatePool();
private readonly ObjectPool<PooledStopwatch> _pool;
private PooledStopwatch(ObjectPool<PooledStopwatch> pool)
{
_pool = pool;
}
public void Free()
{
Reset();
_pool?.Free(this);
}
public static ObjectPool<PooledStopwatch> CreatePool()
{
ObjectPool<PooledStopwatch> pool = null;
pool = new ObjectPool<PooledStopwatch>(() => new PooledStopwatch(pool), 128);
return pool;
}
public static PooledStopwatch StartInstance()
{
var instance = s_poolInstance.Allocate();
instance.Restart();
return instance;
}
}
}
......@@ -27,6 +27,7 @@
<ItemGroup Label="Linked Files">
<Compile Remove="Storage\Sqlite\**\*.cs" Condition="'$(DotNetBuildFromSource)' == 'true'" />
<Compile Include="..\..\..\CodeStyle\Core\CodeFixes\FixAllContextHelper.cs" Link="CodeFixes\FixAllOccurrences\FixAllContextHelper.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\SharedStopwatch.cs" Link="InternalUtilities\SharedStopwatch.cs" />
<Compile Include="..\..\..\Compilers\Shared\DesktopAnalyzerAssemblyLoader.cs">
<Link>Execution\Desktop\DesktopAnalyzerAssemblyLoader.cs</Link>
</Compile>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册