1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 // Implement TimeStamp::Now() with mach_absolute_time
10 // The "tick" unit for mach_absolute_time is defined using mach_timebase_info()
11 // which gives a conversion ratio to nanoseconds. For more information see
14 // This code is inspired by Chromium's time_mac.cc. The biggest
15 // differences are that we explicitly initialize using
16 // TimeStamp::Initialize() instead of lazily in Now() and that
17 // we store the time value in ticks and convert when needed instead
18 // of storing the time value in nanoseconds.
20 #include <mach/mach_time.h>
22 #include <sys/sysctl.h>
26 #include "mozilla/TimeStamp.h"
27 #include "mozilla/Uptime.h"
29 // Estimate of the smallest duration of time we can measure.
30 static uint64_t sResolution
;
31 static uint64_t sResolutionSigDigs
;
33 static const uint64_t kNsPerMs
= 1000000;
34 static const uint64_t kUsPerSec
= 1000000;
35 static const double kNsPerMsd
= 1000000.0;
36 static const double kNsPerSecd
= 1000000000.0;
38 static bool gInitialized
= false;
39 static double sNsPerTick
;
41 static uint64_t ClockTime() {
42 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
43 // with less precision (such as TickCount) just call through to
44 // mach_absolute_time.
46 // At the time of writing mach_absolute_time returns the number of nanoseconds
47 // since boot. This won't overflow 64bits for 500+ years so we aren't going
48 // to worry about that possiblity
49 return mach_absolute_time();
52 static uint64_t ClockResolutionNs() {
53 uint64_t start
= ClockTime();
54 uint64_t end
= ClockTime();
55 uint64_t minres
= (end
- start
);
57 // 10 total trials is arbitrary: what we're trying to avoid by
58 // looping is getting unlucky and being interrupted by a context
59 // switch or signal, or being bitten by paging/cache effects
60 for (int i
= 0; i
< 9; ++i
) {
64 uint64_t candidate
= (start
- end
);
65 if (candidate
< minres
) {
71 // measurable resolution is either incredibly low, ~1ns, or very
72 // high. fall back on NSPR's resolution assumption
73 minres
= 1 * kNsPerMs
;
81 double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks
) {
82 MOZ_ASSERT(gInitialized
, "calling TimeDuration too early");
83 return (aTicks
* sNsPerTick
) / kNsPerSecd
;
86 double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks
) {
87 MOZ_ASSERT(gInitialized
, "calling TimeDuration too early");
88 // don't report a value < mResolution ...
89 int64_t valueSigDigs
= sResolution
* (aTicks
/ sResolution
);
90 // and chop off insignificant digits
91 valueSigDigs
= sResolutionSigDigs
* (valueSigDigs
/ sResolutionSigDigs
);
92 return (valueSigDigs
* sNsPerTick
) / kNsPerSecd
;
95 int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds(
96 double aMilliseconds
) {
97 MOZ_ASSERT(gInitialized
, "calling TimeDuration too early");
98 double result
= (aMilliseconds
* kNsPerMsd
) / sNsPerTick
;
99 if (result
> double(INT64_MAX
)) {
101 } else if (result
< double(INT64_MIN
)) {
108 int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() {
109 MOZ_ASSERT(gInitialized
, "calling TimeDuration too early");
110 return static_cast<int64_t>(sResolution
);
113 void TimeStamp::Startup() {
118 mach_timebase_info_data_t timebaseInfo
;
119 // Apple's QA1398 suggests that the output from mach_timebase_info
120 // will not change while a program is running, so it should be safe
121 // to cache the result.
122 kern_return_t kr
= mach_timebase_info(&timebaseInfo
);
123 if (kr
!= KERN_SUCCESS
) {
124 MOZ_RELEASE_ASSERT(false, "mach_timebase_info failed");
127 sNsPerTick
= double(timebaseInfo
.numer
) / timebaseInfo
.denom
;
129 sResolution
= ClockResolutionNs();
131 // find the number of significant digits in sResolution, for the
132 // sake of ToSecondsSigDigits()
133 for (sResolutionSigDigs
= 1; !(sResolutionSigDigs
== sResolution
||
134 10 * sResolutionSigDigs
> sResolution
);
135 sResolutionSigDigs
*= 10)
141 void TimeStamp::Shutdown() {}
143 TimeStamp
TimeStamp::Now(bool aHighResolution
) {
144 return TimeStamp(ClockTime());
147 uint64_t TimeStamp::RawMachAbsoluteTimeNanoseconds() const {
148 return static_cast<uint64_t>(double(mValue
) * sNsPerTick
);
151 // Computes and returns the process uptime in microseconds.
152 // Returns 0 if an error was encountered.
153 uint64_t TimeStamp::ComputeProcessUptime() {
155 int rv
= gettimeofday(&tv
, nullptr);
167 u_int mibLen
= sizeof(mib
) / sizeof(mib
[0]);
169 struct kinfo_proc proc
;
170 size_t bufferSize
= sizeof(proc
);
171 rv
= sysctl(mib
, mibLen
, &proc
, &bufferSize
, nullptr, 0);
178 ((uint64_t)proc
.kp_proc
.p_un
.__p_starttime
.tv_sec
* kUsPerSec
) +
179 proc
.kp_proc
.p_un
.__p_starttime
.tv_usec
;
180 uint64_t now
= (tv
.tv_sec
* kUsPerSec
) + tv
.tv_usec
;
182 if (startTime
> now
) {
186 return now
- startTime
;
189 } // namespace mozilla