FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
time.hpp File Reference

Time primitives. More...

#include "fastmm/core/config_macros.hpp"
#include "fastmm/core/seqlock.hpp"
#include <x86intrin.h>
#include <compare>
#include <cstdint>

Go to the source code of this file.

Classes

struct  fastmm::Duration
 
struct  fastmm::Timestamp
 
struct  fastmm::Cycles
 
struct  fastmm::TscCalibration
 
struct  fastmm::ClockReadings
 Clock readings used by TscCalibrator. More...
 
struct  fastmm::TscAnchor
 One simultaneous reading of the three clocks (the TSC is the midpoint of a tight bracket). More...
 
struct  fastmm::TscRecalibration
 
class  fastmm::TscCalibrator
 Long-baseline TSC calibration for the calibrator (control) thread. More...
 
class  fastmm::TscClock
 Periodic recalibration (see docs/explanation/architecture.md, "Clock calibration"): a calibrator thread measures with calibrate_tsc() and publishes into a Seqlocked<TscCalibration>; every TscClock user keeps its own copy and calls refresh() from its loop. More...
 
class  fastmm::SimClock
 Deterministic clock for sim/backtest/replay. 1 cycle == 1 ns. More...
 

Namespaces

namespace  fastmm
 

Concepts

concept  fastmm::ClockLike
 

Enumerations

enum class  fastmm::TscKind : std::uint8_t { fastmm::None , fastmm::Constant , fastmm::Invariant }
 What /proc/cpuinfo says about the TSC (cached after the first call): Invariant constant_tsc + nonstop_tsc: wall time and intervals from the TSC. More...
 
enum class  fastmm::TscRefresh : std::uint8_t {
  fastmm::None = 0 , fastmm::Adopted , fastmm::Reanchored , fastmm::Stepped ,
  fastmm::Rejected
}
 Outcome of TscClock::refresh() / reanchor(). More...
 

Functions

constexpr Duration fastmm::nanoseconds (std::int64_t v) noexcept
 
constexpr Duration fastmm::microseconds (std::int64_t v) noexcept
 
constexpr Duration fastmm::milliseconds (std::int64_t v) noexcept
 
constexpr Duration fastmm::seconds (std::int64_t v) noexcept
 
FASTMM_FORCE_INLINE Cycles fastmm::rdtscp () noexcept
 Serialising read (rdtscp) is what latency hops want: it waits for prior loads to retire.
 
FASTMM_FORCE_INLINE Cycles fastmm::rdtsc () noexcept
 
Timestamp fastmm::wall_now () noexcept
 CLOCK_REALTIME via vDSO (~20 ns). Implemented in src/core/time.cpp.
 
Timestamp fastmm::steady_now () noexcept
 
TscKind fastmm::tsc_kind () noexcept
 
bool fastmm::has_invariant_tsc () noexcept
 
FASTMM_FORCE_INLINE std::uint64_t fastmm::tsc_interval_ns (const TscCalibration &c, std::uint64_t cycles) noexcept
 A TSC cycle interval in ns; 0 without a rate.
 
ClockReadings fastmm::system_clock_readings () noexcept
 
TscCalibration fastmm::calibrate_tsc (Duration window=milliseconds(50)) noexcept
 Startup calibration (spins for window): TscCalibrator::start(), so the rate is measured against CLOCK_MONOTONIC_RAW and a host wall-clock step inside the window cannot distort it; the anchor follows CLOCK_REALTIME.
 
TscCalibration fastmm::calibrate_tsc (Duration window, const ClockReadings &readings) noexcept
 
constexpr const char * fastmm::to_string (TscRefresh r) noexcept
 
FASTMM_FORCE_INLINE std::int64_t fastmm::tsc_to_ns (const TscCalibration &c, std::uint64_t tsc) noexcept
 TSC reading -> wall ns under c.
 

Detailed Description

Time primitives.

Timestamp is ns since the Unix epoch; Cycles is a raw TSC reading.

TscClock converts rdtsc to wall-clock ns with a 32.32 fixed-point multiplier calibrated against CLOCK_REALTIME, refreshed at run time through a Seqlocked<TscCalibration> without letting time go backwards. Without an invariant TSC (constant_tsc and nonstop_tsc) wall time comes from clock_gettime (detected once from /proc/cpuinfo, see src/core/time.cpp); with constant_tsc alone, as on many cloud VMs, the TSC still stamps latency intervals. SimClock is the deterministic replacement for backtests/replay: same interface, time only moves when told.