GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
gnuradio-runtime/include/gnuradio/block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2007,2009,2010,2013,2017 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_H
12 #define INCLUDED_GR_RUNTIME_BLOCK_H
13 
14 #include <memory>
15 
16 #include <gnuradio/api.h>
17 #include <gnuradio/basic_block.h>
18 #include <gnuradio/buffer_type.h>
19 #include <gnuradio/config.h>
20 #include <gnuradio/logger.h>
21 #include <gnuradio/tags.h>
22 #ifdef GR_MPLIB_MPIR
23 #include <mpirxx.h>
24 #else
25 #include <gmpxx.h>
26 #endif
27 
28 namespace gr {
29 
30 /*!
31  * \brief The abstract base class for all 'terminal' processing blocks.
32  * \ingroup base_blk
33  *
34  * A signal processing flow is constructed by creating a tree of
35  * hierarchical blocks, which at any level may also contain terminal
36  * nodes that actually implement signal processing functions. This
37  * is the base class for all such leaf nodes.
38  *
39  * Blocks have a set of input streams and output streams. The
40  * input_signature and output_signature define the number of input
41  * streams and output streams respectively, and the type of the data
42  * items in each stream.
43  *
44  * Blocks report the number of items consumed on each input in
45  * general_work(), using consume() or consume_each().
46  *
47  * If the same number of items is produced on each output, the block
48  * returns that number from general_work(). Otherwise, the block
49  * calls produce() for each output, then returns
50  * WORK_CALLED_PRODUCE. The input and output rates are not required
51  * to be related.
52  *
53  * User derived blocks override two methods, forecast and
54  * general_work, to implement their signal processing
55  * behavior. forecast is called by the system scheduler to determine
56  * how many items are required on each input stream in order to
57  * produce a given number of output items.
58  *
59  * general_work is called to perform the signal processing in the
60  * block. It reads the input items and writes the output items.
61  */
63 {
64 public:
65  //! Magic return values from general_work
66  enum work_return_t { WORK_CALLED_PRODUCE = -2, WORK_DONE = -1 };
67 
68  /*!
69  * \brief enum to represent different tag propagation policies.
70  */
72  TPP_DONT = 0, /*!< Scheduler doesn't propagate tags from in- to output. The block
73  itself is free to insert tags as it wants. */
74  TPP_ALL_TO_ALL = 1, /*!< Propagate tags from all in- to all outputs. The scheduler
75  takes care of that. */
76  TPP_ONE_TO_ONE = 2, /*!< Propagate tags from n. input to n. output. Requires same
77  number of in- and outputs */
78  TPP_CUSTOM = 3 /*!< Like TPP_DONT, but signals the block it should implement
79  application-specific forwarding behaviour. */
80  };
81 
82  ~block() override;
83 
84  /*!
85  * Assume block computes y_i = f(x_i, x_i-1, x_i-2, x_i-3...)
86  * History is the number of x_i's that are examined to produce one y_i.
87  * This comes in handy for FIR filters, where we use history to
88  * ensure that our input contains the appropriate "history" for the
89  * filter. History should be equal to the number of filter taps. First
90  * history samples (when there are no previous samples) are
91  * initialized with zeroes.
92  */
93  unsigned history() const;
94  void set_history(unsigned history);
95 
96  /*!
97  * Declares the block's delay in samples. Since the delay of
98  * blocks like filters is derived from the taps and not the block
99  * itself, we cannot automatically calculate this value and so
100  * leave it as a user-defined property. It defaults to 0 is not
101  * set.
102  *
103  * This does not actively set the delay; it just tells the
104  * scheduler what the delay is.
105  *
106  * This delay is mostly used to adjust the placement of the tags
107  * and is not currently used for any signal processing. When a tag
108  * is passed through a block with internal delay, its location
109  * should be moved based on the delay of the block. This interface
110  * allows us to tell the scheduler this value.
111  *
112  * \param which The buffer on which to set the delay.
113  * \param delay The sample delay of the data stream.
114  */
115  void declare_sample_delay(int which, unsigned delay);
116 
117  /*!
118  * Convenience wrapper to gr::block::declare_delay(int which, unsigned delay)
119  * to set all ports to the same delay.
120  */
121  void declare_sample_delay(unsigned delay);
122 
123  /*!
124  * Gets the delay of the block. Since the delay of blocks like
125  * filters is derived from the taps and not the block itself, we
126  * cannot automatically calculate this value and so leave it as a
127  * user-defined property. It defaults to 0 is not set.
128  *
129  * \param which Which port from which to get the sample delay.
130  */
131  unsigned sample_delay(int which) const;
132 
133  /*!
134  * \brief Return true if this block has a fixed input to output rate.
135  *
136  * If true, then fixed_rate_in_to_out and fixed_rate_out_to_in may be called.
137  */
138  bool fixed_rate() const { return d_fixed_rate; }
139 
140  // ----------------------------------------------------------------
141  // override these to define your behavior
142  // ----------------------------------------------------------------
143 
144  /*!
145  * \brief Estimate input requirements given output request
146  *
147  * \param noutput_items number of output items to produce
148  * \param ninput_items_required number of input items required on each input stream
149  *
150  * Given a request to product \p noutput_items, estimate the
151  * number of data items required on each input stream. The
152  * estimate doesn't have to be exact, but should be close.
153  */
154  virtual void forecast(int noutput_items, gr_vector_int& ninput_items_required);
155 
156  /*!
157  * \brief compute output items from input items
158  *
159  * \param noutput_items number of output items to write on each output stream
160  * \param ninput_items number of input items available on each input stream
161  * \param input_items vector of pointers to the input items, one entry per input
162  * stream
163  * \param output_items vector of pointers to the output items, one entry per
164  * output stream
165  *
166  * \returns number of items actually written to each output stream
167  * or WORK_CALLED_PRODUCE or WORK_DONE. It is OK to return a
168  * value less than noutput_items.
169  *
170  * WORK_CALLED_PRODUCE is used where not all outputs produce the
171  * same number of items. general_work must call produce() for each
172  * output to indicate the number of items actually produced.
173  *
174  * WORK_DONE indicates that no more data will be produced by this block.
175  *
176  * general_work must call consume or consume_each to indicate how
177  * many items were consumed on each input stream.
178  */
179  virtual int general_work(int noutput_items,
180  gr_vector_int& ninput_items,
181  gr_vector_const_void_star& input_items,
182  gr_vector_void_star& output_items);
183 
184  /*!
185  * \brief Called to enable drivers, etc for i/o devices.
186  *
187  * This allows a block to enable an associated driver to begin
188  * transferring data just before we start to execute the scheduler.
189  * The end result is that this reduces latency in the pipeline
190  * when dealing with audio devices, usrps, etc.
191  */
192  virtual bool start();
193 
194  /*!
195  * \brief Called to disable drivers, etc for i/o devices.
196  */
197  virtual bool stop();
198 
199  // ----------------------------------------------------------------
200 
201  /*!
202  * \brief Constrain the noutput_items argument passed to forecast and general_work
203  *
204  * set_output_multiple causes the scheduler to ensure that the
205  * noutput_items argument passed to forecast and general_work will
206  * be an integer multiple of \param multiple The default value of
207  * output multiple is 1.
208  */
209  void set_output_multiple(int multiple);
210  int output_multiple() const { return d_output_multiple; }
211  bool output_multiple_set() const { return d_output_multiple_set; }
212 
213  /*!
214  * \brief Constrains buffers to work on a set item alignment (for SIMD)
215  *
216  * set_alignment_multiple causes the scheduler to ensure that the
217  * noutput_items argument passed to forecast and general_work will
218  * be an integer multiple of \param multiple The default value is
219  * 1.
220  *
221  * This control is similar to the output_multiple setting, except
222  * that if the number of items passed to the block is less than
223  * the output_multiple, this value is ignored and the block can
224  * produce like normal. The d_unaligned value is set to the number
225  * of items the block is off by. In the next call to general_work,
226  * the noutput_items is set to d_unaligned or less until
227  * d_unaligned==0. The buffers are now aligned again and the
228  * aligned calls can be performed again.
229  */
230  void set_alignment(int multiple);
231  int alignment() const { return d_output_multiple; }
232 
233  void set_unaligned(int na);
234  int unaligned() const { return d_unaligned; }
235  void set_is_unaligned(bool u);
236  bool is_unaligned() const { return d_is_unaligned; }
237 
238  /*!
239  * \brief Tell the scheduler \p how_many_items of input stream \p
240  * which_input were consumed.
241  *
242  * This function should be used in general_work() to tell the scheduler the
243  * number of input items processed. Calling consume() multiple times in the
244  * same general_work() call is safe. Every invocation of consume() updates
245  * the values returned by nitems_read().
246  */
247  void consume(int which_input, int how_many_items);
248 
249  /*!
250  * \brief Tell the scheduler \p how_many_items were consumed on
251  * each input stream.
252  *
253  * Also see notes on consume().
254  */
255  void consume_each(int how_many_items);
256 
257  /*!
258  * \brief Tell the scheduler \p how_many_items were produced on
259  * output stream \p which_output.
260  *
261  * This function should be used in general_work() to tell the scheduler the
262  * number of output items produced. If produce() is called in
263  * general_work(), general_work() must return \p WORK_CALLED_PRODUCE.
264  * Calling produce() multiple times in the same general_work() call is safe.
265  * Every invocation of produce() updates the values returned by
266  * nitems_written().
267  */
268  void produce(int which_output, int how_many_items);
269 
270  /*!
271  * \brief Set the approximate output rate / input rate
272  *
273  * Provide a hint to the buffer allocator and scheduler.
274  * The default relative_rate is 1.0
275  *
276  * decimators have relative_rates < 1.0
277  * interpolators have relative_rates > 1.0
278  */
279  void set_relative_rate(double relative_rate);
280 
281  /*!
282  * \brief Set the approximate output rate / input rate
283  * using its reciprocal
284  *
285  * This is a convenience function to avoid
286  * numerical problems with tag propagation that calling
287  * set_relative_rate(1.0/relative_rate) might introduce.
288  */
289  void set_inverse_relative_rate(double inverse_relative_rate);
290 
291  /*!
292  * \brief Set the approximate output rate / input rate as an integer ratio
293  *
294  * Provide a hint to the buffer allocator and scheduler.
295  * The default relative_rate is interpolation / decimation = 1 / 1
296  *
297  * decimators have relative_rates < 1.0
298  * interpolators have relative_rates > 1.0
299  */
300  void set_relative_rate(uint64_t interpolation, uint64_t decimation);
301 
302  /*!
303  * \brief return the approximate output rate / input rate
304  */
305  double relative_rate() const { return d_relative_rate; }
306 
307  /*!
308  * \brief return the numerator, or interpolation rate, of the
309  * approximate output rate / input rate
310  */
311  uint64_t relative_rate_i() const
312  {
313  return (uint64_t)d_mp_relative_rate.get_num().get_ui();
314  }
315 
316  /*!
317  * \brief return the denominator, or decimation rate, of the
318  * approximate output rate / input rate
319  */
320  uint64_t relative_rate_d() const
321  {
322  return (uint64_t)d_mp_relative_rate.get_den().get_ui();
323  }
324 
325  /*!
326  * \brief return a reference to the multiple precision rational
327  * representation of the approximate output rate / input rate
328  */
329  mpq_class& mp_relative_rate() { return d_mp_relative_rate; }
330 
331  /*
332  * The following two methods provide special case info to the
333  * scheduler in the event that a block has a fixed input to output
334  * ratio. sync_block, sync_decimator and
335  * sync_interpolator override these. If you're fixed rate,
336  * subclass one of those.
337  */
338  /*!
339  * \brief Given ninput samples, return number of output samples that will be produced.
340  * N.B. this is only defined if fixed_rate returns true.
341  * Generally speaking, you don't need to override this.
342  */
343  virtual int fixed_rate_ninput_to_noutput(int ninput);
344 
345  /*!
346  * \brief Given noutput samples, return number of input samples required to produce
347  * noutput. N.B. this is only defined if fixed_rate returns true. Generally speaking,
348  * you don't need to override this.
349  */
350  virtual int fixed_rate_noutput_to_ninput(int noutput);
351 
352  /*!
353  * \brief Return the number of items read on input stream which_input
354  */
355  uint64_t nitems_read(unsigned int which_input);
356 
357  /*!
358  * \brief Return the number of items written on output stream which_output
359  */
360  uint64_t nitems_written(unsigned int which_output);
361 
362  /*!
363  * \brief Asks for the policy used by the scheduler to moved tags downstream.
364  */
366 
367  /*!
368  * \brief Set the policy by the scheduler to determine how tags are moved downstream.
369  */
371 
372  /*!
373  * \brief Return the minimum number of output items this block can
374  * produce during a call to work.
375  *
376  * Should be 0 for most blocks. Useful if we're dealing with
377  * packets and the block produces one packet per call to work.
378  */
379  int min_noutput_items() const { return d_min_noutput_items; }
380 
381  /*!
382  * \brief Set the minimum number of output items this block can
383  * produce during a call to work.
384  *
385  * \param m the minimum noutput_items this block can produce.
386  */
387  void set_min_noutput_items(int m) { d_min_noutput_items = m; }
388 
389  /*!
390  * \brief Return the maximum number of output items this block will
391  * handle during a call to work.
392  */
394 
395  /*!
396  * \brief Set the maximum number of output items this block will
397  * handle during a call to work.
398  *
399  * \param m the maximum noutput_items this block will handle.
400  */
402 
403  /*!
404  * \brief Clear the switch for using the max_noutput_items value of this block.
405  *
406  * When is_set_max_noutput_items() returns 'true', the scheduler
407  * will use the value returned by max_noutput_items() to limit the
408  * size of the number of items possible for this block's work
409  * function. If is_set_max_notput_items() returns 'false', then
410  * the scheduler ignores the internal value and uses the value set
411  * globally in the top_block.
412  *
413  * Use this value to clear the 'is_set' flag so the scheduler will
414  * ignore this. Use the set_max_noutput_items(m) call to both set
415  * a new value for max_noutput_items and to re-enable its use in
416  * the scheduler.
417  */
419 
420  /*!
421  * \brief Ask the block if the flag is or is not set to use the
422  * internal value of max_noutput_items during a call to work.
423  */
425 
426  /*
427  * Used to expand the vectors that hold the min/max buffer sizes.
428  *
429  * Specifically, when -1 is used, the vectors are just initialized
430  * with 1 value; this is used by the flat_flowgraph to expand when
431  * required to add a new value for new ports on these blocks.
432  */
433  void expand_minmax_buffer(int port);
434 
435  /*!
436  * \brief Returns max buffer size on output port \p i.
437  */
438  long max_output_buffer(size_t i);
439 
440  /*!
441  * \brief Request limit on max buffer size on all output ports.
442  *
443  * \details
444  * This is an advanced feature. Calling this can affect some
445  * fundamental assumptions about the system behavior and
446  * performance.
447  *
448  * The actual buffer size is determined by a number of other
449  * factors from the block and system. This function only provides
450  * a requested maximum. The buffers will always be a multiple of
451  * the system page size, which may be larger than the value asked
452  * for here.
453  *
454  * \param max_output_buffer the requested maximum output size in items.
455  */
456  void set_max_output_buffer(long max_output_buffer);
457 
458  /*!
459  * \brief Request limit on max buffer size on output port \p port.
460  *
461  * \details
462  * This is an advanced feature. Calling this can affect some
463  * fundamental assumptions about the system behavior and
464  * performance.
465  *
466  * The actual buffer size is determined by a number of other
467  * factors from the block and system. This function only provides
468  * a requested maximum. The buffers will always be a multiple of
469  * the system page size, which may be larger than the value asked
470  * for here.
471  *
472  * \param port the output port the request applies to.
473  * \param max_output_buffer the requested maximum output size in items.
474  */
475  void set_max_output_buffer(int port, long max_output_buffer);
476 
477  /*!
478  * \brief Returns min buffer size on output port \p i.
479  */
480  long min_output_buffer(size_t i);
481 
482  /*!
483  * \brief Request limit on the minimum buffer size on all output
484  * ports.
485  *
486  * \details
487  * This is an advanced feature. Calling this can affect some
488  * fundamental assumptions about the system behavior and
489  * performance.
490  *
491  * The actual buffer size is determined by a number of other
492  * factors from the block and system. This function only provides
493  * a requested minimum. The buffers will always be a multiple of
494  * the system page size, which may be larger than the value asked
495  * for here.
496  *
497  * \param min_output_buffer the requested minimum output size in items.
498  */
499  void set_min_output_buffer(long min_output_buffer);
500 
501  /*!
502  * \brief Request limit on min buffer size on output port \p port.
503  *
504  * \details
505  * This is an advanced feature. Calling this can affect some
506  * fundamental assumptions about the system behavior and
507  * performance.
508  *
509  * The actual buffer size is determined by a number of other
510  * factors from the block and system. This function only provides
511  * a requested minimum. The buffers will always be a multiple of
512  * the system page size, which may be larger than the value asked
513  * for here.
514  *
515  * \param port the output port the request applies to.
516  * \param min_output_buffer the requested minimum output size in items.
517  */
518  void set_min_output_buffer(int port, long min_output_buffer);
519 
520  /*!
521  * \brief DEPRECATED Configure the timer set when input is blocked \p port.
522  *
523  * \details
524  * This is an advanced/experimental feature and might be removed in a future
525  * version. Calling this can affect some fundamental assumptions about the
526  * system behavior and
527  * performance.
528  *
529  * In the TPB scheduler, when a block has no work to do because there
530  * is no data at it inputs, it sets a timer and tries again after a
531  * period of time. The default is 250 ms, but this can be configured
532  * differently per block when necessary
533  *
534  * \param timer_value_ms the timer value in milliseconds
535  */
536  void set_blkd_input_timer_value(unsigned int timer_value_ms);
537 
538  /*!
539  * \brief DEPRECATED Returns timer value set when input is blocked
540  */
541  unsigned int blkd_input_timer_value();
542 
543 
544  /*!
545  * \brief Allocate the block_detail and necessary output buffers for this
546  * block.
547  */
548  void allocate_detail(int ninputs,
549  int noutputs,
550  const std::vector<int>& downstream_max_nitems_vec,
551  const std::vector<uint64_t>& downstream_lcm_nitems_vec,
552  const std::vector<uint32_t>& downstream_max_out_mult_vec);
553 
554  // --------------- Custom buffer-related functions -------------
555 
556  /*!
557  * \brief Replace the block's buffer with a new one owned by the block_owner
558  * parameter
559  *
560  * \details
561  * This function is used to replace the buffer on the specified output port
562  * of the block with a new buffer that is "owned" by the specified block. This
563  * function will only be called if a downstream block is using a custom buffer
564  * that is incompatible with the default buffer type created by this block.
565  *
566  */
567  buffer_sptr replace_buffer(size_t src_port, size_t dst_port, block_sptr block_owner);
568 
569  // --------------- Performance counter functions -------------
570 
571  /*!
572  * \brief Gets instantaneous noutput_items performance counter.
573  */
575 
576  /*!
577  * \brief Gets average noutput_items performance counter.
578  */
580 
581  /*!
582  * \brief Gets variance of noutput_items performance counter.
583  */
585 
586  /*!
587  * \brief Gets instantaneous num items produced performance counter.
588  */
589  float pc_nproduced();
590 
591  /*!
592  * \brief Gets average num items produced performance counter.
593  */
595 
596  /*!
597  * \brief Gets variance of num items produced performance counter.
598  */
600 
601  /*!
602  * \brief Gets instantaneous fullness of \p which input buffer.
603  */
604  float pc_input_buffers_full(int which);
605 
606  /*!
607  * \brief Gets average fullness of \p which input buffer.
608  */
609  float pc_input_buffers_full_avg(int which);
610 
611  /*!
612  * \brief Gets variance of fullness of \p which input buffer.
613  */
614  float pc_input_buffers_full_var(int which);
615 
616  /*!
617  * \brief Gets instantaneous fullness of all input buffers.
618  */
619  std::vector<float> pc_input_buffers_full();
620 
621  /*!
622  * \brief Gets average fullness of all input buffers.
623  */
624  std::vector<float> pc_input_buffers_full_avg();
625 
626  /*!
627  * \brief Gets variance of fullness of all input buffers.
628  */
629  std::vector<float> pc_input_buffers_full_var();
630 
631  /*!
632  * \brief Gets instantaneous fullness of \p which output buffer.
633  */
634  float pc_output_buffers_full(int which);
635 
636  /*!
637  * \brief Gets average fullness of \p which output buffer.
638  */
639  float pc_output_buffers_full_avg(int which);
640 
641  /*!
642  * \brief Gets variance of fullness of \p which output buffer.
643  */
644  float pc_output_buffers_full_var(int which);
645 
646  /*!
647  * \brief Gets instantaneous fullness of all output buffers.
648  */
649  std::vector<float> pc_output_buffers_full();
650 
651  /*!
652  * \brief Gets average fullness of all output buffers.
653  */
654  std::vector<float> pc_output_buffers_full_avg();
655 
656  /*!
657  * \brief Gets variance of fullness of all output buffers.
658  */
659  std::vector<float> pc_output_buffers_full_var();
660 
661  /*!
662  * \brief Gets instantaneous clock cycles spent in work.
663  */
664  float pc_work_time();
665 
666  /*!
667  * \brief Gets average clock cycles spent in work.
668  */
670 
671  /*!
672  * \brief Gets average clock cycles spent in work.
673  */
675 
676  /*!
677  * \brief Gets total clock cycles spent in work.
678  */
680 
681  /*!
682  * \brief Gets average throughput.
683  */
685 
686  /*!
687  * \brief Resets the performance counters
688  */
690 
691  /*!
692  * \brief Sets up export of perf. counters to ControlPort. Only
693  * called by the scheduler.
694  */
695  void setup_pc_rpc();
696 
697  /*!
698  * \brief Checks if this block is already exporting perf. counters
699  * to ControlPort.
700  */
701  bool is_pc_rpc_set() const { return d_pc_rpc_set; }
702 
703  /*!
704  * \brief If the block calls this in its constructor, it's
705  * perf. counters will not be exported.
706  */
707  void no_pc_rpc() { d_pc_rpc_set = true; }
708 
709 
710  // ----------------------------------------------------------------------------
711  // Functions to handle thread affinity
712 
713  /*!
714  * \brief Set the thread's affinity to processor core \p n.
715  *
716  * \param mask a vector of ints of the core numbers available to this block.
717  */
718  void set_processor_affinity(const std::vector<int>& mask) override;
719 
720  /*!
721  * \brief Remove processor affinity to a specific core.
722  */
723  void unset_processor_affinity() override;
724 
725  /*!
726  * \brief Get the current processor affinity.
727  */
728  std::vector<int> processor_affinity() override { return d_affinity; }
729 
730  /*!
731  * \brief Get the current thread priority in use
732  */
734 
735  /*!
736  * \brief Get the current thread priority stored
737  */
739 
740  /*!
741  * \brief Set the current thread priority
742  */
743  int set_thread_priority(int priority);
744 
745  bool update_rate() const;
746 
747  // ----------------------------------------------------------------------------
748 
749  /*!
750  * \brief the system message handler
751  */
753 
754  /*!
755  * \brief Set the logger's output level.
756  *
757  * Sets the level of the logger. This takes a string that is
758  * translated to the standard levels and can be (case insensitive):
759  *
760  * \li off , notset
761  * \li debug
762  * \li info
763  * \li notice
764  * \li warn
765  * \li error
766  * \li crit
767  * \li alert
768  * \li fatal
769  * \li emerg
770  */
771  void set_log_level(const std::string& level) override;
772 
773  /*!
774  * \brief Get the logger's output level
775  */
776  std::string log_level() override;
777 
778  /*!
779  * \brief returns true when execution has completed due to a message connection
780  */
781  bool finished();
782 
783 private:
784  int d_output_multiple;
785  bool d_output_multiple_set;
786  int d_unaligned;
787  bool d_is_unaligned;
788  double d_relative_rate; // approx output_rate / input_rate
789  mpq_class d_mp_relative_rate;
790  block_detail_sptr d_detail; // implementation details
791  unsigned d_history;
792  unsigned d_attr_delay; // the block's sample delay
793  bool d_fixed_rate;
794  bool d_max_noutput_items_set; // if d_max_noutput_items is valid
795  int d_max_noutput_items; // value of max_noutput_items for this block
796  int d_min_noutput_items;
798  d_tag_propagation_policy; // policy for moving tags downstream
799  std::vector<int> d_affinity; // thread affinity proc. mask
800  int d_priority; // thread priority level
801  bool d_pc_rpc_set;
802  bool d_update_rate; // should sched update rel rate?
803  bool d_finished; // true if msg ports think we are finished
804 
805 protected:
806  block(void) {} // allows pure virtual interface sub-classes
807  block(const std::string& name,
808  gr::io_signature::sptr input_signature,
809  gr::io_signature::sptr output_signature);
810 
811  void set_fixed_rate(bool fixed_rate) { d_fixed_rate = fixed_rate; }
812 
813  /*!
814  * \brief Adds a new tag onto the given output buffer.
815  *
816  * \param which_output an integer of which output stream to attach the tag
817  * \param abs_offset a uint64 number of the absolute item number
818  * associated with the tag. Can get from nitems_written.
819  * \param key the tag key as a PMT symbol
820  * \param value any PMT holding any value for the given key
821  * \param srcid optional source ID specifier; defaults to PMT_F
822  */
823  inline void add_item_tag(unsigned int which_output,
824  uint64_t abs_offset,
825  const pmt::pmt_t& key,
826  const pmt::pmt_t& value,
827  const pmt::pmt_t& srcid = pmt::PMT_F)
828  {
829  tag_t tag;
830  tag.offset = abs_offset;
831  tag.key = key;
832  tag.value = value;
833  tag.srcid = srcid;
834  this->add_item_tag(which_output, tag);
835  }
836 
837  /*!
838  * \brief Adds a new tag onto the given output buffer.
839  *
840  * \param which_output an integer of which output stream to attach the tag
841  * \param tag the tag object to add
842  */
843  void add_item_tag(unsigned int which_output, const tag_t& tag);
844 
845  /*!
846  * \brief DEPRECATED. Will be removed in 3.8.
847  *
848  * \param which_input an integer of which input stream to remove the tag from
849  * \param abs_offset a uint64 number of the absolute item number
850  * associated with the tag. Can get from nitems_written.
851  * \param key the tag key as a PMT symbol
852  * \param value any PMT holding any value for the given key
853  * \param srcid optional source ID specifier; defaults to PMT_F
854  *
855  * If no such tag is found, does nothing.
856  */
857  inline void remove_item_tag(unsigned int which_input,
858  uint64_t abs_offset,
859  const pmt::pmt_t& key,
860  const pmt::pmt_t& value,
861  const pmt::pmt_t& srcid = pmt::PMT_F)
862  {
863  tag_t tag;
864  tag.offset = abs_offset;
865  tag.key = key;
866  tag.value = value;
867  tag.srcid = srcid;
868  this->remove_item_tag(which_input, tag);
869  }
870 
871  /*!
872  * \brief DEPRECATED. Will be removed in 3.8.
873  *
874  * \param which_input an integer of which input stream to remove the tag from
875  * \param tag the tag object to remove
876  */
877  void remove_item_tag(unsigned int which_input, const tag_t& tag);
878 
879  /*!
880  * \brief Given a [start,end), returns a vector of all tags in the range.
881  *
882  * Range of counts is from start to end-1.
883  *
884  * Tags are tuples of:
885  * (item count, source id, key, value)
886  *
887  * \param v a vector reference to return tags into
888  * \param which_input an integer of which input stream to pull from
889  * \param abs_start a uint64 count of the start of the range of interest
890  * \param abs_end a uint64 count of the end of the range of interest
891  */
892  void get_tags_in_range(std::vector<tag_t>& v,
893  unsigned int which_input,
894  uint64_t abs_start,
895  uint64_t abs_end);
896 
897  /*!
898  * \brief Given a [start,end), returns a vector of all tags in the
899  * range with a given key.
900  *
901  * Range of counts is from start to end-1.
902  *
903  * Tags are tuples of:
904  * (item count, source id, key, value)
905  *
906  * \param v a vector reference to return tags into
907  * \param which_input an integer of which input stream to pull from
908  * \param abs_start a uint64 count of the start of the range of interest
909  * \param abs_end a uint64 count of the end of the range of interest
910  * \param key a PMT symbol key to filter only tags of this key
911  */
912  void get_tags_in_range(std::vector<tag_t>& v,
913  unsigned int which_input,
914  uint64_t abs_start,
915  uint64_t abs_end,
916  const pmt::pmt_t& key);
917 
918  /*!
919  * \brief Gets all tags within the relative window of the current call to work.
920  *
921  * \details
922  *
923  * This operates much like get_tags_in_range but allows us to
924  * work within the current window of items. Item range is
925  * therefore within the possible range of 0 to
926  * ninput_items[whic_input].
927  *
928  * Range of items counts from \p rel_start to \p rel_end-1 within
929  * current window.
930  *
931  * Tags are tuples of:
932  * (item count, source id, key, value)
933  *
934  * \param v a vector reference to return tags into
935  * \param which_input an integer of which input stream to pull from
936  * \param rel_start a uint64 count of the start of the range of interest
937  * \param rel_end a uint64 count of the end of the range of interest
938  */
939  void get_tags_in_window(std::vector<tag_t>& v,
940  unsigned int which_input,
941  uint64_t rel_start,
942  uint64_t rel_end);
943 
944  /*!
945  * \brief Operates like gr::block::get_tags_in_window with the
946  * ability to only return tags with the specified \p key.
947  *
948  * \details
949  *
950  * \param v a vector reference to return tags into
951  * \param which_input an integer of which input stream to pull from
952  * \param rel_start a uint64 count of the start of the range of interest
953  * \param rel_end a uint64 count of the end of the range of interest
954  * \param key a PMT symbol key to filter only tags of this key
955  */
956  void get_tags_in_window(std::vector<tag_t>& v,
957  unsigned int which_input,
958  uint64_t rel_start,
959  uint64_t rel_end,
960  const pmt::pmt_t& key);
961 
962  void enable_update_rate(bool en);
963 
964  /*!
965  * \brief Allocate a buffer for the given output port of this block. Note
966  * that the downstream max number of items must be passed in to this
967  * function for consideration.
968  */
969  buffer_sptr allocate_buffer(size_t port,
970  int downstream_max_nitems,
971  uint64_t downstream_lcm_nitems,
972  uint32_t downstream_max_out_mult);
973 
974  std::vector<long> d_max_output_buffer;
975  std::vector<long> d_min_output_buffer;
976 
977  unsigned int d_blkd_input_timer_value = 250;
978 
979  /*! Used by block's setters and work functions to make
980  * setting/resetting of parameters thread-safe.
981  *
982  * Used by calling gr::thread::scoped_lock l(d_setlock);
983  */
985 
986  // These are really only for internal use, but leaving them public avoids
987  // having to work up an ever-varying list of friend GR_RUNTIME_APIs
988 
989  /*! PMT Symbol for "hey, we're done here"
990  */
992 
993  /*! PMT Symbol of the system port, `pmt::mp("system")`
994  */
996 
997 public:
998  block_detail_sptr detail() const { return d_detail; }
999  void set_detail(block_detail_sptr detail) { d_detail = detail; }
1000 
1001  /*! \brief Tell msg neighbors we are finished
1002  */
1004 
1005  /*! \brief Make sure we don't think we are finished
1006  */
1007  void clear_finished() { d_finished = false; }
1008 
1009  std::string identifier() const;
1010 };
1011 
1012 typedef std::vector<block_sptr> block_vector_t;
1013 typedef std::vector<block_sptr>::iterator block_viter_t;
1014 
1015 inline block_sptr cast_to_block_sptr(basic_block_sptr p)
1016 {
1017  return std::dynamic_pointer_cast<block, basic_block>(p);
1018 }
1019 
1020 GR_RUNTIME_API std::ostream& operator<<(std::ostream& os, const block* m);
1021 
1022 } /* namespace gr */
1023 
1024 #endif /* INCLUDED_GR_RUNTIME_BLOCK_H */
The abstract base class for all signal processing blocks.
Definition: basic_block.h:63
The abstract base class for all 'terminal' processing blocks.
Definition: gnuradio-runtime/include/gnuradio/block.h:63
void set_processor_affinity(const std::vector< int > &mask) override
Set the thread's affinity to processor core n.
float pc_input_buffers_full_var(int which)
Gets variance of fullness of which input buffer.
int unaligned() const
Definition: gnuradio-runtime/include/gnuradio/block.h:234
virtual bool stop()
Called to disable drivers, etc for i/o devices.
float pc_input_buffers_full_avg(int which)
Gets average fullness of which input buffer.
int active_thread_priority()
Get the current thread priority in use.
mpq_class & mp_relative_rate()
return a reference to the multiple precision rational representation of the approximate output rate /...
Definition: gnuradio-runtime/include/gnuradio/block.h:329
virtual int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
compute output items from input items
void set_is_unaligned(bool u)
std::vector< float > pc_output_buffers_full_avg()
Gets average fullness of all output buffers.
float pc_output_buffers_full_avg(int which)
Gets average fullness of which output buffer.
void consume_each(int how_many_items)
Tell the scheduler how_many_items were consumed on each input stream.
buffer_sptr replace_buffer(size_t src_port, size_t dst_port, block_sptr block_owner)
Replace the block's buffer with a new one owned by the block_owner parameter.
float pc_nproduced_var()
Gets variance of num items produced performance counter.
uint64_t nitems_read(unsigned int which_input)
Return the number of items read on input stream which_input.
const pmt::pmt_t d_system_port
Definition: gnuradio-runtime/include/gnuradio/block.h:995
double relative_rate() const
return the approximate output rate / input rate
Definition: gnuradio-runtime/include/gnuradio/block.h:305
float pc_output_buffers_full_var(int which)
Gets variance of fullness of which output buffer.
std::vector< float > pc_input_buffers_full_var()
Gets variance of fullness of all input buffers.
float pc_work_time_total()
Gets total clock cycles spent in work.
void set_inverse_relative_rate(double inverse_relative_rate)
Set the approximate output rate / input rate using its reciprocal.
std::vector< float > pc_input_buffers_full()
Gets instantaneous fullness of all input buffers.
float pc_work_time_avg()
Gets average clock cycles spent in work.
std::vector< int > processor_affinity() override
Get the current processor affinity.
Definition: gnuradio-runtime/include/gnuradio/block.h:728
unsigned int blkd_input_timer_value()
DEPRECATED Returns timer value set when input is blocked.
float pc_noutput_items_avg()
Gets average noutput_items performance counter.
void system_handler(pmt::pmt_t msg)
the system message handler
void allocate_detail(int ninputs, int noutputs, const std::vector< int > &downstream_max_nitems_vec, const std::vector< uint64_t > &downstream_lcm_nitems_vec, const std::vector< uint32_t > &downstream_max_out_mult_vec)
Allocate the block_detail and necessary output buffers for this block.
void get_tags_in_window(std::vector< tag_t > &v, unsigned int which_input, uint64_t rel_start, uint64_t rel_end, const pmt::pmt_t &key)
Operates like gr::block::get_tags_in_window with the ability to only return tags with the specified k...
void unset_max_noutput_items()
Clear the switch for using the max_noutput_items value of this block.
void remove_item_tag(unsigned int which_input, uint64_t abs_offset, const pmt::pmt_t &key, const pmt::pmt_t &value, const pmt::pmt_t &srcid=pmt::PMT_F)
DEPRECATED. Will be removed in 3.8.
Definition: gnuradio-runtime/include/gnuradio/block.h:857
void set_min_output_buffer(int port, long min_output_buffer)
Request limit on min buffer size on output port port.
void consume(int which_input, int how_many_items)
Tell the scheduler how_many_items of input stream which_input were consumed.
void set_tag_propagation_policy(tag_propagation_policy_t p)
Set the policy by the scheduler to determine how tags are moved downstream.
tag_propagation_policy_t tag_propagation_policy()
Asks for the policy used by the scheduler to moved tags downstream.
float pc_work_time_var()
Gets average clock cycles spent in work.
float pc_input_buffers_full(int which)
Gets instantaneous fullness of which input buffer.
void set_alignment(int multiple)
Constrains buffers to work on a set item alignment (for SIMD)
std::vector< long > d_min_output_buffer
Definition: gnuradio-runtime/include/gnuradio/block.h:975
void set_unaligned(int na)
bool is_set_max_noutput_items()
Ask the block if the flag is or is not set to use the internal value of max_noutput_items during a ca...
int thread_priority()
Get the current thread priority stored.
virtual void forecast(int noutput_items, gr_vector_int &ninput_items_required)
Estimate input requirements given output request.
long max_output_buffer(size_t i)
Returns max buffer size on output port i.
unsigned sample_delay(int which) const
block(void)
Definition: gnuradio-runtime/include/gnuradio/block.h:806
void set_output_multiple(int multiple)
Constrain the noutput_items argument passed to forecast and general_work.
void set_min_noutput_items(int m)
Set the minimum number of output items this block can produce during a call to work.
Definition: gnuradio-runtime/include/gnuradio/block.h:387
uint64_t relative_rate_d() const
return the denominator, or decimation rate, of the approximate output rate / input rate
Definition: gnuradio-runtime/include/gnuradio/block.h:320
std::vector< float > pc_output_buffers_full()
Gets instantaneous fullness of all output buffers.
void set_max_output_buffer(long max_output_buffer)
Request limit on max buffer size on all output ports.
void enable_update_rate(bool en)
buffer_sptr allocate_buffer(size_t port, int downstream_max_nitems, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult)
Allocate a buffer for the given output port of this block. Note that the downstream max number of ite...
uint64_t nitems_written(unsigned int which_output)
Return the number of items written on output stream which_output.
void no_pc_rpc()
If the block calls this in its constructor, it's perf. counters will not be exported.
Definition: gnuradio-runtime/include/gnuradio/block.h:707
~block() override
std::string identifier() const
void set_detail(block_detail_sptr detail)
Definition: gnuradio-runtime/include/gnuradio/block.h:999
float pc_throughput_avg()
Gets average throughput.
std::string log_level() override
Get the logger's output level.
virtual bool start()
Called to enable drivers, etc for i/o devices.
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)
Given a [start,end), returns a vector of all tags in the range with a given key.
int set_thread_priority(int priority)
Set the current thread priority.
void unset_processor_affinity() override
Remove processor affinity to a specific core.
float pc_nproduced()
Gets instantaneous num items produced performance counter.
bool finished()
returns true when execution has completed due to a message connection
void add_item_tag(unsigned int which_output, uint64_t abs_offset, const pmt::pmt_t &key, const pmt::pmt_t &value, const pmt::pmt_t &srcid=pmt::PMT_F)
Adds a new tag onto the given output buffer.
Definition: gnuradio-runtime/include/gnuradio/block.h:823
void add_item_tag(unsigned int which_output, const tag_t &tag)
Adds a new tag onto the given output buffer.
void setup_pc_rpc()
Sets up export of perf. counters to ControlPort. Only called by the scheduler.
void remove_item_tag(unsigned int which_input, const tag_t &tag)
DEPRECATED. Will be removed in 3.8.
virtual int fixed_rate_ninput_to_noutput(int ninput)
Given ninput samples, return number of output samples that will be produced. N.B. this is only define...
void set_max_output_buffer(int port, long max_output_buffer)
Request limit on max buffer size on output port port.
int alignment() const
Definition: gnuradio-runtime/include/gnuradio/block.h:231
int output_multiple() const
Definition: gnuradio-runtime/include/gnuradio/block.h:210
void expand_minmax_buffer(int port)
void set_min_output_buffer(long min_output_buffer)
Request limit on the minimum buffer size on all output ports.
void get_tags_in_range(std::vector< tag_t > &v, unsigned int which_input, uint64_t abs_start, uint64_t abs_end)
Given a [start,end), returns a vector of all tags in the range.
unsigned history() const
std::vector< float > pc_input_buffers_full_avg()
Gets average fullness of all input buffers.
void produce(int which_output, int how_many_items)
Tell the scheduler how_many_items were produced on output stream which_output.
bool is_unaligned() const
Definition: gnuradio-runtime/include/gnuradio/block.h:236
uint64_t relative_rate_i() const
return the numerator, or interpolation rate, of the approximate output rate / input rate
Definition: gnuradio-runtime/include/gnuradio/block.h:311
void declare_sample_delay(unsigned delay)
void set_fixed_rate(bool fixed_rate)
Definition: gnuradio-runtime/include/gnuradio/block.h:811
work_return_t
Magic return values from general_work.
Definition: gnuradio-runtime/include/gnuradio/block.h:66
gr::thread::mutex d_setlock
Definition: gnuradio-runtime/include/gnuradio/block.h:984
float pc_output_buffers_full(int which)
Gets instantaneous fullness of which output buffer.
std::vector< long > d_max_output_buffer
Definition: gnuradio-runtime/include/gnuradio/block.h:974
bool fixed_rate() const
Return true if this block has a fixed input to output rate.
Definition: gnuradio-runtime/include/gnuradio/block.h:138
int min_noutput_items() const
Return the minimum number of output items this block can produce during a call to work.
Definition: gnuradio-runtime/include/gnuradio/block.h:379
tag_propagation_policy_t
enum to represent different tag propagation policies.
Definition: gnuradio-runtime/include/gnuradio/block.h:71
void set_blkd_input_timer_value(unsigned int timer_value_ms)
DEPRECATED Configure the timer set when input is blocked port.
virtual int fixed_rate_noutput_to_ninput(int noutput)
Given noutput samples, return number of input samples required to produce noutput....
void get_tags_in_window(std::vector< tag_t > &v, unsigned int which_input, uint64_t rel_start, uint64_t rel_end)
Gets all tags within the relative window of the current call to work.
bool is_pc_rpc_set() const
Checks if this block is already exporting perf. counters to ControlPort.
Definition: gnuradio-runtime/include/gnuradio/block.h:701
long min_output_buffer(size_t i)
Returns min buffer size on output port i.
void set_history(unsigned history)
void declare_sample_delay(int which, unsigned delay)
block_detail_sptr detail() const
Definition: gnuradio-runtime/include/gnuradio/block.h:998
float pc_nproduced_avg()
Gets average num items produced performance counter.
bool output_multiple_set() const
Definition: gnuradio-runtime/include/gnuradio/block.h:211
const pmt::pmt_t d_pmt_done
Definition: gnuradio-runtime/include/gnuradio/block.h:991
void reset_perf_counters()
Resets the performance counters.
void notify_msg_neighbors()
Tell msg neighbors we are finished.
void set_relative_rate(uint64_t interpolation, uint64_t decimation)
Set the approximate output rate / input rate as an integer ratio.
float pc_noutput_items_var()
Gets variance of noutput_items performance counter.
void set_max_noutput_items(int m)
Set the maximum number of output items this block will handle during a call to work.
void set_log_level(const std::string &level) override
Set the logger's output level.
float pc_work_time()
Gets instantaneous clock cycles spent in work.
block(const std::string &name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature)
bool update_rate() const
void clear_finished()
Make sure we don't think we are finished.
Definition: gnuradio-runtime/include/gnuradio/block.h:1007
float pc_noutput_items()
Gets instantaneous noutput_items performance counter.
int max_noutput_items()
Return the maximum number of output items this block will handle during a call to work.
void set_relative_rate(double relative_rate)
Set the approximate output rate / input rate.
std::vector< float > pc_output_buffers_full_var()
Gets variance of fullness of all output buffers.
std::shared_ptr< io_signature > sptr
Definition: io_signature.h:52
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GR_RUNTIME_API const pmt::pmt_t msg()
boost::mutex mutex
Definition: thread.h:37
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::vector< block_sptr >::iterator block_viter_t
Definition: gnuradio-runtime/include/gnuradio/block.h:1013
std::vector< block_sptr > block_vector_t
Definition: gnuradio-runtime/include/gnuradio/block.h:1012
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:436
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:83
#define PMT_F
Definition: pmt.h:123
Definition: tags.h:19
uint64_t offset
the item tag occurred at (as a uint64_t)
Definition: tags.h:21
pmt::pmt_t srcid
the source ID of tag (as a PMT)
Definition: tags.h:30
pmt::pmt_t value
the value of tag (as a PMT)
Definition: tags.h:27
pmt::pmt_t key
the key of tag (as a PMT symbol)
Definition: tags.h:24
Definition: cc_common.h:35
std::vector< const void * > gr_vector_const_void_star
Definition: types.h:28
std::vector< void * > gr_vector_void_star
Definition: types.h:27
std::vector< int > gr_vector_int
Definition: types.h:23