Detect if CLOCK_MONOTONIC_RAW is supported
This commit is contained in:
parent
f6f2148888
commit
f66856dae6
@ -23,10 +23,14 @@ DN_API void DN_Core_Init(DN_Core *core, DN_CoreOnInit on_init)
|
||||
if (on_init & DN_CoreOnInit_LogLibFeatures) {
|
||||
DN_Str8Builder_AppendRef(&builder, DN_STR8("DN initialised:\n"));
|
||||
|
||||
DN_F64 page_size_kib = g_dn_os_core_->page_size / 1024.0;
|
||||
DN_F64 alloc_granularity_kib = g_dn_os_core_->alloc_granularity / 1024.0;
|
||||
DN_Str8Builder_AppendF(
|
||||
&builder, " OS Page Size/Alloc Granularity: %.1f/%.1fKiB\n", page_size_kib, alloc_granularity_kib);
|
||||
DN_F32 page_size_kib = g_dn_os_core_->page_size / 1024.0f;
|
||||
DN_F32 alloc_granularity_kib = g_dn_os_core_->alloc_granularity / 1024.0f;
|
||||
DN_Str8Builder_AppendF(&builder,
|
||||
" OS Page Size/Alloc Granularity: %.1f/%.1fKiB\n"
|
||||
" Logical Processor Count: %u\n",
|
||||
page_size_kib,
|
||||
alloc_granularity_kib,
|
||||
g_dn_os_core_->logical_processor_count);
|
||||
|
||||
#if DN_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
|
||||
if (DN_ASAN_POISON) {
|
||||
@ -43,6 +47,11 @@ DN_API void DN_Core_Init(DN_Core *core, DN_CoreOnInit on_init)
|
||||
#if !defined(DN_NO_PROFILER)
|
||||
DN_Str8Builder_AppendRef(&builder, DN_STR8(" TSC profiler available\n"));
|
||||
#endif
|
||||
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN) || defined(DN_PLATFORM_POSIX)
|
||||
DN_POSIXCore *posix = DN_CAST(DN_POSIXCore *)g_dn_os_core_->platform_context;
|
||||
DN_Str8Builder_AppendF(&builder, " Clock GetTime: %S\n", posix->clock_monotonic_raw ? DN_STR8("CLOCK_MONOTONIC_RAW") : DN_STR8("CLOCK_MONOTONIC"));
|
||||
#endif
|
||||
// TODO(doyle): Add stacktrace feature log
|
||||
}
|
||||
|
||||
|
@ -111,9 +111,9 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
|
||||
|
||||
{
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
|
||||
#else
|
||||
os->arena = DN_Arena_InitFromOSHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack);
|
||||
#else
|
||||
os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
|
||||
#endif
|
||||
|
||||
#if defined(DN_PLATFORM_WIN32)
|
||||
@ -139,9 +139,7 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
|
||||
else
|
||||
DN_LOG_ErrorF("Failed to initialise Windows secure random number generator, error: %d", init_status);
|
||||
#else
|
||||
DN_POSIXCore *posix = DN_CAST(DN_POSIXCore *) os->platform_context;
|
||||
int mutex_init = pthread_mutex_init(&posix->sync_primitive_free_list_mutex, nullptr);
|
||||
DN_Assert(mutex_init == 0);
|
||||
DN_Posix_Init(DN_CAST(DN_POSIXCore *)os->platform_context);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -281,19 +281,27 @@ DN_API void DN_OS_SleepMs(DN_UInt milliseconds)
|
||||
;
|
||||
}
|
||||
|
||||
DN_API uint64_t DN_OS_PerfCounterFrequency()
|
||||
DN_API DN_U64 DN_OS_PerfCounterFrequency()
|
||||
{
|
||||
// NOTE: On Linux we use clock_gettime(CLOCK_MONOTONIC_RAW) which
|
||||
// NOTE: On Linux we use clock_gettime(CLOCK_MONOTONIC_RAW) (or CLOCK_MONOTONIC) which
|
||||
// increments at nanosecond granularity.
|
||||
uint64_t result = 1'000'000'000;
|
||||
DN_U64 result = 1'000'000'000;
|
||||
return result;
|
||||
}
|
||||
|
||||
DN_API uint64_t DN_OS_PerfCounterNow()
|
||||
static DN_POSIXCore *DN_OS_GetPOSIXCore_()
|
||||
{
|
||||
DN_Assert(g_dn_os_core_ && g_dn_os_core_->platform_context);
|
||||
DN_POSIXCore *result = DN_CAST(DN_POSIXCore *)g_dn_os_core_->platform_context;
|
||||
return result;
|
||||
}
|
||||
|
||||
DN_API DN_U64 DN_OS_PerfCounterNow()
|
||||
{
|
||||
DN_POSIXCore *posix = DN_OS_GetPOSIXCore_();
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
||||
uint64_t result = DN_CAST(uint64_t) ts.tv_sec * 1'000'000'000 + DN_CAST(uint64_t) ts.tv_nsec;
|
||||
clock_gettime(posix->clock_monotonic_raw ? CLOCK_MONOTONIC_RAW : CLOCK_MONOTONIC, &ts);
|
||||
DN_U64 result = DN_CAST(DN_U64) ts.tv_sec * 1'000'000'000 + DN_CAST(DN_U64) ts.tv_nsec;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -997,13 +1005,6 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
|
||||
return result;
|
||||
}
|
||||
|
||||
static DN_POSIXCore *DN_OS_GetPOSIXCore_()
|
||||
{
|
||||
DN_Assert(g_dn_os_core_ && g_dn_os_core_->platform_context);
|
||||
DN_POSIXCore *result = DN_CAST(DN_POSIXCore *)g_dn_os_core_->platform_context;
|
||||
return result;
|
||||
}
|
||||
|
||||
static DN_POSIXSyncPrimitive *DN_OS_U64ToPOSIXSyncPrimitive_(DN_U64 u64)
|
||||
{
|
||||
DN_POSIXSyncPrimitive *result = nullptr;
|
||||
@ -1290,6 +1291,19 @@ DN_API DN_U32 DN_OS_ThreadID()
|
||||
return DN_CAST(DN_U32) result;
|
||||
}
|
||||
|
||||
DN_API void DN_Posix_Init(DN_POSIXCore *posix)
|
||||
{
|
||||
int mutex_init = pthread_mutex_init(&posix->sync_primitive_free_list_mutex, nullptr);
|
||||
DN_Assert(mutex_init == 0);
|
||||
|
||||
struct timespec ts;
|
||||
posix->clock_monotonic_raw = clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != -1;
|
||||
if (!posix->clock_monotonic_raw) {
|
||||
int get_result = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
DN_AssertF(get_result != -1, "CLOCK_MONOTONIC_RAW and CLOCK_MONOTONIC are not supported by this platform");
|
||||
}
|
||||
}
|
||||
|
||||
DN_API void DN_Posix_ThreadSetName(DN_Str8 name)
|
||||
{
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
|
@ -70,7 +70,9 @@ struct DN_POSIXCore
|
||||
{
|
||||
DN_POSIXSyncPrimitive *sync_primitive_free_list;
|
||||
pthread_mutex_t sync_primitive_free_list_mutex;
|
||||
bool clock_monotonic_raw;
|
||||
};
|
||||
|
||||
DN_API void DN_Posix_Init(DN_POSIXCore *posix);
|
||||
DN_API void DN_Posix_ThreadSetName(DN_Str8 name);
|
||||
DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus();
|
||||
|
Loading…
x
Reference in New Issue
Block a user