GNU Radio Manual and C++ API Reference  3.9.1.0
The Free & Open Software Radio Ecosystem
block_detail.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2009,2010,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_GR_RUNTIME_BLOCK_DETAIL_H
12 #define INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H
13 
14 #include <gnuradio/api.h>
16 #include <gnuradio/logger.h>
17 #include <gnuradio/runtime_types.h>
18 #include <gnuradio/tags.h>
19 #include <gnuradio/tpb_detail.h>
20 #include <stdexcept>
21 
22 namespace gr {
23 
24 /*!
25  * \brief Implementation details to support the signal processing abstraction
26  * \ingroup internal
27  *
28  * This class contains implementation detail that should be "out of
29  * sight" of almost all users of GNU Radio. This decoupling also
30  * means that we can make changes to the guts without having to
31  * recompile everything.
32  */
34 {
35 public:
36  ~block_detail();
37 
38  int ninputs() const { return d_ninputs; }
39  int noutputs() const { return d_noutputs; }
40  bool sink_p() const { return d_noutputs == 0; }
41  bool source_p() const { return d_ninputs == 0; }
42 
43  void set_done(bool done);
44  bool done() const { return d_done; }
45 
46  void set_input(unsigned int which, buffer_reader_sptr reader);
47  buffer_reader_sptr input(unsigned int which)
48  {
49  if (which >= d_ninputs)
50  throw std::invalid_argument("block_detail::input");
51  return d_input[which];
52  }
53 
54  void set_output(unsigned int which, buffer_sptr buffer);
55  buffer_sptr output(unsigned int which)
56  {
57  if (which >= d_noutputs)
58  throw std::invalid_argument("block_detail::output");
59  return d_output[which];
60  }
61 
62  /*!
63  * \brief Tell the scheduler \p how_many_items of input stream \p
64  * which_input were consumed.
65  */
66  void consume(int which_input, int how_many_items);
67 
68  /*!
69  * \brief Tell the scheduler \p how_many_items were consumed on
70  * each input stream.
71  */
72  void consume_each(int how_many_items);
73 
74  /*!
75  * \brief Tell the scheduler \p how_many_items were produced on
76  * output stream \p which_output.
77  */
78  void produce(int which_output, int how_many_items);
79 
80  /*!
81  * \brief Tell the scheduler \p how_many_items were produced on
82  * each output stream.
83  */
84  void produce_each(int how_many_items);
85 
86  // Return the number of items read on input stream which_input
87  uint64_t nitems_read(unsigned int which_input);
88 
89  // Return the number of items written on output stream which_output
90  uint64_t nitems_written(unsigned int which_output);
91 
92  // sets nitems_read and nitems_written to 0 for all input/output
93  // buffers.
94  void reset_nitem_counters();
95 
96  // Clears all tags from the input buffers.
97  void clear_tags();
98 
99  /*!
100  * \brief Adds a new tag to the given output stream.
101  *
102  * Calls gr::buffer::add_item_tag(),
103  * which appends the tag onto its deque.
104  *
105  * \param which_output an integer of which output stream to attach the tag
106  * \param tag the tag object to add
107  */
108  void add_item_tag(unsigned int which_output, const tag_t& tag);
109 
110  /*!
111  * \brief Removes a tag from the given input stream.
112  *
113  * Calls gr::buffer::remove_item_tag().
114  * The tag in question will then no longer appear on subsequent calls of
115  * get_tags_in_range().
116  *
117  * \param which_input an integer of which input stream to remove the tag from
118  * \param tag the tag object to add
119  * \param id The unique block ID (use gr::block::unique_id())
120  */
121  void remove_item_tag(unsigned int which_input, const tag_t& tag, long id);
122 
123  /*!
124  * \brief Given a [start,end), returns a vector of all tags in the range.
125  *
126  * Pass-through function to gr::buffer_reader to get a vector of
127  * tags in given range. Range of counts is from start to end-1.
128  *
129  * Tags are tuples of:
130  * (item count, source id, key, value)
131  *
132  * \param v a vector reference to return tags into
133  * \param which_input an integer of which input stream to pull from
134  * \param abs_start a uint64 count of the start of the range of interest
135  * \param abs_end a uint64 count of the end of the range of interest
136  * \param id Block ID
137  */
138  void get_tags_in_range(std::vector<tag_t>& v,
139  unsigned int which_input,
140  uint64_t abs_start,
141  uint64_t abs_end,
142  long id);
143 
144  /*!
145  * \brief Given a [start,end), returns a vector of all tags in the
146  * range with a given key.
147  *
148  * Calls get_tags_in_range(which_input, abs_start, abs_end) to get
149  * a vector of tags from the buffers. This function then provides
150  * a secondary filter to the tags to extract only tags with the
151  * given 'key'.
152  *
153  * Tags are tuples of:
154  * (item count, source id, key, value)
155  *
156  * \param v a vector reference to return tags into
157  * \param which_input an integer of which input stream to pull from
158  * \param abs_start a uint64 count of the start of the range of interest
159  * \param abs_end a uint64 count of the end of the range of interest
160  * \param key a PMT symbol to select only tags of this key
161  * \param id Block ID
162  */
163  void get_tags_in_range(std::vector<tag_t>& v,
164  unsigned int which_input,
165  uint64_t abs_start,
166  uint64_t abs_end,
167  const pmt::pmt_t& key,
168  long id);
169 
170  /*!
171  * \brief Set core affinity of block to the cores in the vector
172  * mask.
173  *
174  * \param mask a vector of ints of the core numbers available to
175  * this block.
176  */
177  void set_processor_affinity(const std::vector<int>& mask);
178 
179  /*!
180  * \brief Unset core affinity.
181  */
182  void unset_processor_affinity();
183 
184  /*!
185  * \brief Get the current thread priority
186  */
187  int thread_priority();
188 
189  /*!
190  * \brief Set the current thread priority
191  *
192  * \param priority the new thread priority to set
193  */
194  int set_thread_priority(int priority);
195 
196  bool threaded; // set if thread is currently running.
197  gr::thread::gr_thread_t thread; // portable thread handle
198 
199  void start_perf_counters();
200  void stop_perf_counters(int noutput_items, int nproduced);
201  void reset_perf_counters();
202 
203  // Calls to get performance counter items
204  float pc_noutput_items();
205  float pc_nproduced();
206  float pc_input_buffers_full(size_t which);
207  std::vector<float> pc_input_buffers_full();
208  float pc_output_buffers_full(size_t which);
209  std::vector<float> pc_output_buffers_full();
210  float pc_work_time();
211 
212  float pc_noutput_items_avg();
213  float pc_nproduced_avg();
214  float pc_input_buffers_full_avg(size_t which);
215  std::vector<float> pc_input_buffers_full_avg();
216  float pc_output_buffers_full_avg(size_t which);
217  std::vector<float> pc_output_buffers_full_avg();
218  float pc_work_time_avg();
219  float pc_throughput_avg();
220 
221  float pc_noutput_items_var();
222  float pc_nproduced_var();
223  float pc_input_buffers_full_var(size_t which);
224  std::vector<float> pc_input_buffers_full_var();
225  float pc_output_buffers_full_var(size_t which);
226  std::vector<float> pc_output_buffers_full_var();
227  float pc_work_time_var();
228 
229  float pc_work_time_total();
230 
231  tpb_detail d_tpb; // used by thread-per-block scheduler
233 
234  int consumed() const;
235 
236  // necessary because stupidly block_executor.cc's "propagate_tags" is a function, not
237  // any class member
238  gr::logger_ptr d_logger, d_debug_logger;
239 
240  // ----------------------------------------------------------------------------
241 
242 private:
243  unsigned int d_ninputs;
244  unsigned int d_noutputs;
245  std::vector<buffer_reader_sptr> d_input;
246  std::vector<buffer_sptr> d_output;
247  bool d_done;
248  int d_consumed;
249 
250  // Performance counters
251  float d_ins_noutput_items;
252  float d_avg_noutput_items;
253  float d_var_noutput_items;
254  float d_total_noutput_items;
255  gr::high_res_timer_type d_pc_start_time;
256  gr::high_res_timer_type d_pc_last_work_time;
257  float d_ins_nproduced;
258  float d_avg_nproduced;
259  float d_var_nproduced;
260  std::vector<float> d_ins_input_buffers_full;
261  std::vector<float> d_avg_input_buffers_full;
262  std::vector<float> d_var_input_buffers_full;
263  std::vector<float> d_ins_output_buffers_full;
264  std::vector<float> d_avg_output_buffers_full;
265  std::vector<float> d_var_output_buffers_full;
266  gr::high_res_timer_type d_start_of_work, d_end_of_work;
267  float d_ins_work_time;
268  float d_avg_work_time;
269  float d_var_work_time;
270  float d_total_work_time;
271  float d_avg_throughput;
272  float d_pc_counter;
273 
274  block_detail(unsigned int ninputs, unsigned int noutputs);
275 
276  friend struct tpb_detail;
277 
278  friend GR_RUNTIME_API block_detail_sptr make_block_detail(unsigned int ninputs,
279  unsigned int noutputs);
280 };
281 
282 GR_RUNTIME_API block_detail_sptr make_block_detail(unsigned int ninputs,
283  unsigned int noutputs);
284 
286 
287 } /* namespace gr */
288 
289 #endif /* INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H */
high_res_timer.h
gr::block_detail::d_produce_or
int d_produce_or
Definition: block_detail.h:232
gr::block_detail::d_tpb
tpb_detail d_tpb
Definition: block_detail.h:231
gr::block_detail::d_logger
gr::logger_ptr d_logger
Definition: block_detail.h:238
logger.h
gr::thread::gr_thread_t
pthread_t gr_thread_t
a system-dependent typedef for the underlying thread type.
Definition: thread.h:50
tags.h
gr::block_detail::noutputs
int noutputs() const
Definition: block_detail.h:39
runtime_types.h
gr::tpb_detail
used by thread-per-block scheduler
Definition: tpb_detail.h:26
gr::block_detail::sink_p
bool sink_p() const
Definition: block_detail.h:40
gr::block_detail_ncurrently_allocated
GR_RUNTIME_API long block_detail_ncurrently_allocated()
v
Definition: cc_common.h:35
gr::thread::set_thread_priority
GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority)
set current thread priority for a given thread ID
gr::block_detail::source_p
bool source_p() const
Definition: block_detail.h:41
GR_RUNTIME_API
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
gr::block_detail::threaded
bool threaded
Definition: block_detail.h:196
gr::block_detail::input
buffer_reader_sptr input(unsigned int which)
Definition: block_detail.h:47
gr::block_detail::ninputs
int ninputs() const
Definition: block_detail.h:38
gr::logger_ptr
log4cpp::Category * logger_ptr
GR_LOG macros.
Definition: logger.h:60
pmt::pmt_t
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:84
gr::high_res_timer_type
signed long long high_res_timer_type
Typedef for the timer tick count.
Definition: high_res_timer.h:38
gr::tag_t
Definition: tags.h:19
gr::block_detail::done
bool done() const
Definition: block_detail.h:44
gr::block_detail::output
buffer_sptr output(unsigned int which)
Definition: block_detail.h:55
api.h
gr::buffer
Single writer, multiple reader fifo.
Definition: buffer.h:46
gr
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29
gr::block_detail::thread
gr::thread::gr_thread_t thread
Definition: block_detail.h:197
gr::make_block_detail
GR_RUNTIME_API block_detail_sptr make_block_detail(unsigned int ninputs, unsigned int noutputs)
gr::thread::thread_priority
GR_RUNTIME_API int thread_priority(gr_thread_t thread)
get current thread priority for a given thread ID
tpb_detail.h
gr::block_detail
Implementation details to support the signal processing abstraction.
Definition: block_detail.h:33