Bumping manifests a=b2g-bump
[gecko.git] / xpcom / ds / TimeStamp_darwin.cpp
blob071d0d5a77f1985c15128b66fcb2f5d0cf9ff66a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 //
8 // Implement TimeStamp::Now() with mach_absolute_time
9 //
10 // The "tick" unit for mach_absolute_time is defined using mach_timebase_info() which
11 // gives a conversion ratio to nanoseconds. For more information see Apple's QA1398.
13 // This code is inspired by Chromium's time_mac.cc. The biggest
14 // differences are that we explicitly initialize using
15 // TimeStamp::Initialize() instead of lazily in Now() and that
16 // we store the time value in ticks and convert when needed instead
17 // of storing the time value in nanoseconds.
19 #include <mach/mach_time.h>
20 #include <sys/time.h>
21 #include <sys/sysctl.h>
22 #include <time.h>
23 #include <unistd.h>
25 #include "mozilla/TimeStamp.h"
26 #include "nsDebug.h"
28 // Estimate of the smallest duration of time we can measure.
29 static uint64_t sResolution;
30 static uint64_t sResolutionSigDigs;
32 static const uint64_t kNsPerMs = 1000000;
33 static const uint64_t kUsPerSec = 1000000;
34 static const double kNsPerMsd = 1000000.0;
35 static const double kNsPerSecd = 1000000000.0;
37 static bool gInitialized = false;
38 static double sNsPerTick;
40 static uint64_t
41 ClockTime()
43 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
44 // with less precision (such as TickCount) just call through to
45 // mach_absolute_time.
47 // At the time of writing mach_absolute_time returns the number of nanoseconds
48 // since boot. This won't overflow 64bits for 500+ years so we aren't going
49 // to worry about that possiblity
50 return mach_absolute_time();
53 static uint64_t
54 ClockResolutionNs()
56 uint64_t start = ClockTime();
57 uint64_t end = ClockTime();
58 uint64_t minres = (end - start);
60 // 10 total trials is arbitrary: what we're trying to avoid by
61 // looping is getting unlucky and being interrupted by a context
62 // switch or signal, or being bitten by paging/cache effects
63 for (int i = 0; i < 9; ++i) {
64 start = ClockTime();
65 end = ClockTime();
67 uint64_t candidate = (start - end);
68 if (candidate < minres) {
69 minres = candidate;
73 if (0 == minres) {
74 // measurable resolution is either incredibly low, ~1ns, or very
75 // high. fall back on NSPR's resolution assumption
76 minres = 1 * kNsPerMs;
79 return minres;
82 namespace mozilla {
84 double
85 BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks)
87 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
88 return (aTicks * sNsPerTick) / kNsPerSecd;
91 double
92 BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks)
94 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
95 // don't report a value < mResolution ...
96 int64_t valueSigDigs = sResolution * (aTicks / sResolution);
97 // and chop off insignificant digits
98 valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs);
99 return (valueSigDigs * sNsPerTick) / kNsPerSecd;
102 int64_t
103 BaseTimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds)
105 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
106 double result = (aMilliseconds * kNsPerMsd) / sNsPerTick;
107 if (result > INT64_MAX) {
108 return INT64_MAX;
109 } else if (result < INT64_MIN) {
110 return INT64_MIN;
113 return result;
116 int64_t
117 BaseTimeDurationPlatformUtils::ResolutionInTicks()
119 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
120 return static_cast<int64_t>(sResolution);
123 nsresult
124 TimeStamp::Startup()
126 if (gInitialized) {
127 return NS_OK;
130 mach_timebase_info_data_t timebaseInfo;
131 // Apple's QA1398 suggests that the output from mach_timebase_info
132 // will not change while a program is running, so it should be safe
133 // to cache the result.
134 kern_return_t kr = mach_timebase_info(&timebaseInfo);
135 if (kr != KERN_SUCCESS) {
136 NS_RUNTIMEABORT("mach_timebase_info failed");
139 sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom;
141 sResolution = ClockResolutionNs();
143 // find the number of significant digits in sResolution, for the
144 // sake of ToSecondsSigDigits()
145 for (sResolutionSigDigs = 1;
146 !(sResolutionSigDigs == sResolution ||
147 10 * sResolutionSigDigs > sResolution);
148 sResolutionSigDigs *= 10);
150 gInitialized = true;
152 return NS_OK;
155 void
156 TimeStamp::Shutdown()
160 TimeStamp
161 TimeStamp::Now(bool aHighResolution)
163 return TimeStamp(ClockTime());
166 // Computes and returns the process uptime in microseconds.
167 // Returns 0 if an error was encountered.
169 uint64_t
170 TimeStamp::ComputeProcessUptime()
172 struct timeval tv;
173 int rv = gettimeofday(&tv, nullptr);
175 if (rv == -1) {
176 return 0;
179 int mib[] = {
180 CTL_KERN,
181 KERN_PROC,
182 KERN_PROC_PID,
183 getpid(),
185 u_int mibLen = sizeof(mib) / sizeof(mib[0]);
187 struct kinfo_proc proc;
188 size_t bufferSize = sizeof(proc);
189 rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0);
191 if (rv == -1) {
192 return 0;
195 uint64_t startTime =
196 ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) +
197 proc.kp_proc.p_un.__p_starttime.tv_usec;
198 uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec;
200 if (startTime > now) {
201 return 0;
204 return now - startTime;
207 } // namespace mozilla