| 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 <stdint.h> | 11 #include <stdint.h> |
| 12 #include <sys/sysctl.h> | 12 #include <sys/sysctl.h> |
| 13 #include <sys/time.h> | 13 #include <sys/time.h> |
| 14 #include <sys/types.h> | 14 #include <sys/types.h> |
| 15 #include <time.h> | 15 #include <time.h> |
| 16 | 16 |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 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/numerics/safe_conversions.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 uint64_t ComputeCurrentTicks() { | 26 int64_t ComputeCurrentTicks() { |
| 26 #if defined(OS_IOS) | 27 #if defined(OS_IOS) |
| 27 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 28 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| 28 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 29 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| 29 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 30 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| 30 // clock change. | 31 // clock change. |
| 31 struct timeval boottime; | 32 struct timeval boottime; |
| 32 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 33 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 33 size_t size = sizeof(boottime); | 34 size_t size = sizeof(boottime); |
| 34 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0); | 35 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0); |
| 35 DCHECK_EQ(KERN_SUCCESS, kr); | 36 DCHECK_EQ(KERN_SUCCESS, kr); |
| 36 base::TimeDelta time_difference = base::Time::Now() - | 37 base::TimeDelta time_difference = base::Time::Now() - |
| 37 (base::Time::FromTimeT(boottime.tv_sec) + | 38 (base::Time::FromTimeT(boottime.tv_sec) + |
| 38 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); | 39 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); |
| 39 return time_difference.InMicroseconds(); | 40 return time_difference.InMicroseconds(); |
| 40 #else | 41 #else |
| 41 uint64_t absolute_micro; | |
| 42 | |
| 43 static mach_timebase_info_data_t timebase_info; | 42 static mach_timebase_info_data_t timebase_info; |
| 44 if (timebase_info.denom == 0) { | 43 if (timebase_info.denom == 0) { |
| 45 // Zero-initialization of statics guarantees that denom will be 0 before | 44 // Zero-initialization of statics guarantees that denom will be 0 before |
| 46 // calling mach_timebase_info. mach_timebase_info will never set denom to | 45 // calling mach_timebase_info. mach_timebase_info will never set denom to |
| 47 // 0 as that would be invalid, so the zero-check can be used to determine | 46 // 0 as that would be invalid, so the zero-check can be used to determine |
| 48 // whether mach_timebase_info has already been called. This is | 47 // whether mach_timebase_info has already been called. This is |
| 49 // recommended by Apple's QA1398. | 48 // recommended by Apple's QA1398. |
| 50 kern_return_t kr = mach_timebase_info(&timebase_info); | 49 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 51 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; | 50 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; |
| 52 } | 51 } |
| 53 | 52 |
| 54 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | 53 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
| 55 // with less precision (such as TickCount) just call through to | 54 // with less precision (such as TickCount) just call through to |
| 56 // mach_absolute_time. | 55 // mach_absolute_time. |
| 57 | 56 |
| 58 // timebase_info converts absolute time tick units into nanoseconds. Convert | 57 // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 59 // to microseconds up front to stave off overflows. | 58 // to microseconds up front to stave off overflows. |
| 60 absolute_micro = | 59 base::CheckedNumeric<uint64_t> result( |
| 61 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * | 60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond); |
| 62 timebase_info.numer / timebase_info.denom; | 61 result *= timebase_info.numer; |
| 62 result /= timebase_info.denom; |
| 63 | 63 |
| 64 // Don't bother with the rollover handling that the Windows version does. | 64 // Don't bother with the rollover handling that the Windows version does. |
| 65 // With numer and denom = 1 (the expected case), the 64-bit absolute time | 65 // With numer and denom = 1 (the expected case), the 64-bit absolute time |
| 66 // reported in nanoseconds is enough to last nearly 585 years. | 66 // reported in nanoseconds is enough to last nearly 585 years. |
| 67 return absolute_micro; | 67 return base::checked_cast<int64_t>(result.ValueOrDie()); |
| 68 #endif // defined(OS_IOS) | 68 #endif // defined(OS_IOS) |
| 69 } | 69 } |
| 70 | 70 |
| 71 uint64_t ComputeThreadTicks() { | 71 int64_t ComputeThreadTicks() { |
| 72 #if defined(OS_IOS) | 72 #if defined(OS_IOS) |
| 73 NOTREACHED(); | 73 NOTREACHED(); |
| 74 return 0; | 74 return 0; |
| 75 #else | 75 #else |
| 76 base::mac::ScopedMachSendRight thread(mach_thread_self()); | 76 base::mac::ScopedMachSendRight thread(mach_thread_self()); |
| 77 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; | 77 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; |
| 78 thread_basic_info_data_t thread_info_data; | 78 thread_basic_info_data_t thread_info_data; |
| 79 | 79 |
| 80 if (thread.get() == MACH_PORT_NULL) { | 80 if (thread.get() == MACH_PORT_NULL) { |
| 81 DLOG(ERROR) << "Failed to get mach_thread_self()"; | 81 DLOG(ERROR) << "Failed to get mach_thread_self()"; |
| 82 return 0; | 82 return 0; |
| 83 } | 83 } |
| 84 | 84 |
| 85 kern_return_t kr = thread_info( | 85 kern_return_t kr = thread_info( |
| 86 thread, | 86 thread, |
| 87 THREAD_BASIC_INFO, | 87 THREAD_BASIC_INFO, |
| 88 reinterpret_cast<thread_info_t>(&thread_info_data), | 88 reinterpret_cast<thread_info_t>(&thread_info_data), |
| 89 &thread_info_count); | 89 &thread_info_count); |
| 90 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; | 90 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; |
| 91 | 91 |
| 92 return (thread_info_data.user_time.seconds * | 92 base::CheckedNumeric<int64_t> absolute_micros( |
| 93 base::Time::kMicrosecondsPerSecond) + | 93 thread_info_data.user_time.seconds); |
| 94 thread_info_data.user_time.microseconds; | 94 absolute_micros *= base::Time::kMicrosecondsPerSecond; |
| 95 absolute_micros += thread_info_data.user_time.microseconds; |
| 96 return absolute_micros.ValueOrDie(); |
| 95 #endif // defined(OS_IOS) | 97 #endif // defined(OS_IOS) |
| 96 } | 98 } |
| 97 | 99 |
| 98 } // namespace | 100 } // namespace |
| 99 | 101 |
| 100 namespace base { | 102 namespace base { |
| 101 | 103 |
| 102 // The Time routines in this file use Mach and CoreFoundation APIs, since the | 104 // The Time routines in this file use Mach and CoreFoundation APIs, since the |
| 103 // POSIX definition of time_t in Mac OS X wraps around after 2038--and | 105 // POSIX definition of time_t in Mac OS X wraps around after 2038--and |
| 104 // there are already cookie expiration dates, etc., past that time out in | 106 // there are already cookie expiration dates, etc., past that time out in |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 TimeTicks TimeTicks::Now() { | 220 TimeTicks TimeTicks::Now() { |
| 219 return TimeTicks(ComputeCurrentTicks()); | 221 return TimeTicks(ComputeCurrentTicks()); |
| 220 } | 222 } |
| 221 | 223 |
| 222 // static | 224 // static |
| 223 bool TimeTicks::IsHighResolution() { | 225 bool TimeTicks::IsHighResolution() { |
| 224 return true; | 226 return true; |
| 225 } | 227 } |
| 226 | 228 |
| 227 // static | 229 // static |
| 228 TimeTicks TimeTicks::ThreadNow() { | 230 ThreadTicks ThreadTicks::Now() { |
| 229 return TimeTicks(ComputeThreadTicks()); | 231 return ThreadTicks(ComputeThreadTicks()); |
| 230 } | 232 } |
| 231 | 233 |
| 232 // static | 234 // static |
| 233 TimeTicks TimeTicks::NowFromSystemTraceTime() { | 235 TraceTicks TraceTicks::Now() { |
| 234 return Now(); | 236 return TraceTicks(ComputeCurrentTicks()); |
| 235 } | 237 } |
| 236 | 238 |
| 237 } // namespace base | 239 } // namespace base |
| OLD | NEW |