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}
This commit is contained in:
thaloun
2015-11-17 15:02:44 -08:00
committed by Commit bot
parent c073615d56
commit 2935e01419
3 changed files with 98 additions and 143 deletions

View File

@ -251,8 +251,9 @@ TEST(ProcessThreadImpl, WakeUp) {
rtc::scoped_ptr<EventWrapper> called(EventWrapper::Create()); rtc::scoped_ptr<EventWrapper> called(EventWrapper::Create());
MockModule module; MockModule module;
int64_t start_time = 0; int64_t start_time;
int64_t called_time = 0; int64_t called_time;
// Ask for a callback after 1000ms. // Ask for a callback after 1000ms.
// TimeUntilNextProcess will be called twice. // TimeUntilNextProcess will be called twice.
// The first time we use it to get the thread into a waiting state. // 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); EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1);
thread.Stop(); thread.Stop();
ASSERT_GT(start_time, 0);
ASSERT_GT(called_time, 0);
EXPECT_GE(called_time, start_time); EXPECT_GE(called_time, start_time);
uint32_t diff = called_time - start_time; uint32_t diff = called_time - start_time;
// We should have been called back much quicker than 1sec. // We should have been called back much quicker than 1sec.

View File

@ -56,6 +56,8 @@ class TickTime {
static int64_t TicksToMilliseconds(const int64_t ticks); 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. // Returns a TickTime that is ticks later than the passed TickTime.
friend TickTime operator+(const TickTime lhs, const int64_t ticks); friend TickTime operator+(const TickTime lhs, const int64_t ticks);
TickTime& operator+=(const int64_t& ticks); TickTime& operator+=(const int64_t& ticks);
@ -112,6 +114,14 @@ class TickInterval {
int64_t interval_; 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, inline TickInterval operator+(const TickInterval& lhs,
const TickInterval& rhs) { const TickInterval& rhs) {
return TickInterval(lhs.interval_ + rhs.interval_); return TickInterval(lhs.interval_ + rhs.interval_);
@ -163,76 +173,10 @@ inline TickTime TickTime::Now() {
return TickTime(QueryOsForTicks()); 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 { inline int64_t TickTime::Ticks() const {
return ticks_; 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) { inline TickTime& TickTime::operator+=(const int64_t& ticks) {
ticks_ += ticks; ticks_ += ticks;
return *this; return *this;
@ -245,44 +189,6 @@ inline TickInterval::TickInterval(const int64_t interval)
: interval_(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) { inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) {
interval_ += rhs.interval_; interval_ += rhs.interval_;
return *this; return *this;

View File

@ -17,6 +17,71 @@ namespace webrtc {
bool TickTime::use_fake_clock_ = false; bool TickTime::use_fake_clock_ = false;
int64_t TickTime::fake_ticks_ = 0; 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) { void TickTime::UseFakeClock(int64_t start_millisecond) {
use_fake_clock_ = true; use_fake_clock_ = true;
fake_ticks_ = MillisecondsToTicks(start_millisecond); fake_ticks_ = MillisecondsToTicks(start_millisecond);
@ -27,20 +92,14 @@ void TickTime::AdvanceFakeClock(int64_t milliseconds) {
fake_ticks_ += MillisecondsToTicks(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() { int64_t TickTime::QueryOsForTicks() {
TickTime result;
#if _WIN32 #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 LONG last_time_get_time = 0;
static volatile int64_t num_wrap_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; 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 // 0x0fffffff ~3.1 days, the code will not take that long to execute
// so it must have been a wrap around. // so it must have been a wrap around.
if (old > 0xf0000000 && now < 0x0fffffff) { if (old > 0xf0000000 && now < 0x0fffffff) {
// TODO(pbos): Fix unsafe use of static locals.
num_wrap_time_get_time++; num_wrap_time_get_time++;
} }
} }
result.ticks_ = now + (num_wrap_time_get_time << 32); return now + (num_wrap_time_get_time << 32);
#endif
#elif defined(WEBRTC_LINUX) #elif defined(WEBRTC_LINUX)
struct timespec ts; struct timespec ts;
// TODO(wu): Remove CLOCK_REALTIME implementation. // TODO(wu): Remove CLOCK_REALTIME implementation.
@ -66,33 +125,24 @@ int64_t TickTime::QueryOsForTicks() {
#else #else
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
#endif #endif
result.ticks_ = 1000000000LL * static_cast<int64_t>(ts.tv_sec) + return 1000000000LL * ts.tv_sec + ts.tv_nsec;
static_cast<int64_t>(ts.tv_nsec);
#elif defined(WEBRTC_MAC) #elif defined(WEBRTC_MAC)
static mach_timebase_info_data_t timebase; // Return absolute time as an offset from the first call to this function, so
if (timebase.denom == 0) { // that we can do floating-point (double) operations on it without losing
// Get the timebase if this is the first time we run. // precision. This holds true until the elapsed time is ~11 days,
// Recommended by Apple's QA1398. // at which point we'll start to lose some precision, though not enough to
kern_return_t retval = mach_timebase_info(&timebase); // matter for millisecond accuracy for another couple years after that.
if (retval != KERN_SUCCESS) { // TODO(pbos): Fix unsafe use of static locals.
// TODO(wu): Implement RTC_CHECK for all the platforms. Then replace this static uint64_t timebase_start = 0;
// with a RTC_CHECK_EQ(retval, KERN_SUCCESS); if (timebase_start == 0) {
#ifndef WEBRTC_IOS timebase_start = mach_absolute_time();
asm("int3");
#else
__builtin_trap();
#endif // WEBRTC_IOS
}
} }
// Use timebase to convert absolute time tick units into nanoseconds. return mach_absolute_time() - timebase_start;
result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom;
#else #else
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
result.ticks_ = 1000000LL * static_cast<int64_t>(tv.tv_sec) + return 1000000LL * tv.tv_sec + tv.tv_usec;
static_cast<int64_t>(tv.tv_usec);
#endif #endif
return result.ticks_;
} }
} // namespace webrtc } // namespace webrtc