quill
BackendTscClock.h
1 
7 #pragma once
8 
9 #include "quill/backend/BackendManager.h"
10 #include "quill/core/Attributes.h"
11 #include "quill/core/Rdtsc.h"
12 
13 #include <chrono>
14 #include <cstdint>
15 
16 QUILL_BEGIN_NAMESPACE
17 
34 {
35 public:
36  class RdtscVal
37  {
38  public:
39  RdtscVal(RdtscVal const& other) = default;
40  RdtscVal(RdtscVal&& other) noexcept = default;
41  RdtscVal& operator=(RdtscVal const& other) = default;
42  RdtscVal& operator=(RdtscVal&& other) noexcept = default;
43 
44  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT uint64_t value() const noexcept { return _value; }
45 
46  private:
47  RdtscVal() noexcept : _value(detail::rdtsc()) {}
48  friend BackendTscClock;
49 
50  uint64_t _value;
51  };
52 
53 public:
54  using duration = std::chrono::nanoseconds;
55  using rep = duration::rep;
56  using period = duration::period;
57  using time_point = std::chrono::time_point<BackendTscClock, duration>;
58  static constexpr bool is_steady = false;
59 
64  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static time_point now() noexcept
65  {
66  uint64_t const ts = detail::BackendManager::instance().convert_rdtsc_to_epoch_time(detail::rdtsc());
67 
68  return ts ? time_point{std::chrono::nanoseconds{ts}}
69  : time_point{std::chrono::nanoseconds{
70  std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now())
71  .time_since_epoch()
72  .count()}};
73  }
74 
79  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static RdtscVal rdtsc() noexcept { return RdtscVal{}; }
80 
93  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static time_point to_time_point(RdtscVal rdtsc) noexcept
94  {
95  return time_point{std::chrono::nanoseconds{
96  detail::BackendManager::instance().convert_rdtsc_to_epoch_time(rdtsc.value())}};
97  }
98 };
99 
100 QUILL_END_NAMESPACE
QUILL_NODISCARD static QUILL_ATTRIBUTE_HOT time_point to_time_point(RdtscVal rdtsc) noexcept
Converts a TSC (Time Stamp Counter) value to a wall clock timestamp.
Definition: BackendTscClock.h:93
A utility class for accessing the Time Stamp Counter (TSC) clock used by the backend logging thread...
Definition: BackendTscClock.h:33
QUILL_NODISCARD static QUILL_ATTRIBUTE_HOT time_point now() noexcept
Provides the current synchronized timestamp obtained using the TSC clock maintained by the backend lo...
Definition: BackendTscClock.h:64
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT uint64_t rdtsc() noexcept
Get the TSC counter.
Definition: Rdtsc.h:109
Definition: BackendTscClock.h:36
QUILL_NODISCARD static QUILL_ATTRIBUTE_HOT RdtscVal rdtsc() noexcept
Returns the current value of the TSC timer maintained by the backend logging thread.
Definition: BackendTscClock.h:79