double Clock::mach_time_to_sec(uint64_t elapsed) { if ( sTimebaseInfo.denom == 0 ) { // initialize (yuk, hope it isn't some stray value) (void) mach_timebase_info(&sTimebaseInfo); } double ms = (double) (elapsed) / 1.0e9; ms *= sTimebaseInfo.numer / sTimebaseInfo.denom; return ms; }
void synctime(void) { LOG_INFO("Reseting time"); orwl_timebase = 0.0; orwl_timestart = 0; mach_timebase_info_data_t tb = { 0 }; mach_timebase_info(&tb); orwl_timebase = tb.numer; orwl_timebase /= tb.denom; orwl_timestart = mach_absolute_time(); LOG_INFO("Time reset!"); return; }
void timeThis(void) { #ifdef __APPLE__ static uint64_t stime=0, etime; mach_timebase_info_data_t info; uint64_t diff, titm; if (stime) { etime = mach_absolute_time(); mach_timebase_info(&info); diff = (etime - stime) * info.numer / info.denom; titm = (diff * 100) / insertcnt; printf("Time spent: %lld (per item %lld.%lld) ns\n", diff, titm / 100, titm % 100); stime = etime = 0; } else { stime = mach_absolute_time(); } #elif _WIN32 static LONGLONG stime=0, etime; LONGLONG freq; uint64_t tmp1, tmp2, diff, titm; if (stime) { QueryPerformanceCounter((LARGE_INTEGER*)&etime); QueryPerformanceFrequency((LARGE_INTEGER*)&freq); diff = etime - stime; tmp1 = (diff * 1000000) / freq; tmp2 = ((diff * 1000000) % freq) * 1000 / freq; diff = tmp1 * 1000 + tmp2; titm = (diff * 100) / insertcnt; printf("Time spent: %lld (per item %lld.%lld) ns\n", diff, titm / 100, titm % 100); stime = etime = 0; } else { QueryPerformanceCounter((LARGE_INTEGER*)&stime); } #elif __linux__ static struct timespec stime, etime; static int started = 0; uint64_t t0, t1, diff, titm; if (started) { clock_gettime(CLOCK_MONOTONIC_RAW, &etime); t0 = stime.tv_nsec + stime.tv_sec * 1000000000; t1 = etime.tv_nsec + etime.tv_sec * 1000000000; diff = t1 - t0; titm = (diff * 100) / insertcnt; printf("Time spent: %lld (per item %lld.%lld) ns\n", diff, titm / 100, titm % 100); started = 0; } else { started = 1; clock_gettime(CLOCK_MONOTONIC_RAW, &stime); } #endif }
/*static*/ int64 Profiler::GetTimerFrequency() { #if _IOS mach_timebase_info_data_t info; mach_timebase_info(&info); return 1000000000 * info.numer / info.denom; #else LARGE_INTEGER performanceFrequency; ::QueryPerformanceFrequency(&performanceFrequency); return performanceFrequency.QuadPart; #endif }
Stopwatch::Stopwatch() { #if defined (TARGET_OS_WINDOWS) RecalculateFrequency(); #elif defined (TARGET_OS_MACOSX) mach_timebase_info(&m_timebase); #endif /* We start it here for static Stopwatch instances */ /* where it's impractical to do an initial Start() call */ Start(); }
void NaClThreadNiceInit() { if (mach_timebase_info(&gTimeBase) != KERN_SUCCESS) { NaClLog(LOG_WARNING, "mach_timebase_info failed\n"); gTimeBase.numer = 1; gTimeBase.denom = 1; } if (gTimeBase.denom == 0) { NaClLog(LOG_WARNING, "mach_timebase_info returned bogus result\n"); gTimeBase.numer = 1; gTimeBase.denom = 1; } }
void EosTimer::Init() { #ifndef WIN32 if(sm_toMS == 0) { mach_timebase_info_data_t timeBase; mach_timebase_info( &timeBase ); sm_toMS = timeBase.numer/static_cast<double>(timeBase.denom); sm_toMS /= 1000000; } #endif }
static frame_time_t machdep_mach_gettimebase (void) { struct mach_timebase_info tbinfo; mach_timebase_info(&tbinfo); /* time (in nanosecs) = ticks * tbinfo.numer / tbinfo.numer * * Thus we get the number of ticks per second as follows: */ return (frame_time_t)(1e9 * tbinfo.denom / tbinfo.numer); }
double al_time_current_time(void) { static dispatch_once_t init_once = 0; static mach_timebase_info_data_t timebase_info; dispatch_once(&init_once, ^{ mach_timebase_info((mach_timebase_info_data_t *)&timebase_info); });
double FTime::Update() { #if defined TARGET_OSX uint64_t now = mach_absolute_time(); uint64_t diff = now - time; static double conversion = 0.0; if ( conversion == 0.0 ) { mach_timebase_info_data_t info; kern_return_t err = mach_timebase_info( &info ); if( err == 0 ) conversion = 1e-9*(double)info.numer / (double) info.denom; } time = now; last_update_time = (double)(conversion * (double) diff); #elif defined TARGET_WIN32 LARGE_INTEGER now; QueryPerformanceCounter(&now); LARGE_INTEGER diff; diff.QuadPart = now.QuadPart - time.QuadPart; static double conversion = 0.0; if ( conversion == 0.0 ) { LARGE_INTEGER frequency; QueryPerformanceFrequency(&frequency); conversion = 1.0/double(frequency.QuadPart); } time.QuadPart = now.QuadPart; last_update_time = double(now.QuadPart)*conversion; #else timespec now; clock_gettime(CLOCK_REALTIME, &now); // calculate diff double diff = now.tv_sec - time.tv_sec; diff += 1e-9*double(now.tv_nsec - time.tv_nsec); time = now; last_update_time = diff; #endif return last_update_time; }
time_t lgtd_time_monotonic_msecs(void) { static mach_timebase_info_data_t timebase = { 0, 0 }; if (timebase.denom == 0) { mach_timebase_info(&timebase); } uint64_t time = mach_absolute_time(); return time * timebase.numer / timebase.denom / MSECS_IN_NSEC; }
uint64_t tut_get_time() { uint64_t t; static mach_timebase_info_data_t tinfo; t = mach_absolute_time(); if (tinfo.denom == 0) { mach_timebase_info(&tinfo); } return t * tinfo.numer / tinfo.denom; }
uint64_t monotonic_nseconds() { uint64_t c; static uint8_t i = 0; static mach_timebase_info_data_t sti; c = mach_absolute_time(); if (i==0) { mach_timebase_info(&sti); i = 1; } return c * sti.numer / sti.denom; }
int get_nanoseconds_since_epoch(void *data, uint64_t *nanoseconds) { mach_timebase_info_data_t conversion_factor; GUARD(mach_timebase_info(&conversion_factor)); *nanoseconds = mach_absolute_time(); *nanoseconds *= conversion_factor.numer; *nanoseconds /= conversion_factor.denom; return 0; }
int clock_gettime(clockid_t clk_id, struct timespec *tp) { kern_return_t ret; clock_serv_t clk; clock_id_t clk_serv_id; mach_timespec_t tm; uint64_t start, end, delta, nano; task_basic_info_data_t tinfo; task_thread_times_info_data_t ttinfo; mach_msg_type_number_t tflag; int retval = -1; switch (clk_id) { case CLOCK_REALTIME: case CLOCK_MONOTONIC: clk_serv_id = clk_id == CLOCK_REALTIME ? CALENDAR_CLOCK : SYSTEM_CLOCK; if (KERN_SUCCESS == (ret = host_get_clock_service(mach_host_self(), clk_serv_id, &clk))) { if (KERN_SUCCESS == (ret = clock_get_time(clk, &tm))) { tp->tv_sec = tm.tv_sec; tp->tv_nsec = tm.tv_nsec; retval = 0; } } if (KERN_SUCCESS != ret) { errno = EINVAL; retval = -1; } break; case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_THREAD_CPUTIME_ID: start = mach_absolute_time(); if (clk_id == CLOCK_PROCESS_CPUTIME_ID) { getpid(); } else { sched_yield(); } end = mach_absolute_time(); delta = end - start; if (0 == __clock_gettime_inf.denom) { mach_timebase_info(&__clock_gettime_inf); } nano = delta * __clock_gettime_inf.numer / __clock_gettime_inf.denom; tp->tv_sec = nano * 1e-9; tp->tv_nsec = nano - (tp->tv_sec * 1e9); retval = 0; break; default: errno = EINVAL; retval = -1; } return retval; }
int os_get_reltime(struct os_reltime *t) { #ifndef __MACH__ #if defined(CLOCK_BOOTTIME) static clockid_t clock_id = CLOCK_BOOTTIME; #elif defined(CLOCK_MONOTONIC) static clockid_t clock_id = CLOCK_MONOTONIC; #else static clockid_t clock_id = CLOCK_REALTIME; #endif struct timespec ts; int res; while (1) { res = clock_gettime(clock_id, &ts); if (res == 0) { t->sec = ts.tv_sec; t->usec = ts.tv_nsec / 1000; return 0; } switch (clock_id) { #ifdef CLOCK_BOOTTIME case CLOCK_BOOTTIME: clock_id = CLOCK_MONOTONIC; break; #endif #ifdef CLOCK_MONOTONIC case CLOCK_MONOTONIC: clock_id = CLOCK_REALTIME; break; #endif case CLOCK_REALTIME: return -1; } } #else /* __MACH__ */ uint64_t abstime, nano; static mach_timebase_info_data_t info = { 0, 0 }; if (!info.denom) { if (mach_timebase_info(&info) != KERN_SUCCESS) return -1; } abstime = mach_absolute_time(); nano = (abstime * info.numer) / info.denom; t->sec = nano / NSEC_PER_SEC; t->usec = (nano - (((uint64_t) t->sec) * NSEC_PER_SEC)) / NSEC_PER_USEC; return 0; #endif /* __MACH__ */ }
int clock_gettime(int clk_id, struct timespec *t) { mach_timebase_info_data_t timebase; mach_timebase_info(&timebase); uint64_t time; time = mach_absolute_time(); double nseconds = ((double)time * (double)timebase.numer)/((double)timebase.denom); double seconds = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e9); t->tv_sec = seconds; t->tv_nsec = nseconds; return 0; }
float Timer::resolution() { static double conversion = 0.0; if ( conversion == 0.0 ) { mach_timebase_info_data_t info; kern_return_t err = mach_timebase_info( &info ); if( err == 0 ) conversion = 1e-9 * (double) info.numer / (double) info.denom; } return conversion; }
uint64_t nn_clock_ms (void) { #if defined NN_HAVE_WINDOWS LARGE_INTEGER tps; LARGE_INTEGER time; double tpms; QueryPerformanceFrequency (&tps); QueryPerformanceCounter (&time); tpms = (double) (tps.QuadPart / 1000); return (uint64_t) (time.QuadPart / tpms); #elif defined NN_HAVE_OSX static mach_timebase_info_data_t nn_clock_timebase_info; uint64_t ticks; /* If the global timebase info is not initialised yet, init it. */ if (nn_slow (!nn_clock_timebase_info.denom)) mach_timebase_info (&nn_clock_timebase_info); ticks = mach_absolute_time (); return ticks * nn_clock_timebase_info.numer / nn_clock_timebase_info.denom / 1000000; #elif defined NN_HAVE_CLOCK_MONOTONIC int rc; struct timespec tv; rc = clock_gettime (CLOCK_MONOTONIC, &tv); errno_assert (rc == 0); return tv.tv_sec * (uint64_t) 1000 + tv.tv_nsec / 1000000; #elif defined NN_HAVE_GETHRTIME return gethrtime () / 1000000; #else int rc; struct timeval tv; /* Gettimeofday is slow on some systems. Moreover, it's not necessarily monotonic. Thus, it's used as a last resort mechanism. */ rc = gettimeofday (&tv, NULL); errno_assert (rc == 0); return tv.tv_sec * (uint64_t) 1000 + tv.tv_usec / 1000; #endif }
// Get current time in microseconds... UInt64 GetNanoseconds(void) { #if defined(OVR_CAPTURE_HAS_MACH_ABSOLUTE_TIME) // OSX/iOS doesn't have clock_gettime()... but it does have gettimeofday(), or even better mach_absolute_time() // which is about 50% faster than gettimeofday() and higher precision! // Only 24.5ns per GetNanoseconds() call! But we can do better... // It seems that modern Darwin already returns nanoseconds, so numer==denom // when we test that assumption it brings us down to 16ns per GetNanoseconds() call!!! // Timed on MacBookPro running OSX. static mach_timebase_info_data_t info = {0}; if(!info.denom) mach_timebase_info(&info); const UInt64 t = mach_absolute_time(); if(info.numer==info.denom) return t; return (t * info.numer) / info.denom; #elif defined(OVR_CAPTURE_HAS_CLOCK_GETTIME) // 23ns per call on i7 Desktop running Ubuntu 64 // >800ns per call on Galaxy Note 4 running Android 4.3!!! struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); return ((UInt64)tp.tv_sec)*1000000000 + (UInt64)tp.tv_nsec; #elif defined(OVR_CAPTURE_HAS_GETTIMEOFDAY) // Just here for reference... this timer is only microsecond level of precision, and >2x slower than the mach timer... // And on non-mach platforms clock_gettime() is the preferred method... // 34ns per call on MacBookPro running OSX... // 23ns per call on i7 Desktop running Ubuntu 64 // >800ns per call on Galaxy Note 4 running Android 4.3!!! struct timeval tv; gettimeofday(&tv, 0); const UInt64 us = ((UInt64)tv.tv_sec)*1000000 + (UInt64)tv.tv_usec; return us*1000; #elif defined(OVR_CAPTURE_WINDOWS) static double tonano = 0.0; if(!tonano) { LARGE_INTEGER f; QueryPerformanceFrequency(&f); tonano = 1000000000.0 / f.QuadPart; } LARGE_INTEGER c; QueryPerformanceCounter(&c); return (UInt64)(c.QuadPart * tonano); #else #error Unknown Platform! #endif }
uint32_t CpuTicks::now() { // Initialize the first time CpuTicks::now() is called (See Apple's QA1398). if (CpuTicks_machTime.denom == 0) { if (mach_timebase_info(&CpuTicks_machTime) != KERN_SUCCESS) return 0; } // mach_absolute_time() returns nanoseconds, we need just milliseconds. uint64_t t = mach_absolute_time() / 1000000; t = t * CpuTicks_machTime.numer / CpuTicks_machTime.denom; return static_cast<uint32_t>(t & 0xFFFFFFFFU); }
double OSXTimer::getSecondCoeff() { // If this is the first time we've run, get the timebase. if (mSecondCoeff == 0.0) { mach_timebase_info_data_t timebaseInfo; mach_timebase_info(&timebaseInfo); mSecondCoeff = timebaseInfo.numer * (1.0 / 1000000000) / timebaseInfo.denom; } return mSecondCoeff; }
void RXTimingUpdateTimebase(void) { mach_timebase_info_data_t info; kern_return_t err = mach_timebase_info(&info); debug_assert(err == KERN_SUCCESS); (void)err; // compute the timebase to seconds scale factor g_RXTimebase = 1e-9 * (double)info.numer / (double)info.denom; // compute its inverse to convert from seconds to timebase g_RX1_Timebase = 1.0 / g_RXTimebase; }
double BenchSysTimer::endWall() { uint64_t end_wall = mach_absolute_time(); uint64_t elapsed = end_wall - this->fStartWall; mach_timebase_info_data_t sTimebaseInfo; if (KERN_SUCCESS != mach_timebase_info(&sTimebaseInfo)) { return 0; } else { uint64_t elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom; return elapsedNano / 1000000; } }
//----------------------------------------------------------------------------- uint32_t IPlatformFrame::getTicks () { static struct mach_timebase_info timebaseInfo; static bool initialized = false; if (!initialized) { initialized = true; mach_timebase_info (&timebaseInfo); } uint64_t absTime = mach_absolute_time (); double d = (absTime / timebaseInfo.denom) * timebaseInfo.numer; // nano seconds return static_cast<uint32_t> (d / 1000000); }
double monotonicallyIncreasingTime() { // Based on listing #2 from Apple QA 1398, but modified to be thread-safe. static mach_timebase_info_data_t timebaseInfo; static std::once_flag initializeTimerOnceFlag; std::call_once(initializeTimerOnceFlag, [] { kern_return_t kr = mach_timebase_info(&timebaseInfo); ASSERT_UNUSED(kr, kr == KERN_SUCCESS); ASSERT(timebaseInfo.denom); }); return (mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom); }
static inline void get_nanotime(struct timespec *ts) { static struct mach_timebase_info ti; if (ti.numer == 0) mach_timebase_info(&ti); uint64_t abstime = mach_absolute_time(); uint64_t nanoseconds = (abstime * ti.numer) / ti.denom; ts->tv_sec = nanoseconds / NSEC_PER_SEC; ts->tv_nsec = nanoseconds % NSEC_PER_SEC; }
static uint64_t timer() { static mach_timebase_info_data_t sTimebaseInfo; uint64_t timeNano; if ( sTimebaseInfo.denom == 0 ) { (void) mach_timebase_info(&sTimebaseInfo); } timeNano = mach_absolute_time(); return (uint64_t) (timeNano * sTimebaseInfo.numer) / (sTimebaseInfo.denom * 1000000); }
double Timer::getTimerPeriod() { #if defined(LOVE_MACOSX) || defined(LOVE_IOS) mach_timebase_info_data_t info; mach_timebase_info(&info); return (double) info.numer / (double) info.denom / 1000000000.0; #elif defined(LOVE_WINDOWS) LARGE_INTEGER temp; if (QueryPerformanceFrequency(&temp) != 0 && temp.QuadPart != 0) return 1.0 / (double) temp.QuadPart; #endif return 0; }
double subtractTimes( uint64_t endTime, uint64_t startTime ) { uint64_t difference = endTime - startTime; static double conversion = 0.0; if ( conversion == 0.0 ) { mach_timebase_info_data_t info; kern_return_t err = mach_timebase_info( &info ); if( err == 0 ) conversion = 1e-9 * (double) info.numer / (double) info.denom; } return conversion * (double) difference; }