Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

1 change: 0 additions & 1 deletion src/Native/System.Private.CoreLib.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
project(System.Private.CoreLib.Native)

set(NATIVE_SOURCES
pal_datetime.cpp
pal_dynamicload.cpp
pal_environment.cpp
pal_errno.cpp
Expand Down
27 changes: 0 additions & 27 deletions src/Native/System.Private.CoreLib.Native/pal_datetime.cpp

This file was deleted.

146 changes: 0 additions & 146 deletions src/Native/System.Private.CoreLib.Native/pal_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,6 @@ mach_timebase_info_data_t *InitializeTimebaseInfo()
}
#endif

extern "C" uint64_t CoreLibNative_GetHighPrecisionCount()
{
uint64_t counts = 0;

#if HAVE_MACH_ABSOLUTE_TIME
{
counts = mach_absolute_time();
}
#elif HAVE_CLOCK_MONOTONIC
{
clockid_t clockType = CLOCK_MONOTONIC;
struct timespec ts;
if (clock_gettime(clockType, &ts) != 0)
{
assert(false);
return counts;
}
counts = ts.tv_sec * NanosecondsPerSecond + ts.tv_nsec;
}
#else
{
struct timeval tv;
if (gettimeofday(&tv, nullptr) == -1)
{
assert(false);
return counts;
}
counts = tv.tv_sec * MicrosecondsPerSecond + tv.tv_usec;
}
#endif
return counts;
}

#if HAVE_MACH_ABSOLUTE_TIME
static uint64_t s_highPrecisionCounterFrequency = 0;
#endif
extern "C" uint64_t CoreLibNative_GetHighPrecisionCounterFrequency()
{
#if HAVE_MACH_ABSOLUTE_TIME
if (s_highPrecisionCounterFrequency != 0)
{
return s_highPrecisionCounterFrequency;
}
{
mach_timebase_info_data_t *machTimebaseInfo = GetMachTimebaseInfo();
s_highPrecisionCounterFrequency = NanosecondsPerSecond * static_cast<uint64_t>(machTimebaseInfo->denom) / machTimebaseInfo->numer;
}
return s_highPrecisionCounterFrequency;
#elif HAVE_CLOCK_MONOTONIC_COARSE || HAVE_CLOCK_MONOTONIC
{
return NanosecondsPerSecond;
}
#else
{
return MicrosecondsPerSecond;
}
#endif
return 0;
}

#define SECONDS_TO_MILLISECONDS 1000
#define MILLISECONDS_TO_MICROSECONDS 1000
#define MILLISECONDS_TO_NANOSECONDS 1000000 // 10^6
Expand Down Expand Up @@ -141,89 +81,3 @@ extern "C" uint64_t CoreLibNative_GetTickCount64()
#endif
return retval;
}


struct ProcessCpuInformation
{
uint64_t lastRecordedCurrentTime;
uint64_t lastRecordedKernelTime;
uint64_t lastRecordedUserTime;
};


/*
Function:
CoreLibNative_GetCpuUtilization

The main purpose of this function is to compute the overall CPU utilization
for the CLR thread pool to regulate the number of worker threads.
Since there is no consistent API on Unix to get the CPU utilization
from a user process, getrusage and gettimeofday are used to
compute the current process's CPU utilization instead.

*/

static long numProcessors = 0;
extern "C" int32_t CoreLibNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
{
if (numProcessors <= 0)
{
numProcessors = sysconf(_SC_NPROCESSORS_CONF);
if (numProcessors <= 0)
{
return 0;
}
}

uint64_t kernelTime = 0;
uint64_t userTime = 0;

struct rusage resUsage;
if (getrusage(RUSAGE_SELF, &resUsage) == -1)
{
assert(false);
return 0;
}
else
{
kernelTime = TimeValToNanoseconds(resUsage.ru_stime);
userTime = TimeValToNanoseconds(resUsage.ru_utime);
}

uint64_t currentTime = CoreLibNative_GetHighPrecisionCount() * NanosecondsPerSecond / CoreLibNative_GetHighPrecisionCounterFrequency();

uint64_t lastRecordedCurrentTime = previousCpuInfo->lastRecordedCurrentTime;
uint64_t lastRecordedKernelTime = previousCpuInfo->lastRecordedKernelTime;
uint64_t lastRecordedUserTime = previousCpuInfo->lastRecordedUserTime;

uint64_t cpuTotalTime = 0;
if (currentTime > lastRecordedCurrentTime)
{
// cpuTotalTime is based on clock time. Since multiple threads can run in parallel,
// we need to scale cpuTotalTime cover the same amount of total CPU time.
// rusage time is already scaled across multiple processors.
cpuTotalTime = (currentTime - lastRecordedCurrentTime);
cpuTotalTime *= numProcessors;
}

uint64_t cpuBusyTime = 0;
if (userTime >= lastRecordedUserTime && kernelTime >= lastRecordedKernelTime)
{
cpuBusyTime = (userTime - lastRecordedUserTime) + (kernelTime - lastRecordedKernelTime);
}

int32_t cpuUtilization = 0;
if (cpuTotalTime > 0 && cpuBusyTime > 0)
{
cpuUtilization = static_cast<int32_t>(cpuBusyTime / cpuTotalTime);
}

assert(cpuUtilization >= 0 && cpuUtilization <= 100);

previousCpuInfo->lastRecordedCurrentTime = currentTime;
previousCpuInfo->lastRecordedUserTime = userTime;
previousCpuInfo->lastRecordedKernelTime = kernelTime;

return cpuUtilization;
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal struct ProcessCpuInformation
ulong lastRecordedUserTime;
}

