diff options
-rw-r--r-- | base/debug/trace_event_impl.cc | 4 | ||||
-rw-r--r-- | base/time.h | 9 | ||||
-rw-r--r-- | base/time_mac.cc | 5 | ||||
-rw-r--r-- | base/time_posix.cc | 28 | ||||
-rw-r--r-- | base/time_unittest.cc | 15 | ||||
-rw-r--r-- | base/time_win.cc | 7 |
6 files changed, 61 insertions, 7 deletions
diff --git a/base/debug/trace_event_impl.cc b/base/debug/trace_event_impl.cc index bd997cf..9cf3341 100644 --- a/base/debug/trace_event_impl.cc +++ b/base/debug/trace_event_impl.cc @@ -585,7 +585,7 @@ int TraceLog::AddTraceEvent(char phase, long long threshold, unsigned char flags) { DCHECK(name); - TimeTicks now = TimeTicks::HighResNow(); + TimeTicks now = TimeTicks::NowFromSystemTraceTime(); BufferFullCallback buffer_full_callback_copy; int ret_begin_id = -1; { @@ -710,7 +710,7 @@ void TraceLog::AddClockSyncMetadataEvents() { // debugfs that takes the written data and pushes it onto the trace // buffer. So, to establish clock sync, we write our monotonic clock into that // trace buffer. - TimeTicks now = TimeTicks::HighResNow(); + TimeTicks now = TimeTicks::NowFromSystemTraceTime(); double now_in_seconds = now.ToInternalValue() / 1000000.0; std::string marker = diff --git a/base/time.h b/base/time.h index 8590e99..5eac215 100644 --- a/base/time.h +++ b/base/time.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -482,6 +482,13 @@ class BASE_EXPORT TimeTicks { // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED. static TimeTicks HighResNow(); + // Returns the current system trace time or, if none is defined, the current + // high-res time (i.e. HighResNow()). On systems where a global trace clock + // is defined, timestamping TraceEvents's with this value guarantees + // synchronization between events collected inside chrome and events + // collected outside (e.g. kernel, X server). + static TimeTicks NowFromSystemTraceTime(); + #if defined(OS_WIN) // Get the absolute value of QPC time drift. For testing. static int64 GetQPCDriftMicroseconds(); diff --git a/base/time_mac.cc b/base/time_mac.cc index da17e28..a5720a6 100644 --- a/base/time_mac.cc +++ b/base/time_mac.cc @@ -148,4 +148,9 @@ TimeTicks TimeTicks::HighResNow() { return Now(); } +// static +TimeTicks TimeTicks::NowFromSystemTraceTime() { + return HighResNow(); +} + } // namespace base diff --git a/base/time_posix.cc b/base/time_posix.cc index 829c000..b8f1385 100644 --- a/base/time_posix.cc +++ b/base/time_posix.cc @@ -228,6 +228,34 @@ TimeTicks TimeTicks::HighResNow() { return Now(); } +#if defined(OS_POSIX) && defined(CLOCK_SYSTEM_TRACE) + +// static +TimeTicks TimeTicks::NowFromSystemTraceTime() { + uint64_t absolute_micro; + + struct timespec ts; + if (clock_gettime(CLOCK_SYSTEM_TRACE, &ts) != 0) { + NOTREACHED() << "clock_gettime(CLOCK_SYSTEM_TRACE) failed."; + return HighResNow(); + } + + absolute_micro = + (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + + (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); + + return TimeTicks(absolute_micro); +} + +#else // !(defined(OS_POSIX) && defined(CLOCK_SYSTEM_TRACE)) + +// static +TimeTicks TimeTicks::NowFromSystemTraceTime() { + return HighResNow(); +} + +#endif // defined(OS_POSIX) && defined(CLOCK_SYSTEM_TRACE) + #endif // !OS_MACOSX struct timeval Time::ToTimeVal() const { diff --git a/base/time_unittest.cc b/base/time_unittest.cc index 9823147..b4b166b 100644 --- a/base/time_unittest.cc +++ b/base/time_unittest.cc @@ -464,7 +464,7 @@ TEST(TimeTicks, Deltas) { } } -TEST(TimeTicks, HighResNow) { +static void HighResClockTest(TimeTicks (*GetTicks)()) { #if defined(OS_WIN) // HighResNow doesn't work on some systems. Since the product still works // even if it doesn't work, it makes this entire test questionable. @@ -486,12 +486,12 @@ TEST(TimeTicks, HighResNow) { int retries = 100; // Arbitrary. TimeDelta delta; while (!success && retries--) { - TimeTicks ticks_start = TimeTicks::HighResNow(); + TimeTicks ticks_start = GetTicks(); // Loop until we can detect that the clock has changed. Non-HighRes timers // will increment in chunks, e.g. 15ms. By spinning until we see a clock // change, we detect the minimum time between measurements. do { - delta = TimeTicks::HighResNow() - ticks_start; + delta = GetTicks() - ticks_start; } while (delta.InMilliseconds() == 0); if (delta.InMicroseconds() <= kTargetGranularityUs) @@ -503,6 +503,15 @@ TEST(TimeTicks, HighResNow) { EXPECT_TRUE(success); } +TEST(TimeTicks, HighResNow) { + HighResClockTest(&TimeTicks::HighResNow); +} + +TEST(TimeTicks, NowFromSystemTraceTime) { + // Re-use HighResNow test for now since clock properties are identical. + HighResClockTest(&TimeTicks::NowFromSystemTraceTime); +} + TEST(TimeDelta, FromAndIn) { EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); diff --git a/base/time_win.cc b/base/time_win.cc index 9e6fe53..71c914d 100644 --- a/base/time_win.cc +++ b/base/time_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -436,6 +436,11 @@ TimeTicks TimeTicks::HighResNow() { } // static +TimeTicks TimeTicks::NowFromSystemTraceTime() { + return HighResNow(); +} + +// static int64 TimeTicks::GetQPCDriftMicroseconds() { return HighResNowSingleton::GetInstance()->GetQPCDriftMicroseconds(); } |