Commit 78e483e0 authored by Roman Koshelev's avatar Roman Koshelev Committed by Facebook GitHub Bot

Correcting and adding a coarse_ * clock (#1580)

Summary: Pull Request resolved: https://github.com/facebook/folly/pull/1580

Reviewed By: luciang

Differential Revision: D28627136

Pulled By: yfeldblum

fbshipit-source-id: 1362506502ad3282f53512999d1c79822f2ce6e8
parent 7a06e2f4
......@@ -17,6 +17,7 @@
#pragma once
#include <chrono>
#include <ctime>
#include <stdexcept>
#include <type_traits>
......@@ -175,28 +176,59 @@ namespace folly {
namespace chrono {
struct coarse_steady_clock {
using rep = std::chrono::milliseconds::rep;
using period = std::chrono::milliseconds::period;
using duration = std::chrono::duration<rep, period>;
using time_point = std::chrono::time_point<coarse_steady_clock, duration>;
using duration = std::chrono::milliseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<coarse_steady_clock>;
constexpr static bool is_steady = true;
static time_point now() noexcept {
#ifndef CLOCK_MONOTONIC_COARSE
return time_point(std::chrono::duration_cast<duration>(
std::chrono::steady_clock::now().time_since_epoch()));
auto time = std::chrono::steady_clock::now().time_since_epoch();
#else
timespec ts;
auto ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
int ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
if (kIsDebug && (ret != 0)) {
throw_exception<std::runtime_error>(
"Error using CLOCK_MONOTONIC_COARSE.");
}
auto time =
std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
#endif
return time_point(std::chrono::duration_cast<duration>(time));
}
};
struct coarse_system_clock {
using duration = std::chrono::milliseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<coarse_system_clock>;
constexpr static bool is_steady = false;
return time_point(std::chrono::duration_cast<duration>(
std::chrono::seconds(ts.tv_sec) +
std::chrono::nanoseconds(ts.tv_nsec)));
static time_point now() noexcept {
#ifndef CLOCK_REALTIME_COARSE
auto time = std::chrono::system_clock::now().time_since_epoch();
#else
timespec ts;
int ret = clock_gettime(CLOCK_REALTIME_COARSE, &ts);
if (kIsDebug && (ret != 0)) {
throw_exception<std::runtime_error>("Error using CLOCK_REALTIME_COARSE.");
}
auto time =
std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
#endif
return time_point(std::chrono::duration_cast<duration>(time));
}
static std::time_t to_time_t(const time_point& t) noexcept {
auto d = t.time_since_epoch();
return std::chrono::duration_cast<std::chrono::seconds>(d).count();
}
static time_point from_time_t(std::time_t t) noexcept {
return time_point(
std::chrono::duration_cast<duration>(std::chrono::seconds(t)));
}
};
......
......@@ -43,6 +43,26 @@ BENCHMARK(coarse_steady_clock_now, iters) {
folly::doNotOptimizeAway(r);
}
BENCHMARK(system_clock_now, iters) {
uint64_t r = 0;
while (iters--) {
using clock = std::chrono::system_clock;
auto const s = clock::now().time_since_epoch().count();
r = folly::hash::twang_mix64(r ^ s);
}
folly::doNotOptimizeAway(r);
}
BENCHMARK(coarse_system_clock_now, iters) {
uint64_t r = 0;
while (iters--) {
using clock = folly::chrono::coarse_system_clock;
auto const s = clock::now().time_since_epoch().count();
r = folly::hash::twang_mix64(r ^ s);
}
folly::doNotOptimizeAway(r);
}
BENCHMARK(hardware_timestamp_unserialized, iters) {
uint64_t r = 0;
while (iters--) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment