Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with the latest blobs from...
[gecko.git] / xpcom / ds / TimeStamp_darwin.cpp
blob75a16f3b45aadc867fd35b035611e9620b60d3ca
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 uint64_t kNsPerSec = 1000000000;
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
42 ClockTime()
44 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
45 // with less precision (such as TickCount) just call through to
46 // mach_absolute_time.
48 // At the time of writing mach_absolute_time returns the number of nanoseconds
49 // since boot. This won't overflow 64bits for 500+ years so we aren't going
50 // to worry about that possiblity
51 return mach_absolute_time();
54 static uint64_t
55 ClockResolutionNs()
57 uint64_t start = ClockTime();
58 uint64_t end = ClockTime();
59 uint64_t minres = (end - start);
61 // 10 total trials is arbitrary: what we're trying to avoid by
62 // looping is getting unlucky and being interrupted by a context
63 // switch or signal, or being bitten by paging/cache effects
64 for (int i = 0; i < 9; ++i) {
65 start = ClockTime();
66 end = ClockTime();
68 uint64_t candidate = (start - end);
69 if (candidate < minres)
70 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(int64_t((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;
122 mach_timebase_info_data_t timebaseInfo;
123 // Apple's QA1398 suggests that the output from mach_timebase_info
124 // will not change while a program is running, so it should be safe
125 // to cache the result.
126 kern_return_t kr = mach_timebase_info(&timebaseInfo);
127 if (kr != KERN_SUCCESS)
128 NS_RUNTIMEABORT("mach_timebase_info failed");
130 sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom;
132 sResolution = ClockResolutionNs();
134 // find the number of significant digits in sResolution, for the
135 // sake of ToSecondsSigDigits()
136 for (sResolutionSigDigs = 1;
137 !(sResolutionSigDigs == sResolution
138 || 10*sResolutionSigDigs > sResolution);
139 sResolutionSigDigs *= 10);
141 gInitialized = true;
143 return NS_OK;
146 void
147 TimeStamp::Shutdown()
151 TimeStamp
152 TimeStamp::Now(bool aHighResolution)
154 return TimeStamp(ClockTime());
157 // Computes and returns the process uptime in microseconds.
158 // Returns 0 if an error was encountered.
160 uint64_t
161 TimeStamp::ComputeProcessUptime()
163 struct timeval tv;
164 int rv = gettimeofday(&tv, nullptr);
166 if (rv == -1) {
167 return 0;
170 int mib[] = {
171 CTL_KERN,
172 KERN_PROC,
173 KERN_PROC_PID,
174 getpid(),
176 u_int mibLen = sizeof(mib) / sizeof(mib[0]);
178 struct kinfo_proc proc;
179 size_t bufferSize = sizeof(proc);
180 rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0);
182 if (rv == -1)
183 return 0;
185 uint64_t startTime =
186 ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) +
187 proc.kp_proc.p_un.__p_starttime.tv_usec;
188 uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec;
190 if (startTime > now)
191 return 0;
193 return now - startTime;
196 } // namespace mozilla