| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
| 6 | 6 |
| 7 #include <CoreFoundation/CFDate.h> | 7 #include <CoreFoundation/CFDate.h> |
| 8 #include <CoreFoundation/CFTimeZone.h> | 8 #include <CoreFoundation/CFTimeZone.h> |
| 9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
| 10 #include <mach/mach_time.h> | 10 #include <mach/mach_time.h> |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 #include <stdint.h> | 12 #include <stdint.h> |
| 13 #include <sys/sysctl.h> | 13 #include <sys/sysctl.h> |
| 14 #include <sys/time.h> | 14 #include <sys/time.h> |
| 15 #include <sys/types.h> | 15 #include <sys/types.h> |
| 16 #include <time.h> | 16 #include <time.h> |
| 17 | 17 |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/mac/mach_logging.h" | 19 #include "base/mac/mach_logging.h" |
| 20 #include "base/mac/scoped_cftyperef.h" | 20 #include "base/mac/scoped_cftyperef.h" |
| 21 #include "base/mac/scoped_mach_port.h" | 21 #include "base/mac/scoped_mach_port.h" |
| 22 #include "base/macros.h" | 22 #include "base/macros.h" |
| 23 #include "base/numerics/safe_conversions.h" | 23 #include "base/numerics/safe_conversions.h" |
| 24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 | 25 |
| 26 #if defined(OS_IOS) |
| 27 #include <time.h> |
| 28 #include "base/ios/ios_util.h" |
| 29 #endif |
| 30 |
| 26 namespace { | 31 namespace { |
| 27 | 32 |
| 28 #if defined(OS_MACOSX) && !defined(OS_IOS) | 33 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 29 int64_t MachAbsoluteTimeToTicks(uint64_t mach_absolute_time) { | 34 int64_t MachAbsoluteTimeToTicks(uint64_t mach_absolute_time) { |
| 30 static mach_timebase_info_data_t timebase_info; | 35 static mach_timebase_info_data_t timebase_info; |
| 31 if (timebase_info.denom == 0) { | 36 if (timebase_info.denom == 0) { |
| 32 // Zero-initialization of statics guarantees that denom will be 0 before | 37 // Zero-initialization of statics guarantees that denom will be 0 before |
| 33 // calling mach_timebase_info. mach_timebase_info will never set denom to | 38 // calling mach_timebase_info. mach_timebase_info will never set denom to |
| 34 // 0 as that would be invalid, so the zero-check can be used to determine | 39 // 0 as that would be invalid, so the zero-check can be used to determine |
| 35 // whether mach_timebase_info has already been called. This is | 40 // whether mach_timebase_info has already been called. This is |
| 36 // recommended by Apple's QA1398. | 41 // recommended by Apple's QA1398. |
| 37 kern_return_t kr = mach_timebase_info(&timebase_info); | 42 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 38 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; | 43 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; |
| 39 } | 44 } |
| 40 | 45 |
| 41 // timebase_info converts absolute time tick units into nanoseconds. Convert | 46 // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 42 // to microseconds up front to stave off overflows. | 47 // to microseconds up front to stave off overflows. |
| 43 base::CheckedNumeric<uint64_t> result(mach_absolute_time / | 48 base::CheckedNumeric<uint64_t> result(mach_absolute_time / |
| 44 base::Time::kNanosecondsPerMicrosecond); | 49 base::Time::kNanosecondsPerMicrosecond); |
| 45 result *= timebase_info.numer; | 50 result *= timebase_info.numer; |
| 46 result /= timebase_info.denom; | 51 result /= timebase_info.denom; |
| 47 | 52 |
| 48 // Don't bother with the rollover handling that the Windows version does. | 53 // Don't bother with the rollover handling that the Windows version does. |
| 49 // With numer and denom = 1 (the expected case), the 64-bit absolute time | 54 // With numer and denom = 1 (the expected case), the 64-bit absolute time |
| 50 // reported in nanoseconds is enough to last nearly 585 years. | 55 // reported in nanoseconds is enough to last nearly 585 years. |
| 51 return base::checked_cast<int64_t>(result.ValueOrDie()); | 56 return base::checked_cast<int64_t>(result.ValueOrDie()); |
| 52 } | 57 } |
| 53 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 58 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 54 | 59 |
| 60 // Returns monotonically growing number of ticks in microseconds since some |
| 61 // unspecified starting point. |
| 55 int64_t ComputeCurrentTicks() { | 62 int64_t ComputeCurrentTicks() { |
| 56 #if defined(OS_IOS) | 63 #if defined(OS_IOS) |
| 64 // iOS 10 supports clock_gettime(CLOCK_MONOTONIC, ...), which is |
| 65 // around 15 times faster than sysctl() call. Use it if possible; |
| 66 // otherwise, fall back to sysctl(). |
| 67 if (base::ios::IsRunningOnIOS10OrLater()) { |
| 68 struct timespec tp; |
| 69 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) { |
| 70 return tp.tv_sec * 1000000 + tp.tv_nsec / 1000; |
| 71 } |
| 72 } |
| 73 |
| 57 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 74 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| 58 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 75 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| 59 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 76 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| 60 // clock change. | 77 // clock change. |
| 61 struct timeval boottime; | 78 struct timeval boottime; |
| 62 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 79 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 63 size_t size = sizeof(boottime); | 80 size_t size = sizeof(boottime); |
| 64 int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); | 81 int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); |
| 65 DCHECK_EQ(KERN_SUCCESS, kr); | 82 DCHECK_EQ(KERN_SUCCESS, kr); |
| 66 base::TimeDelta time_difference = | 83 base::TimeDelta time_difference = |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 return Clock::MAC_MACH_ABSOLUTE_TIME; | 291 return Clock::MAC_MACH_ABSOLUTE_TIME; |
| 275 #endif // defined(OS_IOS) | 292 #endif // defined(OS_IOS) |
| 276 } | 293 } |
| 277 | 294 |
| 278 // static | 295 // static |
| 279 ThreadTicks ThreadTicks::Now() { | 296 ThreadTicks ThreadTicks::Now() { |
| 280 return ThreadTicks(ComputeThreadTicks()); | 297 return ThreadTicks(ComputeThreadTicks()); |
| 281 } | 298 } |
| 282 | 299 |
| 283 } // namespace base | 300 } // namespace base |
| OLD | NEW |