diff options
author | fdoray <fdoray@chromium.org> | 2015-10-14 20:46:40 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-15 03:47:30 +0000 |
commit | dc2a659b0ad54c3dcb632b876e40a71fa03019e9 (patch) | |
tree | e3f3a87c7e8a03716389609435214f0be6bd87bf /base/time/time.h | |
parent | 5d49f7978f42d05431b8ed851050ce0687938512 (diff) | |
download | chromium_src-dc2a659b0ad54c3dcb632b876e40a71fa03019e9.zip chromium_src-dc2a659b0ad54c3dcb632b876e40a71fa03019e9.tar.gz chromium_src-dc2a659b0ad54c3dcb632b876e40a71fa03019e9.tar.bz2 |
Implement ThreadTicks::Now on Windows.
Use QueryThreadCycleTime() to get the number of CPU clock cycles used
by the current thread. Convert it to microseconds using a measured
TSC frequency.
The value returned by QueryThreadCycleTime() is based on the rdtsc
instruction. For several years, Intel has been shipping CPUs with a
constant-rate counter, which means that the QueryThreadCycleTime()
results are directly proportional to wall-clock time on most systems
(see crbug.com/280743#c15). ThreadTicks::IsSupported() will return
false if the CPU doesn't have a constant rate TSC.
BUG=280743
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel
Review URL: https://codereview.chromium.org/1390743002
Cr-Commit-Position: refs/heads/master@{#354213}
Diffstat (limited to 'base/time/time.h')
-rw-r--r-- | base/time/time.h | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/base/time/time.h b/base/time/time.h index e0435d0..8da08ff 100644 --- a/base/time/time.h +++ b/base/time/time.h @@ -79,6 +79,8 @@ // For FILETIME in FromFileTime, until it moves to a new converter class. // See TODO(iyengar) below. #include <windows.h> + +#include "base/gtest_prod_util.h" #endif #include <limits> @@ -733,16 +735,28 @@ class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) return true; +#elif defined(OS_WIN) + return IsSupportedWin(); #else return false; #endif } + // Waits until the initialization is completed. Needs to be guarded with a + // call to IsSupported(). + static void WaitUntilInitialized() { +#if defined(OS_WIN) + WaitUntilInitializedWin(); +#endif + } + // Returns thread-specific CPU-time on systems that support this feature. // Needs to be guarded with a call to IsSupported(). Use this timer // to (approximately) measure how much time the calling thread spent doing // actual work vs. being de-scheduled. May return bogus results if the thread - // migrates to another CPU between two calls. + // migrates to another CPU between two calls. Returns an empty ThreadTicks + // object until the initialization is completed. If a clock reading is + // absolutely needed, call WaitUntilInitialized() before this method. static ThreadTicks Now(); private: @@ -752,6 +766,19 @@ class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { // and testing. explicit ThreadTicks(int64 us) : TimeBase(us) { } + +#if defined(OS_WIN) + FRIEND_TEST_ALL_PREFIXES(TimeTicks, TSCTicksPerSecond); + + // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't + // been measured yet. Needs to be guarded with a call to IsSupported(). + // This method is declared here rather than in the anonymous namespace to + // allow testing. + static double TSCTicksPerSecond(); + + static bool IsSupportedWin(); + static void WaitUntilInitializedWin(); +#endif }; // For logging use only. |