GNU Radio C++ API Reference  g65ab7ea
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 ////////////////////////////////////////////////////////////////////////
15 // Use architecture defines to determine the implementation
16 ////////////////////////////////////////////////////////////////////////
17 #if defined(linux) || defined(__linux) || defined(__linux__)
18 #define GNURADIO_HRT_USE_CLOCK_GETTIME
19 #include <ctime>
20 #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
21 #include <windows.h>
22 #define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
23 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
24 #include <mach/mach_time.h>
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 #include <ratio>
31 #define GNURADIO_HRT_USE_GENERIC_CLOCK
32 #endif
33 
34 #include <gnuradio/api.h>
35 #include <chrono>
36 
37 ////////////////////////////////////////////////////////////////////////
38 namespace gr {
39 
40 //! Typedef for the timer tick count
41 typedef signed long long high_res_timer_type;
42 
43 //! Get the current time in ticks
45 
46 //! Get the current time in ticks - for performance monitoring
48 
49 //! Get the number of ticks per second
51 
52 //! Get the tick count at the epoch
54 
55 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
56 //! storage for high res timer type
57 GR_RUNTIME_API extern clockid_t high_res_timer_source;
58 #endif
59 
60 } /* namespace gr */
61 
62 ////////////////////////////////////////////////////////////////////////
63 #ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
65 {
66  timespec ts;
67  clock_gettime(CLOCK_MONOTONIC, &ts);
68  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
69 }
70 
72 {
73  timespec ts;
74  clock_gettime(high_res_timer_source, &ts);
75  return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
76 }
77 
78 inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
79 #endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */
80 
81 ////////////////////////////////////////////////////////////////////////
82 #ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
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 
106 {
107  LARGE_INTEGER counts;
108  QueryPerformanceCounter(&counts);
109  return counts.QuadPart;
110 }
111 
113 {
114  return gr::high_res_timer_now();
115 }
116 
118 {
119  LARGE_INTEGER freq;
120  QueryPerformanceFrequency(&freq);
121  return freq.QuadPart;
122 }
123 #endif
124 
125 ////////////////////////////////////////////////////////////////////////
126 #ifdef GNURADIO_HRT_USE_GENERIC_CLOCK
128 {
129  return std::chrono::duration<gr::high_res_timer_type, std::nano>(
130  std::chrono::steady_clock::now().time_since_epoch())
131  .count();
132 }
133 
135 {
136  return gr::high_res_timer_now();
137 }
138 
139 inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
140 #endif
141 
142 ////////////////////////////////////////////////////////////////////////
144 {
145  static const double ticks_per_second = gr::high_res_timer_tps();
146  const double seconds_since_epoch =
147  std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch())
148  .count();
149  return gr::high_res_timer_now() - seconds_since_epoch * ticks_per_second;
150 }
151 
152 #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:127
high_res_timer_type high_res_timer_tps(void)
Get the number of ticks per second.
Definition: high_res_timer.h:139
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
high_res_timer_type high_res_timer_epoch(void)
Get the tick count at the epoch.
Definition: high_res_timer.h:143
signed long long high_res_timer_type
Typedef for the timer tick count.
Definition: high_res_timer.h:41