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

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: Better time.h comments. Using CheckedNumeric in time_mac.cc and time_posix.cc. 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 <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" 18 #include "base/mac/mach_logging.h"
19 #include "base/mac/scoped_cftyperef.h" 19 #include "base/mac/scoped_cftyperef.h"
20 #include "base/mac/scoped_mach_port.h" 20 #include "base/mac/scoped_mach_port.h"
21 #include "base/numerics/safe_conversions.h"
21 22
22 namespace { 23 namespace {
23 24
24 uint64_t ComputeCurrentTicks() { 25 int64 ComputeCurrentTicks() {
25 #if defined(OS_IOS) 26 #if defined(OS_IOS)
26 // On iOS mach_absolute_time stops while the device is sleeping. Instead use 27 // 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 28 // 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 29 // changes. KERN_BOOTTIME will be updated by the system whenever the system
29 // clock change. 30 // clock change.
30 struct timeval boottime; 31 struct timeval boottime;
31 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; 32 int mib[2] = {CTL_KERN, KERN_BOOTTIME};
32 size_t size = sizeof(boottime); 33 size_t size = sizeof(boottime);
33 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0); 34 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0);
34 DCHECK_EQ(KERN_SUCCESS, kr); 35 DCHECK_EQ(KERN_SUCCESS, kr);
35 base::TimeDelta time_difference = base::Time::Now() - 36 base::TimeDelta time_difference = base::Time::Now() -
36 (base::Time::FromTimeT(boottime.tv_sec) + 37 (base::Time::FromTimeT(boottime.tv_sec) +
37 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); 38 base::TimeDelta::FromMicroseconds(boottime.tv_usec));
38 return time_difference.InMicroseconds(); 39 return time_difference.InMicroseconds();
39 #else 40 #else
40 uint64_t absolute_micro;
41
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 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info";
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 base::CheckedNumeric<uint64> result(
60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * 59 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond);
61 timebase_info.numer / timebase_info.denom; 60 result *= timebase_info.numer;
61 result /= timebase_info.denom;
62 62
63 // Don't bother with the rollover handling that the Windows version does. 63 // 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 64 // With numer and denom = 1 (the expected case), the 64-bit absolute time
65 // reported in nanoseconds is enough to last nearly 585 years. 65 // reported in nanoseconds is enough to last nearly 585 years.
66 return absolute_micro; 66 return base::checked_cast<int64>(result.ValueOrDie());
67 #endif // defined(OS_IOS) 67 #endif // defined(OS_IOS)
68 } 68 }
69 69
70 uint64_t ComputeThreadTicks() { 70 int64 ComputeThreadTicks() {
71 #if defined(OS_IOS) 71 #if defined(OS_IOS)
72 NOTREACHED(); 72 NOTREACHED();
73 return 0; 73 return 0;
74 #else 74 #else
75 base::mac::ScopedMachSendRight thread(mach_thread_self()); 75 base::mac::ScopedMachSendRight thread(mach_thread_self());
76 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; 76 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
77 thread_basic_info_data_t thread_info_data; 77 thread_basic_info_data_t thread_info_data;
78 78
79 if (thread.get() == MACH_PORT_NULL) { 79 if (thread.get() == MACH_PORT_NULL) {
80 DLOG(ERROR) << "Failed to get mach_thread_self()"; 80 DLOG(ERROR) << "Failed to get mach_thread_self()";
81 return 0; 81 return 0;
82 } 82 }
83 83
84 kern_return_t kr = thread_info( 84 kern_return_t kr = thread_info(
85 thread, 85 thread,
86 THREAD_BASIC_INFO, 86 THREAD_BASIC_INFO,
87 reinterpret_cast<thread_info_t>(&thread_info_data), 87 reinterpret_cast<thread_info_t>(&thread_info_data),
88 &thread_info_count); 88 &thread_info_count);
89 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; 89 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
90 90
91 return (thread_info_data.user_time.seconds * 91 base::CheckedNumeric<int64> absolute_micros(
92 base::Time::kMicrosecondsPerSecond) + 92 thread_info_data.user_time.seconds);
93 thread_info_data.user_time.microseconds; 93 absolute_micros *= base::Time::kMicrosecondsPerSecond;
94 absolute_micros += thread_info_data.user_time.microseconds;
95 return absolute_micros.ValueOrDie();
94 #endif // defined(OS_IOS) 96 #endif // defined(OS_IOS)
95 } 97 }
96 98
97 } // namespace 99 } // namespace
98 100
99 namespace base { 101 namespace base {
100 102
101 // The Time routines in this file use Mach and CoreFoundation APIs, since the 103 // 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 104 // 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 105 // 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() { 219 TimeTicks TimeTicks::Now() {
218 return TimeTicks(ComputeCurrentTicks()); 220 return TimeTicks(ComputeCurrentTicks());
219 } 221 }
220 222
221 // static 223 // static
222 bool TimeTicks::IsHighResolution() { 224 bool TimeTicks::IsHighResolution() {
223 return true; 225 return true;
224 } 226 }
225 227
226 // static 228 // static
227 TimeTicks TimeTicks::ThreadNow() { 229 ThreadTicks ThreadTicks::Now() {
228 return TimeTicks(ComputeThreadTicks()); 230 return ThreadTicks(ComputeThreadTicks());
229 } 231 }
230 232
231 // static 233 // static
232 TimeTicks TimeTicks::NowFromSystemTraceTime() { 234 TraceTicks TraceTicks::Now() {
233 return Now(); 235 return TraceTicks(ComputeCurrentTicks());
234 } 236 }
235 237
236 } // namespace base 238 } // 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