25#ifndef JOIN_CORE_CLOCK_HPP
26#define JOIN_CORE_CLOCK_HPP
37 template <
class ClockPolicy>
40 template <
class ClockPolicy>
73 static constexpr
int type () noexcept
75 return CLOCK_REALTIME;
86 using TimePoint = std::chrono::time_point<NanoClock>;
99 static constexpr
int type () noexcept
101 return CLOCK_MONOTONIC;
111 ::clock_gettime (CLOCK_MONOTONIC, &ts);
112 return TimePoint (
Duration (
static_cast<int64_t
> (ts.tv_sec) * 1'000'000'000LL + ts.tv_nsec));
135 static constexpr
int type () noexcept
137 return CLOCK_MONOTONIC_RAW;
147 ::clock_gettime (CLOCK_MONOTONIC_RAW, &ts);
148 return TimePoint (
Duration (
static_cast<int64_t
> (ts.tv_sec) * 1'000'000'000LL + ts.tv_nsec));
176 const uint64_t ns =
static_cast<uint64_t
> ((
static_cast<__uint128_t
> (readCycles ()) * cycleToNs ()) >> 32);
185 static uint64_t readCycles () noexcept
187#if defined(__x86_64__)
189 __asm__
volatile (
"rdtsc" :
"=a"(lo),
"=d"(hi));
190 return (
static_cast<uint64_t
> (hi) << 32) | lo;
191#elif defined(__aarch64__)
193 __asm__
volatile (
"mrs %0, cntvct_el0" :
"=r"(val));
196#error "Rdtsc: unsupported architecture"
204 static uint64_t& cycleToNs () noexcept
206 static uint64_t value = 0;
213 static void calibrate () noexcept
215 static std::once_flag flag;
217 std::call_once (flag, [] {
219 ::clock_gettime (CLOCK_MONOTONIC, &t0);
220 const uint64_t c0 = readCycles ();
222 const timespec delay{0, 100'000'000};
223 ::nanosleep (&delay,
nullptr);
225 ::clock_gettime (CLOCK_MONOTONIC, &t1);
226 const uint64_t c1 = readCycles ();
228 const int64_t ns = (t1.tv_sec - t0.tv_sec) * 1'000'000'000LL + (t1.tv_nsec - t0.tv_nsec);
229 const uint64_t cycles = c1 - c0;
231 cycleToNs () = (
static_cast<uint64_t
> (ns) << 32) / cycles;
lock-free, multi-producer-safe performance statistics collector.
Definition statistics.hpp:50
base timer class.
Definition timer.hpp:48
raw monotonic clock policy (raw hardware clock, may drift).
Definition clock.hpp:120
std::chrono::time_point< NanoClock > TimePoint
Definition clock.hpp:123
constexpr MonotonicRaw() noexcept=default
default constructor.
static TimePoint now() noexcept
read the current time.
Definition clock.hpp:144
std::chrono::nanoseconds Duration
Definition clock.hpp:122
monotonic clock policy (stable, adjusted, recommended default).
Definition clock.hpp:83
constexpr Monotonic() noexcept=default
default constructor.
std::chrono::nanoseconds Duration
Definition clock.hpp:85
static TimePoint now() noexcept
read the current time.
Definition clock.hpp:108
std::chrono::time_point< NanoClock > TimePoint
Definition clock.hpp:86
rdtsc clock policy (requires invariant TSC and CPU pinning).
Definition clock.hpp:156
Rdtsc() noexcept
default constructor.
Definition clock.hpp:165
std::chrono::time_point< NanoClock > TimePoint
Definition clock.hpp:159
std::chrono::nanoseconds Duration
Definition clock.hpp:158
static TimePoint now() noexcept
read the current time.
Definition clock.hpp:174
real-time clock policy.
Definition clock.hpp:60
constexpr RealTime() noexcept=default
default constructor.
Definition acceptor.hpp:32
minimal clock type used as a type tag for time_point parameterization.
Definition clock.hpp:47
int64_t rep
Definition clock.hpp:48
static constexpr bool is_steady
Definition clock.hpp:52
std::nano period
Definition clock.hpp:49
static time_point now() noexcept=delete
std::chrono::time_point< NanoClock > time_point
Definition clock.hpp:51
std::chrono::nanoseconds duration
Definition clock.hpp:50