OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // High resolution timer functions for use in Linux. |
| 6 |
| 7 #ifndef CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_LINUX_H_ |
| 8 #define CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_LINUX_H_ |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 |
| 12 #include <sys/time.h> |
| 13 |
| 14 const uint64 MICROS_IN_SECOND = 1000000L; |
| 15 |
| 16 // A handy class for reliably measuring wall-clock time with decent resolution. |
| 17 // |
| 18 // We want to measure time with high resolution on Linux. What to do? |
| 19 // |
| 20 // RDTSC? Sure, but how do you convert it to wall clock time? |
| 21 // clock_gettime? It's not in all Linuxes. |
| 22 // |
| 23 // Let's just use gettimeofday; it's good to the microsecond. |
| 24 class HighresTimer { |
| 25 public: |
| 26 // Captures the current start time. |
| 27 HighresTimer(); |
| 28 |
| 29 // Captures the current tick, can be used to reset a timer for reuse. |
| 30 void Start(); |
| 31 |
| 32 // Returns the elapsed ticks with full resolution. |
| 33 uint64 GetElapsedTicks() const; |
| 34 |
| 35 // Returns the elapsed time in milliseconds, rounded to the nearest |
| 36 // millisecond. |
| 37 uint64 GetElapsedMs() const; |
| 38 |
| 39 // Returns the elapsed time in seconds, rounded to the nearest second. |
| 40 uint64 GetElapsedSec() const; |
| 41 |
| 42 uint64 start_ticks() const { return start_ticks_; } |
| 43 |
| 44 // Returns timer frequency from cache, should be less overhead than |
| 45 // ::QueryPerformanceFrequency. |
| 46 static uint64 GetTimerFrequency(); |
| 47 // Returns current ticks. |
| 48 static uint64 GetCurrentTicks(); |
| 49 |
| 50 private: |
| 51 // Captured start time. |
| 52 uint64 start_ticks_; |
| 53 }; |
| 54 |
| 55 inline HighresTimer::HighresTimer() { |
| 56 Start(); |
| 57 } |
| 58 |
| 59 inline void HighresTimer::Start() { |
| 60 start_ticks_ = GetCurrentTicks(); |
| 61 } |
| 62 |
| 63 inline uint64 HighresTimer::GetTimerFrequency() { |
| 64 // Fixed; one "tick" is one microsecond. |
| 65 return MICROS_IN_SECOND; |
| 66 } |
| 67 |
| 68 inline uint64 HighresTimer::GetCurrentTicks() { |
| 69 timeval tv; |
| 70 gettimeofday(&tv, 0); |
| 71 |
| 72 return tv.tv_sec * MICROS_IN_SECOND + tv.tv_usec; |
| 73 } |
| 74 |
| 75 inline uint64 HighresTimer::GetElapsedTicks() const { |
| 76 return start_ticks_ - GetCurrentTicks(); |
| 77 } |
| 78 |
| 79 #endif // CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_LINUX_H_ |
OLD | NEW |