| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| [email protected] | 89bf27e | 2013-06-27 18:04:56 | [diff] [blame] | 5 | #include "base/time/time.h" |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 6 | |
| Elly Fong-Jones | 2a9b9b8 | 2021-03-15 16:49:49 | [diff] [blame] | 7 | #import <Foundation/Foundation.h> |
| [email protected] | 4ce5975 | 2013-11-07 01:01:46 | [diff] [blame] | 8 | #include <mach/mach.h> |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 9 | #include <mach/mach_time.h> |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 10 | #include <stddef.h> |
| tfarina | 3208992 | 2015-05-18 22:14:09 | [diff] [blame] | 11 | #include <stdint.h> |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 12 | #include <sys/sysctl.h> |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 13 | #include <sys/time.h> |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 14 | #include <sys/types.h> |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 15 | #include <time.h> |
| 16 | |
| Avi Drissman | 0b20071 | 2023-08-16 20:35:49 | [diff] [blame] | 17 | #include "base/apple/mach_logging.h" |
| Avi Drissman | a09d7dd | 2023-08-17 16:26:58 | [diff] [blame] | 18 | #include "base/apple/scoped_cftyperef.h" |
| Avi Drissman | dd92b53 | 2023-08-16 22:26:17 | [diff] [blame] | 19 | #include "base/apple/scoped_mach_port.h" |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 20 | #include "base/logging.h" |
| miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 21 | #include "base/numerics/safe_conversions.h" |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 22 | #include "base/time/time_override.h" |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 23 | #include "build/build_config.h" |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 24 | |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 25 | namespace { |
| 26 | |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 27 | // Returns a pointer to the initialized Mach timebase info struct. |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 28 | mach_timebase_info_data_t* MachTimebaseInfo() { |
| Sorin Jianu | ad4cc623 | 2024-10-08 19:06:01 | [diff] [blame] | 29 | static mach_timebase_info_data_t timebase_info = [] { |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 30 | mach_timebase_info_data_t info; |
| 31 | kern_return_t kr = mach_timebase_info(&info); |
| [email protected] | 07c19ac | 2014-05-12 18:49:55 | [diff] [blame] | 32 | MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 33 | DCHECK(info.numer); |
| 34 | DCHECK(info.denom); |
| 35 | return info; |
| 36 | }(); |
| 37 | return &timebase_info; |
| 38 | } |
| 39 | |
| 40 | int64_t MachTimeToMicroseconds(uint64_t mach_time) { |
| 41 | // timebase_info gives us the conversion factor between absolute time tick |
| 42 | // units and nanoseconds. |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 43 | mach_timebase_info_data_t* timebase_info = MachTimebaseInfo(); |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 44 | |
| 45 | // Take the fast path when the conversion is 1:1. The result will for sure fit |
| 46 | // into an int_64 because we're going from nanoseconds to microseconds. |
| 47 | if (timebase_info->numer == timebase_info->denom) { |
| 48 | return static_cast<int64_t>(mach_time / |
| 49 | base::Time::kNanosecondsPerMicrosecond); |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 50 | } |
| 51 | |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 52 | uint64_t microseconds = 0; |
| 53 | const uint64_t divisor = |
| 54 | timebase_info->denom * base::Time::kNanosecondsPerMicrosecond; |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 55 | |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 56 | // Microseconds is mach_time * timebase.numer / |
| 57 | // (timebase.denom * kNanosecondsPerMicrosecond). Divide first to reduce |
| 58 | // the chance of overflow. Also stash the remainder right now, a likely |
| 59 | // byproduct of the division. |
| 60 | microseconds = mach_time / divisor; |
| 61 | const uint64_t mach_time_remainder = mach_time % divisor; |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 62 | |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 63 | // Now multiply, keeping an eye out for overflow. |
| 64 | CHECK(!__builtin_umulll_overflow(microseconds, timebase_info->numer, |
| 65 | µseconds)); |
| 66 | |
| 67 | // By dividing first we lose precision. Regain it by adding back the |
| 68 | // microseconds from the remainder, with an eye out for overflow. |
| 69 | uint64_t least_significant_microseconds = |
| 70 | (mach_time_remainder * timebase_info->numer) / divisor; |
| 71 | CHECK(!__builtin_uaddll_overflow(microseconds, least_significant_microseconds, |
| 72 | µseconds)); |
| 73 | |
| 74 | // Don't bother with the rollover handling that the Windows version does. |
| 75 | // The returned time in microseconds is enough for 292,277 years (starting |
| 76 | // from 2^63 because the returned int64_t is signed, |
| 77 | // 9223372036854775807 / (1e6 * 60 * 60 * 24 * 365.2425) = 292,277). |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 78 | return base::checked_cast<int64_t>(microseconds); |
| jameswest | 3de19856c | 2016-09-30 00:44:51 | [diff] [blame] | 79 | } |
| jameswest | 3de19856c | 2016-09-30 00:44:51 | [diff] [blame] | 80 | |
| kapishnikov | 5dc9bafa | 2017-05-24 17:26:01 | [diff] [blame] | 81 | // Returns monotonically growing number of ticks in microseconds since some |
| 82 | // unspecified starting point. |
| jameswest | 3de19856c | 2016-09-30 00:44:51 | [diff] [blame] | 83 | int64_t ComputeCurrentTicks() { |
| jameswest | 3de19856c | 2016-09-30 00:44:51 | [diff] [blame] | 84 | // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
| 85 | // with less precision (such as TickCount) just call through to |
| 86 | // mach_absolute_time. |
| Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 87 | return MachTimeToMicroseconds(mach_absolute_time()); |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 88 | } |
| 89 | |
| miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 90 | int64_t ComputeThreadTicks() { |
| Mikhail Khokhlov | dda73b4 | 2023-10-02 17:07:23 | [diff] [blame] | 91 | struct timespec ts = {}; |
| 92 | CHECK(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0); |
| 93 | base::CheckedNumeric<int64_t> absolute_micros(ts.tv_sec); |
| miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 94 | absolute_micros *= base::Time::kMicrosecondsPerSecond; |
| Mikhail Khokhlov | dda73b4 | 2023-10-02 17:07:23 | [diff] [blame] | 95 | absolute_micros += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond); |
| miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 96 | return absolute_micros.ValueOrDie(); |
| [email protected] | 4ce5975 | 2013-11-07 01:01:46 | [diff] [blame] | 97 | } |
| 98 | |
| [email protected] | f98fb5e | 2012-12-13 10:18:58 | [diff] [blame] | 99 | } // namespace |
| 100 | |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 101 | namespace base { |
| 102 | |
| 103 | // The Time routines in this file use Mach and CoreFoundation APIs, since the |
| Avi Drissman | 6fdbaf08 | 2023-06-07 19:35:09 | [diff] [blame] | 104 | // POSIX definition of time_t in macOS wraps around after 2038--and |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 105 | // there are already cookie expiration dates, etc., past that time out in |
| 106 | // the field. Using CFDate prevents that problem, and using mach_absolute_time |
| 107 | // for TimeTicks gives us nice high-resolution interval timing. |
| 108 | |
| 109 | // Time ----------------------------------------------------------------------- |
| 110 | |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 111 | namespace subtle { |
| 112 | Time TimeNowIgnoringOverride() { |
| 113 | return Time::FromCFAbsoluteTime(CFAbsoluteTimeGetCurrent()); |
| [email protected] | 2e80a19 | 2012-07-24 16:30:34 | [diff] [blame] | 114 | } |
| 115 | |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 116 | Time TimeNowFromSystemTimeIgnoringOverride() { |
| 117 | // Just use TimeNowIgnoringOverride() because it returns the system time. |
| 118 | return TimeNowIgnoringOverride(); |
| 119 | } |
| 120 | } // namespace subtle |
| 121 | |
| [email protected] | 2e80a19 | 2012-07-24 16:30:34 | [diff] [blame] | 122 | // static |
| 123 | Time Time::FromCFAbsoluteTime(CFAbsoluteTime t) { |
| avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 124 | static_assert(std::numeric_limits<CFAbsoluteTime>::has_infinity, |
| 125 | "CFAbsoluteTime must have an infinity value"); |
| Avi Drissman | 89c0d15 | 2023-08-14 23:04:47 | [diff] [blame] | 126 | if (t == 0) { |
| [email protected] | 2e80a19 | 2012-07-24 16:30:34 | [diff] [blame] | 127 | return Time(); // Consider 0 as a null Time. |
| Avi Drissman | 89c0d15 | 2023-08-14 23:04:47 | [diff] [blame] | 128 | } |
| Peter Kasting | cb6f1ec | 2020-08-20 02:37:36 | [diff] [blame] | 129 | return (t == std::numeric_limits<CFAbsoluteTime>::infinity()) |
| 130 | ? Max() |
| Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 131 | : (UnixEpoch() + |
| 132 | Seconds(double{t + kCFAbsoluteTimeIntervalSince1970})); |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 133 | } |
| 134 | |
| [email protected] | 2e80a19 | 2012-07-24 16:30:34 | [diff] [blame] | 135 | CFAbsoluteTime Time::ToCFAbsoluteTime() const { |
| avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 136 | static_assert(std::numeric_limits<CFAbsoluteTime>::has_infinity, |
| 137 | "CFAbsoluteTime must have an infinity value"); |
| Avi Drissman | 89c0d15 | 2023-08-14 23:04:47 | [diff] [blame] | 138 | if (is_null()) { |
| [email protected] | 2e80a19 | 2012-07-24 16:30:34 | [diff] [blame] | 139 | return 0; // Consider 0 as a null Time. |
| Avi Drissman | 89c0d15 | 2023-08-14 23:04:47 | [diff] [blame] | 140 | } |
| Peter Kasting | cb6f1ec | 2020-08-20 02:37:36 | [diff] [blame] | 141 | return is_max() ? std::numeric_limits<CFAbsoluteTime>::infinity() |
| 142 | : (CFAbsoluteTime{(*this - UnixEpoch()).InSecondsF()} - |
| 143 | kCFAbsoluteTimeIntervalSince1970); |
| [email protected] | 2e80a19 | 2012-07-24 16:30:34 | [diff] [blame] | 144 | } |
| 145 | |
| Elly Fong-Jones | 2a9b9b8 | 2021-03-15 16:49:49 | [diff] [blame] | 146 | // static |
| 147 | Time Time::FromNSDate(NSDate* date) { |
| 148 | DCHECK(date); |
| 149 | return FromCFAbsoluteTime(date.timeIntervalSinceReferenceDate); |
| 150 | } |
| 151 | |
| 152 | NSDate* Time::ToNSDate() const { |
| 153 | return [NSDate dateWithTimeIntervalSinceReferenceDate:ToCFAbsoluteTime()]; |
| 154 | } |
| 155 | |
| Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 156 | // TimeDelta ------------------------------------------------------------------ |
| 157 | |
| Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 158 | // static |
| 159 | TimeDelta TimeDelta::FromMachTime(uint64_t mach_time) { |
| Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 160 | return Microseconds(MachTimeToMicroseconds(mach_time)); |
| Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 161 | } |
| Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 162 | |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 163 | // TimeTicks ------------------------------------------------------------------ |
| 164 | |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 165 | namespace subtle { |
| 166 | TimeTicks TimeTicksNowIgnoringOverride() { |
| Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 167 | return TimeTicks() + Microseconds(ComputeCurrentTicks()); |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 168 | } |
| Mike Wittman | eab16764 | 2024-11-06 20:48:41 | [diff] [blame] | 169 | |
| 170 | TimeTicks TimeTicksLowResolutionNowIgnoringOverride() { |
| 171 | return TimeTicks() + Microseconds(MachTimeToMicroseconds( |
| 172 | clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW_APPROX))); |
| 173 | } |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 174 | } // namespace subtle |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 175 | |
| 176 | // static |
| miu | 5990719 | 2015-01-14 05:33:08 | [diff] [blame] | 177 | bool TimeTicks::IsHighResolution() { |
| [email protected] | cef72a9 | 2013-10-15 23:16:05 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | // static |
| majidvp | 4f3e286 | 2016-07-22 21:39:02 | [diff] [blame] | 182 | bool TimeTicks::IsConsistentAcrossProcesses() { |
| 183 | return true; |
| 184 | } |
| 185 | |
| jameswest | 3de19856c | 2016-09-30 00:44:51 | [diff] [blame] | 186 | // static |
| 187 | TimeTicks TimeTicks::FromMachAbsoluteTime(uint64_t mach_absolute_time) { |
| Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 188 | return TimeTicks(MachTimeToMicroseconds(mach_absolute_time)); |
| jameswest | 3de19856c | 2016-09-30 00:44:51 | [diff] [blame] | 189 | } |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 190 | |
| 191 | // static |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 192 | mach_timebase_info_data_t TimeTicks::SetMachTimebaseInfoForTesting( |
| 193 | mach_timebase_info_data_t timebase) { |
| 194 | mach_timebase_info_data_t orig_timebase = *MachTimebaseInfo(); |
| 195 | |
| 196 | *MachTimebaseInfo() = timebase; |
| 197 | |
| 198 | return orig_timebase; |
| Jayson Adams | 608e251f | 2021-05-27 00:26:29 | [diff] [blame] | 199 | } |
| Jayson Adams | 0c3caf9 | 2022-01-06 22:33:02 | [diff] [blame] | 200 | |
| majidvp | 4f3e286 | 2016-07-22 21:39:02 | [diff] [blame] | 201 | // static |
| charliea | fdd7d49 | 2016-03-31 19:05:18 | [diff] [blame] | 202 | TimeTicks::Clock TimeTicks::GetClock() { |
| charliea | fdd7d49 | 2016-03-31 19:05:18 | [diff] [blame] | 203 | return Clock::MAC_MACH_ABSOLUTE_TIME; |
| charliea | fdd7d49 | 2016-03-31 19:05:18 | [diff] [blame] | 204 | } |
| 205 | |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 206 | // ThreadTicks ---------------------------------------------------------------- |
| 207 | |
| 208 | namespace subtle { |
| 209 | ThreadTicks ThreadTicksNowIgnoringOverride() { |
| Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 210 | return ThreadTicks() + Microseconds(ComputeThreadTicks()); |
| [email protected] | 58cd028 | 2013-07-31 06:36:04 | [diff] [blame] | 211 | } |
| Eric Seckler | a9367935 | 2018-01-25 17:04:12 | [diff] [blame] | 212 | } // namespace subtle |
| [email protected] | 58cd028 | 2013-07-31 06:36:04 | [diff] [blame] | 213 | |
| [email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 214 | } // namespace base |