GNU Radio 3.7.1 C++ API
high_res_timer.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2011,2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 
00022 #ifndef INCLUDED_GNURADIO_HIGH_RES_TIMER_H
00023 #define INCLUDED_GNURADIO_HIGH_RES_TIMER_H
00024 
00025 #include <gnuradio/api.h>
00026 
00027 ////////////////////////////////////////////////////////////////////////
00028 // Use architecture defines to determine the implementation
00029 ////////////////////////////////////////////////////////////////////////
00030 #if defined(linux) || defined(__linux) || defined(__linux__)
00031     #define GNURADIO_HRT_USE_CLOCK_GETTIME
00032     #include <ctime>
00033 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00034     #define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
00035 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
00036     #define GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
00037 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
00038     #define GNURADIO_HRT_USE_CLOCK_GETTIME
00039     #include <ctime>
00040 #else
00041     #define GNURADIO_HRT_USE_MICROSEC_CLOCK
00042 #endif
00043 
00044 
00045 ////////////////////////////////////////////////////////////////////////
00046 namespace gr {
00047 
00048     //! Typedef for the timer tick count
00049     typedef signed long long high_res_timer_type;
00050 
00051     //! Get the current time in ticks
00052     high_res_timer_type high_res_timer_now(void);
00053 
00054     //! Get the current time in ticks - for performance monitoring
00055     high_res_timer_type high_res_timer_now_perfmon(void);
00056 
00057     //! Get the number of ticks per second
00058     high_res_timer_type high_res_timer_tps(void);
00059 
00060     //! Get the tick count at the epoch
00061     high_res_timer_type high_res_timer_epoch(void);
00062 
00063 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
00064     //! storage for high res timer type
00065     GR_RUNTIME_API extern clockid_t high_res_timer_source;
00066 #endif
00067 
00068 } /* namespace gr */
00069 
00070 ////////////////////////////////////////////////////////////////////////
00071 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
00072     inline gr::high_res_timer_type gr::high_res_timer_now(void){
00073         timespec ts;
00074         clock_gettime(CLOCK_MONOTONIC, &ts);
00075         return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec;
00076     }
00077 
00078     inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){
00079         timespec ts;
00080         clock_gettime(high_res_timer_source, &ts);
00081         return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec;
00082     }
00083 
00084     inline gr::high_res_timer_type gr::high_res_timer_tps(void){
00085         return 1000000000UL;
00086     }
00087 #endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */
00088 
00089 ////////////////////////////////////////////////////////////////////////
00090 #ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
00091     #include <mach/mach_time.h>
00092 
00093     inline gr::high_res_timer_type gr::high_res_timer_now(void){
00094         return mach_absolute_time();
00095     }
00096 
00097     inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){
00098         return gr::high_res_timer_now();
00099     }
00100 
00101     inline gr::high_res_timer_type gr::high_res_timer_tps(void){
00102         mach_timebase_info_data_t info;
00103         mach_timebase_info(&info);
00104         return gr::high_res_timer_type(info.numer*1000000000UL)/info.denom;
00105     }
00106 #endif
00107 
00108 ////////////////////////////////////////////////////////////////////////
00109 #ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
00110     #include <Windows.h>
00111 
00112     inline gr::high_res_timer_type gr::high_res_timer_now(void){
00113         LARGE_INTEGER counts;
00114         QueryPerformanceCounter(&counts);
00115         return counts.QuadPart;
00116     }
00117 
00118     inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){
00119         return gr::high_res_timer_now();
00120     }
00121 
00122     inline gr::high_res_timer_type gr::high_res_timer_tps(void){
00123         LARGE_INTEGER freq;
00124         QueryPerformanceFrequency(&freq);
00125         return freq.QuadPart;
00126     }
00127 #endif
00128 
00129 ////////////////////////////////////////////////////////////////////////
00130 #ifdef GNURADIO_HRT_USE_MICROSEC_CLOCK
00131     #include <boost/date_time/posix_time/posix_time.hpp>
00132 
00133     inline gr::high_res_timer_type gr::high_res_timer_now(void){
00134         static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0));
00135         return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks();
00136     }
00137 
00138     inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){
00139         return gr::high_res_timer_now();
00140     }
00141 
00142     inline gr::high_res_timer_type gr::high_res_timer_tps(void){
00143         return boost::posix_time::time_duration::ticks_per_second();
00144     }
00145 #endif
00146 
00147 ////////////////////////////////////////////////////////////////////////
00148 #include <boost/date_time/posix_time/posix_time.hpp>
00149 
00150 inline gr::high_res_timer_type gr::high_res_timer_epoch(void){
00151     static const double hrt_ticks_per_utc_ticks = gr::high_res_timer_tps()/double(boost::posix_time::time_duration::ticks_per_second());
00152     boost::posix_time::time_duration utc = boost::posix_time::microsec_clock::universal_time() - boost::posix_time::from_time_t(0);
00153     return gr::high_res_timer_now() - utc.ticks()*hrt_ticks_per_utc_ticks;
00154 }
00155 
00156 #endif /* INCLUDED_GNURADIO_HIGH_RES_TIMER_H */