Bumping manifests a=b2g-bump
[gecko.git] / xpcom / ds / TimeStamp_darwin.cpp
blobc7f63698f903ff02d09738cf23505dccd19976a1
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 TimeDuration::ToSeconds() const
87 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
88 return (mValue * sNsPerTick) / kNsPerSecd;
91 double
92 TimeDuration::ToSecondsSigDigits() const
94 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
95 // don't report a value < mResolution ...
96 int64_t valueSigDigs = sResolution * (mValue / sResolution);
97 // and chop off insignificant digits
98 valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs);
99 return (valueSigDigs * sNsPerTick) / kNsPerSecd;
102 TimeDuration
103 TimeDuration::FromMilliseconds(double aMilliseconds)
105 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
106 return TimeDuration::FromTicks((aMilliseconds * kNsPerMsd) / sNsPerTick);
109 TimeDuration
110 TimeDuration::Resolution()
112 NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
113 return TimeDuration::FromTicks(int64_t(sResolution));
116 nsresult
117 TimeStamp::Startup()
119 if (gInitialized) {
120 return NS_OK;
123 mach_timebase_info_data_t timebaseInfo;
124 // Apple's QA1398 suggests that the output from mach_timebase_info
125 // will not change while a program is running, so it should be safe
126 // to cache the result.
127 kern_return_t kr = mach_timebase_info(&timebaseInfo);
128 if (kr != KERN_SUCCESS) {
129 NS_RUNTIMEABORT("mach_timebase_info failed");
132 sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom;
134 sResolution = ClockResolutionNs();
136 // find the number of significant digits in sResolution, for the
137 // sake of ToSecondsSigDigits()
138 for (sResolutionSigDigs = 1;
139 !(sResolutionSigDigs == sResolution ||
140 10 * sResolutionSigDigs > sResolution);
141 sResolutionSigDigs *= 10);
143 gInitialized = true;
145 return NS_OK;
148 void
149 TimeStamp::Shutdown()
153 TimeStamp
154 TimeStamp::Now(bool aHighResolution)
156 return TimeStamp(ClockTime());
159 // Computes and returns the process uptime in microseconds.
160 // Returns 0 if an error was encountered.
162 uint64_t
163 TimeStamp::ComputeProcessUptime()
165 struct timeval tv;
166 int rv = gettimeofday(&tv, nullptr);
168 if (rv == -1) {
169 return 0;
172 int mib[] = {
173 CTL_KERN,
174 KERN_PROC,
175 KERN_PROC_PID,
176 getpid(),
178 u_int mibLen = sizeof(mib) / sizeof(mib[0]);
180 struct kinfo_proc proc;
181 size_t bufferSize = sizeof(proc);
182 rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0);
184 if (rv == -1) {
185 return 0;
188 uint64_t startTime =
189 ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) +
190 proc.kp_proc.p_un.__p_starttime.tv_usec;
191 uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec;
193 if (startTime > now) {
194 return 0;
197 return now - startTime;
200 } // namespace mozilla