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 
18 QUILL_BEGIN_EXPORT
19 
36 {
37 public:
38  class RdtscVal
39  {
40  public:
41  RdtscVal(RdtscVal const& other) = default;
42  RdtscVal(RdtscVal&& other) noexcept = default;
43  RdtscVal& operator=(RdtscVal const& other) = default;
44  RdtscVal& operator=(RdtscVal&& other) noexcept = default;
45 
46  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT uint64_t value() const noexcept { return _value; }
47 
48  private:
49  RdtscVal() noexcept : _value(detail::rdtsc()) {}
50  friend BackendTscClock;
51 
52  uint64_t _value;
53  };
54 
55 public:
56  using duration = std::chrono::nanoseconds;
57  using rep = duration::rep;
58  using period = duration::period;
59  using time_point = std::chrono::time_point<BackendTscClock, duration>;
60  static constexpr bool is_steady = false;
61 
67  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static time_point now()
68  {
69  uint64_t const ts = detail::BackendManager::instance().convert_rdtsc_to_epoch_time(detail::rdtsc());
70 
71  return ts ? time_point{std::chrono::nanoseconds{ts}}
72  : time_point{std::chrono::nanoseconds{
73  std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now())
74  .time_since_epoch()
75  .count()}};
76  }
77 
82  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static RdtscVal rdtsc() noexcept { return RdtscVal{}; }
83 
97  QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static time_point to_time_point(RdtscVal rdtsc)
98  {
99  return time_point{std::chrono::nanoseconds{
100  detail::BackendManager::instance().convert_rdtsc_to_epoch_time(rdtsc.value())}};
101  }
102 };
103 
104 QUILL_END_EXPORT
105 
106 QUILL_END_NAMESPACE
QUILL_NODISCARD static QUILL_ATTRIBUTE_HOT time_point now()
Provides the current synchronized timestamp obtained using the TSC clock maintained by the backend lo...
Definition: BackendTscClock.h:67
A utility class for accessing the Time Stamp Counter (TSC) clock used by the backend logging thread...
Definition: BackendTscClock.h:35
QUILL_NODISCARD static QUILL_ATTRIBUTE_HOT time_point to_time_point(RdtscVal rdtsc)
Converts a TSC (Time Stamp Counter) value to a wall clock timestamp.
Definition: BackendTscClock.h:97
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT uint64_t rdtsc() noexcept
Get the TSC counter.
Definition: Rdtsc.h:105
Definition: BackendTscClock.h:38
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:82