Chromium Code Reviews| 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 namespace { | 26 namespace { |
| 27 | 27 |
| 28 int64_t ComputeCurrentTicks() { | 28 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.
| |
| 29 #if defined(OS_IOS) | |
| 30 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | |
| 31 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | |
| 32 // changes. KERN_BOOTTIME will be updated by the system whenever the system | |
| 33 // clock change. | |
| 34 struct timeval boottime; | |
| 35 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | |
| 36 size_t size = sizeof(boottime); | |
| 37 int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); | |
| 38 DCHECK_EQ(KERN_SUCCESS, kr); | |
| 39 base::TimeDelta time_difference = base::Time::Now() - | |
| 40 (base::Time::FromTimeT(boottime.tv_sec) + | |
| 41 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); | |
| 42 return time_difference.InMicroseconds(); | |
| 43 #else | |
| 44 static mach_timebase_info_data_t timebase_info; | 29 static mach_timebase_info_data_t timebase_info; |
| 45 if (timebase_info.denom == 0) { | 30 if (timebase_info.denom == 0) { |
| 46 // Zero-initialization of statics guarantees that denom will be 0 before | 31 // Zero-initialization of statics guarantees that denom will be 0 before |
| 47 // calling mach_timebase_info. mach_timebase_info will never set denom to | 32 // calling mach_timebase_info. mach_timebase_info will never set denom to |
| 48 // 0 as that would be invalid, so the zero-check can be used to determine | 33 // 0 as that would be invalid, so the zero-check can be used to determine |
| 49 // whether mach_timebase_info has already been called. This is | 34 // whether mach_timebase_info has already been called. This is |
| 50 // recommended by Apple's QA1398. | 35 // recommended by Apple's QA1398. |
| 51 kern_return_t kr = mach_timebase_info(&timebase_info); | 36 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 52 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; | 37 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; |
| 53 } | 38 } |
| 54 | 39 |
| 55 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | |
| 56 // with less precision (such as TickCount) just call through to | |
| 57 // mach_absolute_time. | |
| 58 | |
| 59 // timebase_info converts absolute time tick units into nanoseconds. Convert | 40 // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 60 // to microseconds up front to stave off overflows. | 41 // to microseconds up front to stave off overflows. |
| 61 base::CheckedNumeric<uint64_t> result( | 42 base::CheckedNumeric<uint64_t> result(mach_absolute_time / |
| 62 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond); | 43 base::Time::kNanosecondsPerMicrosecond); |
| 63 result *= timebase_info.numer; | 44 result *= timebase_info.numer; |
| 64 result /= timebase_info.denom; | 45 result /= timebase_info.denom; |
| 65 | 46 |
| 66 // Don't bother with the rollover handling that the Windows version does. | 47 // Don't bother with the rollover handling that the Windows version does. |
| 67 // With numer and denom = 1 (the expected case), the 64-bit absolute time | 48 // With numer and denom = 1 (the expected case), the 64-bit absolute time |
| 68 // reported in nanoseconds is enough to last nearly 585 years. | 49 // reported in nanoseconds is enough to last nearly 585 years. |
| 69 return base::checked_cast<int64_t>(result.ValueOrDie()); | 50 return base::checked_cast<int64_t>(result.ValueOrDie()); |
| 51 } | |
| 52 | |
| 53 int64_t ComputeCurrentTicks() { | |
| 54 #if defined(OS_IOS) | |
| 55 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | |
| 56 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | |
| 57 // changes. KERN_BOOTTIME will be updated by the system whenever the system | |
| 58 // clock change. | |
| 59 struct timeval boottime; | |
| 60 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | |
| 61 size_t size = sizeof(boottime); | |
| 62 int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); | |
| 63 DCHECK_EQ(KERN_SUCCESS, kr); | |
| 64 base::TimeDelta time_difference = | |
| 65 base::Time::Now() - (base::Time::FromTimeT(boottime.tv_sec) + | |
| 66 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); | |
| 67 return time_difference.InMicroseconds(); | |
| 68 #else | |
| 69 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | |
| 70 // with less precision (such as TickCount) just call through to | |
| 71 // mach_absolute_time. | |
| 72 return MachAbsoluteTimeToTicks(mach_absolute_time()); | |
| 70 #endif // defined(OS_IOS) | 73 #endif // defined(OS_IOS) |
| 71 } | 74 } |
| 72 | 75 |
| 73 int64_t ComputeThreadTicks() { | 76 int64_t ComputeThreadTicks() { |
| 74 #if defined(OS_IOS) | 77 #if defined(OS_IOS) |
| 75 NOTREACHED(); | 78 NOTREACHED(); |
| 76 return 0; | 79 return 0; |
| 77 #else | 80 #else |
| 78 base::mac::ScopedMachSendRight thread(mach_thread_self()); | 81 base::mac::ScopedMachSendRight thread(mach_thread_self()); |
| 79 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; | 82 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 bool TimeTicks::IsHighResolution() { | 260 bool TimeTicks::IsHighResolution() { |
| 258 return true; | 261 return true; |
| 259 } | 262 } |
| 260 | 263 |
| 261 // static | 264 // static |
| 262 bool TimeTicks::IsConsistentAcrossProcesses() { | 265 bool TimeTicks::IsConsistentAcrossProcesses() { |
| 263 return true; | 266 return true; |
| 264 } | 267 } |
| 265 | 268 |
| 266 // static | 269 // static |
| 270 TimeTicks TimeTicks::FromMachAbsoluteTime(uint64_t mach_absolute_time) { | |
| 271 return TimeTicks(MachAbsoluteTimeToTicks(mach_absolute_time)); | |
| 272 } | |
| 273 | |
| 274 // static | |
| 267 TimeTicks::Clock TimeTicks::GetClock() { | 275 TimeTicks::Clock TimeTicks::GetClock() { |
| 268 #if defined(OS_IOS) | 276 #if defined(OS_IOS) |
| 269 return Clock::IOS_CF_ABSOLUTE_TIME_MINUS_KERN_BOOTTIME; | 277 return Clock::IOS_CF_ABSOLUTE_TIME_MINUS_KERN_BOOTTIME; |
| 270 #else | 278 #else |
| 271 return Clock::MAC_MACH_ABSOLUTE_TIME; | 279 return Clock::MAC_MACH_ABSOLUTE_TIME; |
| 272 #endif // defined(OS_IOS) | 280 #endif // defined(OS_IOS) |
| 273 } | 281 } |
| 274 | 282 |
| 275 // static | 283 // static |
| 276 ThreadTicks ThreadTicks::Now() { | 284 ThreadTicks ThreadTicks::Now() { |
| 277 return ThreadTicks(ComputeThreadTicks()); | 285 return ThreadTicks(ComputeThreadTicks()); |
| 278 } | 286 } |
| 279 | 287 |
| 280 } // namespace base | 288 } // namespace base |
| OLD | NEW |