OLD | NEW |
| (Empty) |
1 // Copyright 2003-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 // | |
16 // Timing | |
17 | |
18 #include "omaha/base/timer.h" | |
19 #include "omaha/base/commontypes.h" | |
20 #include "omaha/base/debug.h" | |
21 #include "omaha/base/logging.h" | |
22 #include "omaha/base/string.h" | |
23 #include "omaha/base/time.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 // LowResTimer class is implemented on top of ::GetTickCount. | |
28 // ::GetTickCount wraps around every 49.7 days. The code can't handle | |
29 // this condition so there is a small probability that something can go | |
30 // wrong. | |
31 LowResTimer::LowResTimer(bool running) | |
32 : running_(false), iterations_(0), elapsed_(0), start_(0) { | |
33 if (running) { | |
34 Start(); | |
35 } | |
36 } | |
37 | |
38 LowResTimer::~LowResTimer() { | |
39 } | |
40 | |
41 void LowResTimer::Reset() { | |
42 elapsed_ = 0; | |
43 running_ = 0; | |
44 iterations_ = 0; | |
45 } | |
46 | |
47 void LowResTimer::Start() { | |
48 ASSERT1(!running_); | |
49 | |
50 start_ = ::GetTickCount(); | |
51 running_ = 1; | |
52 } | |
53 | |
54 uint32 LowResTimer::Stop() { | |
55 ASSERT1(running_); | |
56 | |
57 uint32 stop = ::GetTickCount(); | |
58 ASSERT1(stop >= start_); | |
59 uint32 diff = stop - start_; | |
60 elapsed_ += diff; | |
61 iterations_++; | |
62 running_ = 0; | |
63 return diff; | |
64 } | |
65 | |
66 uint32 LowResTimer::GetMilliseconds() const { | |
67 uint32 running_time = 0; | |
68 if (running_) { | |
69 uint32 now = ::GetTickCount(); | |
70 ASSERT1(now >= start_); | |
71 running_time = now - start_; | |
72 } | |
73 return elapsed_ + running_time; | |
74 } | |
75 | |
76 // statics | |
77 // get the frequency only once | |
78 SELECTANY time64 Timer::count_freq_ = 0; | |
79 | |
80 Timer::Timer(bool running) | |
81 : running_(0), iterations_(0), elapsed_(0), start_(0), split_(0) { | |
82 // initialize only once | |
83 if (count_freq_ == 0) { | |
84 count_freq_ = GetRdtscFrequency(); | |
85 if (count_freq_ <= 1) { | |
86 UTIL_LOG(LEVEL_ERROR, | |
87 (_T("[Timer::Timer - high-res counter not supported]"))); | |
88 count_freq_ = 1; | |
89 } | |
90 } | |
91 if (running) { | |
92 Start(); | |
93 } | |
94 } | |
95 | |
96 Timer::~Timer() { | |
97 } | |
98 | |
99 void Timer::Reset() { | |
100 elapsed_ = 0; | |
101 running_ = 0; | |
102 iterations_ = 0; | |
103 } | |
104 | |
105 void Timer::Start() { | |
106 ASSERT1(!running_); | |
107 | |
108 start_ = GetRdtscCounter(); | |
109 split_ = start_; | |
110 running_ = 1; | |
111 } | |
112 | |
113 void Timer::Split(double* split_time_ms, double* total_time_ms) { | |
114 ASSERT1(running_); | |
115 | |
116 time64 now = GetRdtscCounter(); | |
117 if (split_time_ms) { | |
118 *split_time_ms = PerfCountToNanoSeconds(now - split_)/ 1000000; | |
119 } | |
120 if (total_time_ms) { | |
121 *total_time_ms = | |
122 PerfCountToNanoSeconds(elapsed_ + (now - start_)) / 1000000; | |
123 } | |
124 split_ = now; | |
125 } | |
126 | |
127 time64 Timer::Stop() { | |
128 ASSERT1(running_); | |
129 | |
130 time64 stop = GetRdtscCounter(); | |
131 time64 diff = stop - start_; | |
132 elapsed_ += diff; | |
133 iterations_++; | |
134 running_ = 0; | |
135 return diff; | |
136 } | |
137 | |
138 double Timer::GetNanoseconds() const { | |
139 time64 running_time = 0; | |
140 if (running_) { | |
141 time64 now = GetRdtscCounter(); | |
142 running_time = now - start_; | |
143 } | |
144 return PerfCountToNanoSeconds(elapsed_ + running_time); | |
145 } | |
146 | |
147 #ifdef _DEBUG | |
148 CString Timer::DebugString() const { | |
149 CString s; | |
150 double seconds = GetSeconds(); | |
151 if (iterations_) { | |
152 s.Format(_T("%s sec %d iterations %s sec/iteration"), | |
153 String_DoubleToString(seconds, 3), iterations_, | |
154 String_DoubleToString(seconds/iterations_, 3)); | |
155 } else { | |
156 s.Format(_T("%s sec"), String_DoubleToString(seconds, 3)); | |
157 } | |
158 return s; | |
159 } | |
160 #endif | |
161 | |
162 // Computes the frequency (ticks/sec) for the CPU tick-count timer (RDTSC) | |
163 // Don't call this function frequently, because computing the frequency is slow | |
164 // (relatively). | |
165 // | |
166 // TODO(omaha): check return values, and return 0 on failure. | |
167 // But hard to imagine a machine where our program will install/run but this | |
168 // will fail. | |
169 time64 Timer::GetRdtscFrequency() { | |
170 // | |
171 // Get elapsed RDTSC and elapsed QPC over same time period | |
172 // | |
173 | |
174 // compute length of time period to measure | |
175 time64 freq_qpc = 0; // ticks per second | |
176 ::QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq_qpc)); | |
177 | |
178 // fraction of second to run timers for; tradeoff b/w speed and accuracy; | |
179 // 1/1000 (1 msec) seems like good tradeoff | |
180 time64 interval_qpc = freq_qpc / 1000; | |
181 | |
182 // get timer values over same time period | |
183 time64 begin_qpc = 0; | |
184 time64 end_qpc = 0; | |
185 | |
186 ::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&begin_qpc)); | |
187 | |
188 time64 begin_rdtsc = Timer::GetRdtscCounter(); | |
189 | |
190 // spin and protect against infinite loop, if QPC does something wacky | |
191 int count = 0; | |
192 const int count_max = 200000; | |
193 do { | |
194 ::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&end_qpc)); | |
195 ++count; | |
196 } while ((end_qpc - begin_qpc) < interval_qpc && count < count_max); | |
197 | |
198 ::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&end_qpc)); | |
199 time64 end_rdtsc = Timer::GetRdtscCounter(); | |
200 | |
201 ASSERT(count < count_max, (_T("If this assert fires, your machine is either ") | |
202 _T("very fast, or very broken. Increase the ") | |
203 _T("value of const_max to fix this assert."))); | |
204 | |
205 // | |
206 // Compute RDTSC frequency from QPC frequency | |
207 // | |
208 | |
209 time64 diff_qpc = end_qpc - begin_qpc; | |
210 time64 diff_rdtsc = end_rdtsc - begin_rdtsc; | |
211 | |
212 time64 freq_rdtsc = freq_qpc * diff_rdtsc / diff_qpc; | |
213 | |
214 return freq_rdtsc; | |
215 } | |
216 | |
217 } // namespace omaha | |
218 | |
OLD | NEW |