OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/system_wrappers/include/tick_util.h" | 11 #include "webrtc/system_wrappers/include/tick_util.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 namespace webrtc { | 15 namespace webrtc { |
16 | 16 |
17 bool TickTime::use_fake_clock_ = false; | 17 bool TickTime::use_fake_clock_ = false; |
18 int64_t TickTime::fake_ticks_ = 0; | 18 int64_t TickTime::fake_ticks_ = 0; |
19 | 19 |
20 int64_t TickTime::MillisecondTimestamp() { | |
21 return TicksToMilliseconds(TickTime::Now().Ticks()); | |
22 } | |
23 | |
24 int64_t TickTime::MicrosecondTimestamp() { | |
25 return TicksToMicroseconds(TickTime::Now().Ticks()); | |
26 } | |
27 | |
28 int64_t TickTime::MillisecondsToTicks(const int64_t ms) { | |
29 #if _WIN32 | |
30 return ms; | |
31 #elif defined(WEBRTC_LINUX) | |
32 return ms * 1000000LL; | |
33 #elif defined(WEBRTC_MAC) | |
34 // TODO(pbos): Fix unsafe use of static locals. | |
35 static double timebase_from_millisecond_fract = 0.0; | |
36 if (timebase_from_millisecond_fract == 0.0) { | |
37 mach_timebase_info_data_t timebase; | |
38 (void)mach_timebase_info(&timebase); | |
39 timebase_from_millisecond_fract = (timebase.denom * 1e6) / timebase.numer; | |
40 } | |
41 return ms * timebase_from_millisecond_fract; | |
42 #else | |
43 return ms * 1000LL; | |
44 #endif | |
45 } | |
46 | |
47 int64_t TickTime::TicksToMilliseconds(const int64_t ticks) { | |
48 #if _WIN32 | |
49 return ticks; | |
50 #elif defined(WEBRTC_LINUX) | |
51 return ticks / 1000000LL; | |
52 #elif defined(WEBRTC_MAC) | |
53 // TODO(pbos): Fix unsafe use of static locals. | |
54 static double timebase_microsecond_fract = 0.0; | |
55 if (timebase_microsecond_fract == 0.0) { | |
56 mach_timebase_info_data_t timebase; | |
57 (void)mach_timebase_info(&timebase); | |
58 timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e6); | |
59 } | |
60 return ticks * timebase_microsecond_fract; | |
61 #else | |
62 return ticks; | |
63 #endif | |
64 } | |
65 | |
66 int64_t TickTime::TicksToMicroseconds(const int64_t ticks) { | |
67 #if _WIN32 | |
68 return ticks * 1000LL; | |
69 #elif defined(WEBRTC_LINUX) | |
70 return ticks / 1000LL; | |
71 #elif defined(WEBRTC_MAC) | |
72 // TODO(pbos): Fix unsafe use of static locals. | |
73 static double timebase_microsecond_fract = 0.0; | |
74 if (timebase_microsecond_fract == 0.0) { | |
75 mach_timebase_info_data_t timebase; | |
76 (void)mach_timebase_info(&timebase); | |
77 timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e3); | |
78 } | |
79 return ticks * timebase_microsecond_fract; | |
80 #else | |
81 return ticks; | |
82 #endif | |
83 } | |
84 | |
85 void TickTime::UseFakeClock(int64_t start_millisecond) { | 20 void TickTime::UseFakeClock(int64_t start_millisecond) { |
86 use_fake_clock_ = true; | 21 use_fake_clock_ = true; |
87 fake_ticks_ = MillisecondsToTicks(start_millisecond); | 22 fake_ticks_ = MillisecondsToTicks(start_millisecond); |
88 } | 23 } |
89 | 24 |
90 void TickTime::AdvanceFakeClock(int64_t milliseconds) { | 25 void TickTime::AdvanceFakeClock(int64_t milliseconds) { |
91 assert(use_fake_clock_); | 26 assert(use_fake_clock_); |
92 fake_ticks_ += MillisecondsToTicks(milliseconds); | 27 fake_ticks_ += MillisecondsToTicks(milliseconds); |
93 } | 28 } |
94 | 29 |
95 // Gets the native system tick count. The actual unit, resolution, and epoch | |
96 // varies by platform: | |
97 // Windows: Milliseconds of uptime with rollover count in the upper 32-bits. | |
98 // Linux/Android: Nanoseconds since the Unix epoch. | |
99 // Mach (Mac/iOS): "absolute" time since first call. | |
100 // Unknown POSIX: Microseconds since the Unix epoch. | |
101 int64_t TickTime::QueryOsForTicks() { | 30 int64_t TickTime::QueryOsForTicks() { |
| 31 TickTime result; |
102 #if _WIN32 | 32 #if _WIN32 |
| 33 // TODO(wu): Remove QueryPerformanceCounter implementation. |
| 34 #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 35 // QueryPerformanceCounter returns the value from the TSC which is |
| 36 // incremented at the CPU frequency. The algorithm used requires |
| 37 // the CPU frequency to be constant. Technology like speed stepping |
| 38 // which has variable CPU frequency will therefore yield unpredictable, |
| 39 // incorrect time estimations. |
| 40 LARGE_INTEGER qpcnt; |
| 41 QueryPerformanceCounter(&qpcnt); |
| 42 result.ticks_ = qpcnt.QuadPart; |
| 43 #else |
103 static volatile LONG last_time_get_time = 0; | 44 static volatile LONG last_time_get_time = 0; |
104 static volatile int64_t num_wrap_time_get_time = 0; | 45 static volatile int64_t num_wrap_time_get_time = 0; |
105 volatile LONG* last_time_get_time_ptr = &last_time_get_time; | 46 volatile LONG* last_time_get_time_ptr = &last_time_get_time; |
106 DWORD now = timeGetTime(); | 47 DWORD now = timeGetTime(); |
107 // Atomically update the last gotten time | 48 // Atomically update the last gotten time |
108 DWORD old = InterlockedExchange(last_time_get_time_ptr, now); | 49 DWORD old = InterlockedExchange(last_time_get_time_ptr, now); |
109 if (now < old) { | 50 if (now < old) { |
110 // If now is earlier than old, there may have been a race between | 51 // If now is earlier than old, there may have been a race between |
111 // threads. | 52 // threads. |
112 // 0x0fffffff ~3.1 days, the code will not take that long to execute | 53 // 0x0fffffff ~3.1 days, the code will not take that long to execute |
113 // so it must have been a wrap around. | 54 // so it must have been a wrap around. |
114 if (old > 0xf0000000 && now < 0x0fffffff) { | 55 if (old > 0xf0000000 && now < 0x0fffffff) { |
115 // TODO(pbos): Fix unsafe use of static locals. | |
116 num_wrap_time_get_time++; | 56 num_wrap_time_get_time++; |
117 } | 57 } |
118 } | 58 } |
119 return now + (num_wrap_time_get_time << 32); | 59 result.ticks_ = now + (num_wrap_time_get_time << 32); |
| 60 #endif |
120 #elif defined(WEBRTC_LINUX) | 61 #elif defined(WEBRTC_LINUX) |
121 struct timespec ts; | 62 struct timespec ts; |
122 // TODO(wu): Remove CLOCK_REALTIME implementation. | 63 // TODO(wu): Remove CLOCK_REALTIME implementation. |
123 #ifdef WEBRTC_CLOCK_TYPE_REALTIME | 64 #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
124 clock_gettime(CLOCK_REALTIME, &ts); | 65 clock_gettime(CLOCK_REALTIME, &ts); |
125 #else | 66 #else |
126 clock_gettime(CLOCK_MONOTONIC, &ts); | 67 clock_gettime(CLOCK_MONOTONIC, &ts); |
127 #endif | 68 #endif |
128 return 1000000000LL * ts.tv_sec + ts.tv_nsec; | 69 result.ticks_ = 1000000000LL * static_cast<int64_t>(ts.tv_sec) + |
| 70 static_cast<int64_t>(ts.tv_nsec); |
129 #elif defined(WEBRTC_MAC) | 71 #elif defined(WEBRTC_MAC) |
130 // Return absolute time as an offset from the first call to this function, so | 72 static mach_timebase_info_data_t timebase; |
131 // that we can do floating-point (double) operations on it without losing | 73 if (timebase.denom == 0) { |
132 // precision. This holds true until the elapsed time is ~11 days, | 74 // Get the timebase if this is the first time we run. |
133 // at which point we'll start to lose some precision, though not enough to | 75 // Recommended by Apple's QA1398. |
134 // matter for millisecond accuracy for another couple years after that. | 76 kern_return_t retval = mach_timebase_info(&timebase); |
135 // TODO(pbos): Fix unsafe use of static locals. | 77 if (retval != KERN_SUCCESS) { |
136 static uint64_t timebase_start = 0; | 78 // TODO(wu): Implement RTC_CHECK for all the platforms. Then replace this |
137 if (timebase_start == 0) { | 79 // with a RTC_CHECK_EQ(retval, KERN_SUCCESS); |
138 timebase_start = mach_absolute_time(); | 80 #ifndef WEBRTC_IOS |
| 81 asm("int3"); |
| 82 #else |
| 83 __builtin_trap(); |
| 84 #endif // WEBRTC_IOS |
| 85 } |
139 } | 86 } |
140 return mach_absolute_time() - timebase_start; | 87 // Use timebase to convert absolute time tick units into nanoseconds. |
| 88 result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom; |
141 #else | 89 #else |
142 struct timeval tv; | 90 struct timeval tv; |
143 gettimeofday(&tv, NULL); | 91 gettimeofday(&tv, NULL); |
144 return 1000000LL * tv.tv_sec + tv.tv_usec; | 92 result.ticks_ = 1000000LL * static_cast<int64_t>(tv.tv_sec) + |
| 93 static_cast<int64_t>(tv.tv_usec); |
145 #endif | 94 #endif |
| 95 return result.ticks_; |
146 } | 96 } |
147 | 97 |
148 } // namespace webrtc | 98 } // namespace webrtc |
OLD | NEW |