GNU Radio Manual and C++ API Reference  3.9.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  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_GNURADIO_HIGH_RES_TIMER_H
12 #define INCLUDED_GNURADIO_HIGH_RES_TIMER_H
13 
14 #include <gnuradio/api.h>
15 
16 ////////////////////////////////////////////////////////////////////////
17 // Use architecture defines to determine the implementation
18 ////////////////////////////////////////////////////////////////////////
19 #if defined(linux) || defined(__linux) || defined(__linux__)
20 #define GNURADIO_HRT_USE_CLOCK_GETTIME
21 #include <ctime>
22 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
23 #define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
24 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
25 #define GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
26 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
27 #define GNURADIO_HRT_USE_CLOCK_GETTIME
28 #include <ctime>
29 #else
30 #define GNURADIO_HRT_USE_MICROSEC_CLOCK
31 #endif
32 
33 
34 ////////////////////////////////////////////////////////////////////////
35 namespace gr {
36 
37 //! Typedef for the timer tick count
38 typedef signed long long high_res_timer_type;
39 
40 //! Get the current time in ticks
42 
43 //! Get the current time in ticks - for performance monitoring
45 
46 //! Get the number of ticks per second
48 
49 //! Get the tick count at the epoch
51 
52 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
53 //! storage for high res timer type
54 GR_RUNTIME_API extern clockid_t high_res_timer_source;
55 #endif
56 
57 } /* namespace gr */
58 
59 ////////////////////////////////////////////////////////////////////////
60 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
62 {
63  timespec ts;
64  clock_gettime(CLOCK_MONOTONIC, &ts);
65  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
66 }
67 
69 {
70  timespec ts;
71  clock_gettime(high_res_timer_source, &ts);
72  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
73 }
74 
75 inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
76 #endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */
77 
78 ////////////////////////////////////////////////////////////////////////
79 #ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
80 #include <mach/mach_time.h>
81 
83 {
84  return mach_absolute_time();
85 }
86 
88 {
89  return gr::high_res_timer_now();
90 }
91 
93 {
94  mach_timebase_info_data_t info;
95  mach_timebase_info(&info);
96  return gr::high_res_timer_type(info.numer * 1000000000UL) / info.denom;
97 }
98 #endif
99 
100 ////////////////////////////////////////////////////////////////////////
101 #ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
102 #include <windows.h>
103 
105 {
106  LARGE_INTEGER counts;
107  QueryPerformanceCounter(&counts);
108  return counts.QuadPart;
109 }
110 
112 {
113  return gr::high_res_timer_now();
114 }
115 
117 {
118  LARGE_INTEGER freq;
119  QueryPerformanceFrequency(&freq);
120  return freq.QuadPart;
121 }
122 #endif
123 
124 ////////////////////////////////////////////////////////////////////////
125 #ifdef GNURADIO_HRT_USE_MICROSEC_CLOCK
126 #include <boost/date_time/posix_time/posix_time.hpp>
127 
129 {
130  static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0));
131  return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks();
132 }
133 
135 {
136  return gr::high_res_timer_now();
137 }
138 
140 {
141  return boost::posix_time::time_duration::ticks_per_second();
142 }
143 #endif
144 
145 ////////////////////////////////////////////////////////////////////////
146 #include <boost/date_time/posix_time/posix_time.hpp>
147 
149 {
150  static const double hrt_ticks_per_utc_ticks =
152  double(boost::posix_time::time_duration::ticks_per_second());
153  boost::posix_time::time_duration utc =
154  boost::posix_time::microsec_clock::universal_time() -
155  boost::posix_time::from_time_t(0);
156  return gr::high_res_timer_now() - utc.ticks() * hrt_ticks_per_utc_ticks;
157 }
158 
159 #endif /* INCLUDED_GNURADIO_HIGH_RES_TIMER_H */
gr::high_res_timer_tps
high_res_timer_type high_res_timer_tps(void)
Get the number of ticks per second.
Definition: high_res_timer.h:139
gr::high_res_timer_now
high_res_timer_type high_res_timer_now(void)
Get the current time in ticks.
Definition: high_res_timer.h:128
gr::high_res_timer_now_perfmon
high_res_timer_type high_res_timer_now_perfmon(void)
Get the current time in ticks - for performance monitoring.
Definition: high_res_timer.h:134
gr::high_res_timer_epoch
high_res_timer_type high_res_timer_epoch(void)
Get the tick count at the epoch.
Definition: high_res_timer.h:148
GR_RUNTIME_API
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
gr::high_res_timer_type
signed long long high_res_timer_type
Typedef for the timer tick count.
Definition: high_res_timer.h:38
api.h
gr
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29