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_DARWIN)
25#include <mach/mach_time.h>
26#elif defined(ARCH_OS_WINDOWS)
35PXR_NAMESPACE_OPEN_SCOPE
47#if defined(ARCH_OS_DARWIN)
49 return mach_absolute_time();
50#elif defined(ARCH_CPU_INTEL)
53#elif defined (ARCH_CPU_ARM)
55 __asm __volatile(
"mrs %0, CNTVCT_EL0" :
"=&r" (result));
58#error Unknown architecture.
71#if defined (ARCH_OS_DARWIN)
73#elif defined (ARCH_CPU_ARM)
74 std::atomic_signal_fence(std::memory_order_seq_cst);
75 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
76 std::atomic_signal_fence(std::memory_order_seq_cst);
77#elif defined (ARCH_COMPILER_MSVC)
79 std::atomic_signal_fence(std::memory_order_seq_cst);
82 std::atomic_signal_fence(std::memory_order_seq_cst);
83#elif defined(ARCH_CPU_INTEL) && \
84 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
86 std::atomic_signal_fence(std::memory_order_seq_cst);
99#error "Unsupported architecture."
112#if defined (ARCH_OS_DARWIN)
114#elif defined (ARCH_CPU_ARM)
115 std::atomic_signal_fence(std::memory_order_seq_cst);
116 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
117 std::atomic_signal_fence(std::memory_order_seq_cst);
118#elif defined (ARCH_COMPILER_MSVC)
119 std::atomic_signal_fence(std::memory_order_seq_cst);
123 std::atomic_signal_fence(std::memory_order_seq_cst);
124#elif defined(ARCH_CPU_INTEL) && \
125 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
126 std::atomic_signal_fence(std::memory_order_seq_cst);
136 :
"rcx",
"rdx",
"cc");
138#error "Unsupported architecture."
143#if defined (doxygen) || \
144 (!defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_INTEL) && \
145 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC)))
162 std::atomic_signal_fence(std::memory_order_seq_cst);
167 :
"=a"(_startLow),
"=d"(_startHigh) :: );
177 return (uint64_t(_startHigh) << 32) + _startLow;
191 uint32_t stopLow, stopHigh;
192 std::atomic_signal_fence(std::memory_order_seq_cst);
196 :
"=a"(stopLow),
"=d"(stopHigh)
200 return ((uint64_t(stopHigh) << 32) + stopLow) -
201 ((uint64_t(_startHigh) << 32) + _startLow);
204 bool _started =
false;
205 uint32_t _startLow = 0, _startHigh = 0;
243 bool _started =
false;
244 uint64_t _startTicks;
300Arch_MeasureExecutionTime(uint64_t maxTicks,
bool *reachedConsensus,
301 void const *m, uint64_t (*callM)(
void const *,
int));
316 uint64_t maxTicks = 1e7,
317 bool *reachedConsensus =
nullptr)
319 auto measureN = [&fn](
int nTimes) -> uint64_t {
321 for (
int i = nTimes; i--; ) {
322 std::atomic_signal_fence(std::memory_order_seq_cst);
324 std::atomic_signal_fence(std::memory_order_seq_cst);
329 using MeasureNType =
decltype(measureN);
331 return Arch_MeasureExecutionTime(
332 maxTicks, reachedConsensus,
333 static_cast<void const *
>(&measureN),
334 [](
void const *mN,
int nTimes) {
335 return (*
static_cast<MeasureNType
const *
>(mN))(nTimes);
341PXR_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.