GNU Radio Manual and C++ API Reference  3.10.9.1
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 #include <chrono>
16 #include <ratio>
17 
18 ////////////////////////////////////////////////////////////////////////
19 // Use architecture defines to determine the implementation
20 ////////////////////////////////////////////////////////////////////////
21 #if defined(linux) || defined(__linux) || defined(__linux__)
22 #define GNURADIO_HRT_USE_CLOCK_GETTIME
23 #include <ctime>
24 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
25 #define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
26 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
27 #define GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
28 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
29 #define GNURADIO_HRT_USE_CLOCK_GETTIME
30 #include <ctime>
31 #else
32 #define GNURADIO_HRT_USE_GENERIC_CLOCK
33 #endif
34 
35 
36 ////////////////////////////////////////////////////////////////////////
37 namespace gr {
38 
39 //! Typedef for the timer tick count
40 typedef signed long long high_res_timer_type;
41 
42 //! Get the current time in ticks
44 
45 //! Get the current time in ticks - for performance monitoring
47 
48 //! Get the number of ticks per second
50 
51 //! Get the tick count at the epoch
53 
54 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
55 //! storage for high res timer type
56 GR_RUNTIME_API extern clockid_t high_res_timer_source;
57 #endif
58 
59 } /* namespace gr */
60 
61 ////////////////////////////////////////////////////////////////////////
62 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
64 {
65  timespec ts;
66  clock_gettime(CLOCK_MONOTONIC, &ts);
67  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
68 }
69 
71 {
72  timespec ts;
73  clock_gettime(high_res_timer_source, &ts);
74  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
75 }
76 
77 inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
78 #endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */
79 
80 ////////////////////////////////////////////////////////////////////////
81 #ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
82 #include <mach/mach_time.h>
83 
85 {
86  return mach_absolute_time();
87 }
88 
90 {
91  return gr::high_res_timer_now();
92 }
93 
95 {
96  mach_timebase_info_data_t info;
97  mach_timebase_info(&info);
98  return gr::high_res_timer_type(info.denom * 1000000000UL) / info.numer;
99 }
100 #endif
101 
102 ////////////////////////////////////////////////////////////////////////
103 #ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
104 #include <windows.h>
105 
107 {
108  LARGE_INTEGER counts;
109  QueryPerformanceCounter(&counts);
110  return counts.QuadPart;
111 }
112 
114 {
115  return gr::high_res_timer_now();
116 }
117 
119 {
120  LARGE_INTEGER freq;
121  QueryPerformanceFrequency(&freq);
122  return freq.QuadPart;
123 }
124 #endif
125 
126 ////////////////////////////////////////////////////////////////////////
127 #ifdef GNURADIO_HRT_USE_GENERIC_CLOCK
129 {
130  return std::chrono::duration<gr::high_res_timer_type, std::nano>(
131  std::chrono::steady_clock::now().time_since_epoch())
132  .count();
133 }
134 
136 {
137  return gr::high_res_timer_now();
138 }
139 
140 inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
141 #endif
142 
143 ////////////////////////////////////////////////////////////////////////
145 {
146  static const double ticks_per_second = gr::high_res_timer_tps();
147  const double seconds_since_epoch =
148  std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch())
149  .count();
150  return gr::high_res_timer_now() - seconds_since_epoch * ticks_per_second;
151 }
152 
153 #endif /* INCLUDED_GNURADIO_HIGH_RES_TIMER_H */
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29
high_res_timer_type high_res_timer_now(void)
Get the current time in ticks.
Definition: high_res_timer.h:128
high_res_timer_type high_res_timer_tps(void)
Get the number of ticks per second.
Definition: high_res_timer.h:140
high_res_timer_type high_res_timer_now_perfmon(void)
Get the current time in ticks - for performance monitoring.
Definition: high_res_timer.h:135
high_res_timer_type high_res_timer_epoch(void)
Get the tick count at the epoch.
Definition: high_res_timer.h:144
signed long long high_res_timer_type
Typedef for the timer tick count.
Definition: high_res_timer.h:40