Skip to content

Consolidate QueryPerformanceCounter/GetTickCount usages to minipal/time.h #115408

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

Merged
merged 54 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
9cf6494
createdump
huoyaoyuan May 8, 2025
e8c9873
random
huoyaoyuan May 8, 2025
4f61026
jit
huoyaoyuan May 8, 2025
aa05c17
timers
huoyaoyuan May 8, 2025
10f69e0
stresslog and ep
huoyaoyuan May 8, 2025
9e7e1fc
misc vm
huoyaoyuan May 8, 2025
e619b68
nativeaot
huoyaoyuan May 8, 2025
3d32622
gc
huoyaoyuan May 8, 2025
a1e581c
Cleanup from pal
huoyaoyuan May 8, 2025
26b3cb0
SystemNative
huoyaoyuan May 8, 2025
e5e54be
Don't use implicit cast in random.h
huoyaoyuan May 8, 2025
d9ad203
format
huoyaoyuan May 8, 2025
2258a58
Add explicit cast
huoyaoyuan May 9, 2025
fa9b919
Add low resolution tick to minipal
huoyaoyuan May 9, 2025
08fb866
Share implementation with gcenv
huoyaoyuan May 9, 2025
2ed4a92
Cleanup GetTickCount64
huoyaoyuan May 9, 2025
76f28db
Cleanup GetTickCount
huoyaoyuan May 9, 2025
aad6655
Use direct P/Invoke from managed
huoyaoyuan May 9, 2025
5060e16
Cleanup pal
huoyaoyuan May 9, 2025
f571cc9
Adapt more platforms from mono_msec_boottime
huoyaoyuan May 9, 2025
b4ff716
Merge branch 'main' into minipal-time
huoyaoyuan May 10, 2025
48e2a1e
Fix P/Invoke
huoyaoyuan May 10, 2025
b8e71be
Fix implicit conversion
huoyaoyuan May 10, 2025
609b026
Fix build
huoyaoyuan May 10, 2025
41c488b
Cleanup cmake configs
huoyaoyuan May 10, 2025
7558373
Consolidate ep-mono
huoyaoyuan May 10, 2025
5680137
Move managed entry to UnixOrBrowser
huoyaoyuan May 10, 2025
6fc8458
Fix gcc build
huoyaoyuan May 10, 2025
c3d2a5f
Link minipal unconditionally from gc
huoyaoyuan May 10, 2025
82747ab
Fix declaration
huoyaoyuan May 10, 2025
f3f984c
Cleanup mach_absolute_time
huoyaoyuan May 10, 2025
dcc7de1
Merge branch 'main' into minipal-time
huoyaoyuan May 11, 2025
5c8404c
Call GetTickCount for 32bit
huoyaoyuan May 11, 2025
d566884
Revert "Call GetTickCount for 32bit"
huoyaoyuan May 11, 2025
7905654
Update naming
huoyaoyuan May 12, 2025
d4a70d9
Update sign for GetTimestamp
huoyaoyuan May 12, 2025
08b88f3
Cleanup mono-time
huoyaoyuan May 12, 2025
c21c42f
Fix
huoyaoyuan May 12, 2025
e7fb644
Fix P/Invoke definition
huoyaoyuan May 13, 2025
cfcd1d1
Disable tests
huoyaoyuan May 13, 2025
1ac8829
Revert "Disable tests"
huoyaoyuan May 14, 2025
5b67573
Disable CLOCK_MONOTONIC_COARSE on wasm
huoyaoyuan May 14, 2025
fb68fde
Try another approach
huoyaoyuan May 14, 2025
9a1136d
Update assert
huoyaoyuan May 14, 2025
abda30f
Use __EMSCRIPTEN__ instead
huoyaoyuan May 15, 2025
19a9f76
Use #error for unsupported platform
huoyaoyuan May 16, 2025
f87efa8
Update display in createdump
huoyaoyuan May 16, 2025
aec8e75
Merge branch 'main' into minipal-time
huoyaoyuan May 19, 2025
43bb2cf
Fix misdeleted comment
huoyaoyuan May 19, 2025
e90c295
Fix invocation
huoyaoyuan May 20, 2025
afa22fe
Add assert
huoyaoyuan May 20, 2025
0cea29e
Remove unused constant
huoyaoyuan May 20, 2025
a302ed5
Merge branch 'main' into minipal-time
jkotas May 20, 2025
519daf7
Apply suggestions from code review
huoyaoyuan May 21, 2025
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 @@ -108,21 +108,5 @@ private static unsafe string[] InitializeCommandLineArgs(char* exePath, int argc

// Used by VM
internal static string? GetResourceStringLocal(string key) => SR.GetResourceString(key);

/// <summary>Gets the number of milliseconds elapsed since the system started.</summary>
/// <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>
public static extern int TickCount
{
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}

/// <summary>Gets the number of milliseconds elapsed since the system started.</summary>
/// <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>
public static extern long TickCount64
{
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
}
}
18 changes: 0 additions & 18 deletions src/coreclr/classlibnative/bcltype/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@

