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
43 if constexpr (processor::current == processor::x64) {
45 _count = __rdtscp(&tmp);
55 if constexpr (processor::current == processor::x64) {
56 _count = __rdtscp(&_aux);
66 if constexpr (processor::current == processor::x64) {
67 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
69 _count = __rdtscp(&_aux);
70 _thread_id = __readgsdword(NT_TIB_CurrentThreadID);
92 if (_aux_is_cpu_id.
load(std::memory_order::relaxed)) {
96 return cpu_id_fallback();
103 [[nodiscard]]
constexpr uint32_t
thread_id() const noexcept
112 [[nodiscard]]
constexpr uint64_t
count() const noexcept
124 using namespace std::literals::chrono_literals;
126 ttlet [lo, hi] = mul_carry(
count, _period.
load(std::memory_order::relaxed));
127 return 1ns *
static_cast<int64_t
>((hi << 32) | (lo >> 32));
145 [[nodiscard]]
constexpr time_stamp_count operator+(uint64_t rhs)
const noexcept
157 static void set_frequency(uint64_t frequency)
noexcept
159 auto period = (uint64_t{1'000'000'000} << 32) / frequency;
160 _period.
store(period, std::memory_order_relaxed);
185 inline static
std::atomic<uint64_t> _period = 0;
187 inline static
std::atomic<
bool> _aux_is_cpu_id = false;
191 inline static
std::atomic<
size_t> _num_aux_values = 0;
195 inline static
std::array<uint32_t, maximum_num_cpus> _aux_values;
199 inline static
std::array<
size_t, maximum_num_cpus> _cpu_ids;
208 [[nodiscard]]
ssize_t cpu_id_fallback() const noexcept;
210 static
void populate_aux_values() noexcept;
211 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:103
time_stamp_count(time_stamp_count::inplace_with_cpu_id) noexcept
Use a constructor to in-place create the timestamp.
Definition time_stamp_count.hpp:53
constexpr uint64_t count() const noexcept
Get the count since epoch.
Definition time_stamp_count.hpp:112
ssize_t cpu_id() const noexcept
Get the logical CPU index.
Definition time_stamp_count.hpp:90
static uint64_t measure_frequency(std::chrono::milliseconds duration) noexcept
Measure the frequency of the time_stamp_count.
static std::chrono::nanoseconds duration_from_count(uint64_t count) noexcept
Convert a time-stamp count to a duration.
Definition time_stamp_count.hpp:122
std::chrono::nanoseconds time_since_epoch() const noexcept
Convert to nanoseconds since epoch.
Definition time_stamp_count.hpp:134
time_stamp_count(time_stamp_count::inplace_with_thread_id) noexcept
Use a constructor to in-place create the timestamp.
Definition time_stamp_count.hpp:64
static void start_subsystem() noexcept
Start the time_stamp_count subsystem.
time_stamp_count(time_stamp_count::inplace) noexcept
Use a constructor to in-place create the timestamp.
Definition time_stamp_count.hpp:41
static time_stamp_count now() noexcept
Get the current count from the CPU's time stamp count.
Definition time_stamp_count.hpp:79
Definition time_stamp_count.hpp:31
Definition time_stamp_count.hpp:32
Definition time_stamp_count.hpp:33