GNU Radio 3.6.5.1 C++ API
gr_block_detail.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2009,2010 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 detail.
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 #ifndef INCLUDED_GR_BLOCK_DETAIL_H
24 #define INCLUDED_GR_BLOCK_DETAIL_H
25 
26 #include <gr_core_api.h>
27 #include <gr_runtime_types.h>
28 #include <gr_tpb_detail.h>
29 #include <gr_tags.h>
30 #include <gruel/high_res_timer.h>
31 #include <stdexcept>
32 
33 /*!
34  * \brief Implementation details to support the signal processing abstraction
35  * \ingroup internal
36  *
37  * This class contains implementation detail that should be "out of sight"
38  * of almost all users of GNU Radio. This decoupling also means that
39  * we can make changes to the guts without having to recompile everything.
40  */
42  public:
43  ~gr_block_detail ();
44 
45  int ninputs () const { return d_ninputs; }
46  int noutputs () const { return d_noutputs; }
47  bool sink_p () const { return d_noutputs == 0; }
48  bool source_p () const { return d_ninputs == 0; }
49 
50  void set_done (bool done);
51  bool done () const { return d_done; }
52 
53  void set_input (unsigned int which, gr_buffer_reader_sptr reader);
54  gr_buffer_reader_sptr input (unsigned int which)
55  {
56  if (which >= d_ninputs)
57  throw std::invalid_argument ("gr_block_detail::input");
58  return d_input[which];
59  }
60 
61  void set_output (unsigned int which, gr_buffer_sptr buffer);
62  gr_buffer_sptr output (unsigned int which)
63  {
64  if (which >= d_noutputs)
65  throw std::invalid_argument ("gr_block_detail::output");
66  return d_output[which];
67  }
68 
69  /*!
70  * \brief Tell the scheduler \p how_many_items of input stream \p which_input were consumed.
71  */
72  void consume (int which_input, int how_many_items);
73 
74  /*!
75  * \brief Tell the scheduler \p how_many_items were consumed on each input stream.
76  */
77  void consume_each (int how_many_items);
78 
79  /*!
80  * \brief Tell the scheduler \p how_many_items were produced on output stream \p which_output.
81  */
82  void produce (int which_output, int how_many_items);
83 
84  /*!
85  * \brief Tell the scheduler \p how_many_items were produced on each output stream.
86  */
87  void produce_each (int how_many_items);
88 
89  // Return the number of items read on input stream which_input
90  uint64_t nitems_read(unsigned int which_input);
91 
92  // Return the number of items written on output stream which_output
93  uint64_t nitems_written(unsigned int which_output);
94 
95 
96  /*!
97  * \brief Adds a new tag to the given output stream.
98  *
99  * Calls gr_buffer::add_item_tag(),
100  * which appends the tag onto its deque.
101  *
102  * \param which_output an integer of which output stream to attach the tag
103  * \param tag the tag object to add
104  */
105  void add_item_tag(unsigned int which_output, const gr_tag_t &tag);
106 
107  /*!
108  * \brief Removes a tag from the given input stream.
109  *
110  * Calls gr_buffer::remove_item_tag().
111  * The tag in question will then no longer appear on subsequent calls of get_tags_in_range().
112  *
113  * \param which_input an integer of which input stream to remove the tag from
114  * \param tag the tag object to add
115  * \param id The unique block ID (use gr_block::unique_id())
116  */
117  void remove_item_tag(unsigned int which_input, const gr_tag_t &tag, long id);
118 
119  /*!
120  * \brief Given a [start,end), returns a vector of all tags in the range.
121  *
122  * Pass-through function to gr_buffer_reader to get a vector of tags
123  * in given range. Range of counts is from start to end-1.
124  *
125  * Tags are tuples of:
126  * (item count, source id, key, value)
127  *
128  * \param v a vector reference to return tags into
129  * \param which_input an integer of which input stream to pull from
130  * \param abs_start a uint64 count of the start of the range of interest
131  * \param abs_end a uint64 count of the end of the range of interest
132  * \param id Block ID
133  */
134  void get_tags_in_range(std::vector<gr_tag_t> &v,
135  unsigned int which_input,
136  uint64_t abs_start,
137  uint64_t abs_end,
138  long id);
139 
140  /*!
141  * \brief Given a [start,end), returns a vector of all tags in the range
142  * with a given key.
143  *
144  * Calls get_tags_in_range(which_input, abs_start, abs_end) to get a vector of
145  * tags from the buffers. This function then provides a secondary filter to
146  * the tags to extract only tags with the given 'key'.
147  *
148  * Tags are tuples of:
149  * (item count, source id, key, value)
150  *
151  * \param v a vector reference to return tags into
152  * \param which_input an integer of which input stream to pull from
153  * \param abs_start a uint64 count of the start of the range of interest
154  * \param abs_end a uint64 count of the end of the range of interest
155  * \param key a PMT symbol to select only tags of this key
156  * \param id Block ID
157  */
158  void get_tags_in_range(std::vector<gr_tag_t> &v,
159  unsigned int which_input,
160  uint64_t abs_start,
161  uint64_t abs_end,
162  const pmt::pmt_t &key,
163  long id);
164 
165  /*!
166  * \brief Set core affinity of block to the cores in the vector mask.
167  *
168  * \param mask a vector of ints of the core numbers available to this block.
169  */
170  void set_processor_affinity(const std::vector<int> &mask);
171 
172  /*!
173  * \brief Unset core affinity.
174  */
175  void unset_processor_affinity();
176 
177  bool threaded; // set if thread is currently running.
178  gruel::gr_thread_t thread; // portable thread handle
179 
180  void start_perf_counters();
181  void stop_perf_counters(int noutput_items, int nproduced);
182  void reset_perf_counters();
183 
184  // Calls to get performance counter items
185  float pc_noutput_items();
186  float pc_nproduced();
187  float pc_input_buffers_full(size_t which);
188  std::vector<float> pc_input_buffers_full();
189  float pc_output_buffers_full(size_t which);
190  std::vector<float> pc_output_buffers_full();
191  float pc_work_time();
192 
193  float pc_noutput_items_var();
194  float pc_nproduced_var();
195  float pc_input_buffers_full_var(size_t which);
196  std::vector<float> pc_input_buffers_full_var();
197  float pc_output_buffers_full_var(size_t which);
198  std::vector<float> pc_output_buffers_full_var();
199  float pc_work_time_var();
200 
201  gr_tpb_detail d_tpb; // used by thread-per-block scheduler
203 
204  // ----------------------------------------------------------------------------
205 
206  private:
207  unsigned int d_ninputs;
208  unsigned int d_noutputs;
209  std::vector<gr_buffer_reader_sptr> d_input;
210  std::vector<gr_buffer_sptr> d_output;
211  bool d_done;
212 
213  // Performance counters
214  float d_avg_noutput_items;
215  float d_var_noutput_items;
216  float d_avg_nproduced;
217  float d_var_nproduced;
218  std::vector<float> d_avg_input_buffers_full;
219  std::vector<float> d_var_input_buffers_full;
220  std::vector<float> d_avg_output_buffers_full;
221  std::vector<float> d_var_output_buffers_full;
222  gruel::high_res_timer_type d_start_of_work, d_end_of_work;
223  float d_avg_work_time;
224  float d_var_work_time;
225  float d_pc_counter;
226 
227  gr_block_detail (unsigned int ninputs, unsigned int noutputs);
228 
229  friend struct gr_tpb_detail;
230 
232  gr_make_block_detail (unsigned int ninputs, unsigned int noutputs);
233 };
234 
236 gr_make_block_detail (unsigned int ninputs, unsigned int noutputs);
237 
238 GR_CORE_API long
240 
241 #endif /* INCLUDED_GR_BLOCK_DETAIL_H */