gr-baz Package
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(), which removes the tag from its deque.
111  *
112  * \param which_input an integer of which input stream to remove the tag from
113  * \param tag the tag object to add
114  */
115  void remove_item_tag(unsigned int which_input, const gr_tag_t &tag);
116 
117  /*!
118  * \brief Given a [start,end), returns a vector of all tags in the range.
119  *
120  * Pass-through function to gr_buffer_reader to get a vector of tags
121  * in given range. Range of counts is from start to end-1.
122  *
123  * Tags are tuples of:
124  * (item count, source id, key, value)
125  *
126  * \param v a vector reference to return tags into
127  * \param which_input an integer of which input stream to pull from
128  * \param abs_start a uint64 count of the start of the range of interest
129  * \param abs_end a uint64 count of the end of the range of interest
130  */
131  void get_tags_in_range(std::vector<gr_tag_t> &v,
132  unsigned int which_input,
133  uint64_t abs_start,
134  uint64_t abs_end);
135 
136  /*!
137  * \brief Given a [start,end), returns a vector of all tags in the range
138  * with a given key.
139  *
140  * Calls get_tags_in_range(which_input, abs_start, abs_end) to get a vector of
141  * tags from the buffers. This function then provides a secondary filter to
142  * the tags to extract only tags with the given 'key'.
143  *
144  * Tags are tuples of:
145  * (item count, source id, key, value)
146  *
147  * \param v a vector reference to return tags into
148  * \param which_input an integer of which input stream to pull from
149  * \param abs_start a uint64 count of the start of the range of interest
150  * \param abs_end a uint64 count of the end of the range of interest
151  * \param key a PMT symbol to select only tags of this key
152  */
153  void get_tags_in_range(std::vector<gr_tag_t> &v,
154  unsigned int which_input,
155  uint64_t abs_start,
156  uint64_t abs_end,
157  const pmt::pmt_t &key);
158 
159  /*!
160  * \brief Set core affinity of block to the cores in the vector mask.
161  *
162  * \param mask a vector of unsigned ints of the core numbers available to this block.
163  */
164  void set_processor_affinity(const std::vector<unsigned int> &mask);
165 
166  /*!
167  * \brief Unset core affinity.
168  */
169  void unset_processor_affinity();
170 
171  bool threaded; // set if thread is currently running.
172  gruel::gr_thread_t thread; // portable thread handle
173 
174  void start_perf_counters();
175  void stop_perf_counters(int noutput_items, int nproduced);
176  void reset_perf_counters();
177 
178  // Calls to get performance counter items
179  float pc_noutput_items();
180  float pc_nproduced();
181  float pc_input_buffers_full(size_t which);
182  std::vector<float> pc_input_buffers_full();
183  float pc_output_buffers_full(size_t which);
184  std::vector<float> pc_output_buffers_full();
185  float pc_work_time();
186 
187  float pc_noutput_items_var();
188  float pc_nproduced_var();
189  float pc_input_buffers_full_var(size_t which);
190  std::vector<float> pc_input_buffers_full_var();
191  float pc_output_buffers_full_var(size_t which);
192  std::vector<float> pc_output_buffers_full_var();
193  float pc_work_time_var();
194 
195  gr_tpb_detail d_tpb; // used by thread-per-block scheduler
197 
198  // ----------------------------------------------------------------------------
199 
200  private:
201  unsigned int d_ninputs;
202  unsigned int d_noutputs;
203  std::vector<gr_buffer_reader_sptr> d_input;
204  std::vector<gr_buffer_sptr> d_output;
205  bool d_done;
206 
207  // Performance counters
208  float d_avg_noutput_items;
209  float d_var_noutput_items;
210  float d_avg_nproduced;
211  float d_var_nproduced;
212  std::vector<float> d_avg_input_buffers_full;
213  std::vector<float> d_var_input_buffers_full;
214  std::vector<float> d_avg_output_buffers_full;
215  std::vector<float> d_var_output_buffers_full;
216  gruel::high_res_timer_type d_start_of_work, d_end_of_work;
217  float d_avg_work_time;
218  float d_var_work_time;
219  float d_pc_counter;
220 
221  gr_block_detail (unsigned int ninputs, unsigned int noutputs);
222 
223  friend struct gr_tpb_detail;
224 
226  gr_make_block_detail (unsigned int ninputs, unsigned int noutputs);
227 };
228 
230 gr_make_block_detail (unsigned int ninputs, unsigned int noutputs);
231 
232 GR_CORE_API long
234 
235 #endif /* INCLUDED_GR_BLOCK_DETAIL_H */