#include <minipal/cpuid.h>


FCIMPL0(UINT32, SystemNative::GetTickCount)
{
FCALL_CONTRACT;

return ::GetTickCount();
}
FCIMPLEND;

FCIMPL0(UINT64, SystemNative::GetTickCount64)
{
FCALL_CONTRACT;

return ::GetTickCount64();
}
FCIMPLEND;


extern "C" VOID QCALLTYPE Environment_Exit(INT32 exitcode)
{
QCALL_CONTRACT;
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/classlibnative/bcltype/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ class SystemNative

public:
// Functions on the System.Environment class
static FCDECL0(UINT32, GetTickCount);
static FCDECL0(UINT64, GetTickCount64);

static FCDECL1(VOID,SetExitCode,INT32 exitcode);
static FCDECL0(INT32, GetExitCode);

Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/debug/createdump/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
#pragma once

#cmakedefine HAVE_PROCESS_VM_READV
#cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP
#cmakedefine01 HAVE_CLOCK_MONOTONIC
19 changes: 0 additions & 19 deletions src/coreclr/debug/createdump/configure.cmake
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
check_function_exists(process_vm_readv HAVE_PROCESS_VM_READV)

check_symbol_exists(
clock_gettime_nsec_np
time.h
HAVE_CLOCK_GETTIME_NSEC_NP)

check_cxx_source_runs("
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

int main()
{
int ret;
struct timespec ts;
ret = clock_gettime(CLOCK_MONOTONIC, &ts);

exit(ret);
}" HAVE_CLOCK_MONOTONIC)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
31 changes: 6 additions & 25 deletions src/coreclr/debug/createdump/createdumpmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#include "createdump.h"
#include "minipal/time.h"

#ifdef HOST_WINDOWS
#define DEFAULT_DUMP_PATH "%TEMP%\\"
Expand Down Expand Up @@ -45,8 +46,6 @@ bool g_diagnostics = false;
bool g_diagnosticsVerbose = false;
uint64_t g_ticksPerMS = 0;
uint64_t g_startTime = 0;
uint64_t GetTickFrequency();
uint64_t GetTimeStamp();

//
// Common entry point
Expand Down Expand Up @@ -198,8 +197,8 @@ int createdump_main(const int argc, const char* argv[])
return -1;
}

g_ticksPerMS = GetTickFrequency() / 1000UL;
g_startTime = GetTimeStamp();
g_ticksPerMS = minipal_hires_tick_frequency() / 1000UL;
g_startTime = minipal_hires_ticks();
TRACE("TickFrequency: %d ticks per ms\n", g_ticksPerMS);

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

if (CreateDump(options))
{
printf_status("Dump successfully written in %llums\n", GetTimeStamp() - g_startTime);
printf_status("Dump successfully written in %llums\n", minipal_hires_ticks() - g_startTime);
}
else
{
printf_error("Failure took %llums\n", GetTimeStamp() - g_startTime);
printf_error("Failure took %llums\n", minipal_hires_ticks() - g_startTime);
exitCode = -1;
}

Expand Down Expand Up @@ -332,24 +331,6 @@ printf_error(const char* format, ...)
va_end(args);
}

uint64_t
GetTickFrequency()
{
LARGE_INTEGER ret;
ZeroMemory(&ret, sizeof(LARGE_INTEGER));
QueryPerformanceFrequency(&ret);
return ret.QuadPart;
}

