root / gruel / src / include / gruel / high_res_timer.h @ 056d3006
History | View | Annotate | Download (4.5 kB)
| 1 | /*
|
|---|---|
| 2 | * Copyright 2011 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GNU Radio |
| 5 | * |
| 6 | * GNU Radio is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * GNU Radio is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #ifndef INCLUDED_GRUEL_HIGH_RES_TIMER_H
|
| 22 | #define INCLUDED_GRUEL_HIGH_RES_TIMER_H
|
| 23 | |
| 24 | namespace gruel {
|
| 25 | //! Typedef for the timer tick count
|
| 26 | typedef signed long long high_res_timer_type; |
| 27 | |
| 28 | //! Get the current time in ticks
|
| 29 | high_res_timer_type high_res_timer_now(void);
|
| 30 | |
| 31 | //! Get the number of ticks per second
|
| 32 | high_res_timer_type high_res_timer_tps(void);
|
| 33 | |
| 34 | //! Get the tick count at the epoch
|
| 35 | high_res_timer_type high_res_timer_epoch(void);
|
| 36 | |
| 37 | } /* namespace gruel */
|
| 38 | |
| 39 | ////////////////////////////////////////////////////////////////////////
|
| 40 | // Use architecture defines to determine the implementation
|
| 41 | ////////////////////////////////////////////////////////////////////////
|
| 42 | #if defined(linux) || defined(__linux) || defined(__linux__)
|
| 43 | #define GRUEL_HRT_USE_CLOCK_GETTIME
|
| 44 | #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
| 45 | #define GRUEL_HRT_USE_QUERY_PERFORMANCE_COUNTER
|
| 46 | #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
|
| 47 | #define GRUEL_HRT_USE_MACH_ABSOLUTE_TIME
|
| 48 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
| 49 | #define GRUEL_HRT_USE_CLOCK_GETTIME
|
| 50 | #else
|
| 51 | #define GRUEL_HRT_USE_MICROSEC_CLOCK
|
| 52 | #endif
|
| 53 | |
| 54 | ////////////////////////////////////////////////////////////////////////
|
| 55 | #ifdef GRUEL_HRT_USE_CLOCK_GETTIME
|
| 56 | #include <ctime> |
| 57 | |
| 58 | inline gruel::high_res_timer_type gruel::high_res_timer_now(void){ |
| 59 | timespec ts; |
| 60 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 61 | return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec;
|
| 62 | } |
| 63 | |
| 64 | inline gruel::high_res_timer_type gruel::high_res_timer_tps(void){ |
| 65 | return 1000000000UL; |
| 66 | } |
| 67 | #endif /* GRUEL_HRT_USE_CLOCK_GETTIME */ |
| 68 | |
| 69 | ////////////////////////////////////////////////////////////////////////
|
| 70 | #ifdef GRUEL_HRT_USE_MACH_ABSOLUTE_TIME
|
| 71 | #include <mach/mach_time.h> |
| 72 | |
| 73 | inline gruel::high_res_timer_type gruel::high_res_timer_now(void){ |
| 74 | return mach_absolute_time();
|
| 75 | } |
| 76 | |
| 77 | inline gruel::high_res_timer_type gruel::high_res_timer_tps(void){ |
| 78 | mach_timebase_info_data_t info; |
| 79 | mach_timebase_info(&info); |
| 80 | return gruel::high_res_timer_type(info.numer*1000000000UL)/info.denom; |
| 81 | } |
| 82 | #endif
|
| 83 | |
| 84 | ////////////////////////////////////////////////////////////////////////
|
| 85 | #ifdef GRUEL_HRT_USE_QUERY_PERFORMANCE_COUNTER
|
| 86 | #include <Windows.h> |
| 87 | |
| 88 | inline gruel::high_res_timer_type gruel::high_res_timer_now(void){ |
| 89 | LARGE_INTEGER counts; |
| 90 | QueryPerformanceCounter(&counts); |
| 91 | return counts.QuadPart;
|
| 92 | } |
| 93 | |
| 94 | inline gruel::high_res_timer_type gruel::high_res_timer_tps(void){ |
| 95 | LARGE_INTEGER freq; |
| 96 | QueryPerformanceFrequency(&freq); |
| 97 | return freq.QuadPart;
|
| 98 | } |
| 99 | #endif
|
| 100 | |
| 101 | ////////////////////////////////////////////////////////////////////////
|
| 102 | #ifdef GRUEL_HRT_USE_MICROSEC_CLOCK
|
| 103 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 104 | |
| 105 | inline gruel::high_res_timer_type gruel::high_res_timer_now(void){ |
| 106 | static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0)); |
| 107 | return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks();
|
| 108 | } |
| 109 | |
| 110 | inline gruel::high_res_timer_type gruel::high_res_timer_tps(void){ |
| 111 | return boost::posix_time::time_duration::ticks_per_second();
|
| 112 | } |
| 113 | #endif
|
| 114 | |
| 115 | ////////////////////////////////////////////////////////////////////////
|
| 116 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 117 | |
| 118 | inline gruel::high_res_timer_type gruel::high_res_timer_epoch(void){ |
| 119 | static const double hrt_ticks_per_utc_ticks = gruel::high_res_timer_tps()/double(boost::posix_time::time_duration::ticks_per_second()); |
| 120 | boost::posix_time::time_duration utc = boost::posix_time::microsec_clock::universal_time() - boost::posix_time::from_time_t(0);
|
| 121 | return gruel::high_res_timer_now() - utc.ticks()*hrt_ticks_per_utc_ticks;
|
| 122 | } |
| 123 | |
| 124 | #endif /* INCLUDED_GRUEL_HIGH_RES_TIMER_H */ |