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