| 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 #include "omaha/base/highres_timer-win32.h" | |
| 16 | |
| 17 namespace omaha { | |
| 18 | |
| 19 bool HighresTimer::perf_freq_collected_ = false; | |
| 20 ULONGLONG HighresTimer::perf_freq_ = 0; | |
| 21 | |
| 22 ULONGLONG HighresTimer::GetElapsedMs() const { | |
| 23 ULONGLONG end_time = GetCurrentTicks(); | |
| 24 | |
| 25 // Scale to ms and round to nearerst ms - rounding is important | |
| 26 // because otherwise the truncation error may accumulate e.g. in sums. | |
| 27 // | |
| 28 // Given infinite resolution, this expression could be written as: | |
| 29 // trunc((end - start (units:freq*sec))/freq (units:sec) * | |
| 30 // 1000 (unit:ms) + 1/2 (unit:ms)) | |
| 31 ULONGLONG freq = GetTimerFrequency(); | |
| 32 return ((end_time - start_ticks_) * 1000L + freq / 2) / freq; | |
| 33 } | |
| 34 | |
| 35 ULONGLONG HighresTimer::GetElapsedSec() const { | |
| 36 ULONGLONG end_time = GetCurrentTicks(); | |
| 37 | |
| 38 // Scale to ms and round to nearerst ms - rounding is important | |
| 39 // because otherwise the truncation error may accumulate e.g. in sums. | |
| 40 // | |
| 41 // Given infinite resolution, this expression could be written as: | |
| 42 // trunc((end - start (units:freq*sec))/freq (unit:sec) + 1/2 (unit:sec)) | |
| 43 ULONGLONG freq = GetTimerFrequency(); | |
| 44 return ((end_time - start_ticks_) + freq / 2) / freq; | |
| 45 } | |
| 46 | |
| 47 void HighresTimer::CollectPerfFreq() { | |
| 48 LARGE_INTEGER freq; | |
| 49 | |
| 50 // Note that this is racy. | |
| 51 // It's OK, however, because even concurrent executions of this | |
| 52 // are idempotent. | |
| 53 if (::QueryPerformanceFrequency(&freq)) { | |
| 54 perf_freq_ = freq.QuadPart; | |
| 55 perf_freq_collected_ = true; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 } // namespace omaha | |
| 60 | |
| OLD | NEW |