7#include "architecture.hpp"
9#include "int_carry.hpp"
15#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
17#elif TT_OPERATING_SYSTEM == TT_OS_LINUX
42 if constexpr (processor::current == processor::x64) {
43 _count = __rdtscp(&_aux);
53 if constexpr (processor::current == processor::x64) {
54 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
56 _count = __rdtscp(&_aux);
57 _thread_id = __readgsdword(NT_TIB_CurrentThreadID);
79 if (_aux_is_cpu_id.
load(std::memory_order::relaxed)) {
83 return cpu_id_fallback();
90 [[nodiscard]]
constexpr uint32_t
thread_id() const noexcept
99 [[nodiscard]]
constexpr uint64_t
count() const noexcept
110 auto [lo, hi] = wide_mul(_count, _period.
load(std::memory_order::relaxed));
111 return 1ns *
static_cast<int64_t
>((hi << 32) | (lo >> 32));
120 [[nodiscard]]
constexpr time_stamp_count operator+(uint64_t rhs)
const noexcept
132 static void set_frequency(uint64_t frequency)
noexcept
134 auto period = (uint64_t{1'000'000'000} << 32) / frequency;
135 _period.
store(period, std::memory_order_relaxed);
160 inline static
std::atomic<uint64_t> _period = 0;
162 inline static
std::atomic<
bool> _aux_is_cpu_id = false;
166 inline static
std::atomic<
size_t> _num_aux_values = 0;
170 inline static
std::array<uint32_t, maximum_num_cpus> _aux_values;
174 inline static
std::array<
size_t, maximum_num_cpus> _cpu_ids;
183 [[nodiscard]]
ssize_t cpu_id_fallback() const noexcept;
185 static
void populate_aux_values() noexcept;
186 static
void configure_frequency() noexcept;
Since Window's 10 QueryPerformanceCounter() counts at only 10MHz which is too low to measure performa...
Definition time_stamp_count.hpp:29
constexpr uint32_t thread_id() const noexcept
Get the thread id.
Definition time_stamp_count.hpp:90
constexpr uint64_t count() const noexcept
Get the count since epoch.
Definition time_stamp_count.hpp:99
ssize_t cpu_id() const noexcept
Get the logical cpu index.
Definition time_stamp_count.hpp:77
static uint64_t measure_frequency(std::chrono::milliseconds duration) noexcept
Measure the frequency of the time_stamp_count.
std::chrono::nanoseconds time_since_epoch() const noexcept
Convert to nanoseconds since epoch.
Definition time_stamp_count.hpp:108
time_stamp_count(time_stamp_count::inplace_with_thread_id) noexcept
Use a constructor to inplace create the timestamp.
Definition time_stamp_count.hpp:51
static void start_subsystem() noexcept
Start the time_stamp_count subsystem.
time_stamp_count(time_stamp_count::inplace) noexcept
Use a constructor to inplace create the timestamp.
Definition time_stamp_count.hpp:40
static time_stamp_count now() noexcept
Get the current count from the CPU's time stamp count.
Definition time_stamp_count.hpp:66
Definition time_stamp_count.hpp:31
Definition time_stamp_count.hpp:32