Skip to content

Commit

Permalink
Replace PooledStopwatch with SharedStopwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jan 24, 2020
1 parent 6cb854a commit 775c4eb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
67 changes: 67 additions & 0 deletions src/Compilers/Core/Portable/InternalUtilities/SharedStopwatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
41 changes: 0 additions & 41 deletions src/Dependencies/PooledObjects/PooledStopwatch.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 775c4eb

Please sign in to comment.