GNU Radio Manual and C++ API Reference  3.7.4
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
high_res_timer.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2011,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef INCLUDED_GNURADIO_HIGH_RES_TIMER_H
23 #define INCLUDED_GNURADIO_HIGH_RES_TIMER_H
24 
25 #include <gnuradio/api.h>
26 
27 ////////////////////////////////////////////////////////////////////////
28 // Use architecture defines to determine the implementation
29 ////////////////////////////////////////////////////////////////////////
30 #if defined(linux) || defined(__linux) || defined(__linux__)
31  #define GNURADIO_HRT_USE_CLOCK_GETTIME
32  #include <ctime>
33 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
34  #define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
35 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
36  #define GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
37 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
38  #define GNURADIO_HRT_USE_CLOCK_GETTIME
39  #include <ctime>
40 #else
41  #define GNURADIO_HRT_USE_MICROSEC_CLOCK
42 #endif
43 
44 
45 ////////////////////////////////////////////////////////////////////////
46 namespace gr {
47 
48  //! Typedef for the timer tick count
49  typedef signed long long high_res_timer_type;
50 
51  //! Get the current time in ticks
53 
54  //! Get the current time in ticks - for performance monitoring
56 
57  //! Get the number of ticks per second
59 
60  //! Get the tick count at the epoch
62 
63 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
64  //! storage for high res timer type
65  GR_RUNTIME_API extern clockid_t high_res_timer_source;
66 #endif
67 
68 } /* namespace gr */
69 
70 ////////////////////////////////////////////////////////////////////////
71 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
73  timespec ts;
74  clock_gettime(CLOCK_MONOTONIC, &ts);
75  return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec;
76  }
77 
79  timespec ts;
80  clock_gettime(high_res_timer_source, &ts);
81  return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec;
82  }
83 
85  return 1000000000UL;
86  }
87 #endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */
88 
89 ////////////////////////////////////////////////////////////////////////
90 #ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
91  #include <mach/mach_time.h>
92 
94  return mach_absolute_time();
95  }
96 
98  return gr::high_res_timer_now();
99  }
100 
102  mach_timebase_info_data_t info;
103  mach_timebase_info(&info);
104  return gr::high_res_timer_type(info.numer*1000000000UL)/info.denom;
105  }
106 #endif
107 
108 ////////////////////////////////////////////////////////////////////////
109 #ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
110  #include <Windows.h>
111 
113  LARGE_INTEGER counts;
114  QueryPerformanceCounter(&counts);
115  return counts.QuadPart;
116  }
117 
119  return gr::high_res_timer_now();
120  }
121 
123  LARGE_INTEGER freq;
124  QueryPerformanceFrequency(&freq);
125  return freq.QuadPart;
126  }
127 #endif
128 
129 ////////////////////////////////////////////////////////////////////////
130 #ifdef GNURADIO_HRT_USE_MICROSEC_CLOCK
131  #include <boost/date_time/posix_time/posix_time.hpp>
132 
134  static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0));
135  return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks();
136  }
137 
139  return gr::high_res_timer_now();
140  }
141 
143  return boost::posix_time::time_duration::ticks_per_second();
144  }
145 #endif
146 
147 ////////////////////////////////////////////////////////////////////////
148 #include <boost/date_time/posix_time/posix_time.hpp>
149 
151  static const double hrt_ticks_per_utc_ticks = gr::high_res_timer_tps()/double(boost::posix_time::time_duration::ticks_per_second());
152  boost::posix_time::time_duration utc = boost::posix_time::microsec_clock::universal_time() - boost::posix_time::from_time_t(0);
153  return gr::high_res_timer_now() - utc.ticks()*hrt_ticks_per_utc_ticks;
154 }
155 
156 #endif /* INCLUDED_GNURADIO_HIGH_RES_TIMER_H */
high_res_timer_type high_res_timer_now(void)
Get the current time in ticks.
Definition: high_res_timer.h:133
high_res_timer_type high_res_timer_epoch(void)
Get the tick count at the epoch.
Definition: high_res_timer.h:150
high_res_timer_type high_res_timer_tps(void)
Get the number of ticks per second.
Definition: high_res_timer.h:142
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
signed long long high_res_timer_type
Typedef for the timer tick count.
Definition: high_res_timer.h:49
high_res_timer_type high_res_timer_now_perfmon(void)
Get the current time in ticks - for performance monitoring.
Definition: high_res_timer.h:138