uint64_t
GetTimeStamp()
{
LARGE_INTEGER ret;
ZeroMemory(&ret, sizeof(LARGE_INTEGER));
QueryPerformanceCounter(&ret);
return ret.QuadPart / g_ticksPerMS;
}

#ifdef HOST_UNIX

static void
Expand All @@ -360,7 +341,7 @@ trace_prefix(const char* format, va_list args)
{
fprintf(g_stdout, "[createdump] ");
}
fprintf(g_stdout, "%08" PRIx64 " ", GetTimeStamp());
fprintf(g_stdout, "%08" PRIx64 " ", minipal_hires_ticks() / g_ticksPerMS);
vfprintf(g_stdout, format, args);
fflush(g_stdout);
}
Expand Down
46 changes: 0 additions & 46 deletions src/coreclr/debug/createdump/createdumppal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,52 +96,6 @@ UninitializePAL(
}
}

#define tccSecondsToNanoSeconds 1000000000 // 10^9

BOOL
PALAPI
QueryPerformanceCounter(
OUT LARGE_INTEGER* lpPerformanceCount)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
lpPerformanceCount->QuadPart = (LONGLONG)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#elif HAVE_CLOCK_MONOTONIC
struct timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
if (result != 0)
{
return TRUE;
}
else
{
lpPerformanceCount->QuadPart = ((LONGLONG)(ts.tv_sec) * (LONGLONG)(tccSecondsToNanoSeconds)) + (LONGLONG)(ts.tv_nsec);
}
#else
#error "The createdump requires either mach_absolute_time() or clock_gettime(CLOCK_MONOTONIC) to be supported."
#endif
return TRUE;
}

BOOL
PALAPI
QueryPerformanceFrequency(
OUT LARGE_INTEGER* lpFrequency)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
lpFrequency->QuadPart = (LONGLONG)(tccSecondsToNanoSeconds);
#elif HAVE_CLOCK_MONOTONIC
// clock_gettime() returns a result in terms of nanoseconds rather than a count. This
// means that we need to either always scale the result by the actual resolution (to
// get a count) or we need to say the resolution is in terms of nanoseconds. We prefer
// the latter since it allows the highest throughput and should minimize error propagated
// to the user.
lpFrequency->QuadPart = (LONGLONG)(tccSecondsToNanoSeconds);
#else
#error "The createdump requires either mach_absolute_time() or clock_gettime(CLOCK_MONOTONIC) to be supported."
#endif
return TRUE;
}

#define TEMP_DIRECTORY_PATH "/tmp/"

DWORD
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ nativeStringResourceTable_mscorrc
#OutputDebugStringW
#OpenEventW
#OutputDebugStringA
#QueryPerformanceCounter
#QueryPerformanceFrequency
#RaiseException
#RaiseFailFastException
#ReadFile
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/gc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ if(CLR_CMAKE_HOST_WIN32)
advapi32.lib)
endif(CLR_CMAKE_HOST_WIN32)

set (GC_LINK_LIBRARIES ${GC_LINK_LIBRARIES} gc_pal)
set (GC_LINK_LIBRARIES
${GC_LINK_LIBRARIES}
gc_pal
minipal)

if(CLR_CMAKE_TARGET_ARCH_AMD64)
list(APPEND GC_LINK_LIBRARIES
gc_vxsort
minipal
)
endif(CLR_CMAKE_TARGET_ARCH_AMD64)

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/gc/unix/config.gc.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#cmakedefine01 HAVE_SYSCTLBYNAME
#cmakedefine01 HAVE_PTHREAD_CONDATTR_SETCLOCK
#cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP
#cmakedefine01 HAVE_CLOCK_MONOTONIC
#cmakedefine01 HAVE_SCHED_GETAFFINITY
#cmakedefine01 HAVE_SCHED_SETAFFINITY
#cmakedefine01 HAVE_PTHREAD_SETAFFINITY_NP
Expand Down
14 changes: 0 additions & 14 deletions src/coreclr/gc/unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,6 @@ check_symbol_exists(
time.h
HAVE_CLOCK_GETTIME_NSEC_NP)

