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