8#ifndef PXR_ARCH_TIMING_H
9#define PXR_ARCH_TIMING_H
18#if defined(ARCH_OS_LINUX) && defined(ARCH_CPU_INTEL)
20#elif defined(ARCH_OS_DARWIN)
21#include <mach/mach_time.h>
22#elif defined(ARCH_OS_WINDOWS)
43#if defined(ARCH_OS_DARWIN)
45 return mach_absolute_time();
46#elif defined(ARCH_CPU_INTEL)
49#elif defined (ARCH_CPU_ARM)
51 #if defined(ARCH_COMPILER_MSVC)
54 result = _ReadStatusReg(0x5F02);
56 __asm __volatile(
"mrs %0, CNTVCT_EL0" :
"=&r" (result));
60#error Unknown architecture.
73#if defined (ARCH_OS_DARWIN) || \
74 (defined (ARCH_CPU_ARM) && defined (ARCH_COMPILER_MSVC))
76#elif defined (ARCH_CPU_ARM)
77 std::atomic_signal_fence(std::memory_order_seq_cst);
78 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
79 std::atomic_signal_fence(std::memory_order_seq_cst);
80#elif defined (ARCH_COMPILER_MSVC)
82 std::atomic_signal_fence(std::memory_order_seq_cst);
85 std::atomic_signal_fence(std::memory_order_seq_cst);
86#elif defined(ARCH_CPU_INTEL) && \
87 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
89 std::atomic_signal_fence(std::memory_order_seq_cst);
102#error "Unsupported architecture."
115#if defined (ARCH_OS_DARWIN) || \
116 (defined (ARCH_CPU_ARM) && defined (ARCH_COMPILER_MSVC))
118#elif defined (ARCH_CPU_ARM)
119 std::atomic_signal_fence(std::memory_order_seq_cst);
120 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
121 std::atomic_signal_fence(std::memory_order_seq_cst);
122#elif defined (ARCH_COMPILER_MSVC)
123 std::atomic_signal_fence(std::memory_order_seq_cst);
127 std::atomic_signal_fence(std::memory_order_seq_cst);
128#elif defined(ARCH_CPU_INTEL) && \
129 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
130 std::atomic_signal_fence(std::memory_order_seq_cst);
140 :
"rcx",
"rdx",
"cc");
142#error "Unsupported architecture."
147#if defined (doxygen) || \
148 (!defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_INTEL) && \
149 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC)))
166 std::atomic_signal_fence(std::memory_order_seq_cst);
171 :
"=a"(_startLow),
"=d"(_startHigh) :: );
181 return (uint64_t(_startHigh) << 32) + _startLow;
195 uint32_t stopLow, stopHigh;
196 std::atomic_signal_fence(std::memory_order_seq_cst);
200 :
"=a"(stopLow),
"=d"(stopHigh)
204 return ((uint64_t(stopHigh) << 32) + stopLow) -
205 ((uint64_t(_startHigh) << 32) + _startLow);
208 bool _started =
false;
209 uint32_t _startLow = 0, _startHigh = 0;
214struct ArchIntervalTimer
247 bool _started =
false;
248 uint64_t _startTicks;
305 void const *m, uint64_t (*callM)(
void const *,
int));
320 uint64_t maxTicks = 1e7,
321 bool *reachedConsensus =
nullptr)
323 auto measureN = [&fn](
int nTimes) -> uint64_t {
325 for (
int i = nTimes; i--; ) {
326 std::atomic_signal_fence(std::memory_order_seq_cst);
328 std::atomic_signal_fence(std::memory_order_seq_cst);
330 return iTimer.GetElapsedTicks();
333 using MeasureNType =
decltype(measureN);
336 maxTicks, reachedConsensus,
337 static_cast<void const *
>(&measureN),
338 [](
void const *mN,
int nTimes) {
339 return (*
static_cast<MeasureNType
const *
>(mN))(nTimes);
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
Convert a duration measured in "ticks", as returned by ArchGetTickTime(), to nanoseconds.
ARCH_API uint64_t ArchSecondsToTicks(double seconds)
Convert a duration in seconds to "ticks", as returned by ArchGetTickTime().
uint64_t ArchGetStartTickTime()
Get a "start" tick time for measuring an interval of time, followed by a later call to ArchGetStopTic...
Definition timing.h:70
uint64_t ArchGetTickTime()
Return the current time in system-dependent units.
Definition timing.h:41
uint64_t ArchMeasureExecutionTime(Fn const &fn, uint64_t maxTicks=1e7, bool *reachedConsensus=nullptr)
Run fn repeatedly attempting to determine a consensus fastest execution time with low noise,...
Definition timing.h:318
ARCH_API uint64_t ArchGetIntervalTimerTickOverhead()
Return the ticks taken to record an interval of time with ArchIntervalTimer, as measured at startup t...
ARCH_API uint64_t ArchGetTickQuantum()
Return the tick time resolution.
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
Convert a duration measured in "ticks", as returned by ArchGetTickTime(), to seconds.
ARCH_API double ArchGetNanosecondsPerTick()
Get nanoseconds per tick.
ARCH_API uint64_t Arch_MeasureExecutionTime(uint64_t maxTicks, bool *reachedConsensus, void const *m, uint64_t(*callM)(void const *, int))
uint64_t ArchGetStopTickTime()
Get a "stop" tick time for measuring an interval of time.
Definition timing.h:112
A simple timer class for measuring an interval of time using the ArchTickTimer facilities.
Definition timing.h:154
uint64_t GetStartTicks() const
Return this timer's start time, or 0 if it hasn't been started.
Definition timing.h:180
void Start()
Start the timer, or reset the start time if it has already been started.
Definition timing.h:164
bool IsStarted() const
Return true if this timer is started.
Definition timing.h:175
ArchIntervalTimer(bool start=true)
Construct a timer and start timing if start is true.
Definition timing.h:156
uint64_t GetCurrentTicks()
Read and return the current time.
Definition timing.h:185
uint64_t GetElapsedTicks()
Read the current time and return the difference between it and the start time.
Definition timing.h:191