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