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 __asm __volatile(
"mrs %0, CNTVCT_EL0" :
"=&r" (result));
54#error Unknown architecture.
67#if defined (ARCH_OS_DARWIN)
69#elif defined (ARCH_CPU_ARM)
70 std::atomic_signal_fence(std::memory_order_seq_cst);
71 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
72 std::atomic_signal_fence(std::memory_order_seq_cst);
73#elif defined (ARCH_COMPILER_MSVC)
75 std::atomic_signal_fence(std::memory_order_seq_cst);
78 std::atomic_signal_fence(std::memory_order_seq_cst);
79#elif defined(ARCH_CPU_INTEL) && \
80 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
82 std::atomic_signal_fence(std::memory_order_seq_cst);
95#error "Unsupported architecture."
108#if defined (ARCH_OS_DARWIN)
110#elif defined (ARCH_CPU_ARM)
111 std::atomic_signal_fence(std::memory_order_seq_cst);
112 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
113 std::atomic_signal_fence(std::memory_order_seq_cst);
114#elif defined (ARCH_COMPILER_MSVC)
115 std::atomic_signal_fence(std::memory_order_seq_cst);
119 std::atomic_signal_fence(std::memory_order_seq_cst);
120#elif defined(ARCH_CPU_INTEL) && \
121 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
122 std::atomic_signal_fence(std::memory_order_seq_cst);
132 :
"rcx",
"rdx",
"cc");
134#error "Unsupported architecture."
139#if defined (doxygen) || \
140 (!defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_INTEL) && \
141 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC)))
158 std::atomic_signal_fence(std::memory_order_seq_cst);
163 :
"=a"(_startLow),
"=d"(_startHigh) :: );
173 return (uint64_t(_startHigh) << 32) + _startLow;
187 uint32_t stopLow, stopHigh;
188 std::atomic_signal_fence(std::memory_order_seq_cst);
192 :
"=a"(stopLow),
"=d"(stopHigh)
196 return ((uint64_t(stopHigh) << 32) + stopLow) -
197 ((uint64_t(_startHigh) << 32) + _startLow);
200 bool _started =
false;
201 uint32_t _startLow = 0, _startHigh = 0;
206struct ArchIntervalTimer
239 bool _started =
false;
240 uint64_t _startTicks;
297 void const *m, uint64_t (*callM)(
void const *,
int));
312 uint64_t maxTicks = 1e7,
313 bool *reachedConsensus =
nullptr)
315 auto measureN = [&fn](
int nTimes) -> uint64_t {
317 for (
int i = nTimes; i--; ) {
318 std::atomic_signal_fence(std::memory_order_seq_cst);
320 std::atomic_signal_fence(std::memory_order_seq_cst);
322 return iTimer.GetElapsedTicks();
325 using MeasureNType =
decltype(measureN);
328 maxTicks, reachedConsensus,
329 static_cast<void const *
>(&measureN),
330 [](
void const *mN,
int nTimes) {
331 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:64
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:310
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:105
A simple timer class for measuring an interval of time using the ArchTickTimer facilities.
Definition timing.h:146
uint64_t GetStartTicks() const
Return this timer's start time, or 0 if it hasn't been started.
Definition timing.h:172
void Start()
Start the timer, or reset the start time if it has already been started.
Definition timing.h:156
bool IsStarted() const
Return true if this timer is started.
Definition timing.h:167
ArchIntervalTimer(bool start=true)
Construct a timer and start timing if start is true.
Definition timing.h:148
uint64_t GetCurrentTicks()
Read and return the current time.
Definition timing.h:177
uint64_t GetElapsedTicks()
Read the current time and return the difference between it and the start time.
Definition timing.h:183