2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2017-10-21 20:31:20 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include "wpi/timestamp.h"
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2017-11-19 11:47:06 -08:00
|
|
|
#include <atomic>
|
|
|
|
|
|
2016-09-25 17:23:39 -07:00
|
|
|
#ifdef _WIN32
|
2017-10-21 20:31:20 -07:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
2016-09-25 17:23:39 -07:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <exception>
|
|
|
|
|
#else
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// offset in microseconds
|
2020-12-28 17:12:51 -08:00
|
|
|
static uint64_t zerotime() noexcept {
|
2016-09-25 17:23:39 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
FILETIME ft;
|
2017-11-19 11:47:06 -08:00
|
|
|
uint64_t tmpres = 0;
|
2016-09-25 17:23:39 -07:00
|
|
|
// 100-nanosecond intervals since January 1, 1601 (UTC)
|
|
|
|
|
// which means 0.1 us
|
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
|
tmpres |= ft.dwHighDateTime;
|
|
|
|
|
tmpres <<= 32;
|
|
|
|
|
tmpres |= ft.dwLowDateTime;
|
2017-11-19 11:47:06 -08:00
|
|
|
tmpres /= 10u; // convert to us
|
2016-09-25 17:23:39 -07:00
|
|
|
// January 1st, 1970 - January 1st, 1601 UTC ~ 369 years
|
2017-11-19 11:47:06 -08:00
|
|
|
// or 11644473600000000 us
|
|
|
|
|
static const uint64_t deltaepoch = 11644473600000000ull;
|
2016-09-25 17:23:39 -07:00
|
|
|
tmpres -= deltaepoch;
|
|
|
|
|
return tmpres;
|
|
|
|
|
#else
|
2017-10-21 20:31:20 -07:00
|
|
|
// 1-us intervals
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
|
std::chrono::high_resolution_clock::now().time_since_epoch())
|
2017-11-19 11:47:06 -08:00
|
|
|
.count();
|
2016-09-25 17:23:39 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 17:12:51 -08:00
|
|
|
static uint64_t timestamp() noexcept {
|
2016-09-25 17:23:39 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
QueryPerformanceCounter(&li);
|
|
|
|
|
// there is an imprecision with the initial value,
|
|
|
|
|
// but what matters is that timestamps are monotonic and consistent
|
2017-11-19 11:47:06 -08:00
|
|
|
return static_cast<uint64_t>(li.QuadPart);
|
2016-09-25 17:23:39 -07:00
|
|
|
#else
|
2017-11-19 11:47:06 -08:00
|
|
|
// 1-us intervals
|
2017-10-21 20:31:20 -07:00
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch())
|
2017-11-19 11:47:06 -08:00
|
|
|
.count();
|
2016-09-25 17:23:39 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-11-19 11:47:06 -08:00
|
|
|
static uint64_t update_frequency() {
|
2016-09-25 17:23:39 -07:00
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
if (!QueryPerformanceFrequency(&li) || !li.QuadPart) {
|
|
|
|
|
// log something
|
|
|
|
|
std::terminate();
|
|
|
|
|
}
|
2017-11-19 11:47:06 -08:00
|
|
|
return static_cast<uint64_t>(li.QuadPart);
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-11-19 11:47:06 -08:00
|
|
|
static const uint64_t zerotime_val = zerotime();
|
|
|
|
|
static const uint64_t offset_val = timestamp();
|
2016-09-25 17:23:39 -07:00
|
|
|
#ifdef _WIN32
|
2017-11-19 11:47:06 -08:00
|
|
|
static const uint64_t frequency_val = update_frequency();
|
2016-09-25 17:23:39 -07:00
|
|
|
#endif
|
|
|
|
|
|
2017-11-19 11:47:06 -08:00
|
|
|
uint64_t wpi::NowDefault() {
|
2016-09-25 17:23:39 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
assert(offset_val > 0u);
|
|
|
|
|
assert(frequency_val > 0u);
|
2017-11-19 11:47:06 -08:00
|
|
|
uint64_t delta = timestamp() - offset_val;
|
2016-09-25 17:23:39 -07:00
|
|
|
// because the frequency is in update per seconds, we have to multiply the
|
2017-11-19 11:47:06 -08:00
|
|
|
// delta by 1,000,000
|
|
|
|
|
uint64_t delta_in_us = delta * 1000000ull / frequency_val;
|
2016-09-25 17:23:39 -07:00
|
|
|
return delta_in_us + zerotime_val;
|
|
|
|
|
#else
|
|
|
|
|
return zerotime_val + timestamp() - offset_val;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 11:47:06 -08:00
|
|
|
static std::atomic<uint64_t (*)()> now_impl{wpi::NowDefault};
|
|
|
|
|
|
|
|
|
|
void wpi::SetNowImpl(uint64_t (*func)(void)) {
|
|
|
|
|
now_impl = func ? func : NowDefault;
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
2017-11-19 11:47:06 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
uint64_t wpi::Now() {
|
|
|
|
|
return (now_impl.load())();
|
|
|
|
|
}
|
2017-11-19 11:47:06 -08:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
uint64_t WPI_NowDefault(void) {
|
|
|
|
|
return wpi::NowDefault();
|
|
|
|
|
}
|
2017-11-19 11:47:06 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void WPI_SetNowImpl(uint64_t (*func)(void)) {
|
|
|
|
|
wpi::SetNowImpl(func);
|
|
|
|
|
}
|
2017-11-19 11:47:06 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
uint64_t WPI_Now(void) {
|
|
|
|
|
return wpi::Now();
|
|
|
|
|
}
|
2017-11-19 11:47:06 -08:00
|
|
|
|
|
|
|
|
} // extern "C"
|