Clocks: use CLOCK_[UPTIME|UPTIME_RAW|BOOTIME] when possible for Clocks::monotonic

GitOrigin-RevId: d0aeb9d98d54b298093cba3b2ca97c6ab16eba28
This commit is contained in:
Arseny Smirnov 2020-08-14 19:51:45 +03:00
parent 28596f1a3f
commit 775ef44ed8

View File

@ -13,6 +13,42 @@ namespace td {
double Clocks::monotonic() {
// TODO write system specific functions, because std::chrono::steady_clock is steady only under Windows
#ifdef CLOCK_BOOTTIME
{
static bool skip = []() {
struct timespec spec;
return clock_gettime(CLOCK_BOOTTIME, &spec) != 0;
}();
struct timespec spec;
if (!skip && clock_gettime(CLOCK_BOOTTIME, &spec) == 0) {
return static_cast<double>(spec.tv_nsec) * 1e-9 + static_cast<double>(spec.tv_sec);
}
}
#endif
#ifdef CLOCK_UPTIME_RAW
{
static bool skip = []() {
struct timespec spec;
return clock_gettime(CLOCK_UPTIME_RAW, &spec) != 0;
}();
struct timespec spec;
if (!skip && clock_gettime(CLOCK_UPTIME_RAW, &spec) == 0) {
return static_cast<double>(spec.tv_nsec) * 1e-9 + static_cast<double>(spec.tv_sec);
}
}
#endif
#ifdef CLOCK_UPTIME
{
static bool skip = []() {
struct timespec spec;
return clock_gettime(CLOCK_UPTIME, &spec) != 0;
}();
struct timespec spec;
if (!skip && clock_gettime(CLOCK_UPTIME, &spec) == 0) {
return static_cast<double>(spec.tv_nsec) * 1e-9 + static_cast<double>(spec.tv_sec);
}
}
#endif
auto duration = std::chrono::steady_clock::now().time_since_epoch();
return static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()) * 1e-9;
}