Skip to content

Commit b73113f

Browse files
huoyaoyuanjkotas
authored andcommitted
Consolidate QueryPerformanceCounter/GetTickCount usages to minipal/time.h (#115408)
Remove the duplicated implementations and use minipal/time.h as single source of truth. Not touching a bit ifdef'd-out instances, which are supposed for local debug usages only. Co-authored-by: Jan Kotas <[email protected]>
1 parent f5dc18c commit b73113f

File tree

127 files changed

+404
-1316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+404
-1316
lines changed

src/coreclr/System.Private.CoreLib/src/System/Environment.CoreCLR.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,5 @@ private static unsafe string[] InitializeCommandLineArgs(char* exePath, int argc
108108

109109
// Used by VM
110110
internal static string? GetResourceStringLocal(string key) => SR.GetResourceString(key);
111-
112-
/// <summary>Gets the number of milliseconds elapsed since the system started.</summary>
113-
/// <value>A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.</value>
114-
public static extern int TickCount
115-
{
116-
[MethodImpl(MethodImplOptions.InternalCall)]
117-
get;
118-
}
119-
120-
/// <summary>Gets the number of milliseconds elapsed since the system started.</summary>
121-
/// <value>A 64-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.</value>
122-
public static extern long TickCount64
123-
{
124-
[MethodImpl(MethodImplOptions.InternalCall)]
125-
get;
126-
}
127111
}
128112
}

src/coreclr/classlibnative/bcltype/system.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,6 @@
3232

3333
#include <minipal/cpuid.h>
3434

35-
36-
FCIMPL0(UINT32, SystemNative::GetTickCount)
37-
{
38-
FCALL_CONTRACT;
39-
40-
return ::GetTickCount();
41-
}
42-
FCIMPLEND;
43-
44-
FCIMPL0(UINT64, SystemNative::GetTickCount64)
45-
{
46-
FCALL_CONTRACT;
47-
48-
return ::GetTickCount64();
49-
}
50-
FCIMPLEND;
51-
52-
5335
extern "C" VOID QCALLTYPE Environment_Exit(INT32 exitcode)
5436
{
5537
QCALL_CONTRACT;

src/coreclr/classlibnative/bcltype/system.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class SystemNative
3737

3838
public:
3939
// Functions on the System.Environment class
40-
static FCDECL0(UINT32, GetTickCount);
41-
static FCDECL0(UINT64, GetTickCount64);
42-
4340
static FCDECL1(VOID,SetExitCode,INT32 exitcode);
4441
static FCDECL0(INT32, GetExitCode);
4542

src/coreclr/debug/createdump/config.h.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@
44
#pragma once
55

66
#cmakedefine HAVE_PROCESS_VM_READV
7-
#cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP
8-
#cmakedefine01 HAVE_CLOCK_MONOTONIC
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
11
check_function_exists(process_vm_readv HAVE_PROCESS_VM_READV)
22

3-
check_symbol_exists(
4-
clock_gettime_nsec_np
5-
time.h
6-
HAVE_CLOCK_GETTIME_NSEC_NP)
7-
8-
check_cxx_source_runs("
9-
#include <stdlib.h>
10-
#include <time.h>
11-
#include <sys/time.h>
12-
13-
int main()
14-
{
15-
int ret;
16-
struct timespec ts;
17-
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
18-
19-
exit(ret);
20-
}" HAVE_CLOCK_MONOTONIC)
21-
223
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

src/coreclr/debug/createdump/createdumpmain.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#include "createdump.h"
5+
#include "minipal/time.h"
56

67
#ifdef HOST_WINDOWS
78
#define DEFAULT_DUMP_PATH "%TEMP%\\"
@@ -45,8 +46,6 @@ bool g_diagnostics = false;
4546
bool g_diagnosticsVerbose = false;
4647
uint64_t g_ticksPerMS = 0;
4748
uint64_t g_startTime = 0;
48-
uint64_t GetTickFrequency();
49-
uint64_t GetTimeStamp();
5049

5150
//
5251
// Common entry point
@@ -198,8 +197,8 @@ int createdump_main(const int argc, const char* argv[])
198197
return -1;
199198
}
200199

201-
g_ticksPerMS = GetTickFrequency() / 1000UL;
202-
g_startTime = GetTimeStamp();
200+
g_ticksPerMS = minipal_hires_tick_frequency() / 1000UL;
201+
g_startTime = minipal_hires_ticks();
203202
TRACE("TickFrequency: %d ticks per ms\n", g_ticksPerMS);
204203

205204
ArrayHolder<char> tmpPath = new char[MAX_LONGPATH];
@@ -221,11 +220,11 @@ int createdump_main(const int argc, const char* argv[])
221220

222221
if (CreateDump(options))
223222
{
224-
printf_status("Dump successfully written in %llums\n", GetTimeStamp() - g_startTime);
223+
printf_status("Dump successfully written in %llums\n", (minipal_hires_ticks() - g_startTime) / g_ticksPerMS);
225224
}
226225
else
227226
{
228-
printf_error("Failure took %llums\n", GetTimeStamp() - g_startTime);
227+
printf_error("Failure took %llums\n", (minipal_hires_ticks() - g_startTime) / g_ticksPerMS);
229228
exitCode = -1;
230229
}
231230

@@ -332,24 +331,6 @@ printf_error(const char* format, ...)
332331
va_end(args);
333332
}
334333

