12 #include "quill/DeferredFormatCodec.h" 13 #include "quill/backend/RdtscClock.h" 14 #include "quill/core/Attributes.h" 15 #include "quill/core/ChronoTimeUtils.h" 16 #include "quill/core/Common.h" 17 #include "quill/core/Rdtsc.h" 19 #include "quill/bundled/fmt/format.h" 20 #include "quill/std/Chrono.h" 44 template <ClockSourceType ClockType>
48 static_assert((ClockType == ClockSourceType::Tsc) || (ClockType == ClockSourceType::System),
56 if constexpr (ClockType == ClockSourceType::Tsc)
58 _ns_per_tick = RdtscClock::RdtscTicks::instance().ns_per_tick();
63 _start_tp = detail::get_timestamp_ns<std::chrono::steady_clock>();
71 [[nodiscard]] std::chrono::duration<double>
elapsed()
const 73 return elapsed_as<std::chrono::duration<double>>();
83 if constexpr (ClockType == ClockSourceType::Tsc)
85 return std::chrono::duration_cast<T>(std::chrono::nanoseconds{
86 static_cast<uint64_t
>(
static_cast<double>(
rdtsc() - _start_tp) * _ns_per_tick)});
90 return std::chrono::duration_cast<T>(
91 std::chrono::nanoseconds{detail::get_timestamp_ns<std::chrono::steady_clock>() - _start_tp});
100 if constexpr (ClockType == ClockSourceType::Tsc)
106 _start_tp = detail::get_timestamp_ns<std::chrono::steady_clock>();
111 double _ns_per_tick{0};
112 uint64_t _start_tp{0};
128 template <quill::ClockSourceType ClockType>
130 : quill::DeferredFormatCodec<quill::detail::StopWatch<ClockType>>
134 template <quill::ClockSourceType ClockType>
137 template <
typename FormatContext>
138 auto format(quill::detail::StopWatch<ClockType>
const& sw, FormatContext& ctx)
const 139 -> decltype(ctx.out())
141 return fmtquill::formatter<double>::format(sw.elapsed().count(), ctx);
StopWatch()
Constructor.
Definition: StopWatch.h:54
Definition: DocTestExtensions.cpp:27
Setups a signal handler to handle fatal signals.
Definition: BackendManager.h:24
std::chrono::duration< double > elapsed() const
Returns the elapsed time since construction.
Definition: StopWatch.h:71
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT uint64_t rdtsc() noexcept
Get the TSC counter.
Definition: Rdtsc.h:109
T elapsed_as() const
Returns the elapsed time since construction as the specified duration type.
Definition: StopWatch.h:81
void reset()
Resets the stopwatch, starting the measurement from the current time.
Definition: StopWatch.h:98
A stopwatch utility for measuring elapsed time since construction.
Definition: StopWatch.h:45