join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
clock.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_CLOCK_HPP
26#define JOIN_CORE_CLOCK_HPP
27
28// C++.
29#include <chrono>
30#include <mutex>
31
32// C.
33#include <ctime>
34
35namespace join
36{
37 template <class ClockPolicy>
38 class BasicTimer;
39
40 template <class ClockPolicy>
41 class BasicStats;
42
46 struct NanoClock
47 {
48 using rep = int64_t;
49 using period = std::nano;
50 using duration = std::chrono::nanoseconds;
51 using time_point = std::chrono::time_point<NanoClock>;
52 static constexpr bool is_steady = true;
53 static time_point now () noexcept = delete;
54 };
55
60 {
61 public:
63
67 constexpr RealTime () noexcept = default;
68
73 static constexpr int type () noexcept
74 {
75 return CLOCK_REALTIME;
76 }
77 };
78
83 {
84 public:
85 using Duration = std::chrono::nanoseconds;
86 using TimePoint = std::chrono::time_point<NanoClock>;
89
93 constexpr Monotonic () noexcept = default;
94
99 static constexpr int type () noexcept
100 {
101 return CLOCK_MONOTONIC;
102 }
103
108 static TimePoint now () noexcept
109 {
110 timespec ts{};
111 ::clock_gettime (CLOCK_MONOTONIC, &ts);
112 return TimePoint (Duration (static_cast<int64_t> (ts.tv_sec) * 1'000'000'000LL + ts.tv_nsec));
113 }
114 };
115
120 {
121 public:
122 using Duration = std::chrono::nanoseconds;
123 using TimePoint = std::chrono::time_point<NanoClock>;
125
129 constexpr MonotonicRaw () noexcept = default;
130
135 static constexpr int type () noexcept
136 {
137 return CLOCK_MONOTONIC_RAW;
138 }
139
144 static TimePoint now () noexcept
145 {
146 timespec ts{};
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));
149 }
150 };
151
155 class Rdtsc
156 {
157 public:
158 using Duration = std::chrono::nanoseconds;
159 using TimePoint = std::chrono::time_point<NanoClock>;
161
165 Rdtsc () noexcept
166 {
167 calibrate ();
168 }
169
174 static TimePoint now () noexcept
175 {
176 const uint64_t ns = static_cast<uint64_t> ((static_cast<__uint128_t> (readCycles ()) * cycleToNs ()) >> 32);
177 return TimePoint (Duration (static_cast<int64_t> (ns)));
178 }
179
180 private:
185 static uint64_t readCycles () noexcept
186 {
187#if defined(__x86_64__)
188 uint32_t lo, hi;
189 __asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
190 return (static_cast<uint64_t> (hi) << 32) | lo;
191#elif defined(__aarch64__)
192 uint64_t val;
193 __asm__ volatile ("mrs %0, cntvct_el0" : "=r"(val));
194 return val;
195#else
196#error "Rdtsc: unsupported architecture"
197#endif
198 }
199
204 static uint64_t& cycleToNs () noexcept
205 {
206 static uint64_t value = 0;
207 return value;
208 }
209
213 static void calibrate () noexcept
214 {
215 static std::once_flag flag;
216
217 std::call_once (flag, [] {
218 timespec t0{}, t1{};
219 ::clock_gettime (CLOCK_MONOTONIC, &t0);
220 const uint64_t c0 = readCycles ();
221
222 const timespec delay{0, 100'000'000};
223 ::nanosleep (&delay, nullptr);
224
225 ::clock_gettime (CLOCK_MONOTONIC, &t1);
226 const uint64_t c1 = readCycles ();
227
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;
230
231 cycleToNs () = (static_cast<uint64_t> (ns) << 32) / cycles;
232 });
233 }
234 };
235}
236
237#endif
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