Debian Codegraph @

mozglue/misc/TimeStamp_darwin.cpp

raw
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/. */
6
7 //
8 // Implement TimeStamp::Now() with mach_absolute_time
9 //
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
12 // Apple's QA1398.
13 //
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.
19
20 #include <mach/mach_time.h>
21 #include <sys/time.h>
22 #include <sys/sysctl.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "mozilla/TimeStamp.h"
27 #include "mozilla/Uptime.h"
28
29 // Estimate of the smallest duration of time we can measure.
30 static uint64_t sResolution;
31 static uint64_t sResolutionSigDigs;
32
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;
37
38 static bool gInitialized = false;
39 static double sNsPerTick;
40
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.
45 //
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();
50 }
51
52 static uint64_t ClockResolutionNs() {
53 uint64_t start = ClockTime();
54 uint64_t end = ClockTime();
55 uint64_t minres = (end - start);
56
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) {
61 start = ClockTime();
62 end = ClockTime();
63
64 uint64_t candidate = (start - end);
65 if (candidate < minres) {
66 minres = candidate;
67 }
68 }
69
70 if (0 == minres) {
71 // measurable resolution is either incredibly low, ~1ns, or very
72 // high. fall back on NSPR's resolution assumption
73 minres = 1 * kNsPerMs;
74 }
75
76 return minres;
77 }
78
79 namespace mozilla {
80
81 double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) {
82 MOZ_ASSERT(gInitialized, "calling TimeDuration too early");
83 return (aTicks * sNsPerTick) / kNsPerSecd;
84 }
85
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;
93 }
94
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)) {
100 return INT64_MAX;
101 } else if (result < double(INT64_MIN)) {
102 return INT64_MIN;
103 }
104
105 return result;
106 }
107
108 int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() {
109 MOZ_ASSERT(gInitialized, "calling TimeDuration too early");
110 return static_cast<int64_t>(sResolution);
111 }
112
113 void TimeStamp::Startup() {
114 if (gInitialized) {
115 return;
116 }
117
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");
125 }
126
127 sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom;
128
129 sResolution = ClockResolutionNs();
130
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);
136
137 gInitialized = true;
138 }
139
140 void TimeStamp::Shutdown() {}
141
142 TimeStamp TimeStamp::Now(bool aHighResolution) {
143 return TimeStamp(ClockTime());
144 }
145
146 uint64_t TimeStamp::RawMachAbsoluteTimeNanoseconds() const {
147 return static_cast<uint64_t>(double(mValue) * sNsPerTick);
148 }
149
150 // Computes and returns the process uptime in microseconds.
151 // Returns 0 if an error was encountered.
152 uint64_t TimeStamp::ComputeProcessUptime() {
153 struct timeval tv;
154 int rv = gettimeofday(&tv, nullptr);
155
156 if (rv == -1) {
157 return 0;
158 }
159
160 int mib[] = {
161 CTL_KERN,
162 KERN_PROC,
163 KERN_PROC_PID,
164 getpid(),
165 };
166 u_int mibLen = sizeof(mib) / sizeof(mib[0]);
167
168 struct kinfo_proc proc;
169 size_t bufferSize = sizeof(proc);
170 rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0);
171
172 if (rv == -1) {
173 return 0;
174 }
175
176 uint64_t startTime =
177 ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) +
178 proc.kp_proc.p_un.__p_starttime.tv_usec;
179 uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec;
180
181 if (startTime > now) {
182 return 0;
183 }
184
185 return now - startTime;
186 }
187
188 } // namespace mozilla