335-
uint64_t
336-
GetTickFrequency()
337-
{
338-
LARGE_INTEGER ret;
339-
ZeroMemory(&ret, sizeof(LARGE_INTEGER));
340-
QueryPerformanceFrequency(&ret);
341-
return ret.QuadPart;
342-
}
343-
344-
uint64_t
345-
GetTimeStamp()
346-
{
347-
LARGE_INTEGER ret;
348-
ZeroMemory(&ret, sizeof(LARGE_INTEGER));
349-
QueryPerformanceCounter(&ret);
350-
return ret.QuadPart / g_ticksPerMS;
351-
}
352-
353334
#ifdef HOST_UNIX
354335

355336
static void
@@ -360,7 +341,7 @@ trace_prefix(const char* format, va_list args)
360341
{
361342
fprintf(g_stdout, "[createdump] ");
362343
}
363-
fprintf(g_stdout, "%08" PRIx64 " ", GetTimeStamp());
344+
fprintf(g_stdout, "%08" PRIx64 " ", minipal_hires_ticks() / g_ticksPerMS);
364345
vfprintf(g_stdout, format, args);
365346
fflush(g_stdout);
366347
}

src/coreclr/debug/createdump/createdumppal.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,52 +96,6 @@ UninitializePAL(
9696
}
9797
}
9898

99-
#define tccSecondsToNanoSeconds 1000000000 // 10^9
100-
101-
BOOL
102-
PALAPI
103-
QueryPerformanceCounter(
104-
OUT LARGE_INTEGER* lpPerformanceCount)
105-
{
106-
#if HAVE_CLOCK_GETTIME_NSEC_NP
107-
lpPerformanceCount->QuadPart = (LONGLONG)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
108-
#elif HAVE_CLOCK_MONOTONIC
109-
struct timespec ts;
110-
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
111-
if (result != 0)
112-
{
113-
return TRUE;
114-
}
115-
else
116-
{
117-
lpPerformanceCount->QuadPart = ((LONGLONG)(ts.tv_sec) * (LONGLONG)(tccSecondsToNanoSeconds)) + (LONGLONG)(ts.tv_nsec);
118-
}
119-
#else
120-
#error "The createdump requires either mach_absolute_time() or clock_gettime(CLOCK_MONOTONIC) to be supported."
121-
#endif
122-
return TRUE;
123-
}
124-
125-
BOOL
126-
PALAPI
127-
QueryPerformanceFrequency(
128-
OUT LARGE_INTEGER* lpFrequency)
129-
{
130-
#if HAVE_CLOCK_GETTIME_NSEC_NP
131-
lpFrequency->QuadPart = (LONGLONG)(tccSecondsToNanoSeconds);
132-
#elif HAVE_CLOCK_MONOTONIC
133-
// clock_gettime() returns a result in terms of nanoseconds rather than a count. This
134-
// means that we need to either always scale the result by the actual resolution (to
135-
// get a count) or we need to say the resolution is in terms of nanoseconds. We prefer
136-
// the latter since it allows the highest throughput and should minimize error propagated
137-
// to the user.
138-
lpFrequency->QuadPart = (LONGLONG)(tccSecondsToNanoSeconds);
139-
#else
140-
#error "The createdump requires either mach_absolute_time() or clock_gettime(CLOCK_MONOTONIC) to be supported."
141-
#endif
142-
return TRUE;
143-
}
144-
14599
#define TEMP_DIRECTORY_PATH "/tmp/"
146100

