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

Side by Side Diff: webrtc/system_wrappers/source/tick_util.cc

Issue 1452843003: Several Tick counter improvements try #2." (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « webrtc/system_wrappers/include/tick_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
20 void TickTime::UseFakeClock(int64_t start_millisecond) { 85 void TickTime::UseFakeClock(int64_t start_millisecond) {
21 use_fake_clock_ = true; 86 use_fake_clock_ = true;
22 fake_ticks_ = MillisecondsToTicks(start_millisecond); 87 fake_ticks_ = MillisecondsToTicks(start_millisecond);
23 } 88 }
24 89
25 void TickTime::AdvanceFakeClock(int64_t milliseconds) { 90 void TickTime::AdvanceFakeClock(int64_t milliseconds) {
26 assert(use_fake_clock_); 91 assert(use_fake_clock_);
27 fake_ticks_ += MillisecondsToTicks(milliseconds); 92 fake_ticks_ += MillisecondsToTicks(milliseconds);
28 } 93 }
29 94
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.
30 int64_t TickTime::QueryOsForTicks() { 101 int64_t TickTime::QueryOsForTicks() {
31 TickTime result;
32 #if _WIN32 102 #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; 103 static volatile LONG last_time_get_time = 0;
45 static volatile int64_t num_wrap_time_get_time = 0; 104 static volatile int64_t num_wrap_time_get_time = 0;
46 volatile LONG* last_time_get_time_ptr = &last_time_get_time; 105 volatile LONG* last_time_get_time_ptr = &last_time_get_time;
47 DWORD now = timeGetTime(); 106 DWORD now = timeGetTime();
48 // Atomically update the last gotten time 107 // Atomically update the last gotten time
49 DWORD old = InterlockedExchange(last_time_get_time_ptr, now); 108 DWORD old = InterlockedExchange(last_time_get_time_ptr, now);
50 if (now < old) { 109 if (now < old) {
51 // If now is earlier than old, there may have been a race between 110 // If now is earlier than old, there may have been a race between
52 // threads. 111 // threads.
53 // 0x0fffffff ~3.1 days, the code will not take that long to execute 112 // 0x0fffffff ~3.1 days, the code will not take that long to execute
54 // so it must have been a wrap around. 113 // so it must have been a wrap around.
55 if (old > 0xf0000000 && now < 0x0fffffff) { 114 if (old > 0xf0000000 && now < 0x0fffffff) {
115 // TODO(pbos): Fix unsafe use of static locals.
56 num_wrap_time_get_time++; 116 num_wrap_time_get_time++;
57 } 117 }
58 } 118 }
59 result.ticks_ = now + (num_wrap_time_get_time << 32); 119 return now + (num_wrap_time_get_time << 32);
60 #endif
61 #elif defined(WEBRTC_LINUX) 120 #elif defined(WEBRTC_LINUX)
62 struct timespec ts; 121 struct timespec ts;
63 // TODO(wu): Remove CLOCK_REALTIME implementation. 122 // TODO(wu): Remove CLOCK_REALTIME implementation.
64 #ifdef WEBRTC_CLOCK_TYPE_REALTIME 123 #ifdef WEBRTC_CLOCK_TYPE_REALTIME
65 clock_gettime(CLOCK_REALTIME, &ts); 124 clock_gettime(CLOCK_REALTIME, &ts);
66 #else 125 #else
67 clock_gettime(CLOCK_MONOTONIC, &ts); 126 clock_gettime(CLOCK_MONOTONIC, &ts);
68 #endif 127 #endif
69 result.ticks_ = 1000000000LL * static_cast<int64_t>(ts.tv_sec) + 128 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
70 static_cast<int64_t>(ts.tv_nsec);
71 #elif defined(WEBRTC_MAC) 129 #elif defined(WEBRTC_MAC)
72 static mach_timebase_info_data_t timebase; 130 // Return absolute time as an offset from the first call to this function, so
73 if (timebase.denom == 0) { 131 // that we can do floating-point (double) operations on it without losing
74 // Get the timebase if this is the first time we run. 132 // precision. This holds true until the elapsed time is ~11 days,
75 // Recommended by Apple's QA1398. 133 // at which point we'll start to lose some precision, though not enough to
76 kern_return_t retval = mach_timebase_info(&timebase); 134 // matter for millisecond accuracy for another couple years after that.
77 if (retval != KERN_SUCCESS) { 135 // TODO(pbos): Fix unsafe use of static locals.
78 // TODO(wu): Implement RTC_CHECK for all the platforms. Then replace this 136 static uint64_t timebase_start = 0;
79 // with a RTC_CHECK_EQ(retval, KERN_SUCCESS); 137 if (timebase_start == 0) {
80 #ifndef WEBRTC_IOS 138 timebase_start = mach_absolute_time();
81 asm("int3");
82 #else
83 __builtin_trap();
84 #endif // WEBRTC_IOS
85 }
86 } 139 }
87 // Use timebase to convert absolute time tick units into nanoseconds. 140 return mach_absolute_time() - timebase_start;
tommi 2016/01/23 14:09:41 There's a bug here which will affect cases where R
88 result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom;
89 #else 141 #else
90 struct timeval tv; 142 struct timeval tv;
91 gettimeofday(&tv, NULL); 143 gettimeofday(&tv, NULL);
92 result.ticks_ = 1000000LL * static_cast<int64_t>(tv.tv_sec) + 144 return 1000000LL * tv.tv_sec + tv.tv_usec;
93 static_cast<int64_t>(tv.tv_usec);
94 #endif 145 #endif
95 return result.ticks_;
96 } 146 }
97 147
98 } // namespace webrtc 148 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/system_wrappers/include/tick_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698