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 "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/mac/mach_logging.h" | |
19 #include "base/mac/scoped_cftyperef.h" | 18 #include "base/mac/scoped_cftyperef.h" |
20 #include "base/mac/scoped_mach_port.h" | 19 #include "base/mac/scoped_mach_port.h" |
21 | 20 |
22 namespace { | 21 namespace { |
23 | 22 |
24 uint64_t ComputeCurrentTicks() { | 23 uint64_t ComputeCurrentTicks() { |
25 #if defined(OS_IOS) | 24 #if defined(OS_IOS) |
26 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 25 // 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 | 26 // 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 | 27 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
(...skipping 11 matching lines...) Expand all Loading... |
40 uint64_t absolute_micro; | 39 uint64_t absolute_micro; |
41 | 40 |
42 static mach_timebase_info_data_t timebase_info; | 41 static mach_timebase_info_data_t timebase_info; |
43 if (timebase_info.denom == 0) { | 42 if (timebase_info.denom == 0) { |
44 // Zero-initialization of statics guarantees that denom will be 0 before | 43 // Zero-initialization of statics guarantees that denom will be 0 before |
45 // calling mach_timebase_info. mach_timebase_info will never set denom to | 44 // 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 | 45 // 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 | 46 // whether mach_timebase_info has already been called. This is |
48 // recommended by Apple's QA1398. | 47 // recommended by Apple's QA1398. |
49 kern_return_t kr = mach_timebase_info(&timebase_info); | 48 kern_return_t kr = mach_timebase_info(&timebase_info); |
50 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; | 49 DCHECK_EQ(KERN_SUCCESS, kr); |
51 } | 50 } |
52 | 51 |
53 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | 52 // 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 | 53 // with less precision (such as TickCount) just call through to |
55 // mach_absolute_time. | 54 // mach_absolute_time. |
56 | 55 |
57 // timebase_info converts absolute time tick units into nanoseconds. Convert | 56 // timebase_info converts absolute time tick units into nanoseconds. Convert |
58 // to microseconds up front to stave off overflows. | 57 // to microseconds up front to stave off overflows. |
59 absolute_micro = | 58 absolute_micro = |
60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * | 59 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * |
(...skipping 18 matching lines...) Expand all Loading... |
79 if (thread == MACH_PORT_NULL) { | 78 if (thread == MACH_PORT_NULL) { |
80 DLOG(ERROR) << "Failed to get mach_thread_self()"; | 79 DLOG(ERROR) << "Failed to get mach_thread_self()"; |
81 return 0; | 80 return 0; |
82 } | 81 } |
83 | 82 |
84 kern_return_t kr = thread_info( | 83 kern_return_t kr = thread_info( |
85 thread, | 84 thread, |
86 THREAD_BASIC_INFO, | 85 THREAD_BASIC_INFO, |
87 reinterpret_cast<thread_info_t>(&thread_info_data), | 86 reinterpret_cast<thread_info_t>(&thread_info_data), |
88 &thread_info_count); | 87 &thread_info_count); |
89 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; | 88 DCHECK_EQ(KERN_SUCCESS, kr); |
90 | 89 |
91 return (thread_info_data.user_time.seconds * | 90 return (thread_info_data.user_time.seconds * |
92 base::Time::kMicrosecondsPerSecond) + | 91 base::Time::kMicrosecondsPerSecond) + |
93 thread_info_data.user_time.microseconds; | 92 thread_info_data.user_time.microseconds; |
94 #endif // defined(OS_IOS) | 93 #endif // defined(OS_IOS) |
95 } | 94 } |
96 | 95 |
97 } // namespace | 96 } // namespace |
98 | 97 |
99 namespace base { | 98 namespace base { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 TimeTicks TimeTicks::ThreadNow() { | 231 TimeTicks TimeTicks::ThreadNow() { |
233 return TimeTicks(ComputeThreadTicks()); | 232 return TimeTicks(ComputeThreadTicks()); |
234 } | 233 } |
235 | 234 |
236 // static | 235 // static |
237 TimeTicks TimeTicks::NowFromSystemTraceTime() { | 236 TimeTicks TimeTicks::NowFromSystemTraceTime() { |
238 return HighResNow(); | 237 return HighResNow(); |
239 } | 238 } |
240 | 239 |
241 } // namespace base | 240 } // namespace base |
OLD | NEW |