1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2013,2018 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
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "probe_rate_impl.h"
28 #include <gnuradio/io_signature.h>
29 
30 namespace gr {
31 namespace blocks {
32 
make(size_t itemsize,double update_rate_ms,double alpha)33 probe_rate::sptr probe_rate::make(size_t itemsize, double update_rate_ms, double alpha)
34 {
35     return gnuradio::get_initial_sptr(
36         new probe_rate_impl(itemsize, update_rate_ms, alpha));
37 }
38 
probe_rate_impl(size_t itemsize,double update_rate_ms,double alpha)39 probe_rate_impl::probe_rate_impl(size_t itemsize, double update_rate_ms, double alpha)
40     : sync_block("probe_rate",
41                  io_signature::make(1, 1, itemsize),
42                  io_signature::make(0, 0, itemsize)),
43       d_alpha(alpha),
44       d_beta(1.0 - alpha),
45       d_avg(0),
46       d_min_update_time(update_rate_ms),
47       d_lastthru(0),
48       d_port(pmt::mp("rate")),
49       d_dict_avg(pmt::mp("rate_avg")),
50       d_dict_now(pmt::mp("rate_now"))
51 {
52     message_port_register_out(d_port);
53 }
54 
~probe_rate_impl()55 probe_rate_impl::~probe_rate_impl() {}
56 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)57 int probe_rate_impl::work(int noutput_items,
58                           gr_vector_const_void_star& input_items,
59                           gr_vector_void_star& output_items)
60 {
61     d_lastthru += noutput_items;
62     boost::posix_time::ptime now(boost::posix_time::microsec_clock::local_time());
63     boost::posix_time::time_duration diff = now - d_last_update;
64     double diff_ms = diff.total_milliseconds();
65     if (diff_ms >= d_min_update_time) {
66         double rate_this_update = d_lastthru * 1e3 / diff_ms;
67         d_lastthru = 0;
68         d_last_update = now;
69         if (d_avg == 0) {
70             d_avg = rate_this_update;
71         } else {
72             d_avg = rate_this_update * d_alpha + d_avg * d_beta;
73         }
74         pmt::pmt_t d = pmt::make_dict();
75         d = pmt::dict_add(d, d_dict_avg, pmt::mp(d_avg));
76         d = pmt::dict_add(d, d_dict_now, pmt::mp(rate_this_update));
77         message_port_pub(d_port, pmt::cons(d, pmt::PMT_NIL));
78     }
79     return noutput_items;
80 }
81 
setup_rpc()82 void probe_rate_impl::setup_rpc()
83 {
84 #ifdef GR_CTRLPORT
85     add_rpc_variable(rpcbasic_sptr(
86         new rpcbasic_register_get<probe_rate_impl, double>(alias(),
87                                                            "rate_items",
88                                                            &probe_rate_impl::rate,
89                                                            pmt::mp(0),
90                                                            pmt::mp(1e6),
91                                                            pmt::mp(1),
92                                                            "items/sec",
93                                                            "Item rate",
94                                                            RPC_PRIVLVL_MIN,
95                                                            DISPTIME | DISPOPTSTRIP)));
96     add_rpc_variable(rpcbasic_sptr(new rpcbasic_register_get<probe_rate_impl, double>(
97         alias(),
98         "timesincelast",
99         &probe_rate_impl::timesincelast,
100         pmt::mp(0),
101         pmt::mp(d_min_update_time * 2),
102         pmt::mp(0),
103         "ms",
104         "Time since last update",
105         RPC_PRIVLVL_MIN,
106         DISPTIME | DISPOPTSTRIP)));
107 #endif
108 }
109 
set_alpha(double alpha)110 void probe_rate_impl::set_alpha(double alpha) { d_alpha = alpha; }
111 
rate()112 double probe_rate_impl::rate() { return d_avg; }
113 
timesincelast()114 double probe_rate_impl::timesincelast()
115 {
116     boost::posix_time::ptime now(boost::posix_time::microsec_clock::local_time());
117     boost::posix_time::time_duration diff = now - d_last_update;
118     return diff.total_milliseconds();
119 }
120 
start()121 bool probe_rate_impl::start()
122 {
123     d_avg = 0;
124     d_lastthru = 0;
125     d_last_update = boost::posix_time::microsec_clock::local_time();
126     return true;
127 }
128 
stop()129 bool probe_rate_impl::stop() { return true; }
130 
131 
132 } /* namespace blocks */
133 } /* namespace gr */
134