orca/src/platform/win32_clock.c

72 lines
2.0 KiB
C
Raw Normal View History

2023-09-07 12:51:48 +00:00
/*************************************************************************
2023-03-05 15:05:43 +00:00
*
2023-09-07 12:51:48 +00:00
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
2023-03-05 15:05:43 +00:00
*
2023-09-07 12:51:48 +00:00
**************************************************************************/
2023-08-19 12:49:23 +00:00
#include <profileapi.h>
2023-03-05 15:05:43 +00:00
2023-08-19 12:49:23 +00:00
#include "platform_clock.h"
#include "util/typedefs.h"
2023-03-05 15:05:43 +00:00
#ifdef __cplusplus
2023-08-19 12:49:23 +00:00
extern "C"
{
2023-03-05 15:05:43 +00:00
#endif
2023-08-19 12:49:23 +00:00
static u64 __performanceCounterFreq = 0;
2023-03-05 15:05:43 +00:00
2023-08-22 20:40:39 +00:00
const u64 WIN_EPOCH_TO_UNIX_EPOCH_100NS = 116444736000000000; // number of 100ns from Jan 1, 1601 (windows epoch) to Jan 1, 1970 (unix epoch)
2023-08-19 12:49:23 +00:00
void oc_clock_init()
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
__performanceCounterFreq = freq.QuadPart;
}
2023-03-05 15:05:43 +00:00
2023-08-19 12:49:23 +00:00
f64 oc_clock_time(oc_clock_kind clock)
{
2023-08-22 20:40:39 +00:00
switch(clock)
{
case OC_CLOCK_MONOTONIC:
{
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
f64 time = __performanceCounterFreq ? (counter.QuadPart / (f64)__performanceCounterFreq) : 0;
return (time);
}
case OC_CLOCK_UPTIME:
{
LONGLONG unbiasedTime100Ns;
if(QueryUnbiasedInterruptTime(&unbiasedTime100Ns))
{
f64 unbiasedTimeSecs = ((f64)unbiasedTime100Ns) / (10 * 1000 * 1000);
return unbiasedTimeSecs;
}
oc_log_error("OC_CLOCK_UPTIME syscall failed with error: %d", GetLastError());
return 0;
}
case OC_CLOCK_DATE:
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
u64 systemTime100Ns = ((u64)ft.dwHighDateTime << 32) | (u64)ft.dwLowDateTime;
u64 timestamp100Ns = systemTime100Ns - WIN_EPOCH_TO_UNIX_EPOCH_100NS;
f64 timeSecs = ((f64)timestamp100Ns) / (10 * 1000 * 1000);
return timeSecs;
}
}
2023-03-05 15:05:43 +00:00
2023-08-22 20:40:39 +00:00
return 0.0;
2023-08-19 12:49:23 +00:00
}
2023-03-05 15:05:43 +00:00
#ifdef __cplusplus
} // extern "C"
#endif