| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 #ifndef OMAHA_COMMON_HIGHRES_TIMER_WIN32_H__ | |
| 16 #define OMAHA_COMMON_HIGHRES_TIMER_WIN32_H__ | |
| 17 | |
| 18 #include <windows.h> | |
| 19 | |
| 20 namespace omaha { | |
| 21 | |
| 22 /// A handy class for reliably measuring wall-clock time with decent resolution, | |
| 23 /// even on multi-processor machines and on laptops (where RDTSC potentially | |
| 24 /// returns different results on different processors and/or the RDTSC timer | |
| 25 /// clocks at different rates depending on the power state of the CPU, | |
| 26 /// respectively). | |
| 27 class HighresTimer { | |
| 28 public: | |
| 29 /// Captures the current start time | |
| 30 HighresTimer(); | |
| 31 | |
| 32 /// Captures the current tick, can be used to reset a timer for reuse. | |
| 33 void Start(); | |
| 34 | |
| 35 /// Returns the elapsed ticks with full resolution | |
| 36 ULONGLONG GetElapsedTicks() const; | |
| 37 | |
| 38 /// Returns the elapsed time in milliseconds, rounded to the nearest | |
| 39 /// millisecond. | |
| 40 ULONGLONG GetElapsedMs() const; | |
| 41 | |
| 42 /// Returns the elapsed time in seconds, rounded to the nearest second. | |
| 43 ULONGLONG GetElapsedSec() const; | |
| 44 | |
| 45 ULONGLONG start_ticks() const { return start_ticks_; } | |
| 46 | |
| 47 /// Returns timer frequency from cache, should be less | |
| 48 /// overhead than ::QueryPerformanceFrequency | |
| 49 static ULONGLONG GetTimerFrequency(); | |
| 50 /// Returns current ticks | |
| 51 static ULONGLONG GetCurrentTicks(); | |
| 52 | |
| 53 private: | |
| 54 static void CollectPerfFreq(); | |
| 55 | |
| 56 /// Captured start time | |
| 57 ULONGLONG start_ticks_; | |
| 58 | |
| 59 /// Captured performance counter frequency | |
| 60 static bool perf_freq_collected_; | |
| 61 static ULONGLONG perf_freq_; | |
| 62 }; | |
| 63 | |
| 64 inline HighresTimer::HighresTimer() { | |
| 65 Start(); | |
| 66 } | |
| 67 | |
| 68 inline void HighresTimer::Start() { | |
| 69 start_ticks_ = GetCurrentTicks(); | |
| 70 } | |
| 71 | |
| 72 inline ULONGLONG HighresTimer::GetTimerFrequency() { | |
| 73 if (!perf_freq_collected_) | |
| 74 CollectPerfFreq(); | |
| 75 return perf_freq_; | |
| 76 } | |
| 77 | |
| 78 inline ULONGLONG HighresTimer::GetCurrentTicks() { | |
| 79 LARGE_INTEGER ticks; | |
| 80 ::QueryPerformanceCounter(&ticks); | |
| 81 return ticks.QuadPart; | |
| 82 } | |
| 83 | |
| 84 inline ULONGLONG HighresTimer::GetElapsedTicks() const { | |
| 85 return start_ticks_ - GetCurrentTicks(); | |
| 86 } | |
| 87 | |
| 88 } // namespace omaha | |
| 89 | |
| 90 #endif // OMAHA_COMMON_HIGHRES_TIMER_WIN32_H__ | |
| OLD | NEW |