Ticket #3345: high_resolution_timer.2.hpp

File high_resolution_timer.2.hpp, 14.5 KB (added by ej.grace@…, 13 years ago)

Fixed update of the high_precision_timer - differences of unsigned integers are now explicitly mangaged.

Line 
1// Copyright (c) 2005-2009 Hartmut Kaiser
2// Copyright (c) 2009 Edward Grace
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(HIGH_RESOLUTION_TIMER_AUG_14_2009_0425PM)
8#define HIGH_RESOLUTION_TIMER_AUG_14_2009_0425PM
9
10#include <boost/config.hpp>
11#include <boost/throw_exception.hpp>
12
13#if defined(BOOST_HAS_UNISTD_H)
14#include <unistd.h>
15#endif
16#include <time.h>
17
18#if defined(BOOST_WINDOWS)
19
20#include <stdexcept>
21#include <limits>
22#include <windows.h>
23
24namespace util
25{
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // high_resolution_timer
29 // A timer object measures elapsed time.
30 // CAUTION: Windows only!
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 class high_resolution_timer
34 {
35 public:
36 high_resolution_timer()
37 {
38 restart();
39 }
40
41 high_resolution_timer(double t)
42 {
43 LARGE_INTEGER frequency;
44 if (!QueryPerformanceFrequency(&frequency))
45 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
46
47 start_time.QuadPart = (LONGLONG)(t * frequency.QuadPart);
48 }
49
50 high_resolution_timer(high_resolution_timer const& rhs)
51 : start_time(rhs.start_time)
52 {
53 }
54
55 static double now()
56 {
57 SYSTEMTIME st;
58 GetSystemTime(&st);
59
60 FILETIME ft;
61 SystemTimeToFileTime(&st, &ft);
62
63 LARGE_INTEGER now;
64 now.LowPart = ft.dwLowDateTime;
65 now.HighPart = ft.dwHighDateTime;
66
67 // FileTime is in 100ns increments, result needs to be in [s]
68 return now.QuadPart * 1e-7;
69 }
70
71 void restart()
72 {
73 if (!QueryPerformanceCounter(&start_time))
74 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
75 }
76 double elapsed() const // return elapsed time in seconds
77 {
78 LARGE_INTEGER now;
79 if (!QueryPerformanceCounter(&now))
80 boost::throw_exception(std::runtime_error("Couldn't get current time"));
81
82 LARGE_INTEGER frequency;
83 if (!QueryPerformanceFrequency(&frequency))
84 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
85
86 return double(now.QuadPart - start_time.QuadPart) / frequency.QuadPart;
87 }
88
89 double elapsed_max() const // return estimated maximum value for elapsed()
90 {
91 LARGE_INTEGER frequency;
92 if (!QueryPerformanceFrequency(&frequency))
93 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
94
95 return double((std::numeric_limits<LONGLONG>::max)() - start_time.QuadPart) /
96 double(frequency.QuadPart);
97 }
98
99 double elapsed_min() const // return minimum value for elapsed()
100 {
101 LARGE_INTEGER frequency;
102 if (!QueryPerformanceFrequency(&frequency))
103 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
104
105 return 1.0 / frequency.QuadPart;
106 }
107
108 private:
109 LARGE_INTEGER start_time;
110 };
111
112} // namespace util
113
114#elif defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(_POSIX_THREAD_CPUTIME)
115
116#if _POSIX_THREAD_CPUTIME > 0 // timer always supported
117
118namespace util
119{
120
121 ///////////////////////////////////////////////////////////////////////////////
122 //
123 // high_resolution_timer
124 // A timer object measures elapsed time.
125 //
126 ///////////////////////////////////////////////////////////////////////////////
127 class high_resolution_timer
128 {
129 public:
130 high_resolution_timer()
131 {
132 start_time.tv_sec = 0;
133 start_time.tv_nsec = 0;
134
135 restart();
136 }
137
138 high_resolution_timer(double t)
139 {
140 start_time.tv_sec = time_t(t);
141 start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
142 }
143
144 high_resolution_timer(high_resolution_timer const& rhs)
145 : start_time(rhs.start_time)
146 {
147 }
148
149 static double now()
150 {
151 timespec now;
152 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
153 boost::throw_exception(std::runtime_error("Couldn't get current time"));
154 return double(now.tv_sec) + double(now.tv_nsec) * 1e-9;
155 }
156
157 void restart()
158 {
159 if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
160 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
161 }
162 double elapsed() const // return elapsed time in seconds
163 {
164 timespec now;
165 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
166 boost::throw_exception(std::runtime_error("Couldn't get current time"));
167
168 if (now.tv_sec == start_time.tv_sec)
169 return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
170
171 return double(now.tv_sec - start_time.tv_sec) +
172 (double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
173 }
174
175 double elapsed_max() const // return estimated maximum value for elapsed()
176 {
177 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
178 }
179
180 double elapsed_min() const // return minimum value for elapsed()
181 {
182 timespec resolution;
183 if (-1 == clock_getres(CLOCK_REALTIME, &resolution))
184 boost::throw_exception(std::runtime_error("Couldn't get resolution"));
185 return double(resolution.tv_sec + resolution.tv_nsec * 1e-9);
186 }
187
188 private:
189 timespec start_time;
190 };
191
192} // namespace util
193
194#else // _POSIX_THREAD_CPUTIME > 0
195
196#include <boost/timer.hpp>
197
198// availability of high performance timers must be checked at runtime
199namespace util
200{
201 ///////////////////////////////////////////////////////////////////////////////
202 //
203 // high_resolution_timer
204 // A timer object measures elapsed time.
205 //
206 ///////////////////////////////////////////////////////////////////////////////
207 class high_resolution_timer
208 {
209 public:
210 high_resolution_timer()
211 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
212 {
213 if (!use_backup) {
214 start_time.tv_sec = 0;
215 start_time.tv_nsec = 0;
216 }
217 restart();
218 }
219
220 high_resolution_timer(double t)
221 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
222 {
223 if (!use_backup) {
224 start_time.tv_sec = time_t(t);
225 start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
226 }
227 }
228
229 high_resolution_timer(high_resolution_timer const& rhs)
230 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0),
231 start_time(rhs.start_time)
232 {
233 }
234
235 static double now()
236 {
237 if (sysconf(_SC_THREAD_CPUTIME) <= 0)
238 return double(std::clock());
239
240 timespec now;
241 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
242 boost::throw_exception(std::runtime_error("Couldn't get current time"));
243 return double(now.tv_sec) + double(now.tv_nsec) * 1e-9;
244 }
245
246 void restart()
247 {
248 if (use_backup)
249 start_time_backup.restart();
250 else if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
251 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
252 }
253 double elapsed() const // return elapsed time in seconds
254 {
255 if (use_backup)
256 return start_time_backup.elapsed();
257
258 timespec now;
259 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
260 boost::throw_exception(std::runtime_error("Couldn't get current time"));
261
262 if (now.tv_sec == start_time.tv_sec)
263 return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
264
265 return double(now.tv_sec - start_time.tv_sec) +
266 (double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
267 }
268
269 double elapsed_max() const // return estimated maximum value for elapsed()
270 {
271 if (use_backup)
272 start_time_backup.elapsed_max();
273
274 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
275 }
276
277 double elapsed_min() const // return minimum value for elapsed()
278 {
279 if (use_backup)
280 start_time_backup.elapsed_min();
281
282 timespec resolution;
283 if (-1 == clock_getres(CLOCK_REALTIME, &resolution))
284 boost::throw_exception(std::runtime_error("Couldn't get resolution"));
285 return double(resolution.tv_sec + resolution.tv_nsec * 1e-9);
286 }
287
288 private:
289 bool use_backup;
290 timespec start_time;
291 boost::timer start_time_backup;
292 };
293
294} // namespace util
295
296#endif // _POSIX_THREAD_CPUTIME > 0
297
298
299#else // !defined(BOOST_WINDOWS) && (!defined(_POSIX_TIMERS)
300 // || _POSIX_TIMERS <= 0
301 // || !defined(_POSIX_THREAD_CPUTIME)
302 // || _POSIX_THREAD_CPUTIME <= 0)
303
304#if defined(BOOST_HAS_GETTIMEOFDAY)
305
306// For platforms that do not support _POSIX_TIMERS but do have
307// GETTIMEOFDAY, which is still preferable to std::clock()
308#include <stdexcept>
309#include <limits>
310
311namespace util
312{
313
314 ///////////////////////////////////////////////////////////////////////////////
315 //
316 // high_resolution_timer
317 // A timer object measures elapsed time.
318 //
319 // Implemented with gettimeofday() for platforms that support it,
320 // such as Darwin (OS X) but do not support the previous options.
321 //
322 // Copyright (c) 2009 Edward Grace
323 //
324 ///////////////////////////////////////////////////////////////////////////////
325 class high_resolution_timer
326 {
327 template <class U>
328 inline double unsigned_diff(const U &a, const U &b) const {
329 if (a>b)
330 return static_cast<double>(a-b);
331 else
332 return -static_cast<double>(b-a);
333 }
334
335 /**
336 * @brief Return the difference between two timeval types.
337 *
338 * @param t1 The most recent timeval.
339 * @param t0 The historical timeval.
340 *
341 * @return The difference between the two in seconds.
342 */
343 double elapsed(const timeval &t1, const timeval &t0) const {
344 if (t1.tv_sec == t0.tv_sec)
345 return unsigned_diff(t1.tv_usec,t0.tv_usec) * 1E-6;
346
347 // We do it this way as the result of the difference of the
348 // microseconds can be negative if the clock is implemented so
349 // that the seconds timer increases in large steps.
350 //
351 // Naive subtraction of the unsigned types and conversion to
352 // double can wreak havoc!
353 return unsigned_diff(t1.tv_sec,t0.tv_sec) +
354 unsigned_diff(t1.tv_usec,t0.tv_usec)*1e-6;
355 }
356 public:
357 high_resolution_timer()
358 {
359 start_time.tv_sec = 0;
360 start_time.tv_usec = 0;
361
362 restart();
363 }
364
365 high_resolution_timer(double t)
366 {
367 start_time.tv_sec = time_t(t);
368 start_time.tv_usec = (t - start_time.tv_sec) * 1e6;
369 }
370
371 high_resolution_timer(high_resolution_timer const& rhs)
372 : start_time(rhs.start_time)
373 {
374 }
375
376 static double now()
377 {
378 timeval now;
379 // Under some implementations gettimeofday() will always
380 // return zero. If it returns anything else however then
381 // we accept this as evidence of an error. Note we are
382 // not assuming that -1 explicitly indicates the error
383 // condition, just that non zero is indicative of the
384 // error.
385 if (gettimeofday(&now,NULL))
386 boost::throw_exception(std::runtime_error("Couldn't get current time"));
387 return double(now.tv_sec) + double(now.tv_usec) * 1e-6;
388 }
389
390 void restart()
391 {
392 if (gettimeofday(&start_time,NULL))
393 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
394 }
395
396 double elapsed() const // return elapsed time in seconds
397 {
398 timeval now;
399 if (gettimeofday(&now,NULL))
400 boost::throw_exception(std::runtime_error("Couldn't get current time"));
401 return elapsed(now,start_time);
402 }
403
404 double elapsed_max() const // return estimated maximum value for elapsed()
405 {
406 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
407 }
408
409 double elapsed_min() const // return minimum value for elapsed()
410 {
411 timeval t0,t1;
412 double delta(0);
413 // On systems without an explicit clock_getres or similar
414 // we can only estimate an upper bound on the resolution
415 // by repeatedly calling the gettimeofday function. This
416 // is often likely to be indicative of the true
417 // resolution.
418 if (gettimeofday(&t0,NULL))
419 boost::throw_exception(std::runtime_error("Couldn't get resolution."));
420
421 // Spin around in a tight loop until we observe a change
422 // in the reported timer value.
423 do {
424 if (gettimeofday(&t1,NULL))
425 boost::throw_exception(std::runtime_error("Couldn't get resolution."));
426 delta = elapsed(t1,t0);
427 } while (delta <= 0.0);
428
429 return delta;
430 }
431
432 private:
433 timeval start_time;
434 };
435
436}
437
438#else // BOOST_HAS_GETTIMEOFDAY
439
440// For platforms other than Windows or Linux, simply fall back to boost::timer
441#include <boost/timer.hpp>
442
443namespace util
444{
445 struct high_resolution_timer
446 : boost::timer
447 {
448 static double now()
449 {
450 return double(std::clock());
451 }
452 };
453}
454
455#endif
456
457#endif
458
459#endif // HIGH_RESOLUTION_TIMER_AUG_14_2009_0425PM
460
461//
462// $Log: high_resolution_timer.hpp,v $
463// Revision 1.4 2009/08/14 15:28:10 graceej
464// *** empty log message ***
465//
466// Revision 1.3 2009/08/14 15:27:30 graceej
467//
468// * It is entirely possible for the updating clock to increment the
469// * seconds and *decrement* the microseconds field. Consequently
470// * when subtracting these unsigned microseconds fields a wrap-around
471// * error can occur. For this reason elapsed(t1,t0) is used in a
472// * similar maner to cycle.h this preserves the sign of the
473// * difference.
474//
475//
476
477