147101
DWORD

src/coreclr/dlls/mscordac/mscordac_unixexports.src

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ nativeStringResourceTable_mscorrc
113113
#OutputDebugStringW
114114
#OpenEventW
115115
#OutputDebugStringA
116-
#QueryPerformanceCounter
117-
#QueryPerformanceFrequency
118116
#RaiseException
119117
#RaiseFailFastException
120118
#ReadFile

src/coreclr/gc/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ if(CLR_CMAKE_HOST_WIN32)
8585
advapi32.lib)
8686
endif(CLR_CMAKE_HOST_WIN32)
8787

88-
set (GC_LINK_LIBRARIES ${GC_LINK_LIBRARIES} gc_pal)
88+
set (GC_LINK_LIBRARIES
89+
${GC_LINK_LIBRARIES}
90+
gc_pal
91+
minipal)
8992

9093
if(CLR_CMAKE_TARGET_ARCH_AMD64)
9194
list(APPEND GC_LINK_LIBRARIES
9295
gc_vxsort
93-
minipal
9496
)
9597
endif(CLR_CMAKE_TARGET_ARCH_AMD64)
9698

src/coreclr/gc/unix/config.gc.h.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#cmakedefine01 HAVE_SYSCTLBYNAME
1818
#cmakedefine01 HAVE_PTHREAD_CONDATTR_SETCLOCK
1919
#cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP
20-
#cmakedefine01 HAVE_CLOCK_MONOTONIC
2120
#cmakedefine01 HAVE_SCHED_GETAFFINITY
2221
#cmakedefine01 HAVE_SCHED_SETAFFINITY
2322
#cmakedefine01 HAVE_PTHREAD_SETAFFINITY_NP

src/coreclr/gc/unix/configure.cmake

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,6 @@ check_symbol_exists(
8989
time.h
9090
HAVE_CLOCK_GETTIME_NSEC_NP)
9191

