GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
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
52 high_res_timer_type high_res_timer_now(void);
53 
54 //! Get the current time in ticks - for performance monitoring
55 high_res_timer_type high_res_timer_now_perfmon(void);
56 
57 //! Get the number of ticks per second
58 high_res_timer_type high_res_timer_tps(void);
59 
60 //! Get the tick count at the epoch
61 high_res_timer_type high_res_timer_epoch(void);
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 {
74  timespec ts;
75  clock_gettime(CLOCK_MONOTONIC, &ts);
76  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
77 }
78 
80 {
81  timespec ts;
82  clock_gettime(high_res_timer_source, &ts);
83  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
84 }
85 
86 inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
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 {
95  return mach_absolute_time();
96 }
97 
99 {
100  return gr::high_res_timer_now();
101 }
102 
104 {
105  mach_timebase_info_data_t info;
106  mach_timebase_info(&info);
107  return gr::high_res_timer_type(info.numer * 1000000000UL) / info.denom;
108 }
109 #endif
110 
111 ////////////////////////////////////////////////////////////////////////
112 #ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
113 #include <windows.h>
114 
116 {
117  LARGE_INTEGER counts;
118  QueryPerformanceCounter(&counts);
119  return counts.QuadPart;
120 }
121 
123 {
124  return gr::high_res_timer_now();
125 }
126 
128 {
129  LARGE_INTEGER freq;
130  QueryPerformanceFrequency(&freq);
131  return freq.QuadPart;
132 }
133 #endif
134 
135 ////////////////////////////////////////////////////////////////////////
136 #ifdef GNURADIO_HRT_USE_MICROSEC_CLOCK
137 #include <boost/date_time/posix_time/posix_time.hpp>
138 
140 {
141  static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0));
142  return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks();
143 }
144 
146 {
147  return gr::high_res_timer_now();
148 }
149 
151 {
152  return boost::posix_time::time_duration::ticks_per_second();
153 }
154 #endif
155 
156 ////////////////////////////////////////////////////////////////////////
157 #include <boost/date_time/posix_time/posix_time.hpp>
158 
160 {
161  static const double hrt_ticks_per_utc_ticks =
163  double(boost::posix_time::time_duration::ticks_per_second());
164  boost::posix_time::time_duration utc =
165  boost::posix_time::microsec_clock::universal_time() -
166  boost::posix_time::from_time_t(0);
167  return gr::high_res_timer_now() - utc.ticks() * hrt_ticks_per_utc_ticks;
168 }
169 
170 #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:139
high_res_timer_type high_res_timer_epoch(void)
Get the tick count at the epoch.
Definition: high_res_timer.h:159
high_res_timer_type high_res_timer_tps(void)
Get the number of ticks per second.
Definition: high_res_timer.h:150
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
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:145