Skip to content

Commit

Permalink
core: don't use double in monotonic clock
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilledheart committed Sep 14, 2024
1 parent 1eb7ebc commit 7b1f02a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/core/utils_freebsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ uint64_t GetMonotonicTime() {
PLOG(WARNING) << "clock_gettime failed";
return 0;
}
return static_cast<double>(ts.tv_sec - start_ts.tv_sec) * NS_PER_SECOND + ts.tv_nsec - start_ts.tv_nsec;
ts.tv_sec -= start_ts.tv_sec;
ts.tv_nsec -= start_ts.tv_nsec;
return static_cast<uint64_t>(ts.tv_sec) * NS_PER_SECOND + ts.tv_nsec;
}

#endif // BUILDFLAG(IS_FREEBSD)
4 changes: 3 additions & 1 deletion src/core/utils_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ uint64_t GetMonotonicTime() {
PLOG(WARNING) << "clock_gettime failed";
return 0;
}
return static_cast<double>(ts.tv_sec - start_ts.tv_sec) * NS_PER_SECOND + ts.tv_nsec - start_ts.tv_nsec;
ts.tv_sec -= start_ts.tv_sec;
ts.tv_nsec -= start_ts.tv_nsec;
return static_cast<uint64_t>(ts.tv_sec) * NS_PER_SECOND + ts.tv_nsec;
}

#endif // __linux__
20 changes: 17 additions & 3 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ bool SetCurrentThreadName(const std::string& name) {
return SUCCEEDED(ret);
}

uint64_t GetMonotonicTime() {
static uint64_t GetMonotonicTimeQPC() {
static LARGE_INTEGER StartTime, Frequency;
static bool started;

Expand Down Expand Up @@ -197,12 +197,26 @@ uint64_t GetMonotonicTime() {
// to microseconds *before* dividing by ticks-per-second.
//

ElapsedNanoseconds.QuadPart =
static_cast<double>(ElapsedNanoseconds.QuadPart) * NS_PER_SECOND / static_cast<double>(Frequency.QuadPart);
ElapsedNanoseconds.QuadPart *= NS_PER_SECOND;
ElapsedNanoseconds.QuadPart /= Frequency.QuadPart;

return ElapsedNanoseconds.QuadPart;
}

uint64_t GetMonotonicTime() {
#if _WIN32_WINNT >= 0x0600
return GetMonotonicTimeQPC();
#else
/* if vista or later */
static const auto fPointer =
reinterpret_cast<void*>(::GetProcAddress(::GetModuleHandleW(L"Kernel32.dll"), "GetTickCount64"));
if (fPointer) {
return GetMonotonicTimeQPC();
}
return GetTickCount() * 1000000;
#endif
}

static bool IsHandleConsole(HANDLE handle) {
DWORD mode;
return handle != HANDLE() && handle != INVALID_HANDLE_VALUE &&
Expand Down

0 comments on commit 7b1f02a

Please sign in to comment.