92-
check_cxx_source_runs("
93-
#include <stdlib.h>
94-
#include <time.h>
95-
#include <sys/time.h>
96-
97-
int main()
98-
{
99-
int ret;
100-
struct timespec ts;
101-
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
102-
103-
exit(ret);
104-
}" HAVE_CLOCK_MONOTONIC)
105-
10692
check_symbol_exists(
10793
posix_madvise
10894
sys/mman.h

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "gcconfig.h"
2525
#include "numasupport.h"
2626
#include <minipal/thread.h>
27+
#include <minipal/time.h>
2728

2829
#if HAVE_SWAPCTL
2930
#include <sys/swap.h>
@@ -1449,22 +1450,7 @@ void GCToOSInterface::GetMemoryStatus(uint64_t restricted_limit, uint32_t* memor
14491450
// The counter value
14501451
int64_t GCToOSInterface::QueryPerformanceCounter()
14511452
{
1452-
#if HAVE_CLOCK_GETTIME_NSEC_NP
1453-
return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
1454-
#elif HAVE_CLOCK_MONOTONIC
1455-
struct timespec ts;
1456-
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
1457-
1458-
if (result != 0)
1459-
{
1460-
assert(!"clock_gettime(CLOCK_MONOTONIC) failed");
1461-
__UNREACHABLE();
1462-
}
1463-
1464-
return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec);
1465-
#else
1466-
#error " clock_gettime(CLOCK_MONOTONIC) or clock_gettime_nsec_np() must be supported."
1467-
#endif
1453+
return minipal_hires_ticks();
14681454
}
14691455

14701456
// Get a frequency of the high precision performance counter
@@ -1473,50 +1459,15 @@ int64_t GCToOSInterface::QueryPerformanceCounter()
14731459
int64_t GCToOSInterface::QueryPerformanceFrequency()
14741460
{
14751461
// The counter frequency of gettimeofday is in microseconds.
1476-
return tccSecondsToNanoSeconds;
1462+
return minipal_hires_tick_frequency();
14771463
}
14781464

14791465
// Get a time stamp with a low precision
14801466
// Return:
14811467
// Time stamp in milliseconds
14821468
uint64_t GCToOSInterface::GetLowPrecisionTimeStamp()
14831469
{
1484-
uint64_t retval = 0;
1485-
1486-
#if HAVE_CLOCK_GETTIME_NSEC_NP
1487-
retval = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / tccMilliSecondsToNanoSeconds;
1488-
#elif HAVE_CLOCK_MONOTONIC
1489-
struct timespec ts;
1490-
1491-
#if HAVE_CLOCK_MONOTONIC_COARSE
1492-
clockid_t clockType = CLOCK_MONOTONIC_COARSE; // good enough resolution, fastest speed
1493-
#else
1494-
clockid_t clockType = CLOCK_MONOTONIC;
1495-
#endif
1496-
1497-
if (clock_gettime(clockType, &ts) != 0)
1498-
{
1499-
#if HAVE_CLOCK_MONOTONIC_COARSE
1500-
assert(!"clock_gettime(HAVE_CLOCK_MONOTONIC_COARSE) failed\n");
1501-
#else
1502-
assert(!"clock_gettime(CLOCK_MONOTONIC) failed\n");
1503-
#endif
1504-
}
1505-
1506-
retval = (ts.tv_sec * tccSecondsToMilliSeconds) + (ts.tv_nsec / tccMilliSecondsToNanoSeconds);
1507-
#else
1508-
struct timeval tv;
1509-
if (gettimeofday(&tv, NULL) == 0)
1510-
{
1511-
retval = (tv.tv_sec * tccSecondsToMilliSeconds) + (tv.tv_usec / tccMilliSecondsToMicroSeconds);
1512-
}
1513-
else
1514-
{
1515-
assert(!"gettimeofday() failed\n");
1516-
}
1517-
#endif
1518-
1519-
return retval;
1470+
return (uint64_t)minipal_lowres_ticks();
15201471
}
15211472

15221473
// Gets the total number of processors on the machine, not taking

src/coreclr/ilasm/assembler.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -644,22 +644,22 @@ typedef FIFO<MethodBody> MethodBodyList;
644644

645645
struct Clockwork
646646
{
647-
DWORD cBegin;
648-
DWORD cEnd;
649-
DWORD cParsBegin;
650-
DWORD cParsEnd;
651-
DWORD cMDInitBegin;
652-
DWORD cMDInitEnd;
653-
DWORD cMDEmitBegin;
654-
DWORD cMDEmitEnd;
655-
DWORD cMDEmit1;
656-
DWORD cMDEmit2;
657-
DWORD cMDEmit3;
658-
DWORD cMDEmit4;
659-
DWORD cRef2DefBegin;
660-
DWORD cRef2DefEnd;
661-
DWORD cFilegenBegin;
662-
DWORD cFilegenEnd;
647+
int64_t cBegin;
648+
int64_t cEnd;
649+
int64_t cParsBegin;
650+
int64_t cParsEnd;
651+
int64_t cMDInitBegin;
652+
int64_t cMDInitEnd;
653+
int64_t cMDEmitBegin;
654+
int64_t cMDEmitEnd;
655+
int64_t cMDEmit1;
656+
int64_t cMDEmit2;
657+
int64_t cMDEmit3;
658+
int64_t cMDEmit4;
659+
int64_t cRef2DefBegin;
660+
int64_t cRef2DefEnd;
661+
int64_t cFilegenBegin;
662+
int64_t cFilegenEnd;
663663
};
664664

665665
struct TypeDefDescr

0 commit comments

Comments
 (0)