17 #ifndef AGX_HIGHACCURACYTIMER_H
18 #define AGX_HIGHACCURACYTIMER_H
20 #include <agx/config/AGX_USE_CPP11_TIMERS.h>
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
29 #include <mach/mach_time.h>
33 #if AGX_USE_CPP11_TIMERS()
38 #include <mach/mach_time.h>
90 void reset(
bool startAfterReset =
false);
99 UInt64 getNumTicks()
const;
105 bool isRunning()
const;
108 static UInt64 getCurrentTick();
114 static void initClockSpeed();
120 static UInt64 getCurrentTickWindows();
125 #if AGX_USE_CPP11_TIMERS()
126 std::high_resolution_clock::time_point m_start;
127 std::duration<agx::Real64> m_total;
135 static agx::Real s_highAccuracyClockSpeed;
143 if (startImmediately)
155 #if AGX_USE_CPP11_TIMERS()
156 m_start = std::high_resolution_clock::now();
168 #if AGX_USE_CPP11_TIMERS()
169 std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
170 std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
174 m_total += (now - m_start);
211 #if AGX_USE_CPP11_TIMERS()
214 std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
215 std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
216 std::duration<Real64> total = m_total + duration;
217 return total.count() *
Real64(1000);
221 return m_total.count() *
Real64(1000);;
233 agxAssert1(s_highAccuracyClockSpeed >= 0,
"HighAccuracyTimer class not properly initialized, forgot to call agx::init() ?");
236 return s_highAccuracyClockSpeed;
242 #if defined(_MSC_VER)
243 const UInt64 retVal = getCurrentTickWindows();
245 #elif defined(AGX_APPLE_IOS) || defined(__APPLE__)
246 return mach_absolute_time();
247 #elif defined (__GNUC__) && ! defined(__INTEL_COMPILER) && defined(__ia64__)
248 unsigned long long int val;
249 __asm__ __volatile__(
"mov %0=ar.itc" :
"=r"(val) ::
"memory");
251 #elif defined(__INTEL_COMPILER) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
252 return __getReg(_IA64_REG_AR_ITC);
253 #elif defined(__ECC) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
254 return __getReg(_IA64_REG_AR_ITC);
255 #elif (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__)) && !defined(HAVE_TICK_COUNTER)
270 unsigned long long int lowbits, highbits;
271 __asm__ __volatile__(
"rdtsc":
"=a" (lowbits),
"=d"(highbits) : :
"ebx",
"ecx");
273 unsigned long int lowbits, highbits;
274 __asm__ __volatile__(
"pushal\n\t" "cpuid\n\t" "popal\n\t" "rdtsc":
"=a" (lowbits),
"=d"(highbits));
278 return (((
unsigned long long int)highbits << 32) | (
unsigned long long int)lowbits);
281 agxAbort1(
"Timer::getCurrentTick failed!");
290 return clockSpeed() * (
Real64)ticks;