Chromium Code Reviews| Index: base/time/time_mac.cc |
| diff --git a/base/time/time_mac.cc b/base/time/time_mac.cc |
| index 8f589166f0e343d6b3ec5053519b3a8ef7d4be61..ce032881f6c814f7b3d2ec4ce8a4c98eaa27b8a8 100644 |
| --- a/base/time/time_mac.cc |
| +++ b/base/time/time_mac.cc |
| @@ -25,22 +25,7 @@ |
| namespace { |
| -int64_t ComputeCurrentTicks() { |
| -#if defined(OS_IOS) |
| - // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| - // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| - // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| - // clock change. |
| - struct timeval boottime; |
| - int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| - size_t size = sizeof(boottime); |
| - int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); |
| - DCHECK_EQ(KERN_SUCCESS, kr); |
| - base::TimeDelta time_difference = base::Time::Now() - |
| - (base::Time::FromTimeT(boottime.tv_sec) + |
| - base::TimeDelta::FromMicroseconds(boottime.tv_usec)); |
| - return time_difference.InMicroseconds(); |
| -#else |
| +int64_t MachAbsoluteTimeToTicks(uint64_t mach_absolute_time) { |
|
miu
2016/09/16 18:35:58
Please wrap this function with:
#if defined(OS_
jameswest
2016/09/19 23:32:36
Done.
|
| static mach_timebase_info_data_t timebase_info; |
| if (timebase_info.denom == 0) { |
| // Zero-initialization of statics guarantees that denom will be 0 before |
| @@ -52,14 +37,10 @@ int64_t ComputeCurrentTicks() { |
| MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; |
| } |
| - // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
| - // with less precision (such as TickCount) just call through to |
| - // mach_absolute_time. |
| - |
| // timebase_info converts absolute time tick units into nanoseconds. Convert |
| // to microseconds up front to stave off overflows. |
| - base::CheckedNumeric<uint64_t> result( |
| - mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond); |
| + base::CheckedNumeric<uint64_t> result(mach_absolute_time / |
| + base::Time::kNanosecondsPerMicrosecond); |
| result *= timebase_info.numer; |
| result /= timebase_info.denom; |
| @@ -67,6 +48,28 @@ int64_t ComputeCurrentTicks() { |
| // With numer and denom = 1 (the expected case), the 64-bit absolute time |
| // reported in nanoseconds is enough to last nearly 585 years. |
| return base::checked_cast<int64_t>(result.ValueOrDie()); |
| +} |
| + |
| +int64_t ComputeCurrentTicks() { |
| +#if defined(OS_IOS) |
| + // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| + // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| + // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| + // clock change. |
| + struct timeval boottime; |
| + int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| + size_t size = sizeof(boottime); |
| + int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); |
| + DCHECK_EQ(KERN_SUCCESS, kr); |
| + base::TimeDelta time_difference = |
| + base::Time::Now() - (base::Time::FromTimeT(boottime.tv_sec) + |
| + base::TimeDelta::FromMicroseconds(boottime.tv_usec)); |
| + return time_difference.InMicroseconds(); |
| +#else |
| + // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
| + // with less precision (such as TickCount) just call through to |
| + // mach_absolute_time. |
| + return MachAbsoluteTimeToTicks(mach_absolute_time()); |
| #endif // defined(OS_IOS) |
| } |
| @@ -264,6 +267,11 @@ bool TimeTicks::IsConsistentAcrossProcesses() { |
| } |
| // static |
| +TimeTicks TimeTicks::FromMachAbsoluteTime(uint64_t mach_absolute_time) { |
| + return TimeTicks(MachAbsoluteTimeToTicks(mach_absolute_time)); |
| +} |
| + |
| +// static |
| TimeTicks::Clock TimeTicks::GetClock() { |
| #if defined(OS_IOS) |
| return Clock::IOS_CF_ABSOLUTE_TIME_MINUS_KERN_BOOTTIME; |