Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: base/time/time_mac.cc

Issue 1122153002: Fixit: Fork base::TimeTicks --> TimeTicks + ThreadTicks + TraceTicks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@FIXIT_timeclasses_1of2
Patch Set: Comment tweak, plus dcheng's int64_t nit. [+REBASE] Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <sys/sysctl.h> 12 #include <sys/sysctl.h>
12 #include <sys/time.h> 13 #include <sys/time.h>
13 #include <sys/types.h> 14 #include <sys/types.h>
14 #include <time.h> 15 #include <time.h>
15 16
16 #include "base/basictypes.h" 17 #include "base/basictypes.h"
17 #include "base/logging.h" 18 #include "base/logging.h"
18 #include "base/mac/mach_logging.h" 19 #include "base/mac/mach_logging.h"
19 #include "base/mac/scoped_cftyperef.h" 20 #include "base/mac/scoped_cftyperef.h"
20 #include "base/mac/scoped_mach_port.h" 21 #include "base/mac/scoped_mach_port.h"
22 #include "base/numerics/safe_conversions.h"
21 23
22 namespace { 24 namespace {
23 25
24 uint64_t ComputeCurrentTicks() { 26 int64_t 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;
41
42 static mach_timebase_info_data_t timebase_info; 42 static mach_timebase_info_data_t timebase_info;
43 if (timebase_info.denom == 0) { 43 if (timebase_info.denom == 0) {
44 // Zero-initialization of statics guarantees that denom will be 0 before 44 // Zero-initialization of statics guarantees that denom will be 0 before
45 // 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
46 // 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
47 // whether mach_timebase_info has already been called. This is 47 // whether mach_timebase_info has already been called. This is
48 // recommended by Apple's QA1398. 48 // recommended by Apple's QA1398.
49 kern_return_t kr = mach_timebase_info(&timebase_info); 49 kern_return_t kr = mach_timebase_info(&timebase_info);
50 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; 50 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info";
51 } 51 }
52 52
53 // 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
54 // with less precision (such as TickCount) just call through to 54 // with less precision (such as TickCount) just call through to
55 // mach_absolute_time. 55 // mach_absolute_time.
56 56
57 // timebase_info converts absolute time tick units into nanoseconds. Convert 57 // timebase_info converts absolute time tick units into nanoseconds. Convert
58 // to microseconds up front to stave off overflows. 58 // to microseconds up front to stave off overflows.
59 absolute_micro = 59 base::CheckedNumeric<uint64_t> result(
60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * 60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond);
61 timebase_info.numer / timebase_info.denom; 61 result *= timebase_info.numer;
62 result /= timebase_info.denom;
62 63
63 // 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.
64 // 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
65 // reported in nanoseconds is enough to last nearly 585 years. 66 // reported in nanoseconds is enough to last nearly 585 years.
66 return absolute_micro; 67 return base::checked_cast<int64_t>(result.ValueOrDie());
67 #endif // defined(OS_IOS) 68 #endif // defined(OS_IOS)
68 } 69 }
69 70
70 uint64_t ComputeThreadTicks() { 71 int64_t ComputeThreadTicks() {
71 #if defined(OS_IOS) 72 #if defined(OS_IOS)
72 NOTREACHED(); 73 NOTREACHED();
73 return 0; 74 return 0;
74 #else 75 #else
75 base::mac::ScopedMachSendRight thread(mach_thread_self()); 76 base::mac::ScopedMachSendRight thread(mach_thread_self());
76 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;
77 thread_basic_info_data_t thread_info_data; 78 thread_basic_info_data_t thread_info_data;
78 79
79 if (thread.get() == MACH_PORT_NULL) { 80 if (thread.get() == MACH_PORT_NULL) {
80 DLOG(ERROR) << "Failed to get mach_thread_self()"; 81 DLOG(ERROR) << "Failed to get mach_thread_self()";
81 return 0; 82 return 0;
82 } 83 }
83 84
84 kern_return_t kr = thread_info( 85 kern_return_t kr = thread_info(
85 thread, 86 thread,
86 THREAD_BASIC_INFO, 87 THREAD_BASIC_INFO,
87 reinterpret_cast<thread_info_t>(&thread_info_data), 88 reinterpret_cast<thread_info_t>(&thread_info_data),
88 &thread_info_count); 89 &thread_info_count);
89 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; 90 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
90 91
91 return (thread_info_data.user_time.seconds * 92 base::CheckedNumeric<int64_t> absolute_micros(
92 base::Time::kMicrosecondsPerSecond) + 93 thread_info_data.user_time.seconds);
93 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();
94 #endif // defined(OS_IOS) 97 #endif // defined(OS_IOS)
95 } 98 }
96 99
97 } // namespace 100 } // namespace
98 101
99 namespace base { 102 namespace base {
100 103
101 // 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
102 // 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
103 // 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
217 TimeTicks TimeTicks::Now() { 220 TimeTicks TimeTicks::Now() {
218 return TimeTicks(ComputeCurrentTicks()); 221 return TimeTicks(ComputeCurrentTicks());
219 } 222 }
220 223
221 // static 224 // static
222 bool TimeTicks::IsHighResolution() { 225 bool TimeTicks::IsHighResolution() {
223 return true; 226 return true;
224 } 227 }
225 228
226 // static 229 // static
227 TimeTicks TimeTicks::ThreadNow() { 230 ThreadTicks ThreadTicks::Now() {
228 return TimeTicks(ComputeThreadTicks()); 231 return ThreadTicks(ComputeThreadTicks());
229 } 232 }
230 233
231 // static 234 // static
232 TimeTicks TimeTicks::NowFromSystemTraceTime() { 235 TraceTicks TraceTicks::Now() {
233 return Now(); 236 return TraceTicks(ComputeCurrentTicks());
234 } 237 }
235 238
236 } // namespace base 239 } // namespace base
OLDNEW
« base/time/time.h ('K') | « base/time/time.cc ('k') | base/time/time_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698