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 <sys/sysctl.h> | 11 #include <sys/sysctl.h> |
12 #include <sys/time.h> | 12 #include <sys/time.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <time.h> | 14 #include <time.h> |
15 | 15 |
16 #include <limits> | |
17 | |
16 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
17 #include "base/logging.h" | 19 #include "base/logging.h" |
18 #include "base/mac/mach_logging.h" | 20 #include "base/mac/mach_logging.h" |
19 #include "base/mac/scoped_cftyperef.h" | 21 #include "base/mac/scoped_cftyperef.h" |
20 #include "base/mac/scoped_mach_port.h" | 22 #include "base/mac/scoped_mach_port.h" |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 uint64_t ComputeCurrentTicks() { | 26 int64 ComputeCurrentTicks() { |
25 #if defined(OS_IOS) | 27 #if defined(OS_IOS) |
26 // 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 |
27 // 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 |
28 // 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 |
29 // clock change. | 31 // clock change. |
30 struct timeval boottime; | 32 struct timeval boottime; |
31 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 33 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
32 size_t size = sizeof(boottime); | 34 size_t size = sizeof(boottime); |
33 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0); | 35 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0); |
34 DCHECK_EQ(KERN_SUCCESS, kr); | 36 DCHECK_EQ(KERN_SUCCESS, kr); |
35 base::TimeDelta time_difference = base::Time::Now() - | 37 base::TimeDelta time_difference = base::Time::Now() - |
36 (base::Time::FromTimeT(boottime.tv_sec) + | 38 (base::Time::FromTimeT(boottime.tv_sec) + |
37 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); | 39 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); |
38 return time_difference.InMicroseconds(); | 40 return time_difference.InMicroseconds(); |
39 #else | 41 #else |
40 uint64_t absolute_micro; | 42 uint64_t absolute_micro; |
Lei Zhang
2015/05/13 23:31:47
Also use CheckedNumeric<int64_t> here?
miu
2015/05/19 03:14:32
Done.
| |
41 | 43 |
42 static mach_timebase_info_data_t timebase_info; | 44 static mach_timebase_info_data_t timebase_info; |
43 if (timebase_info.denom == 0) { | 45 if (timebase_info.denom == 0) { |
44 // Zero-initialization of statics guarantees that denom will be 0 before | 46 // Zero-initialization of statics guarantees that denom will be 0 before |
45 // calling mach_timebase_info. mach_timebase_info will never set denom to | 47 // calling mach_timebase_info. mach_timebase_info will never set denom to |
46 // 0 as that would be invalid, so the zero-check can be used to determine | 48 // 0 as that would be invalid, so the zero-check can be used to determine |
47 // whether mach_timebase_info has already been called. This is | 49 // whether mach_timebase_info has already been called. This is |
48 // recommended by Apple's QA1398. | 50 // recommended by Apple's QA1398. |
49 kern_return_t kr = mach_timebase_info(&timebase_info); | 51 kern_return_t kr = mach_timebase_info(&timebase_info); |
50 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; | 52 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; |
51 } | 53 } |
52 | 54 |
53 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | 55 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
54 // with less precision (such as TickCount) just call through to | 56 // with less precision (such as TickCount) just call through to |
55 // mach_absolute_time. | 57 // mach_absolute_time. |
56 | 58 |
57 // timebase_info converts absolute time tick units into nanoseconds. Convert | 59 // timebase_info converts absolute time tick units into nanoseconds. Convert |
58 // to microseconds up front to stave off overflows. | 60 // to microseconds up front to stave off overflows. |
59 absolute_micro = | 61 absolute_micro = |
60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * | 62 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * |
61 timebase_info.numer / timebase_info.denom; | 63 timebase_info.numer / timebase_info.denom; |
64 DCHECK_LE(absolute_micro, | |
65 static_cast<uint64_t>(std::numeric_limits<int64>::max())); | |
62 | 66 |
63 // Don't bother with the rollover handling that the Windows version does. | 67 // Don't bother with the rollover handling that the Windows version does. |
64 // With numer and denom = 1 (the expected case), the 64-bit absolute time | 68 // With numer and denom = 1 (the expected case), the 64-bit absolute time |
65 // reported in nanoseconds is enough to last nearly 585 years. | 69 // reported in nanoseconds is enough to last nearly 585 years. |
66 return absolute_micro; | 70 return static_cast<int64>(absolute_micro); |
67 #endif // defined(OS_IOS) | 71 #endif // defined(OS_IOS) |
68 } | 72 } |
69 | 73 |
70 uint64_t ComputeThreadTicks() { | 74 int64 ComputeThreadTicks() { |
71 #if defined(OS_IOS) | 75 #if defined(OS_IOS) |
72 NOTREACHED(); | 76 NOTREACHED(); |
73 return 0; | 77 return 0; |
74 #else | 78 #else |
75 base::mac::ScopedMachSendRight thread(mach_thread_self()); | 79 base::mac::ScopedMachSendRight thread(mach_thread_self()); |
76 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; | 80 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; |
77 thread_basic_info_data_t thread_info_data; | 81 thread_basic_info_data_t thread_info_data; |
78 | 82 |
79 if (thread.get() == MACH_PORT_NULL) { | 83 if (thread.get() == MACH_PORT_NULL) { |
80 DLOG(ERROR) << "Failed to get mach_thread_self()"; | 84 DLOG(ERROR) << "Failed to get mach_thread_self()"; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 TimeTicks TimeTicks::Now() { | 221 TimeTicks TimeTicks::Now() { |
218 return TimeTicks(ComputeCurrentTicks()); | 222 return TimeTicks(ComputeCurrentTicks()); |
219 } | 223 } |
220 | 224 |
221 // static | 225 // static |
222 bool TimeTicks::IsHighResolution() { | 226 bool TimeTicks::IsHighResolution() { |
223 return true; | 227 return true; |
224 } | 228 } |
225 | 229 |
226 // static | 230 // static |
227 TimeTicks TimeTicks::ThreadNow() { | 231 ThreadTicks ThreadTicks::Now() { |
228 return TimeTicks(ComputeThreadTicks()); | 232 return ThreadTicks(ComputeThreadTicks()); |
229 } | 233 } |
230 | 234 |
231 // static | 235 // static |
232 TimeTicks TimeTicks::NowFromSystemTraceTime() { | 236 TraceTicks TraceTicks::Now() { |
233 return Now(); | 237 return TraceTicks(ComputeCurrentTicks()); |
234 } | 238 } |
235 | 239 |
236 } // namespace base | 240 } // namespace base |
OLD | NEW |