GNU Radio Manual and C++ API Reference 3.10.1.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 <boost/date_time/posix_time/posix_time.hpp>
16
17////////////////////////////////////////////////////////////////////////
18// Use architecture defines to determine the implementation
19////////////////////////////////////////////////////////////////////////
20#if defined(linux) || defined(__linux) || defined(__linux__)
21#define GNURADIO_HRT_USE_CLOCK_GETTIME
22#include <ctime>
23#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
24#define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
25#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
26#define GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
27#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
28#define GNURADIO_HRT_USE_CLOCK_GETTIME
29#include <ctime>
30#else
31#define GNURADIO_HRT_USE_MICROSEC_CLOCK
32#endif
33
34
35////////////////////////////////////////////////////////////////////////
36namespace gr {
37
38//! Typedef for the timer tick count
39typedef signed long long high_res_timer_type;
40
41//! Get the current time in ticks
43
44//! Get the current time in ticks - for performance monitoring
46
47//! Get the number of ticks per second
49
50//! Get the tick count at the epoch
52
53#ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
54//! storage for high res timer type
55GR_RUNTIME_API extern clockid_t high_res_timer_source;
56#endif
57
58} /* namespace gr */
59
60////////////////////////////////////////////////////////////////////////
61#ifdef GNURADIO_HRT_USE_CLOCK_GETTIME
63{
64 timespec ts;
65 clock_gettime(CLOCK_MONOTONIC, &ts);
66 return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
67}
68
70{
71 timespec ts;
72 clock_gettime(high_res_timer_source, &ts);
73 return ts.tv_sec * high_res_timer_tps() + ts.tv_nsec;
74}
75
76inline gr::high_res_timer_type gr::high_res_timer_tps(void) { return 1000000000UL; }
77#endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */
78
79////////////////////////////////////////////////////////////////////////
80#ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME
81#include <mach/mach_time.h>
82
84{
85 return mach_absolute_time();
86}
87
89{
91}
92
94{
95 mach_timebase_info_data_t info;
96 mach_timebase_info(&info);
97 return gr::high_res_timer_type(info.numer * 1000000000UL) / info.denom;
98}
99#endif
100
101////////////////////////////////////////////////////////////////////////
102#ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER
103#include <windows.h>
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_MICROSEC_CLOCK
128{
129 static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0));
130 return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks();
131}
132
134{
135 return gr::high_res_timer_now();
136}
137
139{
140 return boost::posix_time::time_duration::ticks_per_second();
141}
142#endif
143
144////////////////////////////////////////////////////////////////////////
146{
147 static const double hrt_ticks_per_utc_ticks =
149 double(boost::posix_time::time_duration::ticks_per_second());
150 boost::posix_time::time_duration utc =
151 boost::posix_time::microsec_clock::universal_time() -
152 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 */
#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:138
high_res_timer_type high_res_timer_now_perfmon(void)
Get the current time in ticks - for performance monitoring.
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:145
signed long long high_res_timer_type
Typedef for the timer tick count.
Definition: high_res_timer.h:39