| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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.h" | 5 #include "base/time.h" |
| 6 | 6 |
| 7 #ifdef OS_MACOSX | |
| 8 #include <mach/mach_time.h> | |
| 9 #endif | |
| 10 #include <sys/time.h> | 7 #include <sys/time.h> |
| 11 #include <time.h> | 8 #include <time.h> |
| 12 | 9 |
| 13 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 14 #include "base/logging.h" | 11 #include "base/logging.h" |
| 15 | 12 |
| 16 namespace base { | 13 namespace base { |
| 17 | 14 |
| 18 // The Time routines in this file use standard POSIX routines, or almost- | 15 // The Time routines in this file use standard POSIX routines, or almost- |
| 19 // standard routines in the case of timegm. We need to use a Mach-specific | 16 // standard routines in the case of timegm. We need to use a Mach-specific |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 exploded->second = timestruct.tm_sec; | 82 exploded->second = timestruct.tm_sec; |
| 86 exploded->millisecond = milliseconds % kMillisecondsPerSecond; | 83 exploded->millisecond = milliseconds % kMillisecondsPerSecond; |
| 87 } | 84 } |
| 88 | 85 |
| 89 // TimeTicks ------------------------------------------------------------------ | 86 // TimeTicks ------------------------------------------------------------------ |
| 90 | 87 |
| 91 // static | 88 // static |
| 92 TimeTicks TimeTicks::Now() { | 89 TimeTicks TimeTicks::Now() { |
| 93 uint64_t absolute_micro; | 90 uint64_t absolute_micro; |
| 94 | 91 |
| 95 #if defined(OS_MACOSX) | 92 #if defined(OS_POSIX) && \ |
| 96 | |
| 97 static mach_timebase_info_data_t timebase_info; | |
| 98 if (timebase_info.denom == 0) { | |
| 99 // Zero-initialization of statics guarantees that denom will be 0 before | |
| 100 // calling mach_timebase_info. mach_timebase_info will never set denom to | |
| 101 // 0 as that would be invalid, so the zero-check can be used to determine | |
| 102 // whether mach_timebase_info has already been called. This is | |
| 103 // recommended by Apple's QA1398. | |
| 104 kern_return_t kr = mach_timebase_info(&timebase_info); | |
| 105 DCHECK(kr == KERN_SUCCESS); | |
| 106 } | |
| 107 | |
| 108 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | |
| 109 // with less precision (such as TickCount) just call through to | |
| 110 // mach_absolute_time. | |
| 111 | |
| 112 // timebase_info converts absolute time tick units into nanoseconds. Convert | |
| 113 // to microseconds up front to stave off overflows. | |
| 114 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * | |
| 115 timebase_info.numer / timebase_info.denom; | |
| 116 | |
| 117 // Don't bother with the rollover handling that the Windows version does. | |
| 118 // With numer and denom = 1 (the expected case), the 64-bit absolute time | |
| 119 // reported in nanoseconds is enough to last nearly 585 years. | |
| 120 | |
| 121 #elif defined(OS_POSIX) && \ | |
| 122 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 | 93 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
| 123 | 94 |
| 124 struct timespec ts; | 95 struct timespec ts; |
| 125 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | 96 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 126 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; | 97 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; |
| 127 return TimeTicks(); | 98 return TimeTicks(); |
| 128 } | 99 } |
| 129 | 100 |
| 130 absolute_micro = | 101 absolute_micro = |
| 131 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 102 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
| 132 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 103 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
| 133 | 104 |
| 134 #else // _POSIX_MONOTONIC_CLOCK | 105 #else // _POSIX_MONOTONIC_CLOCK |
| 135 #error No usable tick clock function on this platform. | 106 #error No usable tick clock function on this platform. |
| 136 #endif // _POSIX_MONOTONIC_CLOCK | 107 #endif // _POSIX_MONOTONIC_CLOCK |
| 137 | 108 |
| 138 return TimeTicks(absolute_micro); | 109 return TimeTicks(absolute_micro); |
| 139 } | 110 } |
| 140 | 111 |
| 141 // static | 112 // static |
| 142 TimeTicks TimeTicks::HighResNow() { | 113 TimeTicks TimeTicks::HighResNow() { |
| 143 return Now(); | 114 return Now(); |
| 144 } | 115 } |
| 145 | 116 |
| 146 } // namespace base | 117 } // namespace base |
| OLD | NEW |