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