[DllImport(Libraries.CoreLibNative, EntryPoint = "CoreLibNative_GetCpuUtilization")]
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetCpuUtilization")]
internal static extern unsafe int GetCpuUtilization(ref ProcessCpuInformation previousCpuInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static partial class Interop
{
internal unsafe partial class Sys
{
[DllImport(Interop.Libraries.CoreLibNative, EntryPoint = "CoreLibNative_GetSystemTimeAsTicks")]
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetSystemTimeAsTicks")]
internal static extern long GetSystemTimeAsTicks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1107,11 +1107,14 @@
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.FLock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.FSync.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.FTruncate.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetCpuUtilization.cs" Condition="'$(FeaturePortableThreadPool)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetCwd.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetEUid.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetHostName.cs" />
<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.GetUnixName.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetUnixRelease.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.LockFileRegion.cs" />
Expand Down
13 changes: 2 additions & 11 deletions src/System.Private.CoreLib/src/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@
<Compile Condition="'$(EnableWinRT)' != 'true'" Include="..\..\Common\src\Interop\Windows\mincore\Interop.DynamicLoad.cs">
<Link>Interop\Windows\mincore\Interop.DynamicLoad.cs</Link>
</Compile>
<Compile Include="System\HighPerformanceCounter.Windows.cs" />
<Compile Include="System\HighPerformanceCounter.Windows.cs" Condition="'$(FeaturePortableThreadPool)' == 'true'" />
<Compile Include="System\Threading\LowLevelMonitor.Windows.cs" />
<Compile Include="System\Threading\LowLevelLifoSemaphore.Windows.cs" />
<Compile Include="System\Threading\ThreadPool.Windows.cs" Condition="'$(FeaturePortableThreadPool)' != 'true'" />
Expand Down Expand Up @@ -461,7 +461,7 @@
<Compile Include="System\AppContext.Unix.CoreRT.cs" />
<Compile Include="System\DateTime.Unix.CoreRT.cs" />
<Compile Include="System\Environment.CoreRT.Unix.cs" />
<Compile Include="System\HighPerformanceCounter.Unix.cs" />
<Compile Include="System\HighPerformanceCounter.Unix.cs" Condition="'$(FeaturePortableThreadPool)' == 'true'" />
<Compile Include="System\Runtime\InteropServices\PInvokeMarshal.Unix.cs" />
<Compile Include="System\Threading\EventWaitHandle.Unix.cs" />
<Compile Include="System\Threading\LowLevelLifoSemaphore.Unix.cs" />
Expand All @@ -477,24 +477,15 @@
<Compile Include="..\..\Common\src\Interop\Unix\Interop.Libraries.cs">
<Link>Interop\Unix\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Native\Interop.GetSystemTimeAsTicks.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.GetSystemTimeAsTicks.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.Exit.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.Exit.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.GetCpuUtilization.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.GetCpuUtilization.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.GetEnv.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.GetEnv.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.GetExecutableAbsolutePath.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.GetExecutableAbsolutePath.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.HighPrecisionCounter.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.HighPrecisionCounter.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.MemAllocFree.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.MemAllocFree.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
// 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.GetHighPrecisionCount();
public static ulong TickCount
{
get
{
long tickCount;
bool success = Interop.Sys.GetTimestamp(out tickCount);
Debug.Assert(success);
return (ulong)tickCount;
}
}

public static ulong Frequency { get; } = GetFrequency();

// 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.GetHighPrecisionCounterFrequency();
private static ulong GetFrequency()
{
// Cache the frequency on the managed side to avoid the cost of P/Invoke on every access to Frequency
long frequency;
bool success = Interop.Sys.GetTimestampResolution(out frequency);
Debug.Assert(success);
return (ulong)frequency;
}
}
}