check_cxx_source_runs("
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

int main()
{
int ret;
struct timespec ts;
ret = clock_gettime(CLOCK_MONOTONIC, &ts);

exit(ret);
}" HAVE_CLOCK_MONOTONIC)

check_symbol_exists(
posix_madvise
sys/mman.h
Expand Down
57 changes: 4 additions & 53 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gcconfig.h"
#include "numasupport.h"
#include <minipal/thread.h>
#include <minipal/time.h>

#if HAVE_SWAPCTL
#include <sys/swap.h>
Expand Down Expand Up @@ -1449,22 +1450,7 @@ void GCToOSInterface::GetMemoryStatus(uint64_t restricted_limit, uint32_t* memor
// The counter value
int64_t GCToOSInterface::QueryPerformanceCounter()
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#elif HAVE_CLOCK_MONOTONIC
struct timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);

if (result != 0)
{
assert(!"clock_gettime(CLOCK_MONOTONIC) failed");
__UNREACHABLE();
}

return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec);
#else
#error " clock_gettime(CLOCK_MONOTONIC) or clock_gettime_nsec_np() must be supported."
#endif
return minipal_hires_ticks();
}

// Get a frequency of the high precision performance counter
Expand All @@ -1473,50 +1459,15 @@ int64_t GCToOSInterface::QueryPerformanceCounter()
int64_t GCToOSInterface::QueryPerformanceFrequency()
{
// The counter frequency of gettimeofday is in microseconds.
return tccSecondsToNanoSeconds;
return minipal_hires_tick_frequency();
}

// Get a time stamp with a low precision
// Return:
// Time stamp in milliseconds
uint64_t GCToOSInterface::GetLowPrecisionTimeStamp()
{
uint64_t retval = 0;

#if HAVE_CLOCK_GETTIME_NSEC_NP
retval = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / tccMilliSecondsToNanoSeconds;
#elif HAVE_CLOCK_MONOTONIC
struct timespec ts;

#if HAVE_CLOCK_MONOTONIC_COARSE
clockid_t clockType = CLOCK_MONOTONIC_COARSE; // good enough resolution, fastest speed
#else
clockid_t clockType = CLOCK_MONOTONIC;
#endif

if (clock_gettime(clockType, &ts) != 0)
{
#if HAVE_CLOCK_MONOTONIC_COARSE
assert(!"clock_gettime(HAVE_CLOCK_MONOTONIC_COARSE) failed\n");
#else
assert(!"clock_gettime(CLOCK_MONOTONIC) failed\n");
#endif
}

retval = (ts.tv_sec * tccSecondsToMilliSeconds) + (ts.tv_nsec / tccMilliSecondsToNanoSeconds);
#else
struct timeval tv;
if (gettimeofday(&tv, NULL) == 0)
{
retval = (tv.tv_sec * tccSecondsToMilliSeconds) + (tv.tv_usec / tccMilliSecondsToMicroSeconds);
}
else
{
assert(!"gettimeofday() failed\n");
}
#endif

return retval;
return (uint64_t)minipal_lowres_ticks();
}

// Gets the total number of processors on the machine, not taking
Expand Down
32 changes: 16 additions & 16 deletions src/coreclr/ilasm/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,22 +644,22 @@ typedef FIFO<MethodBody> MethodBodyList;

struct Clockwork
{
DWORD cBegin;
DWORD cEnd;
DWORD cParsBegin;
DWORD cParsEnd;
DWORD cMDInitBegin;
DWORD cMDInitEnd;
DWORD cMDEmitBegin;
DWORD cMDEmitEnd;
DWORD cMDEmit1;
DWORD cMDEmit2;
DWORD cMDEmit3;
DWORD cMDEmit4;
DWORD cRef2DefBegin;
DWORD cRef2DefEnd;
DWORD cFilegenBegin;
DWORD cFilegenEnd;
int64_t cBegin;
int64_t cEnd;
int64_t cParsBegin;
int64_t cParsEnd;
int64_t cMDInitBegin;
int64_t cMDInitEnd;
int64_t cMDEmitBegin;
int64_t cMDEmitEnd;
int64_t cMDEmit1;
int64_t cMDEmit2;
int64_t cMDEmit3;
int64_t cMDEmit4;
int64_t cRef2DefBegin;
int64_t cRef2DefEnd;
int64_t cFilegenBegin;
int64_t cFilegenEnd;
};

struct TypeDefDescr
Expand Down
Loading
Loading