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 static double timebase_from_millisecond_fract = 0.0; | |
35 if (timebase_from_millisecond_fract == 0.0) { | |
pbos-webrtc
2015/11/03 10:39:31
Racy, put a TODO please.
Tim H
2015/11/04 22:20:52
Done.
| |
36 mach_timebase_info_data_t timebase; | |
37 (void)mach_timebase_info(&timebase); | |
38 timebase_from_millisecond_fract = (timebase.denom * 1e6) / timebase.numer; | |
39 } | |
40 return ms * timebase_from_millisecond_fract; | |
41 #else | |
42 return ms * 1000LL; | |
43 #endif | |
44 } | |
45 | |
46 int64_t TickTime::TicksToMilliseconds(const int64_t ticks) { | |
pbos-webrtc
2015/11/03 10:39:31
Return TicksToMicroseconds() / 1000; maybe? I don'
Tim H
2015/11/04 22:20:52
Your issue is with the similarity of the two funct
| |
47 #if _WIN32 | |
48 return ticks; | |
49 #elif defined(WEBRTC_LINUX) | |
50 return ticks / 1000000LL; | |
51 #elif defined(WEBRTC_MAC) | |
52 static double timebase_millisecond_fract = 0.0; | |
pbos-webrtc
2015/11/03 10:39:31
Racy, put a TODO please.
Tim H
2015/11/04 22:20:52
Done.
| |
53 if (timebase_millisecond_fract == 0.0) { | |
54 mach_timebase_info_data_t timebase; | |
55 (void)mach_timebase_info(&timebase); | |
56 timebase_millisecond_fract = timebase.numer / (timebase.denom * 1e6); | |
57 } | |
58 return ticks * timebase_millisecond_fract; | |
59 #else | |
60 return ticks / 1000LL; | |
61 #endif | |
62 } | |
63 | |
64 int64_t TickTime::TicksToMicroseconds(const int64_t ticks) { | |
65 #if _WIN32 | |
66 return ticks * 1000LL; | |
67 #elif defined(WEBRTC_LINUX) | |
68 return ticks / 1000LL; | |
69 #elif defined(WEBRTC_MAC) | |
70 static double timebase_microsecond_fract = 0.0; | |
pbos-webrtc
2015/11/03 10:39:31
Racy, put a TODO please.
| |
71 if (timebase_microsecond_fract == 0.0) { | |
72 mach_timebase_info_data_t timebase; | |
73 (void)mach_timebase_info(&timebase); | |
74 timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e3); | |
75 } | |
76 return ticks * timebase_microsecond_fract; | |
77 #else | |
78 return ticks; | |
79 #endif | |
80 } | |
81 | |
20 void TickTime::UseFakeClock(int64_t start_millisecond) { | 82 void TickTime::UseFakeClock(int64_t start_millisecond) { |
21 use_fake_clock_ = true; | 83 use_fake_clock_ = true; |
22 fake_ticks_ = MillisecondsToTicks(start_millisecond); | 84 fake_ticks_ = MillisecondsToTicks(start_millisecond); |
23 } | 85 } |
24 | 86 |
25 void TickTime::AdvanceFakeClock(int64_t milliseconds) { | 87 void TickTime::AdvanceFakeClock(int64_t milliseconds) { |
26 assert(use_fake_clock_); | 88 assert(use_fake_clock_); |
27 fake_ticks_ += MillisecondsToTicks(milliseconds); | 89 fake_ticks_ += MillisecondsToTicks(milliseconds); |
28 } | 90 } |
29 | 91 |
92 // Gets the native system tick count. The actual unit, resolution, and epoch | |
93 // varies by platform: | |
94 // Windows: Milliseconds of uptime with rollover count in the upper 32-bits. | |
95 // Linux/Android: Nanoseconds since the Unix epoch. | |
96 // Mach (Mac/iOS): "absolute" time since first call. | |
97 // Unknown POSIX: Microseconds since the Unix epoch. | |
30 int64_t TickTime::QueryOsForTicks() { | 98 int64_t TickTime::QueryOsForTicks() { |
31 TickTime result; | |
32 #if _WIN32 | 99 #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 | |
44 static volatile LONG last_time_get_time = 0; | 100 static volatile LONG last_time_get_time = 0; |
45 static volatile int64_t num_wrap_time_get_time = 0; | 101 static volatile int64_t num_wrap_time_get_time = 0; |
46 volatile LONG* last_time_get_time_ptr = &last_time_get_time; | 102 volatile LONG* last_time_get_time_ptr = &last_time_get_time; |
47 DWORD now = timeGetTime(); | 103 DWORD now = timeGetTime(); |
48 // Atomically update the last gotten time | 104 // Atomically update the last gotten time |
49 DWORD old = InterlockedExchange(last_time_get_time_ptr, now); | 105 DWORD old = InterlockedExchange(last_time_get_time_ptr, now); |
50 if (now < old) { | 106 if (now < old) { |
51 // If now is earlier than old, there may have been a race between | 107 // If now is earlier than old, there may have been a race between |
52 // threads. | 108 // threads. |
53 // 0x0fffffff ~3.1 days, the code will not take that long to execute | 109 // 0x0fffffff ~3.1 days, the code will not take that long to execute |
54 // so it must have been a wrap around. | 110 // so it must have been a wrap around. |
55 if (old > 0xf0000000 && now < 0x0fffffff) { | 111 if (old > 0xf0000000 && now < 0x0fffffff) { |
56 num_wrap_time_get_time++; | 112 num_wrap_time_get_time++; |
pbos-webrtc
2015/11/03 10:39:31
This is racy, >1 thread can enter here. Can you pu
Tim H
2015/11/04 22:20:52
Done.
| |
57 } | 113 } |
58 } | 114 } |
59 result.ticks_ = now + (num_wrap_time_get_time << 32); | 115 return now + (num_wrap_time_get_time << 32); |
60 #endif | |
61 #elif defined(WEBRTC_LINUX) | 116 #elif defined(WEBRTC_LINUX) |
62 struct timespec ts; | 117 struct timespec ts; |
63 // TODO(wu): Remove CLOCK_REALTIME implementation. | 118 // TODO(wu): Remove CLOCK_REALTIME implementation. |
64 #ifdef WEBRTC_CLOCK_TYPE_REALTIME | 119 #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
65 clock_gettime(CLOCK_REALTIME, &ts); | 120 clock_gettime(CLOCK_REALTIME, &ts); |
66 #else | 121 #else |
67 clock_gettime(CLOCK_MONOTONIC, &ts); | 122 clock_gettime(CLOCK_MONOTONIC, &ts); |
68 #endif | 123 #endif |
69 result.ticks_ = 1000000000LL * static_cast<int64_t>(ts.tv_sec) + | 124 return 1000000000LL * ts.tv_sec + ts.tv_nsec; |
70 static_cast<int64_t>(ts.tv_nsec); | |
71 #elif defined(WEBRTC_MAC) | 125 #elif defined(WEBRTC_MAC) |
72 static mach_timebase_info_data_t timebase; | 126 // Return absolute time as an offset from the first call to this function, so |
73 if (timebase.denom == 0) { | 127 // that we can do floating-point (double) operations on it without losing |
74 // Get the timebase if this is the first time we run. | 128 // precision. This holds true until the elapsed time is ~11 days, |
75 // Recommended by Apple's QA1398. | 129 // at which point we'll start to lose some precision, though not enough to |
76 kern_return_t retval = mach_timebase_info(&timebase); | 130 // matter for millisecond accuracy for another couple years after that. |
77 if (retval != KERN_SUCCESS) { | 131 static uint64_t timebase_start = 0; |
78 // TODO(wu): Implement RTC_CHECK for all the platforms. Then replace this | 132 if (timebase_start == 0) { |
pbos-webrtc
2015/11/03 10:39:31
This is racy, can you put a TODO here (since it wa
Tim H
2015/11/04 22:20:52
Done.
| |
79 // with a RTC_CHECK_EQ(retval, KERN_SUCCESS); | 133 timebase_start = mach_absolute_time(); |
80 #ifndef WEBRTC_IOS | |
81 asm("int3"); | |
82 #else | |
83 __builtin_trap(); | |
84 #endif // WEBRTC_IOS | |
85 } | |
86 } | 134 } |
87 // Use timebase to convert absolute time tick units into nanoseconds. | 135 return mach_absolute_time() - timebase_start; |
88 result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom; | |
89 #else | 136 #else |
90 struct timeval tv; | 137 struct timeval tv; |
91 gettimeofday(&tv, NULL); | 138 gettimeofday(&tv, NULL); |
92 result.ticks_ = 1000000LL * static_cast<int64_t>(tv.tv_sec) + | 139 return 1000000LL * tv.tv_sec + tv.tv_usec; |
93 static_cast<int64_t>(tv.tv_usec); | |
94 #endif | 140 #endif |
95 return result.ticks_; | |
96 } | 141 } |
97 | 142 |
98 } // namespace webrtc | 143 } // namespace webrtc |
OLD | NEW |