7#ifndef PXR_BASE_ARCH_TIMING_H
8#define PXR_BASE_ARCH_TIMING_H
15#include "pxr/base/arch/api.h"
16#include "pxr/base/arch/defines.h"
22#if defined(ARCH_OS_LINUX) && defined(ARCH_CPU_INTEL)
24#elif defined(ARCH_OS_WASM_VM)
25#include <emscripten.h>
26#elif defined(ARCH_OS_DARWIN)
27#include <mach/mach_time.h>
28#elif defined(ARCH_OS_WINDOWS)
37PXR_NAMESPACE_OPEN_SCOPE
49#if defined(ARCH_OS_DARWIN)
51 return mach_absolute_time();
52#elif defined(ARCH_CPU_INTEL)
55#elif defined (ARCH_CPU_ARM)
57 #if defined(ARCH_COMPILER_MSVC)
60 result = _ReadStatusReg(0x5F02);
62 __asm __volatile(
"mrs %0, CNTVCT_EL0" :
"=&r" (result));
65#elif defined(ARCH_OS_WASM_VM)
66 return static_cast<int64_t
>(emscripten_get_now() * 1e+6);
68#error Unknown architecture.
81#if defined (ARCH_OS_DARWIN) || defined(ARCH_OS_WASM_VM) || \
82 (defined (ARCH_CPU_ARM) && defined (ARCH_COMPILER_MSVC))
84#elif defined (ARCH_CPU_ARM)
85 std::atomic_signal_fence(std::memory_order_seq_cst);
86 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
87 std::atomic_signal_fence(std::memory_order_seq_cst);
88#elif defined (ARCH_COMPILER_MSVC)
90 std::atomic_signal_fence(std::memory_order_seq_cst);
93 std::atomic_signal_fence(std::memory_order_seq_cst);
94#elif defined(ARCH_CPU_INTEL) && \
95 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
97 std::atomic_signal_fence(std::memory_order_seq_cst);
110#error "Unsupported architecture."
123#if defined (ARCH_OS_DARWIN) || defined(ARCH_OS_WASM_VM) || \
124 (defined (ARCH_CPU_ARM) && defined (ARCH_COMPILER_MSVC))
126#elif defined (ARCH_CPU_ARM)
127 std::atomic_signal_fence(std::memory_order_seq_cst);
128 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
129 std::atomic_signal_fence(std::memory_order_seq_cst);
130#elif defined (ARCH_COMPILER_MSVC)
131 std::atomic_signal_fence(std::memory_order_seq_cst);
135 std::atomic_signal_fence(std::memory_order_seq_cst);
136#elif defined(ARCH_CPU_INTEL) && \
137 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
138 std::atomic_signal_fence(std::memory_order_seq_cst);
148 :
"rcx",
"rdx",
"cc");
150#error "Unsupported architecture."
155#if defined (doxygen) || \
156 (!defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_INTEL) && \
157 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC)))
174 std::atomic_signal_fence(std::memory_order_seq_cst);
179 :
"=a"(_startLow),
"=d"(_startHigh) :: );
189 return (uint64_t(_startHigh) << 32) + _startLow;
203 uint32_t stopLow, stopHigh;
204 std::atomic_signal_fence(std::memory_order_seq_cst);
208 :
"=a"(stopLow),
"=d"(stopHigh)
212 return ((uint64_t(stopHigh) << 32) + stopLow) -
213 ((uint64_t(_startHigh) << 32) + _startLow);
216 bool _started =
false;
217 uint32_t _startLow = 0, _startHigh = 0;
255 bool _started =
false;
256 uint64_t _startTicks;
312Arch_MeasureExecutionTime(uint64_t maxTicks,
bool *reachedConsensus,
313 void const *m, uint64_t (*callM)(
void const *,
int));
328 uint64_t maxTicks = 1e7,
329 bool *reachedConsensus =
nullptr)
331 auto measureN = [&fn](
int nTimes) -> uint64_t {
333 for (
int i = nTimes; i--; ) {
334 std::atomic_signal_fence(std::memory_order_seq_cst);
336 std::atomic_signal_fence(std::memory_order_seq_cst);
341 using MeasureNType =
decltype(measureN);
343 return Arch_MeasureExecutionTime(
344 maxTicks, reachedConsensus,
345 static_cast<void const *
>(&measureN),
346 [](
void const *mN,
int nTimes) {
347 return (*
static_cast<MeasureNType
const *
>(mN))(nTimes);
353PXR_NAMESPACE_CLOSE_SCOPE
uint64_t ArchGetTickTime()
Return the current time in system-dependent units.
ARCH_API uint64_t ArchGetTickQuantum()
Return the tick time resolution.
uint64_t ArchGetStartTickTime()
Get a "start" tick time for measuring an interval of time, followed by a later call to ArchGetStopTic...
ARCH_API uint64_t ArchGetIntervalTimerTickOverhead()
Return the ticks taken to record an interval of time with ArchIntervalTimer, as measured at startup t...
ARCH_API double ArchGetNanosecondsPerTick()
Get nanoseconds per tick.
uint64_t ArchGetStopTickTime()
Get a "stop" tick time for measuring an interval of time.
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,...
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
Convert a duration measured in "ticks", as returned by ArchGetTickTime(), to seconds.
ARCH_API uint64_t ArchSecondsToTicks(double seconds)
Convert a duration in seconds to "ticks", as returned by ArchGetTickTime().
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
Convert a duration measured in "ticks", as returned by ArchGetTickTime(), to nanoseconds.
A simple timer class for measuring an interval of time using the ArchTickTimer facilities.
uint64_t GetCurrentTicks()
Read and return the current time.
void Start()
Start the timer, or reset the start time if it has already been started.
uint64_t GetStartTicks() const
Return this timer's start time, or 0 if it hasn't been started.
uint64_t GetElapsedTicks()
Read the current time and return the difference between it and the start time.
ArchIntervalTimer(bool start=true)
Construct a timer and start timing if start is true.
bool IsStarted() const
Return true if this timer is started.