Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of DateTime.UtcNow on Windows #44771

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@
<Compile Include="$(BclSourcesRoot)\System\Threading\LowLevelLifoSemaphore.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="$(BclSourcesRoot)\System\DateTime.Windows.CoreCLR.cs" />
<Compile Include="$(CommonPath)Interop\Windows\OleAut32\Interop.VariantClear.cs">
<Link>Common\Interop\Windows\OleAut32\Interop.VariantClear.cs</Link>
</Compile>
Expand Down

This file was deleted.

139 changes: 1 addition & 138 deletions src/coreclr/src/classlibnative/bcltype/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,69 +30,12 @@
#include "array.h"
#include "eepolicy.h"

#ifndef TARGET_UNIX
typedef void(WINAPI *pfnGetSystemTimeAsFileTime)(LPFILETIME lpSystemTimeAsFileTime);
extern pfnGetSystemTimeAsFileTime g_pfnGetSystemTimeAsFileTime;

void WINAPI InitializeGetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime)
{
pfnGetSystemTimeAsFileTime func = NULL;

HMODULE hKernel32 = WszLoadLibrary(W("kernel32.dll"));
if (hKernel32 != NULL)
{
func = (pfnGetSystemTimeAsFileTime)GetProcAddress(hKernel32, "GetSystemTimePreciseAsFileTime");
if (func != NULL)
{
// GetSystemTimePreciseAsFileTime exists and we'd like to use it. However, on
// misconfigured systems, it's possible for the "precise" time to be inaccurate:
// https://github.com/dotnet/runtime/issues/9014
// If it's inaccurate, though, we expect it to be wildly inaccurate, so as a
// workaround/heuristic, we get both the "normal" and "precise" times, and as
// long as they're close, we use the precise one. This workaround can be removed
// when we better understand what's causing the drift and the issue is no longer
// a problem or can be better worked around on all targeted OSes.

FILETIME systemTimeResult;
::GetSystemTimeAsFileTime(&systemTimeResult);

FILETIME preciseSystemTimeResult;
func(&preciseSystemTimeResult);

LONG64 systemTimeLong100ns = (LONG64)((((ULONG64)systemTimeResult.dwHighDateTime) << 32) | (ULONG64)systemTimeResult.dwLowDateTime);
LONG64 preciseSystemTimeLong100ns = (LONG64)((((ULONG64)preciseSystemTimeResult.dwHighDateTime) << 32) | (ULONG64)preciseSystemTimeResult.dwLowDateTime);

const INT32 THRESHOLD_100NS = 1000000; // 100ms
if (abs(preciseSystemTimeLong100ns - systemTimeLong100ns) > THRESHOLD_100NS)
{
// Too much difference. Don't use GetSystemTimePreciseAsFileTime.
func = NULL;
}
}
}
if (func == NULL)
{
func = &::GetSystemTimeAsFileTime;
}

InterlockedCompareExchangeT(&g_pfnGetSystemTimeAsFileTime, func, &InitializeGetSystemTimeAsFileTime);

g_pfnGetSystemTimeAsFileTime(lpSystemTimeAsFileTime);
}

pfnGetSystemTimeAsFileTime g_pfnGetSystemTimeAsFileTime = &InitializeGetSystemTimeAsFileTime;
#endif // TARGET_UNIX

#ifdef TARGET_UNIX
FCIMPL0(INT64, SystemNative::__GetSystemTimeAsFileTime)
{
FCALL_CONTRACT;

INT64 timestamp;
#ifndef TARGET_UNIX
g_pfnGetSystemTimeAsFileTime((FILETIME*)&timestamp);
#else
GetSystemTimeAsFileTime((FILETIME*)&timestamp);
#endif

#if BIGENDIAN
timestamp = (INT64)(((UINT64)timestamp >> 32) | ((UINT64)timestamp << 32));
Expand All @@ -101,88 +44,8 @@ FCIMPL0(INT64, SystemNative::__GetSystemTimeAsFileTime)
return timestamp;
}
FCIMPLEND;


#ifndef TARGET_UNIX

FCIMPL1(VOID, SystemNative::GetSystemTimeWithLeapSecondsHandling, FullSystemTime *time)
{
FCALL_CONTRACT;
INT64 timestamp;

g_pfnGetSystemTimeAsFileTime((FILETIME*)&timestamp);

if (::FileTimeToSystemTime((FILETIME*)&timestamp, &(time->systemTime)))
{
// to keep the time precision
time->hundredNanoSecond = timestamp % 10000; // 10000 is the number of 100-nano seconds per Millisecond
}
else
{
::GetSystemTime(&(time->systemTime));
time->hundredNanoSecond = 0;
}

if (time->systemTime.wSecond > 59)
{
// we have a leap second, force it to last second in the minute as DateTime doesn't account for leap seconds in its calculation.
// we use the maxvalue from the milliseconds and the 100-nano seconds to avoid reporting two out of order 59 seconds
time->systemTime.wSecond = 59;
time->systemTime.wMilliseconds = 999;
time->hundredNanoSecond = 9999;
}
}
FCIMPLEND;

