GNU Radio 3.7.1 C++ API
block_detail.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004,2009,2010,2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more detail.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H
00024 #define INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H
00025 
00026 #include <gnuradio/api.h>
00027 #include <gnuradio/runtime_types.h>
00028 #include <gnuradio/tpb_detail.h>
00029 #include <gnuradio/tags.h>
00030 #include <gnuradio/high_res_timer.h>
00031 #include <stdexcept>
00032 
00033 namespace gr {
00034 
00035   /*!
00036    * \brief Implementation details to support the signal processing abstraction
00037    * \ingroup internal
00038    *
00039    * This class contains implementation detail that should be "out of
00040    * sight" of almost all users of GNU Radio.  This decoupling also
00041    * means that we can make changes to the guts without having to
00042    * recompile everything.
00043    */
00044   class GR_RUNTIME_API block_detail
00045   {
00046   public:
00047     ~block_detail();
00048 
00049     int ninputs() const { return d_ninputs; }
00050     int noutputs() const { return d_noutputs; }
00051     bool sink_p() const { return d_noutputs == 0; }
00052     bool source_p() const { return d_ninputs == 0; }
00053 
00054     void set_done(bool done);
00055     bool done() const { return d_done; }
00056 
00057     void set_input(unsigned int which, buffer_reader_sptr reader);
00058     buffer_reader_sptr input(unsigned int which)
00059     {
00060       if(which >= d_ninputs)
00061         throw std::invalid_argument("block_detail::input");
00062       return d_input[which];
00063     }
00064 
00065     void set_output(unsigned int which, buffer_sptr buffer);
00066     buffer_sptr output(unsigned int which)
00067     {
00068       if(which >= d_noutputs)
00069         throw std::invalid_argument("block_detail::output");
00070       return d_output[which];
00071     }
00072 
00073     /*!
00074      * \brief Tell the scheduler \p how_many_items of input stream \p
00075      * which_input were consumed.
00076      */
00077     void consume(int which_input, int how_many_items);
00078 
00079     /*!
00080      * \brief Tell the scheduler \p how_many_items were consumed on
00081      * each input stream.
00082      */
00083     void consume_each(int how_many_items);
00084 
00085     /*!
00086      * \brief Tell the scheduler \p how_many_items were produced on
00087      * output stream \p which_output.
00088      */
00089     void produce(int which_output, int how_many_items);
00090 
00091     /*!
00092      * \brief Tell the scheduler \p how_many_items were produced on
00093      * each output stream.
00094      */
00095     void produce_each(int how_many_items);
00096 
00097     // Return the number of items read on input stream which_input
00098     uint64_t nitems_read(unsigned int which_input);
00099 
00100     // Return the number of items written on output stream which_output
00101     uint64_t nitems_written(unsigned int which_output);
00102 
00103     /*!
00104      * \brief  Adds a new tag to the given output stream.
00105      *
00106      * Calls gr::buffer::add_item_tag(),
00107      * which appends the tag onto its deque.
00108      *
00109      * \param which_output  an integer of which output stream to attach the tag
00110      * \param tag the tag object to add
00111      */
00112     void add_item_tag(unsigned int which_output, const tag_t &tag);
00113 
00114     /*!
00115      * \brief  Removes a tag from the given input stream.
00116      *
00117      * Calls gr::buffer::remove_item_tag().
00118      * The tag in question will then no longer appear on subsequent calls of get_tags_in_range().
00119      *
00120      * \param which_input  an integer of which input stream to remove the tag from
00121      * \param tag the tag object to add
00122      * \param id The unique block ID (use gr::block::unique_id())
00123      */
00124     void remove_item_tag(unsigned int which_input, const tag_t &tag, long id);
00125 
00126     /*!
00127      * \brief Given a [start,end), returns a vector of all tags in the range.
00128      *
00129      * Pass-through function to gr::buffer_reader to get a vector of
00130      * tags in given range. Range of counts is from start to end-1.
00131      *
00132      * Tags are tuples of:
00133      *      (item count, source id, key, value)
00134      *
00135      * \param v            a vector reference to return tags into
00136      * \param which_input  an integer of which input stream to pull from
00137      * \param abs_start    a uint64 count of the start of the range of interest
00138      * \param abs_end      a uint64 count of the end of the range of interest
00139      * \param id           Block ID
00140      */
00141     void get_tags_in_range(std::vector<tag_t> &v,
00142                            unsigned int which_input,
00143                            uint64_t abs_start,
00144                            uint64_t abs_end,
00145                            long id);
00146 
00147     /*!
00148      * \brief Given a [start,end), returns a vector of all tags in the
00149      * range with a given key.
00150      *
00151      * Calls get_tags_in_range(which_input, abs_start, abs_end) to get
00152      * a vector of tags from the buffers. This function then provides
00153      * a secondary filter to the tags to extract only tags with the
00154      * given 'key'.
00155      *
00156      * Tags are tuples of:
00157      *      (item count, source id, key, value)
00158      *
00159      * \param v            a vector reference to return tags into
00160      * \param which_input  an integer of which input stream to pull from
00161      * \param abs_start    a uint64 count of the start of the range of interest
00162      * \param abs_end      a uint64 count of the end of the range of interest
00163      * \param key          a PMT symbol to select only tags of this key
00164      * \param id           Block ID
00165      */
00166     void get_tags_in_range(std::vector<tag_t> &v,
00167                            unsigned int which_input,
00168                            uint64_t abs_start,
00169                            uint64_t abs_end,
00170                            const pmt::pmt_t &key,
00171                            long id);
00172 
00173     /*!
00174      * \brief Set core affinity of block to the cores in the vector
00175      * mask.
00176      *
00177      * \param mask a vector of ints of the core numbers available to
00178      * this block.
00179      */
00180     void set_processor_affinity(const std::vector<int> &mask);
00181 
00182     /*!
00183      * \brief Unset core affinity.
00184      */
00185     void unset_processor_affinity();
00186 
00187     /*!
00188      * \brief Get the current thread priority
00189      */
00190     int thread_priority();
00191 
00192     /*!
00193      * \brief Set the current thread priority
00194      *
00195      * \param priority the new thread priority to set
00196      */
00197     int set_thread_priority(int priority);
00198 
00199     bool                    threaded;  // set if thread is currently running.
00200     gr::thread::gr_thread_t thread;    // portable thread handle
00201 
00202     void start_perf_counters();
00203     void stop_perf_counters(int noutput_items, int nproduced);
00204     void reset_perf_counters();
00205 
00206     // Calls to get performance counter items
00207     float pc_noutput_items();
00208     float pc_nproduced();
00209     float pc_input_buffers_full(size_t which);
00210     std::vector<float> pc_input_buffers_full();
00211     float pc_output_buffers_full(size_t which);
00212     std::vector<float> pc_output_buffers_full();
00213     float pc_work_time();
00214 
00215     float pc_noutput_items_avg();
00216     float pc_nproduced_avg();
00217     float pc_input_buffers_full_avg(size_t which);
00218     std::vector<float> pc_input_buffers_full_avg();
00219     float pc_output_buffers_full_avg(size_t which);
00220     std::vector<float> pc_output_buffers_full_avg();
00221     float pc_work_time_avg();
00222 
00223     float pc_noutput_items_var();
00224     float pc_nproduced_var();
00225     float pc_input_buffers_full_var(size_t which);
00226     std::vector<float> pc_input_buffers_full_var();
00227     float pc_output_buffers_full_var(size_t which);
00228     std::vector<float> pc_output_buffers_full_var();
00229     float pc_work_time_var();
00230  
00231     tpb_detail d_tpb;   // used by thread-per-block scheduler
00232     int d_produce_or;
00233 
00234     // ----------------------------------------------------------------------------
00235 
00236   private:
00237     unsigned int                    d_ninputs;
00238     unsigned int                    d_noutputs;
00239     std::vector<buffer_reader_sptr> d_input;
00240     std::vector<buffer_sptr>        d_output;
00241     bool                            d_done;
00242 
00243     // Performance counters
00244     float d_ins_noutput_items;
00245     float d_avg_noutput_items;
00246     float d_var_noutput_items;
00247     float d_ins_nproduced;
00248     float d_avg_nproduced;
00249     float d_var_nproduced;
00250     std::vector<float> d_ins_input_buffers_full;
00251     std::vector<float> d_avg_input_buffers_full;
00252     std::vector<float> d_var_input_buffers_full;
00253     std::vector<float> d_ins_output_buffers_full;
00254     std::vector<float> d_avg_output_buffers_full;
00255     std::vector<float> d_var_output_buffers_full;
00256     gr::high_res_timer_type d_start_of_work, d_end_of_work;
00257     float d_ins_work_time;
00258     float d_avg_work_time;
00259     float d_var_work_time;
00260     float d_pc_counter;
00261   
00262     block_detail(unsigned int ninputs, unsigned int noutputs);
00263 
00264     friend struct tpb_detail;
00265 
00266     friend GR_RUNTIME_API block_detail_sptr
00267       make_block_detail(unsigned int ninputs, unsigned int noutputs);
00268   };
00269 
00270   GR_RUNTIME_API block_detail_sptr
00271   make_block_detail(unsigned int ninputs, unsigned int noutputs);
00272 
00273   GR_RUNTIME_API long
00274   block_detail_ncurrently_allocated();
00275 
00276 } /* namespace gr */
00277 
00278 #endif /* INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H */