aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthaloun <thaloun@google.com>2015-11-17 15:02:44 -0800
committerCommit bot <commit-bot@chromium.org>2015-11-17 23:02:59 +0000
commit2935e0141939adc642d73da6d8048a4e918a8382 (patch)
tree36a469ea4524b10b5e21d8760d6a2fa3ac9f1543
parentc073615d56b8f626bf694868a276fd135898da11 (diff)
downloadwebrtc-2935e0141939adc642d73da6d8048a4e918a8382.tar.gz
Several Tick counter improvements try #2."
This reverts commit c91d1738709b038fee84d569180cba2bbcbfe5d7. BUG= Review URL: https://codereview.webrtc.org/1452843003 Cr-Commit-Position: refs/heads/master@{#10682}
-rw-r--r--webrtc/modules/utility/source/process_thread_impl_unittest.cc7
-rw-r--r--webrtc/system_wrappers/include/tick_util.h114
-rw-r--r--webrtc/system_wrappers/source/tick_util.cc120
3 files changed, 98 insertions, 143 deletions
diff --git a/webrtc/modules/utility/source/process_thread_impl_unittest.cc b/webrtc/modules/utility/source/process_thread_impl_unittest.cc
index 34ed5c5a38..92cd561e45 100644
--- a/webrtc/modules/utility/source/process_thread_impl_unittest.cc
+++ b/webrtc/modules/utility/source/process_thread_impl_unittest.cc
@@ -251,8 +251,9 @@ TEST(ProcessThreadImpl, WakeUp) {
rtc::scoped_ptr<EventWrapper> called(EventWrapper::Create());
MockModule module;
- int64_t start_time = 0;
- int64_t called_time = 0;
+ int64_t start_time;
+ int64_t called_time;
+
// Ask for a callback after 1000ms.
// TimeUntilNextProcess will be called twice.
// The first time we use it to get the thread into a waiting state.
@@ -281,8 +282,6 @@ TEST(ProcessThreadImpl, WakeUp) {
EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1);
thread.Stop();
- ASSERT_GT(start_time, 0);
- ASSERT_GT(called_time, 0);
EXPECT_GE(called_time, start_time);
uint32_t diff = called_time - start_time;
// We should have been called back much quicker than 1sec.
diff --git a/webrtc/system_wrappers/include/tick_util.h b/webrtc/system_wrappers/include/tick_util.h
index f839ff646c..6e3b05edb2 100644
--- a/webrtc/system_wrappers/include/tick_util.h
+++ b/webrtc/system_wrappers/include/tick_util.h
@@ -56,6 +56,8 @@ class TickTime {
static int64_t TicksToMilliseconds(const int64_t ticks);
+ static int64_t TicksToMicroseconds(const int64_t ticks);
+
// Returns a TickTime that is ticks later than the passed TickTime.
friend TickTime operator+(const TickTime lhs, const int64_t ticks);
TickTime& operator+=(const int64_t& ticks);
@@ -112,6 +114,14 @@ class TickInterval {
int64_t interval_;
};
+inline int64_t TickInterval::Milliseconds() const {
+ return TickTime::TicksToMilliseconds(interval_);
+}
+
+inline int64_t TickInterval::Microseconds() const {
+ return TickTime::TicksToMicroseconds(interval_);
+}
+
inline TickInterval operator+(const TickInterval& lhs,
const TickInterval& rhs) {
return TickInterval(lhs.interval_ + rhs.interval_);
@@ -163,76 +173,10 @@ inline TickTime TickTime::Now() {
return TickTime(QueryOsForTicks());
}
-inline int64_t TickTime::MillisecondTimestamp() {
- int64_t ticks = TickTime::Now().Ticks();
-#if _WIN32
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- LARGE_INTEGER qpfreq;
- QueryPerformanceFrequency(&qpfreq);
- return (ticks * 1000) / qpfreq.QuadPart;
-#else
- return ticks;
-#endif
-#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
- return ticks / 1000000LL;
-#else
- return ticks / 1000LL;
-#endif
-}
-
-inline int64_t TickTime::MicrosecondTimestamp() {
- int64_t ticks = TickTime::Now().Ticks();
-#if _WIN32
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- LARGE_INTEGER qpfreq;
- QueryPerformanceFrequency(&qpfreq);
- return (ticks * 1000) / (qpfreq.QuadPart / 1000);
-#else
- return ticks * 1000LL;
-#endif
-#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
- return ticks / 1000LL;
-#else
- return ticks;
-#endif
-}
-
inline int64_t TickTime::Ticks() const {
return ticks_;
}
-inline int64_t TickTime::MillisecondsToTicks(const int64_t ms) {
-#if _WIN32
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- LARGE_INTEGER qpfreq;
- QueryPerformanceFrequency(&qpfreq);
- return (qpfreq.QuadPart * ms) / 1000;
-#else
- return ms;
-#endif
-#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
- return ms * 1000000LL;
-#else
- return ms * 1000LL;
-#endif
-}
-
-inline int64_t TickTime::TicksToMilliseconds(const int64_t ticks) {
-#if _WIN32
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- LARGE_INTEGER qpfreq;
- QueryPerformanceFrequency(&qpfreq);
- return (ticks * 1000) / qpfreq.QuadPart;
-#else
- return ticks;
-#endif
-#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
- return ticks / 1000000LL;
-#else
- return ticks / 1000LL;
-#endif
-}
-
inline TickTime& TickTime::operator+=(const int64_t& ticks) {
ticks_ += ticks;
return *this;
@@ -245,44 +189,6 @@ inline TickInterval::TickInterval(const int64_t interval)
: interval_(interval) {
}
-inline int64_t TickInterval::Milliseconds() const {
-#if _WIN32
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- LARGE_INTEGER qpfreq;
- QueryPerformanceFrequency(&qpfreq);
- return (interval_ * 1000) / qpfreq.QuadPart;
-#else
- // interval_ is in ms
- return interval_;
-#endif
-#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
- // interval_ is in ns
- return interval_ / 1000000;
-#else
- // interval_ is usecs
- return interval_ / 1000;
-#endif
-}
-
-inline int64_t TickInterval::Microseconds() const {
-#if _WIN32
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- LARGE_INTEGER qpfreq;
- QueryPerformanceFrequency(&qpfreq);
- return (interval_ * 1000000) / qpfreq.QuadPart;
-#else
- // interval_ is in ms
- return interval_ * 1000LL;
-#endif
-#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
- // interval_ is in ns
- return interval_ / 1000;
-#else
- // interval_ is usecs
- return interval_;
-#endif
-}
-
inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) {
interval_ += rhs.interval_;
return *this;
diff --git a/webrtc/system_wrappers/source/tick_util.cc b/webrtc/system_wrappers/source/tick_util.cc
index bc8fcfe91b..cae817dc55 100644
--- a/webrtc/system_wrappers/source/tick_util.cc
+++ b/webrtc/system_wrappers/source/tick_util.cc
@@ -17,6 +17,71 @@ namespace webrtc {
bool TickTime::use_fake_clock_ = false;
int64_t TickTime::fake_ticks_ = 0;
+int64_t TickTime::MillisecondTimestamp() {
+ return TicksToMilliseconds(TickTime::Now().Ticks());
+}
+
+int64_t TickTime::MicrosecondTimestamp() {
+ return TicksToMicroseconds(TickTime::Now().Ticks());
+}
+
+int64_t TickTime::MillisecondsToTicks(const int64_t ms) {
+#if _WIN32
+ return ms;
+#elif defined(WEBRTC_LINUX)
+ return ms * 1000000LL;
+#elif defined(WEBRTC_MAC)
+ // TODO(pbos): Fix unsafe use of static locals.
+ static double timebase_from_millisecond_fract = 0.0;
+ if (timebase_from_millisecond_fract == 0.0) {
+ mach_timebase_info_data_t timebase;
+ (void)mach_timebase_info(&timebase);
+ timebase_from_millisecond_fract = (timebase.denom * 1e6) / timebase.numer;
+ }
+ return ms * timebase_from_millisecond_fract;
+#else
+ return ms * 1000LL;
+#endif
+}
+
+int64_t TickTime::TicksToMilliseconds(const int64_t ticks) {
+#if _WIN32
+ return ticks;
+#elif defined(WEBRTC_LINUX)
+ return ticks / 1000000LL;
+#elif defined(WEBRTC_MAC)
+ // TODO(pbos): Fix unsafe use of static locals.
+ static double timebase_microsecond_fract = 0.0;
+ if (timebase_microsecond_fract == 0.0) {
+ mach_timebase_info_data_t timebase;
+ (void)mach_timebase_info(&timebase);
+ timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e6);
+ }
+ return ticks * timebase_microsecond_fract;
+#else
+ return ticks;
+#endif
+}
+
+int64_t TickTime::TicksToMicroseconds(const int64_t ticks) {
+#if _WIN32
+ return ticks * 1000LL;
+#elif defined(WEBRTC_LINUX)
+ return ticks / 1000LL;
+#elif defined(WEBRTC_MAC)
+ // TODO(pbos): Fix unsafe use of static locals.
+ static double timebase_microsecond_fract = 0.0;
+ if (timebase_microsecond_fract == 0.0) {
+ mach_timebase_info_data_t timebase;
+ (void)mach_timebase_info(&timebase);
+ timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e3);
+ }
+ return ticks * timebase_microsecond_fract;
+#else
+ return ticks;
+#endif
+}
+
void TickTime::UseFakeClock(int64_t start_millisecond) {
use_fake_clock_ = true;
fake_ticks_ = MillisecondsToTicks(start_millisecond);
@@ -27,20 +92,14 @@ void TickTime::AdvanceFakeClock(int64_t milliseconds) {
fake_ticks_ += MillisecondsToTicks(milliseconds);
}
+// Gets the native system tick count. The actual unit, resolution, and epoch
+// varies by platform:
+// Windows: Milliseconds of uptime with rollover count in the upper 32-bits.
+// Linux/Android: Nanoseconds since the Unix epoch.
+// Mach (Mac/iOS): "absolute" time since first call.
+// Unknown POSIX: Microseconds since the Unix epoch.
int64_t TickTime::QueryOsForTicks() {
- TickTime result;
#if _WIN32
- // TODO(wu): Remove QueryPerformanceCounter implementation.
-#ifdef USE_QUERY_PERFORMANCE_COUNTER
- // QueryPerformanceCounter returns the value from the TSC which is
- // incremented at the CPU frequency. The algorithm used requires
- // the CPU frequency to be constant. Technology like speed stepping
- // which has variable CPU frequency will therefore yield unpredictable,
- // incorrect time estimations.
- LARGE_INTEGER qpcnt;
- QueryPerformanceCounter(&qpcnt);
- result.ticks_ = qpcnt.QuadPart;
-#else
static volatile LONG last_time_get_time = 0;
static volatile int64_t num_wrap_time_get_time = 0;
volatile LONG* last_time_get_time_ptr = &last_time_get_time;
@@ -53,11 +112,11 @@ int64_t TickTime::QueryOsForTicks() {
// 0x0fffffff ~3.1 days, the code will not take that long to execute
// so it must have been a wrap around.
if (old > 0xf0000000 && now < 0x0fffffff) {
+ // TODO(pbos): Fix unsafe use of static locals.
num_wrap_time_get_time++;
}
}
- result.ticks_ = now + (num_wrap_time_get_time << 32);
-#endif
+ return now + (num_wrap_time_get_time << 32);
#elif defined(WEBRTC_LINUX)
struct timespec ts;
// TODO(wu): Remove CLOCK_REALTIME implementation.
@@ -66,33 +125,24 @@ int64_t TickTime::QueryOsForTicks() {
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
- result.ticks_ = 1000000000LL * static_cast<int64_t>(ts.tv_sec) +
- static_cast<int64_t>(ts.tv_nsec);
+ return 1000000000LL * ts.tv_sec + ts.tv_nsec;
#elif defined(WEBRTC_MAC)
- static mach_timebase_info_data_t timebase;
- if (timebase.denom == 0) {
- // Get the timebase if this is the first time we run.
- // Recommended by Apple's QA1398.
- kern_return_t retval = mach_timebase_info(&timebase);
- if (retval != KERN_SUCCESS) {
- // TODO(wu): Implement RTC_CHECK for all the platforms. Then replace this
- // with a RTC_CHECK_EQ(retval, KERN_SUCCESS);
-#ifndef WEBRTC_IOS
- asm("int3");
-#else
- __builtin_trap();
-#endif // WEBRTC_IOS
- }
+ // Return absolute time as an offset from the first call to this function, so
+ // that we can do floating-point (double) operations on it without losing
+ // precision. This holds true until the elapsed time is ~11 days,
+ // at which point we'll start to lose some precision, though not enough to
+ // matter for millisecond accuracy for another couple years after that.
+ // TODO(pbos): Fix unsafe use of static locals.
+ static uint64_t timebase_start = 0;
+ if (timebase_start == 0) {
+ timebase_start = mach_absolute_time();
}
- // Use timebase to convert absolute time tick units into nanoseconds.
- result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom;
+ return mach_absolute_time() - timebase_start;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
- result.ticks_ = 1000000LL * static_cast<int64_t>(tv.tv_sec) +
- static_cast<int64_t>(tv.tv_usec);
+ return 1000000LL * tv.tv_sec + tv.tv_usec;
#endif
- return result.ticks_;
}
} // namespace webrtc