michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // michael@0: // Implement TimeStamp::Now() with mach_absolute_time michael@0: // michael@0: // The "tick" unit for mach_absolute_time is defined using mach_timebase_info() which michael@0: // gives a conversion ratio to nanoseconds. For more information see Apple's QA1398. michael@0: // michael@0: // This code is inspired by Chromium's time_mac.cc. The biggest michael@0: // differences are that we explicitly initialize using michael@0: // TimeStamp::Initialize() instead of lazily in Now() and that michael@0: // we store the time value in ticks and convert when needed instead michael@0: // of storing the time value in nanoseconds. michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "mozilla/TimeStamp.h" michael@0: #include "nsDebug.h" michael@0: michael@0: // Estimate of the smallest duration of time we can measure. michael@0: static uint64_t sResolution; michael@0: static uint64_t sResolutionSigDigs; michael@0: michael@0: static const uint64_t kNsPerMs = 1000000; michael@0: static const uint64_t kUsPerSec = 1000000; michael@0: static const double kNsPerMsd = 1000000.0; michael@0: static const double kNsPerSecd = 1000000000.0; michael@0: michael@0: static bool gInitialized = false; michael@0: static double sNsPerTick; michael@0: michael@0: static uint64_t michael@0: ClockTime() michael@0: { michael@0: // mach_absolute_time is it when it comes to ticks on the Mac. Other calls michael@0: // with less precision (such as TickCount) just call through to michael@0: // mach_absolute_time. michael@0: // michael@0: // At the time of writing mach_absolute_time returns the number of nanoseconds michael@0: // since boot. This won't overflow 64bits for 500+ years so we aren't going michael@0: // to worry about that possiblity michael@0: return mach_absolute_time(); michael@0: } michael@0: michael@0: static uint64_t michael@0: ClockResolutionNs() michael@0: { michael@0: uint64_t start = ClockTime(); michael@0: uint64_t end = ClockTime(); michael@0: uint64_t minres = (end - start); michael@0: michael@0: // 10 total trials is arbitrary: what we're trying to avoid by michael@0: // looping is getting unlucky and being interrupted by a context michael@0: // switch or signal, or being bitten by paging/cache effects michael@0: for (int i = 0; i < 9; ++i) { michael@0: start = ClockTime(); michael@0: end = ClockTime(); michael@0: michael@0: uint64_t candidate = (start - end); michael@0: if (candidate < minres) michael@0: minres = candidate; michael@0: } michael@0: michael@0: if (0 == minres) { michael@0: // measurable resolution is either incredibly low, ~1ns, or very michael@0: // high. fall back on NSPR's resolution assumption michael@0: minres = 1 * kNsPerMs; michael@0: } michael@0: michael@0: return minres; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: michael@0: double michael@0: TimeDuration::ToSeconds() const michael@0: { michael@0: NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early"); michael@0: return (mValue * sNsPerTick) / kNsPerSecd; michael@0: } michael@0: michael@0: double michael@0: TimeDuration::ToSecondsSigDigits() const michael@0: { michael@0: NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early"); michael@0: // don't report a value < mResolution ... michael@0: int64_t valueSigDigs = sResolution * (mValue / sResolution); michael@0: // and chop off insignificant digits michael@0: valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs); michael@0: return (valueSigDigs * sNsPerTick) / kNsPerSecd; michael@0: } michael@0: michael@0: TimeDuration michael@0: TimeDuration::FromMilliseconds(double aMilliseconds) michael@0: { michael@0: NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early"); michael@0: return TimeDuration::FromTicks(int64_t((aMilliseconds * kNsPerMsd) / sNsPerTick)); michael@0: } michael@0: michael@0: TimeDuration michael@0: TimeDuration::Resolution() michael@0: { michael@0: NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early"); michael@0: return TimeDuration::FromTicks(int64_t(sResolution)); michael@0: } michael@0: michael@0: nsresult michael@0: TimeStamp::Startup() michael@0: { michael@0: if (gInitialized) michael@0: return NS_OK; michael@0: michael@0: mach_timebase_info_data_t timebaseInfo; michael@0: // Apple's QA1398 suggests that the output from mach_timebase_info michael@0: // will not change while a program is running, so it should be safe michael@0: // to cache the result. michael@0: kern_return_t kr = mach_timebase_info(&timebaseInfo); michael@0: if (kr != KERN_SUCCESS) michael@0: NS_RUNTIMEABORT("mach_timebase_info failed"); michael@0: michael@0: sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom; michael@0: michael@0: sResolution = ClockResolutionNs(); michael@0: michael@0: // find the number of significant digits in sResolution, for the michael@0: // sake of ToSecondsSigDigits() michael@0: for (sResolutionSigDigs = 1; michael@0: !(sResolutionSigDigs == sResolution michael@0: || 10*sResolutionSigDigs > sResolution); michael@0: sResolutionSigDigs *= 10); michael@0: michael@0: gInitialized = true; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: TimeStamp::Shutdown() michael@0: { michael@0: } michael@0: michael@0: TimeStamp michael@0: TimeStamp::Now(bool aHighResolution) michael@0: { michael@0: return TimeStamp(ClockTime()); michael@0: } michael@0: michael@0: // Computes and returns the process uptime in microseconds. michael@0: // Returns 0 if an error was encountered. michael@0: michael@0: uint64_t michael@0: TimeStamp::ComputeProcessUptime() michael@0: { michael@0: struct timeval tv; michael@0: int rv = gettimeofday(&tv, nullptr); michael@0: michael@0: if (rv == -1) { michael@0: return 0; michael@0: } michael@0: michael@0: int mib[] = { michael@0: CTL_KERN, michael@0: KERN_PROC, michael@0: KERN_PROC_PID, michael@0: getpid(), michael@0: }; michael@0: u_int mibLen = sizeof(mib) / sizeof(mib[0]); michael@0: michael@0: struct kinfo_proc proc; michael@0: size_t bufferSize = sizeof(proc); michael@0: rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0); michael@0: michael@0: if (rv == -1) michael@0: return 0; michael@0: michael@0: uint64_t startTime = michael@0: ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) + michael@0: proc.kp_proc.p_un.__p_starttime.tv_usec; michael@0: uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec; michael@0: michael@0: if (startTime > now) michael@0: return 0; michael@0: michael@0: return now - startTime; michael@0: } michael@0: michael@0: } // namespace mozilla