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/. */
8 // Implement TimeStamp::Now() with mach_absolute_time
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>
21 #include <sys/sysctl.h>
25 #include "mozilla/TimeStamp.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
;
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();
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
) {
68 uint64_t candidate
= (start
- end
);
69 if (candidate
< minres
)
74 // measurable resolution is either incredibly low, ~1ns, or very
75 // high. fall back on NSPR's resolution assumption
76 minres
= 1 * kNsPerMs
;
85 TimeDuration::ToSeconds() const
87 NS_ABORT_IF_FALSE(gInitialized
, "calling TimeDuration too early");
88 return (mValue
* sNsPerTick
) / kNsPerSecd
;
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
;
103 TimeDuration::FromMilliseconds(double aMilliseconds
)
105 NS_ABORT_IF_FALSE(gInitialized
, "calling TimeDuration too early");
106 return TimeDuration::FromTicks(int64_t((aMilliseconds
* kNsPerMsd
) / sNsPerTick
));
110 TimeDuration::Resolution()
112 NS_ABORT_IF_FALSE(gInitialized
, "calling TimeDuration too early");
113 return TimeDuration::FromTicks(int64_t(sResolution
));
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);
147 TimeStamp::Shutdown()
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.
161 TimeStamp::ComputeProcessUptime()
164 int rv
= gettimeofday(&tv
, nullptr);
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);
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
;
193 return now
- startTime
;
196 } // namespace mozilla