From 168a79e33fbe4ee16b87e297bd02fa8fb5c47104 Mon Sep 17 00:00:00 2001
From: Vladimir Sadov <vsadov@microsoft.com>
Date: Thu, 24 Oct 2019 17:55:33 -0700
Subject: [PATCH] Update Corelib to include Stopwatch (#27417)

* Update Corelib to include Stopwatch

* Delete HighPerformanceCounter
---
 .../System.Private.CoreLib.Shared.projitems   | 11 ++++----
 .../System/HighPerformanceCounter.Unix.cs     | 16 -----------
 .../System/HighPerformanceCounter.Windows.cs  | 28 -------------------
 .../System/Threading/PortableThreadPool.cs    |  9 +++---
 4 files changed, 11 insertions(+), 53 deletions(-)
 delete mode 100644 src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Unix.cs
 delete mode 100644 src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Windows.cs

diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
index ccf3d3f6741c1..813998ba6f9f0 100644
--- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
+++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
@@ -218,6 +218,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\StackTrace.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\StackTraceHiddenAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\SymbolStore\ISymbolDocumentWriter.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Stopwatch.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\DivideByZeroException.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\DllNotFoundException.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Double.cs" />
@@ -1087,8 +1088,8 @@
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.MEMORYSTATUSEX.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.MultiByteToWideChar.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.OutputDebugString.cs" />
-    <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.QueryPerformanceCounter.cs" Condition="'$(FeaturePortableThreadPool)' == 'true'" />
-    <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.QueryPerformanceFrequency.cs" Condition="'$(FeaturePortableThreadPool)' == 'true'" />
+    <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.QueryPerformanceCounter.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.QueryPerformanceFrequency.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.QueryUnbiasedInterruptTime.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_NativeOverlapped.cs" />
@@ -1123,6 +1124,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)System\DateTime.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Environment.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\DebugProvider.Windows.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Stopwatch.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CalendarData.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureInfo.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareInfo.Windows.cs" />
@@ -1247,7 +1249,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetPwUid.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetRandomBytes.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetSystemTimeAsTicks.cs" />
-    <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetTimestamp.cs" Condition="'$(FeaturePortableThreadPool)' == 'true'" />
+    <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetTimestamp.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetUnixName.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetUnixRelease.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.LockFileRegion.cs" />
@@ -1273,6 +1275,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)System\Buffer.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\DateTime.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\DebugProvider.Unix.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Stopwatch.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Environment.NoRegistry.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Environment.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CalendarData.Unix.cs" />
@@ -1368,8 +1371,6 @@
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetSystemTimes.cs" Condition="'$(TargetsWindows)' == 'true'" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.Windows.cs" Condition="'$(TargetsWindows)'=='true'" />
-    <Compile Include="$(MSBuildThisFileDirectory)System\HighPerformanceCounter.Unix.cs" Condition="'$(TargetsUnix)' == 'true'" />
-    <Compile Include="$(MSBuildThisFileDirectory)System\HighPerformanceCounter.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
   </ItemGroup>
   <ItemGroup Condition="'$(FeaturePortableThreadPool)' == 'true' OR '$(FeatureThreadInt64PersistentCounter)' == 'true'">
     <Compile Include="$(MSBuildThisFileDirectory)System\Threading\ThreadInt64PersistentCounter.cs" />
diff --git a/src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Unix.cs b/src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Unix.cs
deleted file mode 100644
index cfe582ddfdc73..0000000000000
--- a/src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Unix.cs
+++ /dev/null
@@ -1,16 +0,0 @@
-// 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.Diagnostics;
-
-namespace System
-{
-    internal static class HighPerformanceCounter
-    {
-        public static ulong TickCount => Interop.Sys.GetTimestamp();
-
-        // Cache the frequency on the managed side to avoid the cost of P/Invoke on every access to Frequency
-        public static ulong Frequency { get; } = Interop.Sys.GetTimestampResolution();
-    }
-}
diff --git a/src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Windows.cs b/src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Windows.cs
deleted file mode 100644
index 1bd40ab843ac8..0000000000000
--- a/src/System.Private.CoreLib/shared/System/HighPerformanceCounter.Windows.cs
+++ /dev/null
@@ -1,28 +0,0 @@
-// 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.
-
-namespace System
-{
-    internal static unsafe class HighPerformanceCounter
-    {
-        public static ulong TickCount
-        {
-            get
-            {
-                long counter;
-                Interop.Kernel32.QueryPerformanceCounter(&counter);
-                return (ulong)counter;
-            }
-        }
-
-        public static ulong Frequency { get; } = GetFrequency();
-
-        private static ulong GetFrequency()
-        {
-            long frequency;
-            Interop.Kernel32.QueryPerformanceFrequency(&frequency);
-            return (ulong)frequency;
-        }
-    }
-}
diff --git a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs
index aad21dfa5e384..3ea1c15b9b321 100644
--- a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs
+++ b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs
@@ -2,6 +2,7 @@
 // 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.Diagnostics;
 using System.Runtime.InteropServices;
 
 namespace System.Threading
@@ -57,7 +58,7 @@ private struct CacheLineSeparated
         }
 
         private CacheLineSeparated _separated;
-        private ulong _currentSampleStartTime;
+        private long _currentSampleStartTime;
         private readonly ThreadInt64PersistentCounter _completionCounter = new ThreadInt64PersistentCounter();
         private int _threadAdjustmentIntervalMs;
 
@@ -225,9 +226,9 @@ private void AdjustMaxWorkersActive()
             int currentTicks = Environment.TickCount;
             int totalNumCompletions = (int)_completionCounter.Count;
             int numCompletions = totalNumCompletions - _separated.priorCompletionCount;
-            ulong startTime = _currentSampleStartTime;
-            ulong endTime = HighPerformanceCounter.TickCount;
-            ulong freq = HighPerformanceCounter.Frequency;
+            long startTime = _currentSampleStartTime;
+            long endTime = Stopwatch.GetTimestamp();
+            long freq = Stopwatch.Frequency;
 
             double elapsedSeconds = (double)(endTime - startTime) / freq;