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