FCIMPL2(FC_BOOL_RET, SystemNative::FileTimeToSystemTime, INT64 fileTime, FullSystemTime *time)
{
FCALL_CONTRACT;
if (::FileTimeToSystemTime((FILETIME*)&fileTime, (LPSYSTEMTIME) time))
{
// to keep the time precision
time->hundredNanoSecond = fileTime % 10000; // 10000 is the number of 100-nano seconds per Millisecond
if (time->systemTime.wSecond > 59)
{
// we have a leap second, force it to last second in the minute as DateTime doesn't account for leap seconds in its calculation.
// we use the maxvalue from the milliseconds and the 100-nano seconds to avoid reporting two out of order 59 seconds
time->systemTime.wSecond = 59;
time->systemTime.wMilliseconds = 999;
time->hundredNanoSecond = 9999;
}
FC_RETURN_BOOL(TRUE);
}
FC_RETURN_BOOL(FALSE);
}
FCIMPLEND;

FCIMPL2(FC_BOOL_RET, SystemNative::ValidateSystemTime, SYSTEMTIME *time, CLR_BOOL localTime)
{
FCALL_CONTRACT;

if (localTime)
{
SYSTEMTIME st;
FC_RETURN_BOOL(::TzSpecificLocalTimeToSystemTime(NULL, time, &st));
}
else
{
FILETIME timestamp;
FC_RETURN_BOOL(::SystemTimeToFileTime(time, &timestamp));
}
}
FCIMPLEND;

FCIMPL2(FC_BOOL_RET, SystemNative::SystemTimeToFileTime, SYSTEMTIME *time, INT64 *pFileTime)
{
FCALL_CONTRACT;

BOOL ret = ::SystemTimeToFileTime(time, (LPFILETIME) pFileTime);
FC_RETURN_BOOL(ret);
}
FCIMPLEND;
#endif // TARGET_UNIX


FCIMPL0(UINT32, SystemNative::GetTickCount)
{
FCALL_CONTRACT;
Expand Down
8 changes: 2 additions & 6 deletions src/coreclr/src/classlibnative/bcltype/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ class SystemNative

public:
// Functions on the System.Environment class
#ifndef TARGET_UNIX
static FCDECL1(VOID, GetSystemTimeWithLeapSecondsHandling, FullSystemTime *time);
static FCDECL2(FC_BOOL_RET, ValidateSystemTime, SYSTEMTIME *time, CLR_BOOL localTime);
static FCDECL2(FC_BOOL_RET, FileTimeToSystemTime, INT64 fileTime, FullSystemTime *time);
static FCDECL2(FC_BOOL_RET, SystemTimeToFileTime, SYSTEMTIME *time, INT64 *pFileTime);
#endif // TARGET_UNIX
#ifdef TARGET_UNIX
static FCDECL0(INT64, __GetSystemTimeAsFileTime);
#endif // TARGET_UNIX
static FCDECL0(UINT32, GetTickCount);
static FCDECL0(UINT64, GetTickCount64);

Expand Down
8 changes: 2 additions & 6 deletions src/coreclr/src/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,9 @@ FCFuncStart(gDiagnosticsStackTrace)
FCFuncEnd()

FCFuncStart(gDateTimeFuncs)
#if !defined(TARGET_UNIX)
FCFuncElement("GetSystemTimeWithLeapSecondsHandling", SystemNative::GetSystemTimeWithLeapSecondsHandling)
FCFuncElement("ValidateSystemTime", SystemNative::ValidateSystemTime)
FCFuncElement("FileTimeToSystemTime", SystemNative::FileTimeToSystemTime)
FCFuncElement("SystemTimeToFileTime", SystemNative::SystemTimeToFileTime)
#endif // TARGET_UNIX
#if defined(TARGET_UNIX)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be trivial to enable the managed implementation on Unix too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas The Unix implementation is here, but I don't know how to translate this to C#/CoreLib, especially with that ifdef. Do you have a suggestion for who on the team I can lean on for expertise here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FCFuncElement("GetSystemTimeAsFileTime", SystemNative::__GetSystemTimeAsFileTime)
#endif // TARGET_UNIX
FCFuncEnd()

FCFuncStart(gEnvironmentFuncs)
Expand Down
Loading