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 #if defined(ARCH_COMPILER_MSVC)
58 result = _ReadStatusReg(0x5F02);
60 __asm __volatile(
"mrs %0, CNTVCT_EL0" :
"=&r" (result));
64#error Unknown architecture.
77#if defined (ARCH_OS_DARWIN) || \
78 (defined (ARCH_CPU_ARM) && defined (ARCH_COMPILER_MSVC))
80#elif defined (ARCH_CPU_ARM)
81 std::atomic_signal_fence(std::memory_order_seq_cst);
82 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
83 std::atomic_signal_fence(std::memory_order_seq_cst);
84#elif defined (ARCH_COMPILER_MSVC)
86 std::atomic_signal_fence(std::memory_order_seq_cst);
89 std::atomic_signal_fence(std::memory_order_seq_cst);
90#elif defined(ARCH_CPU_INTEL) && \
91 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
93 std::atomic_signal_fence(std::memory_order_seq_cst);
106#error "Unsupported architecture."
119#if defined (ARCH_OS_DARWIN) || \
120 (defined (ARCH_CPU_ARM) && defined (ARCH_COMPILER_MSVC))
122#elif defined (ARCH_CPU_ARM)
123 std::atomic_signal_fence(std::memory_order_seq_cst);
124 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(t));
125 std::atomic_signal_fence(std::memory_order_seq_cst);
126#elif defined (ARCH_COMPILER_MSVC)
127 std::atomic_signal_fence(std::memory_order_seq_cst);
131 std::atomic_signal_fence(std::memory_order_seq_cst);
132#elif defined(ARCH_CPU_INTEL) && \
133 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC))
134 std::atomic_signal_fence(std::memory_order_seq_cst);
144 :
"rcx",
"rdx",
"cc");
146#error "Unsupported architecture."
151#if defined (doxygen) || \
152 (!defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_INTEL) && \
153 (defined(ARCH_COMPILER_CLANG) || defined(ARCH_COMPILER_GCC)))
170 std::atomic_signal_fence(std::memory_order_seq_cst);
175 :
"=a"(_startLow),
"=d"(_startHigh) :: );
185 return (uint64_t(_startHigh) << 32) + _startLow;
199 uint32_t stopLow, stopHigh;
200 std::atomic_signal_fence(std::memory_order_seq_cst);
204 :
"=a"(stopLow),
"=d"(stopHigh)
208 return ((uint64_t(stopHigh) << 32) + stopLow) -
209 ((uint64_t(_startHigh) << 32) + _startLow);
212 bool _started =
false;
213 uint32_t _startLow = 0, _startHigh = 0;
251 bool _started =
false;
252 uint64_t _startTicks;
308Arch_MeasureExecutionTime(uint64_t maxTicks,
bool *reachedConsensus,
309 void const *m, uint64_t (*callM)(
void const *,
int));
324 uint64_t maxTicks = 1e7,
325 bool *reachedConsensus =
nullptr)
327 auto measureN = [&fn](
int nTimes) -> uint64_t {
329 for (
int i = nTimes; i--; ) {
330 std::atomic_signal_fence(std::memory_order_seq_cst);
332 std::atomic_signal_fence(std::memory_order_seq_cst);
337 using MeasureNType =
decltype(measureN);
339 return Arch_MeasureExecutionTime(
340 maxTicks, reachedConsensus,
341 static_cast<void const *
>(&measureN),
342 [](
void const *mN,
int nTimes) {
343 return (*
static_cast<MeasureNType
const *
>(mN))(nTimes);
349PXR_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.