From be1314af1004775eae0b461c9656d281a8152d02 Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Sat, 3 Aug 2013 19:01:31 +0200 Subject: [PATCH] runtime: import minimal gr runtime for standalone mode (wip) stripped pmt and message infrastructure, no logging, no nothing... --- CMakeLists.txt | 60 +- cmake/msvc/config.h | 58 + cmake/msvc/inttypes.h | 301 +++++ cmake/msvc/stdbool.h | 45 + cmake/msvc/stdint.h | 251 ++++ cmake/msvc/sys/time.h | 69 ++ cmake/msvc/unistd.h | 10 + gnuradio-osmosdr.pc.in | 12 +- include/osmosdr/runtime/CMakeLists.txt | 78 ++ include/osmosdr/runtime/gnuradio/api.h | 33 + include/osmosdr/runtime/gnuradio/attributes.h | 74 ++ .../osmosdr/runtime/gnuradio/basic_block.h | 209 ++++ include/osmosdr/runtime/gnuradio/block.h | 573 +++++++++ .../osmosdr/runtime/gnuradio/block_detail.h | 207 ++++ .../osmosdr/runtime/gnuradio/block_gateway.h | 178 +++ .../osmosdr/runtime/gnuradio/block_registry.h | 66 ++ include/osmosdr/runtime/gnuradio/blocks/api.h | 33 + .../runtime/gnuradio/blocks/null_sink.h | 55 + .../runtime/gnuradio/blocks/null_source.h | 53 + .../runtime/gnuradio/blocks/throttle.h | 63 + include/osmosdr/runtime/gnuradio/buffer.h | 258 +++++ include/osmosdr/runtime/gnuradio/constants.h | 58 + include/osmosdr/runtime/gnuradio/endianness.h | 32 + include/osmosdr/runtime/gnuradio/expj.h | 38 + include/osmosdr/runtime/gnuradio/feval.h | 186 +++ include/osmosdr/runtime/gnuradio/flowgraph.h | 194 ++++ include/osmosdr/runtime/gnuradio/fxpt.h | 108 ++ include/osmosdr/runtime/gnuradio/fxpt_nco.h | 160 +++ include/osmosdr/runtime/gnuradio/fxpt_vco.h | 80 ++ include/osmosdr/runtime/gnuradio/gr_complex.h | 45 + .../osmosdr/runtime/gnuradio/hier_block2.h | 192 +++ .../osmosdr/runtime/gnuradio/high_res_timer.h | 156 +++ .../osmosdr/runtime/gnuradio/io_signature.h | 111 ++ include/osmosdr/runtime/gnuradio/logger.h | 733 ++++++++++++ include/osmosdr/runtime/gnuradio/logger.h.in | 733 ++++++++++++ include/osmosdr/runtime/gnuradio/math.h | 227 ++++ include/osmosdr/runtime/gnuradio/misc.h | 39 + include/osmosdr/runtime/gnuradio/prefs.h | 152 +++ include/osmosdr/runtime/gnuradio/random.h | 81 ++ include/osmosdr/runtime/gnuradio/realtime.h | 39 + .../osmosdr/runtime/gnuradio/realtime_impl.h | 96 ++ .../osmosdr/runtime/gnuradio/runtime_types.h | 56 + include/osmosdr/runtime/gnuradio/sincos.h | 35 + include/osmosdr/runtime/gnuradio/sptr_magic.h | 57 + include/osmosdr/runtime/gnuradio/sync_block.h | 69 ++ .../osmosdr/runtime/gnuradio/sync_decimator.h | 72 ++ .../runtime/gnuradio/sync_interpolator.h | 74 ++ include/osmosdr/runtime/gnuradio/sys_paths.h | 37 + .../osmosdr/runtime/gnuradio/thread/thread.h | 156 +++ .../gnuradio/thread/thread_body_wrapper.h | 72 ++ .../runtime/gnuradio/thread/thread_group.h | 48 + include/osmosdr/runtime/gnuradio/top_block.h | 146 +++ include/osmosdr/runtime/gnuradio/tpb_detail.h | 92 ++ include/osmosdr/runtime/gnuradio/types.h | 65 ++ lib/CMakeLists.txt | 143 +++ lib/ConfigChecks.cmake | 204 ++++ lib/runtime/basic_block.cc | 77 ++ lib/runtime/block.cc | 626 ++++++++++ lib/runtime/block_detail.cc | 435 +++++++ lib/runtime/block_executor.cc | 419 +++++++ lib/runtime/block_executor.h | 77 ++ lib/runtime/block_gateway_impl.cc | 186 +++ lib/runtime/block_gateway_impl.h | 75 ++ lib/runtime/block_registry.cc | 111 ++ lib/runtime/blocks/null_sink_impl.cc | 60 + lib/runtime/blocks/null_sink_impl.h | 45 + lib/runtime/blocks/null_source_impl.cc | 63 + lib/runtime/blocks/null_source_impl.h | 45 + lib/runtime/blocks/throttle_impl.cc | 102 ++ lib/runtime/blocks/throttle_impl.h | 56 + lib/runtime/buffer.cc | 269 +++++ lib/runtime/circular_file.cc | 208 ++++ lib/runtime/circular_file.h | 65 ++ lib/runtime/constants.cc.in | 61 + lib/runtime/feval.cc | 136 +++ lib/runtime/flat_flowgraph.cc | 389 +++++++ lib/runtime/flat_flowgraph.h | 90 ++ lib/runtime/flowgraph.cc | 475 ++++++++ lib/runtime/hier_block2.cc | 142 +++ lib/runtime/hier_block2_detail.cc | 566 +++++++++ lib/runtime/hier_block2_detail.h | 77 ++ lib/runtime/high_res_timer.cc | 8 + lib/runtime/io_signature.cc | 117 ++ lib/runtime/local_sighandler.cc | 189 +++ lib/runtime/local_sighandler.h | 74 ++ lib/runtime/logger.cc | 323 ++++++ lib/runtime/math/fast_atan2f.cc | 202 ++++ lib/runtime/math/fxpt.cc | 38 + lib/runtime/math/random.cc | 188 +++ lib/runtime/math/sincos.cc | 85 ++ lib/runtime/math/sine_table.h | 1025 +++++++++++++++++ lib/runtime/math/vco.h | 99 ++ lib/runtime/misc.cc | 70 ++ lib/runtime/misc.h | 42 + lib/runtime/pagesize.cc | 59 + lib/runtime/pagesize.h | 37 + lib/runtime/posix_memalign.cc | 114 ++ lib/runtime/posix_memalign.h | 42 + lib/runtime/prefs.cc | 401 +++++++ lib/runtime/realtime.cc | 37 + lib/runtime/realtime_impl.cc | 192 +++ lib/runtime/scheduler.cc | 39 + lib/runtime/scheduler.h | 68 ++ lib/runtime/scheduler_sts.cc | 90 ++ lib/runtime/scheduler_sts.h | 66 ++ lib/runtime/scheduler_tpb.cc | 106 ++ lib/runtime/scheduler_tpb.h | 66 ++ lib/runtime/single_threaded_scheduler.cc | 363 ++++++ lib/runtime/single_threaded_scheduler.h | 65 ++ lib/runtime/sptr_magic.cc | 72 ++ lib/runtime/sync_block.cc | 72 ++ lib/runtime/sync_decimator.cc | 72 ++ lib/runtime/sync_interpolator.cc | 73 ++ lib/runtime/sys_paths.cc | 65 ++ lib/runtime/thread/thread.cc | 288 +++++ lib/runtime/thread/thread_body_wrapper.cc | 91 ++ lib/runtime/thread/thread_group.cc | 98 ++ lib/runtime/top_block.cc | 135 +++ lib/runtime/top_block_impl.cc | 212 ++++ lib/runtime/top_block_impl.h | 90 ++ lib/runtime/tpb_detail.cc | 71 ++ lib/runtime/tpb_thread_body.cc | 121 ++ lib/runtime/tpb_thread_body.h | 49 + lib/runtime/vmcircbuf.cc | 301 +++++ lib/runtime/vmcircbuf.h | 128 ++ lib/runtime/vmcircbuf_createfilemapping.cc | 208 ++++ lib/runtime/vmcircbuf_createfilemapping.h | 81 ++ lib/runtime/vmcircbuf_mmap_shm_open.cc | 208 ++++ lib/runtime/vmcircbuf_mmap_shm_open.h | 70 ++ lib/runtime/vmcircbuf_mmap_tmpfile.cc | 200 ++++ lib/runtime/vmcircbuf_mmap_tmpfile.h | 70 ++ lib/runtime/vmcircbuf_prefs.cc | 118 ++ lib/runtime/vmcircbuf_prefs.h | 39 + lib/runtime/vmcircbuf_sysv_shm.cc | 210 ++++ lib/runtime/vmcircbuf_sysv_shm.h | 70 ++ 135 files changed, 19735 insertions(+), 9 deletions(-) create mode 100644 cmake/msvc/config.h create mode 100644 cmake/msvc/inttypes.h create mode 100644 cmake/msvc/stdbool.h create mode 100644 cmake/msvc/stdint.h create mode 100644 cmake/msvc/sys/time.h create mode 100644 cmake/msvc/unistd.h create mode 100644 include/osmosdr/runtime/CMakeLists.txt create mode 100644 include/osmosdr/runtime/gnuradio/api.h create mode 100644 include/osmosdr/runtime/gnuradio/attributes.h create mode 100644 include/osmosdr/runtime/gnuradio/basic_block.h create mode 100644 include/osmosdr/runtime/gnuradio/block.h create mode 100644 include/osmosdr/runtime/gnuradio/block_detail.h create mode 100644 include/osmosdr/runtime/gnuradio/block_gateway.h create mode 100644 include/osmosdr/runtime/gnuradio/block_registry.h create mode 100644 include/osmosdr/runtime/gnuradio/blocks/api.h create mode 100644 include/osmosdr/runtime/gnuradio/blocks/null_sink.h create mode 100644 include/osmosdr/runtime/gnuradio/blocks/null_source.h create mode 100644 include/osmosdr/runtime/gnuradio/blocks/throttle.h create mode 100644 include/osmosdr/runtime/gnuradio/buffer.h create mode 100644 include/osmosdr/runtime/gnuradio/constants.h create mode 100644 include/osmosdr/runtime/gnuradio/endianness.h create mode 100644 include/osmosdr/runtime/gnuradio/expj.h create mode 100644 include/osmosdr/runtime/gnuradio/feval.h create mode 100644 include/osmosdr/runtime/gnuradio/flowgraph.h create mode 100644 include/osmosdr/runtime/gnuradio/fxpt.h create mode 100644 include/osmosdr/runtime/gnuradio/fxpt_nco.h create mode 100644 include/osmosdr/runtime/gnuradio/fxpt_vco.h create mode 100644 include/osmosdr/runtime/gnuradio/gr_complex.h create mode 100644 include/osmosdr/runtime/gnuradio/hier_block2.h create mode 100644 include/osmosdr/runtime/gnuradio/high_res_timer.h create mode 100644 include/osmosdr/runtime/gnuradio/io_signature.h create mode 100644 include/osmosdr/runtime/gnuradio/logger.h create mode 100644 include/osmosdr/runtime/gnuradio/logger.h.in create mode 100644 include/osmosdr/runtime/gnuradio/math.h create mode 100644 include/osmosdr/runtime/gnuradio/misc.h create mode 100644 include/osmosdr/runtime/gnuradio/prefs.h create mode 100644 include/osmosdr/runtime/gnuradio/random.h create mode 100644 include/osmosdr/runtime/gnuradio/realtime.h create mode 100644 include/osmosdr/runtime/gnuradio/realtime_impl.h create mode 100644 include/osmosdr/runtime/gnuradio/runtime_types.h create mode 100644 include/osmosdr/runtime/gnuradio/sincos.h create mode 100644 include/osmosdr/runtime/gnuradio/sptr_magic.h create mode 100644 include/osmosdr/runtime/gnuradio/sync_block.h create mode 100644 include/osmosdr/runtime/gnuradio/sync_decimator.h create mode 100644 include/osmosdr/runtime/gnuradio/sync_interpolator.h create mode 100644 include/osmosdr/runtime/gnuradio/sys_paths.h create mode 100644 include/osmosdr/runtime/gnuradio/thread/thread.h create mode 100644 include/osmosdr/runtime/gnuradio/thread/thread_body_wrapper.h create mode 100644 include/osmosdr/runtime/gnuradio/thread/thread_group.h create mode 100644 include/osmosdr/runtime/gnuradio/top_block.h create mode 100644 include/osmosdr/runtime/gnuradio/tpb_detail.h create mode 100644 include/osmosdr/runtime/gnuradio/types.h create mode 100644 lib/ConfigChecks.cmake create mode 100644 lib/runtime/basic_block.cc create mode 100644 lib/runtime/block.cc create mode 100644 lib/runtime/block_detail.cc create mode 100644 lib/runtime/block_executor.cc create mode 100644 lib/runtime/block_executor.h create mode 100644 lib/runtime/block_gateway_impl.cc create mode 100644 lib/runtime/block_gateway_impl.h create mode 100644 lib/runtime/block_registry.cc create mode 100644 lib/runtime/blocks/null_sink_impl.cc create mode 100644 lib/runtime/blocks/null_sink_impl.h create mode 100644 lib/runtime/blocks/null_source_impl.cc create mode 100644 lib/runtime/blocks/null_source_impl.h create mode 100644 lib/runtime/blocks/throttle_impl.cc create mode 100644 lib/runtime/blocks/throttle_impl.h create mode 100644 lib/runtime/buffer.cc create mode 100644 lib/runtime/circular_file.cc create mode 100644 lib/runtime/circular_file.h create mode 100644 lib/runtime/constants.cc.in create mode 100644 lib/runtime/feval.cc create mode 100644 lib/runtime/flat_flowgraph.cc create mode 100644 lib/runtime/flat_flowgraph.h create mode 100644 lib/runtime/flowgraph.cc create mode 100644 lib/runtime/hier_block2.cc create mode 100644 lib/runtime/hier_block2_detail.cc create mode 100644 lib/runtime/hier_block2_detail.h create mode 100644 lib/runtime/high_res_timer.cc create mode 100644 lib/runtime/io_signature.cc create mode 100644 lib/runtime/local_sighandler.cc create mode 100644 lib/runtime/local_sighandler.h create mode 100644 lib/runtime/logger.cc create mode 100644 lib/runtime/math/fast_atan2f.cc create mode 100644 lib/runtime/math/fxpt.cc create mode 100644 lib/runtime/math/random.cc create mode 100644 lib/runtime/math/sincos.cc create mode 100644 lib/runtime/math/sine_table.h create mode 100644 lib/runtime/math/vco.h create mode 100644 lib/runtime/misc.cc create mode 100644 lib/runtime/misc.h create mode 100644 lib/runtime/pagesize.cc create mode 100644 lib/runtime/pagesize.h create mode 100644 lib/runtime/posix_memalign.cc create mode 100644 lib/runtime/posix_memalign.h create mode 100644 lib/runtime/prefs.cc create mode 100644 lib/runtime/realtime.cc create mode 100644 lib/runtime/realtime_impl.cc create mode 100644 lib/runtime/scheduler.cc create mode 100644 lib/runtime/scheduler.h create mode 100644 lib/runtime/scheduler_sts.cc create mode 100644 lib/runtime/scheduler_sts.h create mode 100644 lib/runtime/scheduler_tpb.cc create mode 100644 lib/runtime/scheduler_tpb.h create mode 100644 lib/runtime/single_threaded_scheduler.cc create mode 100644 lib/runtime/single_threaded_scheduler.h create mode 100644 lib/runtime/sptr_magic.cc create mode 100644 lib/runtime/sync_block.cc create mode 100644 lib/runtime/sync_decimator.cc create mode 100644 lib/runtime/sync_interpolator.cc create mode 100644 lib/runtime/sys_paths.cc create mode 100644 lib/runtime/thread/thread.cc create mode 100644 lib/runtime/thread/thread_body_wrapper.cc create mode 100644 lib/runtime/thread/thread_group.cc create mode 100644 lib/runtime/top_block.cc create mode 100644 lib/runtime/top_block_impl.cc create mode 100644 lib/runtime/top_block_impl.h create mode 100644 lib/runtime/tpb_detail.cc create mode 100644 lib/runtime/tpb_thread_body.cc create mode 100644 lib/runtime/tpb_thread_body.h create mode 100644 lib/runtime/vmcircbuf.cc create mode 100644 lib/runtime/vmcircbuf.h create mode 100644 lib/runtime/vmcircbuf_createfilemapping.cc create mode 100644 lib/runtime/vmcircbuf_createfilemapping.h create mode 100644 lib/runtime/vmcircbuf_mmap_shm_open.cc create mode 100644 lib/runtime/vmcircbuf_mmap_shm_open.h create mode 100644 lib/runtime/vmcircbuf_mmap_tmpfile.cc create mode 100644 lib/runtime/vmcircbuf_mmap_tmpfile.h create mode 100644 lib/runtime/vmcircbuf_prefs.cc create mode 100644 lib/runtime/vmcircbuf_prefs.h create mode 100644 lib/runtime/vmcircbuf_sysv_shm.cc create mode 100644 lib/runtime/vmcircbuf_sysv_shm.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 14b7ffe..031e774 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,7 @@ MESSAGE(STATUS "Configuring Boost C++ Libraries...") SET(BOOST_REQUIRED_COMPONENTS thread system + filesystem ) if(UNIX AND NOT BOOST_ROOT AND EXISTS "/usr/lib64") @@ -140,6 +141,26 @@ set(GR_LIBEXEC_DIR libexec) set(GR_PKG_LIBEXEC_DIR ${GR_LIBEXEC_DIR}/${CMAKE_PROJECT_NAME}) set(GRC_BLOCKS_DIR ${GR_PKG_DATA_DIR}/grc/blocks) +# Special exception if prefix is /usr so we don't make a /usr/etc. +string(COMPARE EQUAL ${CMAKE_INSTALL_PREFIX} "/usr" isusr) +if(isusr) + set(SYSCONFDIR "/${GR_CONF_DIR}" CACHE PATH "System configuration directory") +else(isusr) + set(SYSCONFDIR "${CMAKE_INSTALL_PREFIX}/${GR_CONF_DIR}" CACHE PATH "System configuration directory" FORCE) +endif(isusr) + +set(GR_PREFSDIR ${SYSCONFDIR}/${CMAKE_PROJECT_NAME}/conf.d) + +######################################################################## +# Variables replaced when configuring the package config files +######################################################################## +file(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}" prefix) +file(TO_NATIVE_PATH "\${prefix}" exec_prefix) +file(TO_NATIVE_PATH "\${exec_prefix}/${GR_LIBRARY_DIR}" libdir) +file(TO_NATIVE_PATH "\${prefix}/${GR_INCLUDE_DIR}" includedir) +file(TO_NATIVE_PATH "${SYSCONFDIR}" SYSCONFDIR) +file(TO_NATIVE_PATH "${GR_PREFSDIR}" GR_PREFSDIR) + ######################################################################## # Find build dependencies ######################################################################## @@ -157,9 +178,41 @@ find_package(LibHackRF) find_package(LibbladeRF) find_package(Doxygen) +set(PYTHON_RUNTIME_CHECK TRUE) + if(NOT GNURADIO_RUNTIME_FOUND) - message(FATAL_ERROR "GnuRadio Runtime required to build " ${CMAKE_PROJECT_NAME}) -endif() + message(STATUS "Enabling built-in GNU Radio runtime...") + set(RUNTIME_MODE TRUE CACHE INTERNAL "Built-in GNU Radio runtime") + + add_subdirectory(include/osmosdr/runtime) + + set(GNURADIO_RUNTIME_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include/osmosdr/runtime) + set(GNURADIO_RUNTIME_LIBRARY_DIRS "") + set(GNURADIO_RUNTIME_LIBRARIES "") + + set(GNURADIO_PMT_INCLUDE_DIRS "") + set(GNURADIO_PMT_LIBRARY_DIRS "") + set(GNURADIO_PMT_LIBRARIES "") + + set(GNURADIO_BLOCKS_INCLUDE_DIRS "") + set(GNURADIO_BLOCKS_LIBRARY_DIRS "") + set(GNURADIO_BLOCKS_LIBRARIES "") + + LIST(APPEND GR_OSMOSDR_PC_CFLAGS "-I\${includedir}/osmosdr/runtime") + + # disable python bindings as long as we don't have any swig files for the runtime + set(PYTHON_RUNTIME_CHECK FALSE) +endif(NOT GNURADIO_RUNTIME_FOUND) + +if(GNURADIO_RUNTIME_FOUND) + LIST(APPEND GR_OSMOSDR_PC_REQUIRES "gnuradio-runtime") +endif(GNURADIO_RUNTIME_FOUND) +if(GNURADIO_PMT_FOUND) + LIST(APPEND GR_OSMOSDR_PC_REQUIRES "gnuradio-pmt") +endif(GNURADIO_PMT_FOUND) +if(GNURADIO_BLOCKS_FOUND) + LIST(APPEND GR_OSMOSDR_PC_REQUIRES "gnuradio-blocks") +endif(GNURADIO_BLOCKS_FOUND) ######################################################################## # Setup the include and linker paths @@ -211,10 +264,11 @@ if(SWIG_FOUND) endif(SWIG_FOUND) include(GrComponent) -GR_REGISTER_COMPONENT("Python support" ENABLE_PYTHON +GR_REGISTER_COMPONENT("Python bindings" ENABLE_PYTHON PYTHONLIBS_FOUND SWIG_FOUND SWIG_VERSION_CHECK + PYTHON_RUNTIME_CHECK ) ######################################################################## diff --git a/cmake/msvc/config.h b/cmake/msvc/config.h new file mode 100644 index 0000000..43792c7 --- /dev/null +++ b/cmake/msvc/config.h @@ -0,0 +1,58 @@ +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_CONFIG_H_ // [ +#define _MSC_CONFIG_H_ + +//////////////////////////////////////////////////////////////////////// +// enable inline functions for C code +//////////////////////////////////////////////////////////////////////// +#ifndef __cplusplus +# define inline __inline +#endif + +//////////////////////////////////////////////////////////////////////// +// signed size_t +//////////////////////////////////////////////////////////////////////// +#include +typedef ptrdiff_t ssize_t; + +//////////////////////////////////////////////////////////////////////// +// rint functions +//////////////////////////////////////////////////////////////////////// +#include +static inline long lrint(double x){return (long)(x > 0.0 ? x + 0.5 : x - 0.5);} +static inline long lrintf(float x){return (long)(x > 0.0f ? x + 0.5f : x - 0.5f);} +static inline long long llrint(double x){return (long long)(x > 0.0 ? x + 0.5 : x - 0.5);} +static inline long long llrintf(float x){return (long long)(x > 0.0f ? x + 0.5f : x - 0.5f);} +static inline double rint(double x){return (x > 0.0)? floor(x + 0.5) : ceil(x - 0.5);} +static inline float rintf(float x){return (x > 0.0f)? floorf(x + 0.5f) : ceilf(x - 0.5f);} + +//////////////////////////////////////////////////////////////////////// +// math constants +//////////////////////////////////////////////////////////////////////// +#define INFINITY HUGE_VAL + +# define M_E 2.7182818284590452354 /* e */ +# define M_LOG2E 1.4426950408889634074 /* log_2 e */ +# define M_LOG10E 0.43429448190325182765 /* log_10 e */ +# define M_LN2 0.69314718055994530942 /* log_e 2 */ +# define M_LN10 2.30258509299404568402 /* log_e 10 */ +# define M_PI 3.14159265358979323846 /* pi */ +# define M_PI_2 1.57079632679489661923 /* pi/2 */ +# define M_PI_4 0.78539816339744830962 /* pi/4 */ +# define M_1_PI 0.31830988618379067154 /* 1/pi */ +# define M_2_PI 0.63661977236758134308 /* 2/pi */ +# define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ +# define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ +# define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ + +//////////////////////////////////////////////////////////////////////// +// random and srandom +//////////////////////////////////////////////////////////////////////// +#include +static inline long int random (void) { return rand(); } +static inline void srandom (unsigned int seed) { srand(seed); } + +#endif // _MSC_CONFIG_H_ ] diff --git a/cmake/msvc/inttypes.h b/cmake/msvc/inttypes.h new file mode 100644 index 0000000..0a1b60f --- /dev/null +++ b/cmake/msvc/inttypes.h @@ -0,0 +1,301 @@ +// ISO C9x compliant inttypes.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. The name of the author may be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_INTTYPES_H_ // [ +#define _MSC_INTTYPES_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#include + +// 7.8 Format conversion of integer types + +typedef struct { + intmax_t quot; + intmax_t rem; +} imaxdiv_t; + +// 7.8.1 Macros for format specifiers + +// The fprintf macros for signed integers are: +#define PRId8 "d" +#define PRIi8 "i" +#define PRIdLEAST8 "d" +#define PRIiLEAST8 "i" +#define PRIdFAST8 "d" +#define PRIiFAST8 "i" + +#define PRId16 "hd" +#define PRIi16 "hi" +#define PRIdLEAST16 "hd" +#define PRIiLEAST16 "hi" +#define PRIdFAST16 "hd" +#define PRIiFAST16 "hi" + +#define PRId32 "I32d" +#define PRIi32 "I32i" +#define PRIdLEAST32 "I32d" +#define PRIiLEAST32 "I32i" +#define PRIdFAST32 "I32d" +#define PRIiFAST32 "I32i" + +#define PRId64 "I64d" +#define PRIi64 "I64i" +#define PRIdLEAST64 "I64d" +#define PRIiLEAST64 "I64i" +#define PRIdFAST64 "I64d" +#define PRIiFAST64 "I64i" + +#define PRIdMAX "I64d" +#define PRIiMAX "I64i" + +#define PRIdPTR "Id" +#define PRIiPTR "Ii" + +// The fprintf macros for unsigned integers are: +#define PRIo8 "o" +#define PRIu8 "u" +#define PRIx8 "x" +#define PRIX8 "X" +#define PRIoLEAST8 "o" +#define PRIuLEAST8 "u" +#define PRIxLEAST8 "x" +#define PRIXLEAST8 "X" +#define PRIoFAST8 "o" +#define PRIuFAST8 "u" +#define PRIxFAST8 "x" +#define PRIXFAST8 "X" + +#define PRIo16 "ho" +#define PRIu16 "hu" +#define PRIx16 "hx" +#define PRIX16 "hX" +#define PRIoLEAST16 "ho" +#define PRIuLEAST16 "hu" +#define PRIxLEAST16 "hx" +#define PRIXLEAST16 "hX" +#define PRIoFAST16 "ho" +#define PRIuFAST16 "hu" +#define PRIxFAST16 "hx" +#define PRIXFAST16 "hX" + +#define PRIo32 "I32o" +#define PRIu32 "I32u" +#define PRIx32 "I32x" +#define PRIX32 "I32X" +#define PRIoLEAST32 "I32o" +#define PRIuLEAST32 "I32u" +#define PRIxLEAST32 "I32x" +#define PRIXLEAST32 "I32X" +#define PRIoFAST32 "I32o" +#define PRIuFAST32 "I32u" +#define PRIxFAST32 "I32x" +#define PRIXFAST32 "I32X" + +#define PRIo64 "I64o" +#define PRIu64 "I64u" +#define PRIx64 "I64x" +#define PRIX64 "I64X" +#define PRIoLEAST64 "I64o" +#define PRIuLEAST64 "I64u" +#define PRIxLEAST64 "I64x" +#define PRIXLEAST64 "I64X" +#define PRIoFAST64 "I64o" +#define PRIuFAST64 "I64u" +#define PRIxFAST64 "I64x" +#define PRIXFAST64 "I64X" + +#define PRIoMAX "I64o" +#define PRIuMAX "I64u" +#define PRIxMAX "I64x" +#define PRIXMAX "I64X" + +#define PRIoPTR "Io" +#define PRIuPTR "Iu" +#define PRIxPTR "Ix" +#define PRIXPTR "IX" + +// The fscanf macros for signed integers are: +#define SCNd8 "d" +#define SCNi8 "i" +#define SCNdLEAST8 "d" +#define SCNiLEAST8 "i" +#define SCNdFAST8 "d" +#define SCNiFAST8 "i" + +#define SCNd16 "hd" +#define SCNi16 "hi" +#define SCNdLEAST16 "hd" +#define SCNiLEAST16 "hi" +#define SCNdFAST16 "hd" +#define SCNiFAST16 "hi" + +#define SCNd32 "ld" +#define SCNi32 "li" +#define SCNdLEAST32 "ld" +#define SCNiLEAST32 "li" +#define SCNdFAST32 "ld" +#define SCNiFAST32 "li" + +#define SCNd64 "I64d" +#define SCNi64 "I64i" +#define SCNdLEAST64 "I64d" +#define SCNiLEAST64 "I64i" +#define SCNdFAST64 "I64d" +#define SCNiFAST64 "I64i" + +#define SCNdMAX "I64d" +#define SCNiMAX "I64i" + +#ifdef _WIN64 // [ +# define SCNdPTR "I64d" +# define SCNiPTR "I64i" +#else // _WIN64 ][ +# define SCNdPTR "ld" +# define SCNiPTR "li" +#endif // _WIN64 ] + +// The fscanf macros for unsigned integers are: +#define SCNo8 "o" +#define SCNu8 "u" +#define SCNx8 "x" +#define SCNX8 "X" +#define SCNoLEAST8 "o" +#define SCNuLEAST8 "u" +#define SCNxLEAST8 "x" +#define SCNXLEAST8 "X" +#define SCNoFAST8 "o" +#define SCNuFAST8 "u" +#define SCNxFAST8 "x" +#define SCNXFAST8 "X" + +#define SCNo16 "ho" +#define SCNu16 "hu" +#define SCNx16 "hx" +#define SCNX16 "hX" +#define SCNoLEAST16 "ho" +#define SCNuLEAST16 "hu" +#define SCNxLEAST16 "hx" +#define SCNXLEAST16 "hX" +#define SCNoFAST16 "ho" +#define SCNuFAST16 "hu" +#define SCNxFAST16 "hx" +#define SCNXFAST16 "hX" + +#define SCNo32 "lo" +#define SCNu32 "lu" +#define SCNx32 "lx" +#define SCNX32 "lX" +#define SCNoLEAST32 "lo" +#define SCNuLEAST32 "lu" +#define SCNxLEAST32 "lx" +#define SCNXLEAST32 "lX" +#define SCNoFAST32 "lo" +#define SCNuFAST32 "lu" +#define SCNxFAST32 "lx" +#define SCNXFAST32 "lX" + +#define SCNo64 "I64o" +#define SCNu64 "I64u" +#define SCNx64 "I64x" +#define SCNX64 "I64X" +#define SCNoLEAST64 "I64o" +#define SCNuLEAST64 "I64u" +#define SCNxLEAST64 "I64x" +#define SCNXLEAST64 "I64X" +#define SCNoFAST64 "I64o" +#define SCNuFAST64 "I64u" +#define SCNxFAST64 "I64x" +#define SCNXFAST64 "I64X" + +#define SCNoMAX "I64o" +#define SCNuMAX "I64u" +#define SCNxMAX "I64x" +#define SCNXMAX "I64X" + +#ifdef _WIN64 // [ +# define SCNoPTR "I64o" +# define SCNuPTR "I64u" +# define SCNxPTR "I64x" +# define SCNXPTR "I64X" +#else // _WIN64 ][ +# define SCNoPTR "lo" +# define SCNuPTR "lu" +# define SCNxPTR "lx" +# define SCNXPTR "lX" +#endif // _WIN64 ] + +// 7.8.2 Functions for greatest-width integer types + +// 7.8.2.1 The imaxabs function +#define imaxabs _abs64 + +// 7.8.2.2 The imaxdiv function + +// This is modified version of div() function from Microsoft's div.c found +// in %MSVC.NET%\crt\src\div.c +#ifdef STATIC_IMAXDIV // [ +static +#else // STATIC_IMAXDIV ][ +_inline +#endif // STATIC_IMAXDIV ] +imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) +{ + imaxdiv_t result; + + result.quot = numer / denom; + result.rem = numer % denom; + + if (numer < 0 && result.rem > 0) { + // did division wrong; must fix up + ++result.quot; + result.rem -= denom; + } + + return result; +} + +// 7.8.2.3 The strtoimax and strtoumax functions +#define strtoimax _strtoi64 +#define strtoumax _strtoui64 + +// 7.8.2.4 The wcstoimax and wcstoumax functions +#define wcstoimax _wcstoi64 +#define wcstoumax _wcstoui64 + + +#endif // _MSC_INTTYPES_H_ ] diff --git a/cmake/msvc/stdbool.h b/cmake/msvc/stdbool.h new file mode 100644 index 0000000..ca4581d --- /dev/null +++ b/cmake/msvc/stdbool.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2005, 2006 Apple Computer, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef STDBOOL_WIN32_H +#define STDBOOL_WIN32_H + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef __cplusplus + +typedef unsigned char bool; + +#define true 1 +#define false 0 + +#ifndef CASSERT +#define CASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]; +#endif + +CASSERT(sizeof(bool) == 1, bool_is_one_byte) +CASSERT(true, true_is_true) +CASSERT(!false, false_is_false) + +#endif + +#endif diff --git a/cmake/msvc/stdint.h b/cmake/msvc/stdint.h new file mode 100644 index 0000000..108bc89 --- /dev/null +++ b/cmake/msvc/stdint.h @@ -0,0 +1,251 @@ +// ISO C9x compliant stdint.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2008 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. The name of the author may be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_STDINT_H_ // [ +#define _MSC_STDINT_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#include + +// For Visual Studio 6 in C++ mode and for many Visual Studio versions when +// compiling for ARM we should wrap include with 'extern "C++" {}' +// or compiler give many errors like this: +// error C2733: second C linkage of overloaded function 'wmemchr' not allowed +#ifdef __cplusplus +extern "C" { +#endif +# include +#ifdef __cplusplus +} +#endif + +// Define _W64 macros to mark types changing their size, like intptr_t. +#ifndef _W64 +# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 +# define _W64 __w64 +# else +# define _W64 +# endif +#endif + + +// 7.18.1 Integer types + +// 7.18.1.1 Exact-width integer types + +// Visual Studio 6 and Embedded Visual C++ 4 doesn't +// realize that, e.g. char has the same size as __int8 +// so we give up on __intX for them. +#if (_MSC_VER < 1300) + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; +#else + typedef signed __int8 int8_t; + typedef signed __int16 int16_t; + typedef signed __int32 int32_t; + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; +#endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; + + +// 7.18.1.2 Minimum-width integer types +typedef int8_t int_least8_t; +typedef int16_t int_least16_t; +typedef int32_t int_least32_t; +typedef int64_t int_least64_t; +typedef uint8_t uint_least8_t; +typedef uint16_t uint_least16_t; +typedef uint32_t uint_least32_t; +typedef uint64_t uint_least64_t; + +// 7.18.1.3 Fastest minimum-width integer types +typedef int8_t int_fast8_t; +typedef int16_t int_fast16_t; +typedef int32_t int_fast32_t; +typedef int64_t int_fast64_t; +typedef uint8_t uint_fast8_t; +typedef uint16_t uint_fast16_t; +typedef uint32_t uint_fast32_t; +typedef uint64_t uint_fast64_t; + +// 7.18.1.4 Integer types capable of holding object pointers +#ifdef _WIN64 // [ + typedef signed __int64 intptr_t; + typedef unsigned __int64 uintptr_t; +#else // _WIN64 ][ + typedef _W64 signed int intptr_t; + typedef _W64 unsigned int uintptr_t; +#endif // _WIN64 ] + +// 7.18.1.5 Greatest-width integer types +typedef int64_t intmax_t; +typedef uint64_t uintmax_t; + + +// 7.18.2 Limits of specified-width integer types + +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 + +// 7.18.2.1 Limits of exact-width integer types +#define INT8_MIN ((int8_t)_I8_MIN) +#define INT8_MAX _I8_MAX +#define INT16_MIN ((int16_t)_I16_MIN) +#define INT16_MAX _I16_MAX +#define INT32_MIN ((int32_t)_I32_MIN) +#define INT32_MAX _I32_MAX +#define INT64_MIN ((int64_t)_I64_MIN) +#define INT64_MAX _I64_MAX +#define UINT8_MAX _UI8_MAX +#define UINT16_MAX _UI16_MAX +#define UINT32_MAX _UI32_MAX +#define UINT64_MAX _UI64_MAX + +// 7.18.2.2 Limits of minimum-width integer types +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +// 7.18.2.3 Limits of fastest minimum-width integer types +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST16_MAX INT16_MAX +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT16_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +// 7.18.2.4 Limits of integer types capable of holding object pointers +#ifdef _WIN64 // [ +# define INTPTR_MIN INT64_MIN +# define INTPTR_MAX INT64_MAX +# define UINTPTR_MAX UINT64_MAX +#else // _WIN64 ][ +# define INTPTR_MIN INT32_MIN +# define INTPTR_MAX INT32_MAX +# define UINTPTR_MAX UINT32_MAX +#endif // _WIN64 ] + +// 7.18.2.5 Limits of greatest-width integer types +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +// 7.18.3 Limits of other integer types + +#ifdef _WIN64 // [ +# define PTRDIFF_MIN _I64_MIN +# define PTRDIFF_MAX _I64_MAX +#else // _WIN64 ][ +# define PTRDIFF_MIN _I32_MIN +# define PTRDIFF_MAX _I32_MAX +#endif // _WIN64 ] + +#define SIG_ATOMIC_MIN INT_MIN +#define SIG_ATOMIC_MAX INT_MAX + +#ifndef SIZE_MAX // [ +# ifdef _WIN64 // [ +# define SIZE_MAX _UI64_MAX +# else // _WIN64 ][ +# define SIZE_MAX _UI32_MAX +# endif // _WIN64 ] +#endif // SIZE_MAX ] + +// WCHAR_MIN and WCHAR_MAX are also defined in +#ifndef WCHAR_MIN // [ +# define WCHAR_MIN 0 +#endif // WCHAR_MIN ] +#ifndef WCHAR_MAX // [ +# define WCHAR_MAX _UI16_MAX +#endif // WCHAR_MAX ] + +#define WINT_MIN 0 +#define WINT_MAX _UI16_MAX + +#endif // __STDC_LIMIT_MACROS ] + + +// 7.18.4 Limits of other integer types + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +#ifndef INTMAX_C +#define INTMAX_C INT64_C +#endif +#ifndef UINTMAX_C +#define UINTMAX_C UINT64_C +#endif + +#endif // __STDC_CONSTANT_MACROS ] + + +#endif // _MSC_STDINT_H_ ] diff --git a/cmake/msvc/sys/time.h b/cmake/msvc/sys/time.h new file mode 100644 index 0000000..8800ed5 --- /dev/null +++ b/cmake/msvc/sys/time.h @@ -0,0 +1,69 @@ +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_SYS_TIME_H_ +#define _MSC_SYS_TIME_H_ + +//http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/430449b3-f6dd-4e18-84de-eebd26a8d668 +#include < time.h > +#include //I've ommited this line. +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timespec { + +time_t tv_sec; /* Seconds since 00:00:00 GMT, */ + +/* 1 January 1970 */ + +long tv_nsec; /* Additional nanoseconds since */ + +/* tv_sec */ + +}; + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +static inline int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} + +#endif //_MSC_SYS_TIME_H_ diff --git a/cmake/msvc/unistd.h b/cmake/msvc/unistd.h new file mode 100644 index 0000000..de412e4 --- /dev/null +++ b/cmake/msvc/unistd.h @@ -0,0 +1,10 @@ +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_UNISTD_H_ // [ +#define _MSC_UNISTD_H_ + +#include + +#endif // _MSC_UNISTD_H_ ] diff --git a/gnuradio-osmosdr.pc.in b/gnuradio-osmosdr.pc.in index 5f1ae69..9899500 100644 --- a/gnuradio-osmosdr.pc.in +++ b/gnuradio-osmosdr.pc.in @@ -1,14 +1,14 @@ -prefix=@CMAKE_INSTALL_PREFIX@ -exec_prefix=${prefix} -libdir=${exec_prefix}/@GR_LIBRARY_DIR@ -includedir=${prefix}/@GR_INCLUDE_DIR@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ Name: @CPACK_PACKAGE_NAME@ Description: @CPACK_PACKAGE_DESCRIPTION_SUMMARY@ URL: http://sdr.osmocom.org/trac/wiki/GrOsmoSDR Version: @CPACK_PACKAGE_VERSION@ -Requires: gnuradio-runtime gnuradio-blocks -Requires.private: @GR_OSMOSDR_PC_REQUIRES@ +Requires: @GR_OSMOSDR_PC_REQUIRES@ +Requires.private: Conflicts: Cflags: -I${includedir} @GR_OSMOSDR_PC_CFLAGS@ Libs: -L${libdir} -lgnuradio-osmosdr diff --git a/include/osmosdr/runtime/CMakeLists.txt b/include/osmosdr/runtime/CMakeLists.txt new file mode 100644 index 0000000..9c9687f --- /dev/null +++ b/include/osmosdr/runtime/CMakeLists.txt @@ -0,0 +1,78 @@ +# Copyright 2011 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +######################################################################## +# Install public header files +######################################################################## +install(FILES + gnuradio/blocks/api.h + gnuradio/blocks/null_sink.h + gnuradio/blocks/null_source.h + gnuradio/blocks/throttle.h + DESTINATION include/osmosdr/runtime/gnuradio/blocks +) + +install(FILES + gnuradio/thread/thread.h + gnuradio/thread/thread_body_wrapper.h + gnuradio/thread/thread_group.h + + DESTINATION include/osmosdr/runtime/gnuradio/thread +) + +install(FILES + gnuradio/api.h + gnuradio/attributes.h + gnuradio/basic_block.h + gnuradio/block.h + gnuradio/block_detail.h + gnuradio/block_gateway.h + gnuradio/block_registry.h + gnuradio/buffer.h + gnuradio/constants.h + gnuradio/endianness.h + gnuradio/expj.h + gnuradio/feval.h + gnuradio/flowgraph.h + gnuradio/fxpt.h + gnuradio/fxpt_nco.h + gnuradio/fxpt_vco.h + gnuradio/gr_complex.h + gnuradio/hier_block2.h + gnuradio/high_res_timer.h + gnuradio/io_signature.h + gnuradio/logger.h + gnuradio/math.h + gnuradio/misc.h + gnuradio/prefs.h + gnuradio/random.h + gnuradio/realtime.h + gnuradio/realtime_impl.h + gnuradio/runtime_types.h + gnuradio/sincos.h + gnuradio/sptr_magic.h + gnuradio/sync_block.h + gnuradio/sync_decimator.h + gnuradio/sync_interpolator.h + gnuradio/sys_paths.h + gnuradio/top_block.h + gnuradio/tpb_detail.h + gnuradio/types.h + DESTINATION include/osmosdr/runtime/gnuradio +) diff --git a/include/osmosdr/runtime/gnuradio/api.h b/include/osmosdr/runtime/gnuradio/api.h new file mode 100644 index 0000000..1c68b8c --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/api.h @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_RUNTIME_API_H +#define INCLUDED_GR_RUNTIME_RUNTIME_API_H + +#include + +#ifdef gnuradio_runtime_EXPORTS +# define GR_RUNTIME_API __GR_ATTR_EXPORT +#else +# define GR_RUNTIME_API __GR_ATTR_IMPORT +#endif + +#endif /* INCLUDED_GR_RUNTIME_RUNTIME_API_H */ diff --git a/include/osmosdr/runtime/gnuradio/attributes.h b/include/osmosdr/runtime/gnuradio/attributes.h new file mode 100644 index 0000000..5baa52e --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/attributes.h @@ -0,0 +1,74 @@ +/* + * Copyright 2010 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GNURADIO_ATTRIBUTES_H +#define INCLUDED_GNURADIO_ATTRIBUTES_H + +//////////////////////////////////////////////////////////////////////// +// Cross-platform attribute macros +//////////////////////////////////////////////////////////////////////// +#if defined __GNUC__ +# define __GR_ATTR_ALIGNED(x) __attribute__((aligned(x))) +# define __GR_ATTR_UNUSED __attribute__((unused)) +# define __GR_ATTR_INLINE __attribute__((always_inline)) +# define __GR_ATTR_DEPRECATED __attribute__((deprecated)) +# if __GNUC__ >= 4 +# define __GR_ATTR_EXPORT __attribute__((visibility("default"))) +# define __GR_ATTR_IMPORT __attribute__((visibility("default"))) +# else +# define __GR_ATTR_EXPORT +# define __GR_ATTR_IMPORT +# endif +#elif _MSC_VER +# define __GR_ATTR_ALIGNED(x) __declspec(align(x)) +# define __GR_ATTR_UNUSED +# define __GR_ATTR_INLINE __forceinline +# define __GR_ATTR_DEPRECATED __declspec(deprecated) +# define __GR_ATTR_EXPORT __declspec(dllexport) +# define __GR_ATTR_IMPORT __declspec(dllimport) +#else +# define __GR_ATTR_ALIGNED(x) +# define __GR_ATTR_UNUSED +# define __GR_ATTR_INLINE +# define __GR_ATTR_DEPRECATED +# define __GR_ATTR_EXPORT +# define __GR_ATTR_IMPORT +#endif + +//////////////////////////////////////////////////////////////////////// +// define inline when building C +//////////////////////////////////////////////////////////////////////// +#if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline) +# define inline __inline +#endif + +//////////////////////////////////////////////////////////////////////// +// suppress warnings +//////////////////////////////////////////////////////////////////////// +#ifdef _MSC_VER +# pragma warning(disable: 4251) // class 'A' needs to have dll-interface to be used by clients of class 'B' +# pragma warning(disable: 4275) // non dll-interface class ... used as base for dll-interface class ... +# pragma warning(disable: 4244) // conversion from 'double' to 'float', possible loss of data +# pragma warning(disable: 4305) // 'initializing' : truncation from 'double' to 'float' +# pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +#endif + +#endif /* INCLUDED_GNURADIO_ATTRIBUTES_H */ diff --git a/include/osmosdr/runtime/gnuradio/basic_block.h b/include/osmosdr/runtime/gnuradio/basic_block.h new file mode 100644 index 0000000..83973aa --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/basic_block.h @@ -0,0 +1,209 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2008,2009,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_BASIC_BLOCK_H +#define INCLUDED_GR_BASIC_BLOCK_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef GR_CTRLPORT +#include +#endif + +namespace gr { + + /*! + * \brief The abstract base class for all signal processing blocks. + * \ingroup internal + * + * Basic blocks are the bare abstraction of an entity that has a + * name, a set of inputs and outputs, and a message queue. These + * are never instantiated directly; rather, this is the abstract + * parent class of both gr_hier_block, which is a recursive + * container, and block, which implements actual signal + * processing functions. + */ + class GR_RUNTIME_API basic_block : public boost::enable_shared_from_this + { + private: + gr::thread::mutex mutex; //< protects all vars + + protected: + friend class flowgraph; + friend class flat_flowgraph; // TODO: will be redundant + friend class tpb_thread_body; + + enum vcolor { WHITE, GREY, BLACK }; + + std::string d_name; + gr::io_signature::sptr d_input_signature; + gr::io_signature::sptr d_output_signature; + long d_unique_id; + long d_symbolic_id; + std::string d_symbol_name; + std::string d_symbol_alias; + vcolor d_color; + bool d_rpc_set; + + std::vector d_rpc_vars; // container for all RPC variables + + basic_block(void) {} // allows pure virtual interface sub-classes + + //! Protected constructor prevents instantiation by non-derived classes + basic_block(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature); + + //! may only be called during constructor + void set_input_signature(gr::io_signature::sptr iosig) { + d_input_signature = iosig; + } + + //! may only be called during constructor + void set_output_signature(gr::io_signature::sptr iosig) { + d_output_signature = iosig; + } + + /*! + * \brief Allow the flowgraph to set for sorting and partitioning + */ + void set_color(vcolor color) { d_color = color; } + vcolor color() const { return d_color; } + + public: + virtual ~basic_block(); + long unique_id() const { return d_unique_id; } + long symbolic_id() const { return d_symbolic_id; } + std::string name() const { return d_name; } + std::string symbol_name() const { return d_symbol_name; } + gr::io_signature::sptr input_signature() const { return d_input_signature; } + gr::io_signature::sptr output_signature() const { return d_output_signature; } + basic_block_sptr to_basic_block(); // Needed for Python type coercion + bool alias_set() { return !d_symbol_alias.empty(); } + std::string alias(){ return alias_set()?d_symbol_alias:symbol_name(); } + void set_block_alias(std::string name); + + + +#ifdef GR_CTRLPORT + /*! + * \brief Add an RPC variable (get or set). + * + * Using controlport, we create new getters/setters and need to + * store them. Each block has a vector to do this, and these never + * need to be accessed again once they are registered with the RPC + * backend. This function takes a + * boost::shared_sptr so that when the block is + * deleted, all RPC registered variables are cleaned up. + * + * \param s an rpcbasic_sptr of the new RPC variable register to store. + */ + void add_rpc_variable(rpcbasic_sptr s) + { + d_rpc_vars.push_back(s); + } +#endif /* GR_CTRLPORT */ + + /*! + * \brief Set up the RPC registered variables. + * + * This must be overloaded by a block that wants to use + * controlport. This is where rpcbasic_register_{get,set} pointers + * are created, which then get wrapped as shared pointers + * (rpcbasic_sptr(...)) and stored using add_rpc_variable. + */ + virtual void setup_rpc() {}; + + /*! + * \brief Ask if this block has been registered to the RPC. + * + * We can only register a block once, so we use this to protect us + * from calling it multiple times. + */ + bool is_rpc_set() { return d_rpc_set; } + + /*! + * \brief When the block is registered with the RPC, set this. + */ + void rpc_set() { d_rpc_set = true; } + + /*! + * \brief Confirm that ninputs and noutputs is an acceptable combination. + * + * \param ninputs number of input streams connected + * \param noutputs number of output streams connected + * + * \returns true if this is a valid configuration for this block. + * + * This function is called by the runtime system whenever the + * topology changes. Most classes do not need to override this. + * This check is in addition to the constraints specified by the + * input and output gr::io_signatures. + */ + virtual bool check_topology(int ninputs, int noutputs) { + (void)ninputs; + (void)noutputs; + return true; + } + + virtual void set_processor_affinity(const std::vector &mask) + { throw std::runtime_error("set_processor_affinity not overloaded in child class."); } + + virtual void unset_processor_affinity() + { throw std::runtime_error("unset_processor_affinity not overloaded in child class."); } + + virtual std::vector processor_affinity() + { throw std::runtime_error("processor_affinity not overloaded in child class."); } + }; + + inline bool operator<(basic_block_sptr lhs, basic_block_sptr rhs) + { + return lhs->unique_id() < rhs->unique_id(); + } + + typedef std::vector basic_block_vector_t; + typedef std::vector::iterator basic_block_viter_t; + + GR_RUNTIME_API long basic_block_ncurrently_allocated(); + + inline std::ostream &operator << (std::ostream &os, basic_block_sptr basic_block) + { + os << basic_block->name() << "(" << basic_block->unique_id() << ")"; + return os; + } + +} /* namespace gr */ + +#endif /* INCLUDED_GR_BASIC_BLOCK_H */ diff --git a/include/osmosdr/runtime/gnuradio/block.h b/include/osmosdr/runtime/gnuradio/block.h new file mode 100644 index 0000000..27e73f3 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/block.h @@ -0,0 +1,573 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007,2009,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_BLOCK_H +#define INCLUDED_GR_RUNTIME_BLOCK_H + +#include +#include +#include + +namespace gr { + + /*! + * \brief The abstract base class for all 'terminal' processing blocks. + * \ingroup base_blk + * + * A signal processing flow is constructed by creating a tree of + * hierarchical blocks, which at any level may also contain terminal + * nodes that actually implement signal processing functions. This + * is the base class for all such leaf nodes. + * + * Blocks have a set of input streams and output streams. The + * input_signature and output_signature define the number of input + * streams and output streams respectively, and the type of the data + * items in each stream. + * + * Although blocks may consume data on each input stream at a + * different rate, all outputs streams must produce data at the same + * rate. That rate may be different from any of the input rates. + * + * User derived blocks override two methods, forecast and + * general_work, to implement their signal processing + * behavior. forecast is called by the system scheduler to determine + * how many items are required on each input stream in order to + * produce a given number of output items. + * + * general_work is called to perform the signal processing in the + * block. It reads the input items and writes the output items. + */ + class GR_RUNTIME_API block : public basic_block + { + public: + + //! Magic return values from general_work + enum { + WORK_CALLED_PRODUCE = -2, + WORK_DONE = -1 + }; + + enum tag_propagation_policy_t { + TPP_DONT = 0, + TPP_ALL_TO_ALL = 1, + TPP_ONE_TO_ONE = 2 + }; + + virtual ~block(); + + /*! + * Assume block computes y_i = f(x_i, x_i-1, x_i-2, x_i-3...) + * History is the number of x_i's that are examined to produce one y_i. + * This comes in handy for FIR filters, where we use history to + * ensure that our input contains the appropriate "history" for the + * filter. History should be equal to the number of filter taps. + */ + unsigned history() const { return d_history; } + void set_history(unsigned history) { d_history = history; } + + /*! + * \brief Return true if this block has a fixed input to output rate. + * + * If true, then fixed_rate_in_to_out and fixed_rate_out_to_in may be called. + */ + bool fixed_rate() const { return d_fixed_rate; } + + // ---------------------------------------------------------------- + // override these to define your behavior + // ---------------------------------------------------------------- + + /*! + * \brief Estimate input requirements given output request + * + * \param noutput_items number of output items to produce + * \param ninput_items_required number of input items required on each input stream + * + * Given a request to product \p noutput_items, estimate the + * number of data items required on each input stream. The + * estimate doesn't have to be exact, but should be close. + */ + virtual void forecast(int noutput_items, + gr_vector_int &ninput_items_required); + + /*! + * \brief compute output items from input items + * + * \param noutput_items number of output items to write on each output stream + * \param ninput_items number of input items available on each input stream + * \param input_items vector of pointers to the input items, one entry per input stream + * \param output_items vector of pointers to the output items, one entry per output stream + * + * \returns number of items actually written to each output stream, or -1 on EOF. + * It is OK to return a value less than noutput_items. -1 <= return value <= noutput_items + * + * general_work must call consume or consume_each to indicate how + * many items were consumed on each input stream. + */ + 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); + + /*! + * \brief Called to enable drivers, etc for i/o devices. + * + * This allows a block to enable an associated driver to begin + * transfering data just before we start to execute the scheduler. + * The end result is that this reduces latency in the pipeline + * when dealing with audio devices, usrps, etc. + */ + virtual bool start(); + + /*! + * \brief Called to disable drivers, etc for i/o devices. + */ + virtual bool stop(); + + // ---------------------------------------------------------------- + + /*! + * \brief Constrain the noutput_items argument passed to forecast and general_work + * + * set_output_multiple causes the scheduler to ensure that the + * noutput_items argument passed to forecast and general_work will + * be an integer multiple of \param multiple The default value of + * output multiple is 1. + */ + void set_output_multiple(int multiple); + int output_multiple() const { return d_output_multiple; } + bool output_multiple_set() const { return d_output_multiple_set; } + + /*! + * \brief Constrains buffers to work on a set item alignment (for SIMD) + * + * set_alignment_multiple causes the scheduler to ensure that the + * noutput_items argument passed to forecast and general_work will + * be an integer multiple of \param multiple The default value is + * 1. + * + * This control is similar to the output_multiple setting, except + * that if the number of items passed to the block is less than + * the output_multiple, this value is ignored and the block can + * produce like normal. The d_unaligned value is set to the number + * of items the block is off by. In the next call to general_work, + * the noutput_items is set to d_unaligned or less until + * d_unaligned==0. The buffers are now aligned again and the + * aligned calls can be performed again. + */ + void set_alignment(int multiple); + int alignment() const { return d_output_multiple; } + + void set_unaligned(int na); + int unaligned() const { return d_unaligned; } + void set_is_unaligned(bool u); + bool is_unaligned() const { return d_is_unaligned; } + + /*! + * \brief Tell the scheduler \p how_many_items of input stream \p + * which_input were consumed. + */ + void consume(int which_input, int how_many_items); + + /*! + * \brief Tell the scheduler \p how_many_items were consumed on + * each input stream. + */ + void consume_each(int how_many_items); + + /*! + * \brief Tell the scheduler \p how_many_items were produced on + * output stream \p which_output. + * + * If the block's general_work method calls produce, \p + * general_work must return WORK_CALLED_PRODUCE. + */ + void produce(int which_output, int how_many_items); + + /*! + * \brief Set the approximate output rate / input rate + * + * Provide a hint to the buffer allocator and scheduler. + * The default relative_rate is 1.0 + * + * decimators have relative_rates < 1.0 + * interpolators have relative_rates > 1.0 + */ + void set_relative_rate(double relative_rate); + + /*! + * \brief return the approximate output rate / input rate + */ + double relative_rate() const { return d_relative_rate; } + + /* + * The following two methods provide special case info to the + * scheduler in the event that a block has a fixed input to output + * ratio. sync_block, sync_decimator and + * sync_interpolator override these. If you're fixed rate, + * subclass one of those. + */ + /*! + * \brief Given ninput samples, return number of output samples that will be produced. + * N.B. this is only defined if fixed_rate returns true. + * Generally speaking, you don't need to override this. + */ + virtual int fixed_rate_ninput_to_noutput(int ninput); + + /*! + * \brief Given noutput samples, return number of input samples required to produce noutput. + * N.B. this is only defined if fixed_rate returns true. + * Generally speaking, you don't need to override this. + */ + virtual int fixed_rate_noutput_to_ninput(int noutput); + + /*! + * \brief Return the number of items read on input stream which_input + */ + uint64_t nitems_read(unsigned int which_input); + + /*! + * \brief Return the number of items written on output stream which_output + */ + uint64_t nitems_written(unsigned int which_output); + + /*! + * \brief Return the minimum number of output items this block can + * produce during a call to work. + * + * Should be 0 for most blocks. Useful if we're dealing with + * packets and the block produces one packet per call to work. + */ + int min_noutput_items() const { return d_min_noutput_items; } + + /*! + * \brief Set the minimum number of output items this block can + * produce during a call to work. + * + * \param m the minimum noutput_items this block can produce. + */ + void set_min_noutput_items(int m) { d_min_noutput_items = m; } + + /*! + * \brief Return the maximum number of output items this block will + * handle during a call to work. + */ + int max_noutput_items(); + + /*! + * \brief Set the maximum number of output items this block will + * handle during a call to work. + * + * \param m the maximum noutput_items this block will handle. + */ + void set_max_noutput_items(int m); + + /*! + * \brief Clear the switch for using the max_noutput_items value of this block. + * + * When is_set_max_noutput_items() returns 'true', the scheduler + * will use the value returned by max_noutput_items() to limit the + * size of the number of items possible for this block's work + * function. If is_set_max_notput_items() returns 'false', then + * the scheduler ignores the internal value and uses the value set + * globally in the top_block. + * + * Use this value to clear the 'is_set' flag so the scheduler will + * ignore this. Use the set_max_noutput_items(m) call to both set + * a new value for max_noutput_items and to reenable its use in + * the scheduler. + */ + void unset_max_noutput_items(); + + /*! + * \brief Ask the block if the flag is or is not set to use the + * internal value of max_noutput_items during a call to work. + */ + bool is_set_max_noutput_items(); + + /* + * Used to expand the vectors that hold the min/max buffer sizes. + * + * Specifically, when -1 is used, the vectors are just initialized + * with 1 value; this is used by the flat_flowgraph to expand when + * required to add a new value for new ports on these blocks. + */ + void expand_minmax_buffer(int port); + + /*! + * \brief Returns max buffer size on output port \p i. + */ + long max_output_buffer(size_t i); + + /*! + * \brief Sets max buffer size on all output ports. + */ + void set_max_output_buffer(long max_output_buffer); + + /*! + * \brief Sets max buffer size on output port \p port. + */ + void set_max_output_buffer(int port, long max_output_buffer); + + /*! + * \brief Returns min buffer size on output port \p i. + */ + long min_output_buffer(size_t i); + + /*! + * \brief Sets min buffer size on all output ports. + */ + void set_min_output_buffer(long min_output_buffer); + + /*! + * \brief Sets min buffer size on output port \p port. + */ + void set_min_output_buffer(int port, long min_output_buffer); + + // --------------- Performance counter functions ------------- + + /*! + * \brief Gets instantaneous noutput_items performance counter. + */ + float pc_noutput_items(); + + /*! + * \brief Gets average noutput_items performance counter. + */ + float pc_noutput_items_avg(); + + /*! + * \brief Gets variance of noutput_items performance counter. + */ + float pc_noutput_items_var(); + + /*! + * \brief Gets instantaneous num items produced performance counter. + */ + float pc_nproduced(); + + /*! + * \brief Gets average num items produced performance counter. + */ + float pc_nproduced_avg(); + + /*! + * \brief Gets variance of num items produced performance counter. + */ + float pc_nproduced_var(); + + /*! + * \brief Gets instantaneous fullness of \p which input buffer. + */ + float pc_input_buffers_full(int which); + + /*! + * \brief Gets average fullness of \p which input buffer. + */ + float pc_input_buffers_full_avg(int which); + + /*! + * \brief Gets variance of fullness of \p which input buffer. + */ + float pc_input_buffers_full_var(int which); + + /*! + * \brief Gets instantaneous fullness of all input buffers. + */ + std::vector pc_input_buffers_full(); + + /*! + * \brief Gets average fullness of all input buffers. + */ + std::vector pc_input_buffers_full_avg(); + + /*! + * \brief Gets variance of fullness of all input buffers. + */ + std::vector pc_input_buffers_full_var(); + + /*! + * \brief Gets instantaneous fullness of \p which input buffer. + */ + float pc_output_buffers_full(int which); + + /*! + * \brief Gets average fullness of \p which input buffer. + */ + float pc_output_buffers_full_avg(int which); + + /*! + * \brief Gets variance of fullness of \p which input buffer. + */ + float pc_output_buffers_full_var(int which); + + /*! + * \brief Gets instantaneous fullness of all output buffers. + */ + std::vector pc_output_buffers_full(); + + /*! + * \brief Gets average fullness of all output buffers. + */ + std::vector pc_output_buffers_full_avg(); + + /*! + * \brief Gets variance of fullness of all output buffers. + */ + std::vector pc_output_buffers_full_var(); + + /*! + * \brief Gets instantaneous clock cycles spent in work. + */ + float pc_work_time(); + + /*! + * \brief Gets average clock cycles spent in work. + */ + float pc_work_time_avg(); + + /*! + * \brief Gets average clock cycles spent in work. + */ + float pc_work_time_var(); + + /*! + * \brief Resets the performance counters + */ + void reset_perf_counters(); + + /*! + * \brief Sets up export of perf. counters to ControlPort. Only + * called by the scheduler. + */ + void setup_pc_rpc(); + + /*! + * \brief Checks if this block is already exporting perf. counters + * to ControlPort. + */ + bool is_pc_rpc_set() { return d_pc_rpc_set; } + + /*! + * \brief If the block calls this in its constructor, it's + * perf. counters will not be exported. + */ + void no_pc_rpc() { d_pc_rpc_set = true; } + + + // ---------------------------------------------------------------------------- + // Functions to handle thread affinity + + /*! + * \brief Set the thread's affinity to processor core \p n. + * + * \param mask a vector of ints of the core numbers available to this block. + */ + void set_processor_affinity(const std::vector &mask); + + /*! + * \brief Remove processor affinity to a specific core. + */ + void unset_processor_affinity(); + + /*! + * \brief Get the current processor affinity. + */ + std::vector processor_affinity() { return d_affinity; } + + /*! + * \brief Get the current thread priority in use + */ + int active_thread_priority(); + + /*! + * \brief Get the current thread priority stored + */ + int thread_priority(); + + /*! + * \brief Set the current thread priority + */ + int set_thread_priority(int priority); + + // ---------------------------------------------------------------------------- + + private: + int d_output_multiple; + bool d_output_multiple_set; + int d_unaligned; + bool d_is_unaligned; + double d_relative_rate; // approx output_rate / input_rate + block_detail_sptr d_detail; // implementation details + unsigned d_history; + bool d_fixed_rate; + bool d_max_noutput_items_set; // if d_max_noutput_items is valid + int d_max_noutput_items; // value of max_noutput_items for this block + int d_min_noutput_items; + std::vector d_affinity; // thread affinity proc. mask + int d_priority; // thread priority level + bool d_pc_rpc_set; + + protected: + block(void) {} // allows pure virtual interface sub-classes + block(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature); + + void set_fixed_rate(bool fixed_rate) { d_fixed_rate = fixed_rate; } + + std::vector d_max_output_buffer; + std::vector d_min_output_buffer; + + /*! Used by block's setters and work functions to make + * setting/resetting of parameters thread-safe. + * + * Used by calling gr::thread::scoped_lock l(d_setlock); + */ + gr::thread::mutex d_setlock; + + /*! Used by blocks to access the logger system. + */ + gr::logger_ptr d_logger; + gr::logger_ptr d_debug_logger; + + // These are really only for internal use, but leaving them public avoids + // having to work up an ever-varying list of friend GR_RUNTIME_APIs + + public: + block_detail_sptr detail() const { return d_detail; } + void set_detail(block_detail_sptr detail) { d_detail = detail; } + }; + + typedef std::vector block_vector_t; + typedef std::vector::iterator block_viter_t; + + inline block_sptr cast_to_block_sptr(basic_block_sptr p) + { + return boost::dynamic_pointer_cast(p); + } + + std::ostream& + operator << (std::ostream& os, const block *m); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_BLOCK_H */ diff --git a/include/osmosdr/runtime/gnuradio/block_detail.h b/include/osmosdr/runtime/gnuradio/block_detail.h new file mode 100644 index 0000000..229682b --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/block_detail.h @@ -0,0 +1,207 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2009,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more detail. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H +#define INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H + +#include +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief Implementation details to support the signal processing abstraction + * \ingroup internal + * + * This class contains implementation detail that should be "out of + * sight" of almost all users of GNU Radio. This decoupling also + * means that we can make changes to the guts without having to + * recompile everything. + */ + class GR_RUNTIME_API block_detail + { + public: + ~block_detail(); + + int ninputs() const { return d_ninputs; } + int noutputs() const { return d_noutputs; } + bool sink_p() const { return d_noutputs == 0; } + bool source_p() const { return d_ninputs == 0; } + + void set_done(bool done); + bool done() const { return d_done; } + + void set_input(unsigned int which, buffer_reader_sptr reader); + buffer_reader_sptr input(unsigned int which) + { + if(which >= d_ninputs) + throw std::invalid_argument("block_detail::input"); + return d_input[which]; + } + + void set_output(unsigned int which, buffer_sptr buffer); + buffer_sptr output(unsigned int which) + { + if(which >= d_noutputs) + throw std::invalid_argument("block_detail::output"); + return d_output[which]; + } + + /*! + * \brief Tell the scheduler \p how_many_items of input stream \p + * which_input were consumed. + */ + void consume(int which_input, int how_many_items); + + /*! + * \brief Tell the scheduler \p how_many_items were consumed on + * each input stream. + */ + void consume_each(int how_many_items); + + /*! + * \brief Tell the scheduler \p how_many_items were produced on + * output stream \p which_output. + */ + void produce(int which_output, int how_many_items); + + /*! + * \brief Tell the scheduler \p how_many_items were produced on + * each output stream. + */ + void produce_each(int how_many_items); + + // Return the number of items read on input stream which_input + uint64_t nitems_read(unsigned int which_input); + + // Return the number of items written on output stream which_output + uint64_t nitems_written(unsigned int which_output); + + /*! + * \brief Set core affinity of block to the cores in the vector + * mask. + * + * \param mask a vector of ints of the core numbers available to + * this block. + */ + void set_processor_affinity(const std::vector &mask); + + /*! + * \brief Unset core affinity. + */ + void unset_processor_affinity(); + + /*! + * \brief Get the current thread priority + */ + int thread_priority(); + + /*! + * \brief Set the current thread priority + * + * \param priority the new thread priority to set + */ + int set_thread_priority(int priority); + + bool threaded; // set if thread is currently running. + gr::thread::gr_thread_t thread; // portable thread handle + + void start_perf_counters(); + void stop_perf_counters(int noutput_items, int nproduced); + void reset_perf_counters(); + + // Calls to get performance counter items + float pc_noutput_items(); + float pc_nproduced(); + float pc_input_buffers_full(size_t which); + std::vector pc_input_buffers_full(); + float pc_output_buffers_full(size_t which); + std::vector pc_output_buffers_full(); + float pc_work_time(); + + float pc_noutput_items_avg(); + float pc_nproduced_avg(); + float pc_input_buffers_full_avg(size_t which); + std::vector pc_input_buffers_full_avg(); + float pc_output_buffers_full_avg(size_t which); + std::vector pc_output_buffers_full_avg(); + float pc_work_time_avg(); + + float pc_noutput_items_var(); + float pc_nproduced_var(); + float pc_input_buffers_full_var(size_t which); + std::vector pc_input_buffers_full_var(); + float pc_output_buffers_full_var(size_t which); + std::vector pc_output_buffers_full_var(); + float pc_work_time_var(); + + tpb_detail d_tpb; // used by thread-per-block scheduler + int d_produce_or; + + // ---------------------------------------------------------------------------- + + private: + unsigned int d_ninputs; + unsigned int d_noutputs; + std::vector d_input; + std::vector d_output; + bool d_done; + + // Performance counters + float d_ins_noutput_items; + float d_avg_noutput_items; + float d_var_noutput_items; + float d_ins_nproduced; + float d_avg_nproduced; + float d_var_nproduced; + std::vector d_ins_input_buffers_full; + std::vector d_avg_input_buffers_full; + std::vector d_var_input_buffers_full; + std::vector d_ins_output_buffers_full; + std::vector d_avg_output_buffers_full; + std::vector d_var_output_buffers_full; + gr::high_res_timer_type d_start_of_work, d_end_of_work; + float d_ins_work_time; + float d_avg_work_time; + float d_var_work_time; + float d_pc_counter; + + block_detail(unsigned int ninputs, unsigned int noutputs); + + friend struct tpb_detail; + + friend GR_RUNTIME_API block_detail_sptr + make_block_detail(unsigned int ninputs, unsigned int noutputs); + }; + + GR_RUNTIME_API block_detail_sptr + make_block_detail(unsigned int ninputs, unsigned int noutputs); + + GR_RUNTIME_API long + block_detail_ncurrently_allocated(); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_BLOCK_DETAIL_H */ diff --git a/include/osmosdr/runtime/gnuradio/block_gateway.h b/include/osmosdr/runtime/gnuradio/block_gateway.h new file mode 100644 index 0000000..461fac1 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/block_gateway.h @@ -0,0 +1,178 @@ +/* -*- c++ -*- */ +/* + * Copyright 2011-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_RUNTIME_BLOCK_GATEWAY_H +#define INCLUDED_RUNTIME_BLOCK_GATEWAY_H + +#include +#include +#include + +namespace gr { + + /*! + * The work type enum tells the gateway what kind of block to + * implement. The choices are familiar gnuradio block overloads + * (sync, decim, interp). + */ + enum block_gw_work_type { + GR_BLOCK_GW_WORK_GENERAL, + GR_BLOCK_GW_WORK_SYNC, + GR_BLOCK_GW_WORK_DECIM, + GR_BLOCK_GW_WORK_INTERP, + }; + + /*! + * Shared message structure between python and gateway. + * Each action type represents a scheduler-called function. + */ + struct block_gw_message_type { + enum action_type { + ACTION_GENERAL_WORK, //dispatch work + ACTION_WORK, //dispatch work + ACTION_FORECAST, //dispatch forecast + ACTION_START, //dispatch start + ACTION_STOP, //dispatch stop + }; + + action_type action; + + int general_work_args_noutput_items; + std::vector general_work_args_ninput_items; + std::vector general_work_args_input_items; //TODO this should be const void*, but swig cant int cast it right + std::vector general_work_args_output_items; + int general_work_args_return_value; + + int work_args_ninput_items; + int work_args_noutput_items; + std::vector work_args_input_items; //TODO this should be const void*, but swig cant int cast it right + std::vector work_args_output_items; + int work_args_return_value; + + int forecast_args_noutput_items; + std::vector forecast_args_ninput_items_required; + + bool start_args_return_value; + + bool stop_args_return_value; + }; + + /*! + * The gateway block which performs all the magic. + * + * The gateway provides access to all the gr::block routines. + * The methods prefixed with gr::block__ are renamed + * to class methods without the prefix in python. + */ + class GR_RUNTIME_API block_gateway : virtual public gr::block + { + public: + // gr::block_gateway::sptr + typedef boost::shared_ptr sptr; + + /*! + * Make a new gateway block. + * \param handler the swig director object with callback + * \param name the name of the block (Ex: "Shirley") + * \param in_sig the input signature for this block + * \param out_sig the output signature for this block + * \param work_type the type of block overload to implement + * \param factor the decimation or interpolation factor + * \return a new gateway block + */ + static sptr make(gr::feval_ll *handler, + const std::string &name, + gr::io_signature::sptr in_sig, + gr::io_signature::sptr out_sig, + const block_gw_work_type work_type, + const unsigned factor); + + //! Provide access to the shared message object + virtual block_gw_message_type &block_message(void) = 0; + + long block__unique_id(void) const { + return gr::block::unique_id(); + } + + std::string block__name(void) const { + return gr::block::name(); + } + + unsigned block__history(void) const { + return gr::block::history(); + } + + void block__set_history(unsigned history) { + return gr::block::set_history(history); + } + + void block__set_fixed_rate(bool fixed_rate) { + return gr::block::set_fixed_rate(fixed_rate); + } + + bool block__fixed_rate(void) const { + return gr::block::fixed_rate(); + } + + void block__set_output_multiple(int multiple) { + return gr::block::set_output_multiple(multiple); + } + + int block__output_multiple(void) const { + return gr::block::output_multiple(); + } + + void block__consume(int which_input, int how_many_items) { + return gr::block::consume(which_input, how_many_items); + } + + void block__consume_each(int how_many_items) { + return gr::block::consume_each(how_many_items); + } + + void block__produce(int which_output, int how_many_items) { + return gr::block::produce(which_output, how_many_items); + } + + void block__set_relative_rate(double relative_rate) { + return gr::block::set_relative_rate(relative_rate); + } + + double block__relative_rate(void) const { + return gr::block::relative_rate(); + } + + uint64_t block__nitems_read(unsigned int which_input) { + return gr::block::nitems_read(which_input); + } + + uint64_t block__nitems_written(unsigned int which_output) { + return gr::block::nitems_written(which_output); + } + + protected: + + }; + +} /* namespace gr */ + +#endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */ diff --git a/include/osmosdr/runtime/gnuradio/block_registry.h b/include/osmosdr/runtime/gnuradio/block_registry.h new file mode 100644 index 0000000..6f2397f --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/block_registry.h @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_RUNTIME_BLOCK_REGISTRY_H +#define GR_RUNTIME_BLOCK_REGISTRY_H + +#include +#include +#include + +namespace gr { + +#ifndef GR_BASIC_BLOCK_H + class basic_block; + class block; +#endif + + class GR_RUNTIME_API block_registry + { + public: + block_registry(); + + long block_register(basic_block* block); + void block_unregister(basic_block* block); + + std::string register_symbolic_name(basic_block* block); + void register_symbolic_name(basic_block* block, std::string name); + + void register_primitive(std::string blk, gr::block* ref); + void unregister_primitive(std::string blk); + void notify_blk(std::string blk); + + private: + //typedef std::map< long, basic_block_sptr > blocksubmap_t; + typedef std::map< long, basic_block* > blocksubmap_t; + typedef std::map< std::string, blocksubmap_t > blockmap_t; + + blockmap_t d_map; + std::map< std::string, block*> primitive_map; + gr::thread::mutex d_mutex; + }; + +} /* namespace gr */ + +GR_RUNTIME_API extern gr::block_registry global_block_registry; + +#endif /* GR_RUNTIME_BLOCK_REGISTRY_H */ diff --git a/include/osmosdr/runtime/gnuradio/blocks/api.h b/include/osmosdr/runtime/gnuradio/blocks/api.h new file mode 100644 index 0000000..fd4e605 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/blocks/api.h @@ -0,0 +1,33 @@ +/* + * Copyright 2012 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_BLOCKS_API_H +#define INCLUDED_BLOCKS_API_H + +#include + +#ifdef gnuradio_blocks_EXPORTS +# define BLOCKS_API __GR_ATTR_EXPORT +#else +# define BLOCKS_API __GR_ATTR_IMPORT +#endif + +#endif /* INCLUDED_BLOCKS_API_H */ diff --git a/include/osmosdr/runtime/gnuradio/blocks/null_sink.h b/include/osmosdr/runtime/gnuradio/blocks/null_sink.h new file mode 100644 index 0000000..cbce9b8 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/blocks/null_sink.h @@ -0,0 +1,55 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_NULL_SINK_H +#define INCLUDED_GR_NULL_SINK_H + +#include +#include +#include // size_t + +namespace gr { + namespace blocks { + + /*! + * \brief Bit bucket. Use as a termination point when a sink is + * required and we don't want to do anything real. + * \ingroup misc_blk + */ + class BLOCKS_API null_sink : virtual public sync_block + { + public: + // gr::blocks::null_sink::sptr + typedef boost::shared_ptr sptr; + + /*! + * Build a null sink block. + * + * \param sizeof_stream_item size of the stream items in bytes. + */ + static sptr make(size_t sizeof_stream_item); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_NULL_SINK_H */ diff --git a/include/osmosdr/runtime/gnuradio/blocks/null_source.h b/include/osmosdr/runtime/gnuradio/blocks/null_source.h new file mode 100644 index 0000000..eccebe8 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/blocks/null_source.h @@ -0,0 +1,53 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_NULL_SOURCE_H +#define INCLUDED_GR_NULL_SOURCE_H + +#include +#include + +namespace gr { + namespace blocks { + + /*! + * \brief A source of zeros used mainly for testing. + * \ingroup misc_blk + */ + class BLOCKS_API null_source : virtual public sync_block + { + public: + // gr::blocks::null_source::sptr + typedef boost::shared_ptr sptr; + + /*! + * Build a null source block. + * + * \param sizeof_stream_item size of the stream items in bytes. + */ + static sptr make(size_t sizeof_stream_item); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_NULL_SOURCE_H */ diff --git a/include/osmosdr/runtime/gnuradio/blocks/throttle.h b/include/osmosdr/runtime/gnuradio/blocks/throttle.h new file mode 100644 index 0000000..662582e --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/blocks/throttle.h @@ -0,0 +1,63 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_THROTTLE_H +#define INCLUDED_GR_THROTTLE_H + +#include +#include + +namespace gr { + namespace blocks { + + /*! + * \brief throttle flow of samples such that the average rate does + * not exceed samples_per_sec. + * \ingroup misc_blk + * + * \details + * input: one stream of itemsize; output: one stream of itemsize + * + * N.B. this should only be used in GUI apps where there is no + * other rate limiting block. It is not intended nor effective at + * precisely controlling the rate of samples. That should be + * controlled by a source or sink tied to sample clock. E.g., a + * USRP or audio card. + */ + class BLOCKS_API throttle : virtual public sync_block + { + public: + typedef boost::shared_ptr sptr; + + static sptr make(size_t itemsize, double samples_per_sec); + + //! Sets the sample rate in samples per second. + virtual void set_sample_rate(double rate) = 0; + + //! Get the sample rate in samples per second. + virtual double sample_rate() const = 0; + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_THROTTLE_H */ diff --git a/include/osmosdr/runtime/gnuradio/buffer.h b/include/osmosdr/runtime/gnuradio/buffer.h new file mode 100644 index 0000000..819c2fa --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/buffer.h @@ -0,0 +1,258 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2009-2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_BUFFER_H +#define INCLUDED_GR_RUNTIME_BUFFER_H + +#include +#include +#include +#include +#include + +namespace gr { + + class vmcircbuf; + + /*! + * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item. + * + * The total size of the buffer will be rounded up to a system + * dependent boundary. This is typically the system page size, but + * under MS windows is 64KB. + * + * \param nitems is the minimum number of items the buffer will hold. + * \param sizeof_item is the size of an item in bytes. + * \param link is the block that writes to this buffer. + */ + GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, + block_sptr link=block_sptr()); + + /*! + * \brief Single writer, multiple reader fifo. + * \ingroup internal + */ + class GR_RUNTIME_API buffer + { + public: + virtual ~buffer(); + + /*! + * \brief return number of items worth of space available for writing + */ + int space_available(); + + /*! + * \brief return size of this buffer in items + */ + int bufsize() const { return d_bufsize; } + + /*! + * \brief return pointer to write buffer. + * + * The return value points at space that can hold at least + * space_available() items. + */ + void *write_pointer(); + + /*! + * \brief tell buffer that we wrote \p nitems into it + */ + void update_write_pointer(int nitems); + + void set_done(bool done); + bool done() const { return d_done; } + + /*! + * \brief Return the block that writes to this buffer. + */ + block_sptr link() { return block_sptr(d_link); } + + size_t nreaders() const { return d_readers.size(); } + buffer_reader* reader(size_t index) { return d_readers[index]; } + + gr::thread::mutex *mutex() { return &d_mutex; } + + uint64_t nitems_written() { return d_abs_write_offset; } + + size_t get_sizeof_item() { return d_sizeof_item; } + + // ------------------------------------------------------------------------- + + private: + friend class buffer_reader; + friend GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, block_sptr link); + friend GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link); + + protected: + char *d_base; // base address of buffer + unsigned int d_bufsize; // in items + private: + gr::vmcircbuf *d_vmcircbuf; + size_t d_sizeof_item; // in bytes + std::vector d_readers; + boost::weak_ptr d_link; // block that writes to this buffer + + // + // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags + // and the d_read_index's and d_abs_read_offset's in the buffer readers. + // + gr::thread::mutex d_mutex; + unsigned int d_write_index; // in items [0,d_bufsize) + uint64_t d_abs_write_offset; // num items written since the start + bool d_done; + uint64_t d_last_min_items_read; + + unsigned index_add(unsigned a, unsigned b) + { + unsigned s = a + b; + + if(s >= d_bufsize) + s -= d_bufsize; + + assert(s < d_bufsize); + return s; + } + + unsigned index_sub(unsigned a, unsigned b) + { + int s = a - b; + + if(s < 0) + s += d_bufsize; + + assert((unsigned) s < d_bufsize); + return s; + } + + virtual bool allocate_buffer(int nitems, size_t sizeof_item); + + /*! + * \brief constructor is private. Use gr_make_buffer to create instances. + * + * Allocate a buffer that holds at least \p nitems of size \p sizeof_item. + * + * \param nitems is the minimum number of items the buffer will hold. + * \param sizeof_item is the size of an item in bytes. + * \param link is the block that writes to this buffer. + * + * The total size of the buffer will be rounded up to a system + * dependent boundary. This is typically the system page size, but + * under MS windows is 64KB. + */ + buffer(int nitems, size_t sizeof_item, block_sptr link); + + /*! + * \brief disassociate \p reader from this buffer + */ + void drop_reader(buffer_reader *reader); + }; + + /*! + * \brief Create a new gr::buffer_reader and attach it to buffer \p buf + * \param buf is the buffer the \p gr::buffer_reader reads from. + * \param nzero_preload -- number of zero items to "preload" into buffer. + * \param link is the block that reads from the buffer using this gr::buffer_reader. + */ + GR_RUNTIME_API buffer_reader_sptr + buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link=block_sptr()); + + //! returns # of buffers currently allocated + GR_RUNTIME_API long buffer_ncurrently_allocated(); + + + // --------------------------------------------------------------------------- + + /*! + * \brief How we keep track of the readers of a gr::buffer. + * \ingroup internal + */ + class GR_RUNTIME_API buffer_reader + { + public: + ~buffer_reader(); + + /*! + * \brief Return number of items available for reading. + */ + int items_available() const; + + /*! + * \brief Return buffer this reader reads from. + */ + buffer_sptr buffer() const { return d_buffer; } + + /*! + * \brief Return maximum number of items that could ever be available for reading. + * This is used as a sanity check in the scheduler to avoid looping forever. + */ + int max_possible_items_available() const { return d_buffer->d_bufsize - 1; } + + /*! + * \brief return pointer to read buffer. + * + * The return value points to items_available() number of items + */ + const void *read_pointer(); + + /* + * \brief tell buffer we read \p items from it + */ + void update_read_pointer(int nitems); + + void set_done(bool done) { d_buffer->set_done(done); } + bool done() const { return d_buffer->done(); } + + gr::thread::mutex *mutex() { return d_buffer->mutex(); } + + uint64_t nitems_read() { return d_abs_read_offset; } + + size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); } + + /*! + * \brief Return the block that reads via this reader. + * + */ + block_sptr link() { return block_sptr(d_link); } + + // ------------------------------------------------------------------------- + + private: + friend class buffer; + friend GR_RUNTIME_API buffer_reader_sptr + buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link); + + buffer_sptr d_buffer; + unsigned int d_read_index; // in items [0,d->buffer.d_bufsize) + uint64_t d_abs_read_offset; // num items seen since the start + boost::weak_ptr d_link; // block that reads via this buffer reader + + //! constructor is private. Use gr::buffer::add_reader to create instances + buffer_reader(buffer_sptr buffer, unsigned int read_index, block_sptr link); + }; + + //! returns # of buffer_readers currently allocated + GR_RUNTIME_API long buffer_reader_ncurrently_allocated (); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_BUFFER_H */ diff --git a/include/osmosdr/runtime/gnuradio/constants.h b/include/osmosdr/runtime/gnuradio/constants.h new file mode 100644 index 0000000..beba6f2 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/constants.h @@ -0,0 +1,58 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_CONSTANTS_H +#define INCLUDED_GR_CONSTANTS_H + +#include +#include + +namespace gr { + + /*! + * \brief return SYSCONFDIR. Typically ${CMAKE_INSTALL_PREFIX}/etc or /etc + */ + GR_RUNTIME_API const std::string prefix(); + + /*! + * \brief return SYSCONFDIR. Typically ${CMAKE_INSTALL_PREFIX}/etc or /etc + */ + GR_RUNTIME_API const std::string sysconfdir(); + + /*! + * \brief return preferences file directory. Typically ${SYSCONFDIR}/etc/conf.d + */ + GR_RUNTIME_API const std::string prefsdir(); + + /*! + * \brief return date/time of build, as set when 'cmake' is run + */ + GR_RUNTIME_API const std::string build_date(); + + /*! + * \brief return version string defined by cmake (GrVersion.cmake) + */ + GR_RUNTIME_API const std::string version(); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_CONSTANTS_H */ diff --git a/include/osmosdr/runtime/gnuradio/endianness.h b/include/osmosdr/runtime/gnuradio/endianness.h new file mode 100644 index 0000000..43c21a1 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/endianness.h @@ -0,0 +1,32 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_ENDIANNESS_H +#define INCLUDED_GR_ENDIANNESS_H + +namespace gr { + + typedef enum {GR_MSB_FIRST, GR_LSB_FIRST} endianness_t; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_ENDIANNESS_H */ diff --git a/include/osmosdr/runtime/gnuradio/expj.h b/include/osmosdr/runtime/gnuradio/expj.h new file mode 100644 index 0000000..7556e17 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/expj.h @@ -0,0 +1,38 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_GR_EXPJ_H +#define INCLUDED_GR_EXPJ_H + +#include +#include +#include + +static inline gr_complex +gr_expj(float phase) +{ + float t_imag, t_real; + gr::sincosf(phase, &t_imag, &t_real); + return gr_complex(t_real, t_imag); +} + + +#endif /* INCLUDED_GR_EXPJ_H */ diff --git a/include/osmosdr/runtime/gnuradio/feval.h b/include/osmosdr/runtime/gnuradio/feval.h new file mode 100644 index 0000000..5b3bf9f --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/feval.h @@ -0,0 +1,186 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_FEVAL_H +#define INCLUDED_GR_FEVAL_H + +#include +#include + +namespace gr { + + /*! + * \brief base class for evaluating a function: double -> double + * \ingroup misc + * + * This class is designed to be subclassed in Python or C++ and is + * callable from both places. It uses SWIG's "director" feature to + * implement the magic. + * + * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. + */ + class GR_RUNTIME_API feval_dd + { + protected: + /*! + * \brief override this to define the function + */ + virtual double eval(double x); + + public: + feval_dd() {} + virtual ~feval_dd(); + + virtual double calleval(double x); // invoke "eval" + }; + + /*! + * \brief base class for evaluating a function: complex -> complex + * \ingroup misc + * + * This class is designed to be subclassed in Python or C++ and is + * callable from both places. It uses SWIG's "director" feature to + * implement the magic. + * + * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. + */ + class GR_RUNTIME_API feval_cc + { + protected: + /*! + * \brief override this to define the function + */ + virtual gr_complex eval(gr_complex x); + + public: + feval_cc() {} + virtual ~feval_cc(); + + virtual gr_complex calleval(gr_complex x); // invoke "eval" + }; + + /*! + * \brief base class for evaluating a function: long -> long + * \ingroup misc + * + * This class is designed to be subclassed in Python or C++ and is + * callable from both places. It uses SWIG's "director" feature to + * implement the magic. + * + * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. + */ + class GR_RUNTIME_API feval_ll + { + protected: + /*! + * \brief override this to define the function + */ + virtual long eval(long x); + + public: + feval_ll() {} + virtual ~feval_ll(); + + virtual long calleval(long x); // invoke "eval" + }; + + /*! + * \brief base class for evaluating a function: void -> void + * \ingroup misc + * + * This class is designed to be subclassed in Python or C++ and is + * callable from both places. It uses SWIG's "director" feature to + * implement the magic. + * + * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. + */ + class GR_RUNTIME_API feval + { + protected: + /*! + * \brief override this to define the function + */ + virtual void eval(); + + public: + feval() {} + virtual ~feval(); + + virtual void calleval(); // invoke "eval" + }; + + /*! + * \brief base class for evaluating a function: pmt -> void + * \ingroup misc + * + * This class is designed to be subclassed in Python or C++ and is + * callable from both places. It uses SWIG's "director" feature to + * implement the magic. + * + * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. + */ + class GR_RUNTIME_API feval_p + { + protected: + /*! + * \brief override this to define the function + */ + virtual void eval(double); + + public: + feval_p() {} + virtual ~feval_p(); + + virtual void calleval(double); // invoke "eval" + }; + + /*! + * \brief trivial examples / test cases showing C++ calling Python code + */ + GR_RUNTIME_API double feval_dd_example(feval_dd *f, double x); + GR_RUNTIME_API gr_complex feval_cc_example(feval_cc *f, gr_complex x); + GR_RUNTIME_API long feval_ll_example(feval_ll *f, long x); + GR_RUNTIME_API void feval_example(feval *f); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_FEVAL_H */ diff --git a/include/osmosdr/runtime/gnuradio/flowgraph.h b/include/osmosdr/runtime/gnuradio/flowgraph.h new file mode 100644 index 0000000..d1dc67b --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/flowgraph.h @@ -0,0 +1,194 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2007,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_FLOWGRAPH_H +#define INCLUDED_GR_RUNTIME_FLOWGRAPH_H + +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief Class representing a specific input or output graph endpoint + * \ingroup internal + */ + class GR_RUNTIME_API endpoint + { + private: + basic_block_sptr d_basic_block; + int d_port; + + public: + endpoint() : d_basic_block(), d_port(0) { } + endpoint(basic_block_sptr block, int port) { d_basic_block = block; d_port = port; } + basic_block_sptr block() const { return d_basic_block; } + int port() const { return d_port; } + + bool operator==(const endpoint &other) const; + }; + + inline bool endpoint::operator==(const endpoint &other) const + { + return (d_basic_block == other.d_basic_block && + d_port == other.d_port); + } + + // Hold vectors of gr::endpoint objects + typedef std::vector endpoint_vector_t; + typedef std::vector::iterator endpoint_viter_t; + + /*! + *\brief Class representing a connection between to graph endpoints + */ + class GR_RUNTIME_API edge + { + public: + edge() : d_src(), d_dst() { }; + edge(const endpoint &src, const endpoint &dst) + : d_src(src), d_dst(dst) { } + ~edge(); + + const endpoint &src() const { return d_src; } + const endpoint &dst() const { return d_dst; } + + private: + endpoint d_src; + endpoint d_dst; + }; + + // Hold vectors of gr::edge objects + typedef std::vector edge_vector_t; + typedef std::vector::iterator edge_viter_t; + + // Create a shared pointer to a heap allocated flowgraph + // (types defined in runtime_types.h) + GR_RUNTIME_API flowgraph_sptr make_flowgraph(); + + /*! + * \brief Class representing a directed, acyclic graph of basic blocks + * \ingroup internal + */ + class GR_RUNTIME_API flowgraph + { + public: + friend GR_RUNTIME_API flowgraph_sptr make_flowgraph(); + + // Destruct an arbitrary flowgraph + ~flowgraph(); + + // Connect two endpoints + void connect(const endpoint &src, const endpoint &dst); + + // Disconnect two endpoints + void disconnect(const endpoint &src, const endpoint &dst); + + // Connect an output port to an input port (convenience) + void connect(basic_block_sptr src_block, int src_port, + basic_block_sptr dst_block, int dst_port); + + // Disconnect an input port from an output port (convenience) + void disconnect(basic_block_sptr src_block, int src_port, + basic_block_sptr dst_block, int dst_port); + + // Validate connectivity, raise exception if invalid + void validate(); + + // Clear existing flowgraph + void clear(); + + // Return vector of edges + const edge_vector_t &edges() const { return d_edges; } + + // Return vector of connected blocks + basic_block_vector_t calc_used_blocks(); + + // Return toplogically sorted vector of blocks. All the sources come first. + basic_block_vector_t topological_sort(basic_block_vector_t &blocks); + + // Return vector of vectors of disjointly connected blocks, + // topologically sorted. + std::vector partition(); + + protected: + basic_block_vector_t d_blocks; + edge_vector_t d_edges; + + flowgraph(); + std::vector calc_used_ports(basic_block_sptr block, bool check_inputs); + basic_block_vector_t calc_downstream_blocks(basic_block_sptr block, int port); + edge_vector_t calc_upstream_edges(basic_block_sptr block); + bool has_block_p(basic_block_sptr block); + edge calc_upstream_edge(basic_block_sptr block, int port); + + private: + void check_valid_port(gr::io_signature::sptr sig, int port); + void check_dst_not_used(const endpoint &dst); + void check_type_match(const endpoint &src, const endpoint &dst); + edge_vector_t calc_connections(basic_block_sptr block, bool check_inputs); // false=use outputs + void check_contiguity(basic_block_sptr block, const std::vector &used_ports, bool check_inputs); + + basic_block_vector_t calc_downstream_blocks(basic_block_sptr block); + basic_block_vector_t calc_reachable_blocks(basic_block_sptr block, basic_block_vector_t &blocks); + void reachable_dfs_visit(basic_block_sptr block, basic_block_vector_t &blocks); + basic_block_vector_t calc_adjacent_blocks(basic_block_sptr block, basic_block_vector_t &blocks); + basic_block_vector_t sort_sources_first(basic_block_vector_t &blocks); + bool source_p(basic_block_sptr block); + void topological_dfs_visit(basic_block_sptr block, basic_block_vector_t &output); + }; + + // Convenience functions + inline + void flowgraph::connect(basic_block_sptr src_block, int src_port, + basic_block_sptr dst_block, int dst_port) + { + connect(endpoint(src_block, src_port), + endpoint(dst_block, dst_port)); + } + + inline + void flowgraph::disconnect(basic_block_sptr src_block, int src_port, + basic_block_sptr dst_block, int dst_port) + { + disconnect(endpoint(src_block, src_port), + endpoint(dst_block, dst_port)); + } + + inline std::ostream& + operator <<(std::ostream &os, const endpoint endp) + { + os << endp.block()->alias() << ":" << endp.port(); + return os; + } + + inline std::ostream& + operator <<(std::ostream &os, const edge edge) + { + os << edge.src() << "->" << edge.dst(); + return os; + } + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_FLOWGRAPH_H */ diff --git a/include/osmosdr/runtime/gnuradio/fxpt.h b/include/osmosdr/runtime/gnuradio/fxpt.h new file mode 100644 index 0000000..6143aca --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/fxpt.h @@ -0,0 +1,108 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_FXPT_H +#define INCLUDED_GR_FXPT_H + +#include +#include + +namespace gr { + + /*! + * \brief fixed point sine and cosine and friends. + * \ingroup misc + * + * fixed pt radians + * --------- -------- + * -2**31 -pi + * 0 0 + * 2**31-1 pi - epsilon + */ + class GR_RUNTIME_API fxpt + { + static const int WORDBITS = 32; + static const int NBITS = 10; + static const float s_sine_table[1 << NBITS][2]; + static const float PI; + static const float TWO_TO_THE_31; + + public: + static gr_int32 + float_to_fixed(float x) + { + // Fold x into -PI to PI. + int d = (int)floor(x/2/PI+0.5); + x -= d*2*PI; + // And convert to an integer. + return (gr_int32) ((float) x * TWO_TO_THE_31 / PI); + } + + static float + fixed_to_float (gr_int32 x) + { + return x * (PI / TWO_TO_THE_31); + } + + /*! + * \brief Given a fixed point angle x, return float sine (x) + */ + static float + sin(gr_int32 x) + { + gr_uint32 ux = x; + int index = ux >> (WORDBITS - NBITS); + return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1]; + } + + /* + * \brief Given a fixed point angle x, return float cosine (x) + */ + static float + cos (gr_int32 x) + { + gr_uint32 ux = x + 0x40000000; + int index = ux >> (WORDBITS - NBITS); + return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1]; + } + + /* + * \brief Given a fixedpoint angle x, return float cos(x) and sin (x) + */ + static void sincos(gr_int32 x, float *s, float *c) + { + gr_uint32 ux = x; + int sin_index = ux >> (WORDBITS - NBITS); + *s = s_sine_table[sin_index][0] * (ux >> 1) + s_sine_table[sin_index][1]; + + ux = x + 0x40000000; + int cos_index = ux >> (WORDBITS - NBITS); + *c = s_sine_table[cos_index][0] * (ux >> 1) + s_sine_table[cos_index][1]; + + return; + } + + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_FXPT_H */ diff --git a/include/osmosdr/runtime/gnuradio/fxpt_nco.h b/include/osmosdr/runtime/gnuradio/fxpt_nco.h new file mode 100644 index 0000000..33a176d --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/fxpt_nco.h @@ -0,0 +1,160 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_FXPT_NCO_H +#define INCLUDED_GR_FXPT_NCO_H + +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief Numerically Controlled Oscillator (NCO) + * \ingroup misc + */ + class /*GR_RUNTIME_API*/ fxpt_nco + { + uint32_t d_phase; + int32_t d_phase_inc; + + public: + fxpt_nco() : d_phase(0), d_phase_inc(0) {} + + ~fxpt_nco() {} + + // radians + void set_phase(float angle) { + d_phase = gr::fxpt::float_to_fixed(angle); + } + + void adjust_phase(float delta_phase) { + d_phase += gr::fxpt::float_to_fixed(delta_phase); + } + + // angle_rate is in radians / step + void set_freq(float angle_rate){ + d_phase_inc = gr::fxpt::float_to_fixed(angle_rate); + } + + // angle_rate is a delta in radians / step + void adjust_freq(float delta_angle_rate) + { + d_phase_inc += gr::fxpt::float_to_fixed(delta_angle_rate); + } + + // increment current phase angle + + void step() + { + d_phase += d_phase_inc; + } + + void step(int n) + { + d_phase += d_phase_inc * n; + } + + // units are radians / step + float get_phase() const { return gr::fxpt::fixed_to_float(d_phase); } + float get_freq() const { return gr::fxpt::fixed_to_float(d_phase_inc); } + + // compute sin and cos for current phase angle + void sincos(float *sinx, float *cosx) const + { + *sinx = gr::fxpt::sin(d_phase); + *cosx = gr::fxpt::cos(d_phase); + } + + // compute cos and sin for a block of phase angles + void sincos(gr_complex *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = gr_complex(gr::fxpt::cos(d_phase) * ampl, gr::fxpt::sin(d_phase) * ampl); + step(); + } + } + + // compute sin for a block of phase angles + void sin(float *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (float)(gr::fxpt::sin(d_phase) * ampl); + step(); + } + } + + // compute cos for a block of phase angles + void cos(float *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (float)(gr::fxpt::cos(d_phase) * ampl); + step (); + } + } + + // compute sin for a block of phase angles + void sin(short *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (short)(gr::fxpt::sin(d_phase) * ampl); + step(); + } + } + + // compute cos for a block of phase angles + void cos(short *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (short)(gr::fxpt::cos(d_phase) * ampl); + step(); + } + } + + // compute sin for a block of phase angles + void sin(int *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (int)(gr::fxpt::sin(d_phase) * ampl); + step(); + } + } + + // compute cos for a block of phase angles + void cos(int *output, int noutput_items, double ampl=1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (int)(gr::fxpt::cos(d_phase) * ampl); + step(); + } + } + + // compute cos or sin for current phase angle + float cos() const { return gr::fxpt::cos(d_phase); } + float sin() const { return gr::fxpt::sin(d_phase); } + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_FXPT_NCO_H */ diff --git a/include/osmosdr/runtime/gnuradio/fxpt_vco.h b/include/osmosdr/runtime/gnuradio/fxpt_vco.h new file mode 100644 index 0000000..77d5867 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/fxpt_vco.h @@ -0,0 +1,80 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2004,2005,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_FXPT_VCO_H +#define INCLUDED_GR_FXPT_VCO_H + +#include +#include +#include + +namespace gr { + + /*! + * \brief Voltage Controlled Oscillator (VCO) + * \ingroup misc + */ + class /*GR_RUNTIME_API*/ fxpt_vco { + gr_int32 d_phase; + + public: + fxpt_vco () : d_phase(0) {} + + ~fxpt_vco() {} + + // radians + void set_phase(float angle) { + d_phase = fxpt::float_to_fixed(angle); + } + + void adjust_phase(float delta_phase) { + d_phase += fxpt::float_to_fixed(delta_phase); + } + + float get_phase() const { + return fxpt::fixed_to_float(d_phase); + } + + // compute sin and cos for current phase angle + void sincos(float *sinx, float *cosx) const + { + *sinx = fxpt::sin(d_phase); + *cosx = fxpt::cos(d_phase); + } + + // compute a block at a time + void cos(float *output, const float *input, int noutput_items, float k, float ampl = 1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = (float)(fxpt::cos(d_phase) * ampl); + adjust_phase(input[i] * k); + } + } + + // compute cos or sin for current phase angle + float cos() const { return fxpt::cos(d_phase); } + float sin() const { return fxpt::sin(d_phase); } + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_FXPT_VCO_H */ diff --git a/include/osmosdr/runtime/gnuradio/gr_complex.h b/include/osmosdr/runtime/gnuradio/gr_complex.h new file mode 100644 index 0000000..bd94c0f --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/gr_complex.h @@ -0,0 +1,45 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_COMPLEX_H +#define INCLUDED_GR_COMPLEX_H + +#include +typedef std::complex gr_complex; +typedef std::complex gr_complexd; + +inline bool is_complex (gr_complex x) { (void) x; return true;} +inline bool is_complex (gr_complexd x) { (void) x; return true;} +inline bool is_complex (float x) { (void) x; return false;} +inline bool is_complex (double x) { (void) x; return false;} +inline bool is_complex (int x) { (void) x; return false;} +inline bool is_complex (char x) { (void) x; return false;} +inline bool is_complex (short x) { (void) x; return false;} + +// this doesn't really belong here, but there are worse places for it... + +#define CPPUNIT_ASSERT_COMPLEXES_EQUAL(expected,actual,delta) \ + CPPUNIT_ASSERT_DOUBLES_EQUAL (expected.real(), actual.real(), delta); \ + CPPUNIT_ASSERT_DOUBLES_EQUAL (expected.imag(), actual.imag(), delta); + +#endif /* INCLUDED_GR_COMPLEX_H */ + diff --git a/include/osmosdr/runtime/gnuradio/hier_block2.h b/include/osmosdr/runtime/gnuradio/hier_block2.h new file mode 100644 index 0000000..4afefeb --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/hier_block2.h @@ -0,0 +1,192 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_HIER_BLOCK2_H +#define INCLUDED_GR_RUNTIME_HIER_BLOCK2_H + +#include +#include + +namespace gr { + + /*! + * \brief public constructor for hier_block2 + */ + GR_RUNTIME_API hier_block2_sptr + make_hier_block2(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature); + + class hier_block2_detail; + + /*! + * \brief Hierarchical container class for gr::block's and gr::hier_block2's + * \ingroup container_blk + * \ingroup base_blk + */ + class GR_RUNTIME_API hier_block2 : public basic_block + { + private: + friend class hier_block2_detail; + friend GR_RUNTIME_API hier_block2_sptr + make_hier_block2(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature); + + /*! + * \brief Private implementation details of gr::hier_block2 + */ + hier_block2_detail *d_detail; + + protected: + hier_block2(void) {} // allows pure virtual interface sub-classes + hier_block2(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature); + + public: + virtual ~hier_block2(); + + /*! + * \brief typedef for object returned from self(). + * + * This type is only guaranteed to be passable to connect and + * disconnect. No other assumptions should be made about it. + */ + typedef basic_block_sptr opaque_self; + + /*! + * \brief Return an object, representing the current block, which + * can be passed to connect. + * + * The returned object may only be used as an argument to connect + * or disconnect. Any other use of self() results in unspecified + * (erroneous) behavior. + */ + opaque_self self(); + + /*! + * \brief Add a stand-alone (possibly hierarchical) block to + * internal graph + * + * This adds a gr-block or hierarchical block to the internal + * graph without wiring it to anything else. + */ + void connect(basic_block_sptr block); + + /*! + * \brief Add gr-blocks or hierarchical blocks to internal graph + * and wire together + * + * This adds (if not done earlier by another connect) a pair of + * gr-blocks or hierarchical blocks to the internal flowgraph, and + * wires the specified output port to the specified input port. + */ + void connect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port); + + /*! + * \brief Remove a gr-block or hierarchical block from the + * internal flowgraph. + * + * This removes a gr-block or hierarchical block from the internal + * flowgraph, disconnecting it from other blocks as needed. + */ + void disconnect(basic_block_sptr block); + + /*! + * \brief Disconnect a pair of gr-blocks or hierarchical blocks in + * internal flowgraph. + * + * This disconnects the specified input port from the specified + * output port of a pair of gr-blocks or hierarchical blocks. + */ + void disconnect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port); + + /*! + * \brief Disconnect all connections in the internal flowgraph. + * + * This call removes all output port to input port connections in + * the internal flowgraph. + */ + void disconnect_all(); + + /*! + * Lock a flowgraph in preparation for reconfiguration. When an + * equal number of calls to lock() and unlock() have occurred, the + * flowgraph will be reconfigured. + * + * N.B. lock() and unlock() may not be called from a flowgraph + * thread (E.g., gr::block::work method) or deadlock will occur + * when reconfiguration happens. + */ + virtual void lock(); + + /*! + * Unlock a flowgraph in preparation for reconfiguration. When an + * equal number of calls to lock() and unlock() have occurred, the + * flowgraph will be reconfigured. + * + * N.B. lock() and unlock() may not be called from a flowgraph + * thread (E.g., gr::block::work method) or deadlock will occur + * when reconfiguration happens. + */ + virtual void unlock(); + + // This is a public method for ease of code organization, but should be + // ignored by the user. + flat_flowgraph_sptr flatten() const; + + hier_block2_sptr to_hier_block2(); // Needed for Python type coercion + + /*! + * \brief Set the affinity of all blocks in hier_block2 to processor core \p n. + * + * \param mask a vector of ints of the core numbers available to this block. + */ + void set_processor_affinity(const std::vector &mask); + + /*! + * \brief Remove processor affinity for all blocks in hier_block2. + */ + void unset_processor_affinity(); + + /*! + * \brief Get the current processor affinity. + * + * \details This returns the processor affinity value for the first + * block in the hier_block2's list of blocks with the assumption + * that they have always only been set through the hier_block2's + * interface. If any block has been individually set, then this + * call could be misleading. + */ + std::vector processor_affinity(); + }; + + inline hier_block2_sptr cast_to_hier_block2_sptr(basic_block_sptr block) { + return boost::dynamic_pointer_cast(block); + } + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_HIER_BLOCK2_H */ diff --git a/include/osmosdr/runtime/gnuradio/high_res_timer.h b/include/osmosdr/runtime/gnuradio/high_res_timer.h new file mode 100644 index 0000000..ce11cd8 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/high_res_timer.h @@ -0,0 +1,156 @@ +/* -*- c++ -*- */ +/* + * Copyright 2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_GNURADIO_HIGH_RES_TIMER_H +#define INCLUDED_GNURADIO_HIGH_RES_TIMER_H + +#include + +//////////////////////////////////////////////////////////////////////// +// Use architecture defines to determine the implementation +//////////////////////////////////////////////////////////////////////// +#if defined(linux) || defined(__linux) || defined(__linux__) + #define GNURADIO_HRT_USE_CLOCK_GETTIME + #include +#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + #define GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER +#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) + #define GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME +#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) + #define GNURADIO_HRT_USE_CLOCK_GETTIME + #include +#else + #define GNURADIO_HRT_USE_MICROSEC_CLOCK +#endif + + +//////////////////////////////////////////////////////////////////////// +namespace gr { + + //! Typedef for the timer tick count + typedef signed long long high_res_timer_type; + + //! Get the current time in ticks + high_res_timer_type high_res_timer_now(void); + + //! Get the current time in ticks - for performance monitoring + high_res_timer_type high_res_timer_now_perfmon(void); + + //! Get the number of ticks per second + high_res_timer_type high_res_timer_tps(void); + + //! Get the tick count at the epoch + high_res_timer_type high_res_timer_epoch(void); + +#ifdef GNURADIO_HRT_USE_CLOCK_GETTIME + //! storage for high res timer type + GR_RUNTIME_API extern clockid_t high_res_timer_source; +#endif + +} /* namespace gr */ + +//////////////////////////////////////////////////////////////////////// +#ifdef GNURADIO_HRT_USE_CLOCK_GETTIME + inline gr::high_res_timer_type gr::high_res_timer_now(void){ + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec; + } + + inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){ + timespec ts; + clock_gettime(high_res_timer_source, &ts); + return ts.tv_sec*high_res_timer_tps() + ts.tv_nsec; + } + + inline gr::high_res_timer_type gr::high_res_timer_tps(void){ + return 1000000000UL; + } +#endif /* GNURADIO_HRT_USE_CLOCK_GETTIME */ + +//////////////////////////////////////////////////////////////////////// +#ifdef GNURADIO_HRT_USE_MACH_ABSOLUTE_TIME + #include + + inline gr::high_res_timer_type gr::high_res_timer_now(void){ + return mach_absolute_time(); + } + + inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){ + return gr::high_res_timer_now(); + } + + inline gr::high_res_timer_type gr::high_res_timer_tps(void){ + mach_timebase_info_data_t info; + mach_timebase_info(&info); + return gr::high_res_timer_type(info.numer*1000000000UL)/info.denom; + } +#endif + +//////////////////////////////////////////////////////////////////////// +#ifdef GNURADIO_HRT_USE_QUERY_PERFORMANCE_COUNTER + #include + + inline gr::high_res_timer_type gr::high_res_timer_now(void){ + LARGE_INTEGER counts; + QueryPerformanceCounter(&counts); + return counts.QuadPart; + } + + inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){ + return gr::high_res_timer_now(); + } + + inline gr::high_res_timer_type gr::high_res_timer_tps(void){ + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + return freq.QuadPart; + } +#endif + +//////////////////////////////////////////////////////////////////////// +#ifdef GNURADIO_HRT_USE_MICROSEC_CLOCK + #include + + inline gr::high_res_timer_type gr::high_res_timer_now(void){ + static const boost::posix_time::ptime epoch(boost::posix_time::from_time_t(0)); + return (boost::posix_time::microsec_clock::universal_time() - epoch).ticks(); + } + + inline gr::high_res_timer_type gr::high_res_timer_now_perfmon(void){ + return gr::high_res_timer_now(); + } + + inline gr::high_res_timer_type gr::high_res_timer_tps(void){ + return boost::posix_time::time_duration::ticks_per_second(); + } +#endif + +//////////////////////////////////////////////////////////////////////// +#include + +inline gr::high_res_timer_type gr::high_res_timer_epoch(void){ + static const double hrt_ticks_per_utc_ticks = gr::high_res_timer_tps()/double(boost::posix_time::time_duration::ticks_per_second()); + boost::posix_time::time_duration utc = boost::posix_time::microsec_clock::universal_time() - boost::posix_time::from_time_t(0); + return gr::high_res_timer_now() - utc.ticks()*hrt_ticks_per_utc_ticks; +} + +#endif /* INCLUDED_GNURADIO_HIGH_RES_TIMER_H */ diff --git a/include/osmosdr/runtime/gnuradio/io_signature.h b/include/osmosdr/runtime/gnuradio/io_signature.h new file mode 100644 index 0000000..973f93c --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/io_signature.h @@ -0,0 +1,111 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_IO_SIGNATURE_H +#define INCLUDED_IO_SIGNATURE_H + +#include +#include + +namespace gr { + + /*! + * \brief i/o signature for input and output ports. + * \brief misc + */ + class GR_RUNTIME_API io_signature + { + int d_min_streams; + int d_max_streams; + std::vector d_sizeof_stream_item; + + io_signature(int min_streams, int max_streams, + const std::vector &sizeof_stream_items); + + public: + typedef boost::shared_ptr sptr; + + static const int IO_INFINITE = -1; + + ~io_signature(); + + /*! + * \brief Create an i/o signature + * + * \ingroup internal + * \param min_streams specify minimum number of streams (>= 0) + * \param max_streams specify maximum number of streams (>= min_streams or -1 -> infinite) + * \param sizeof_stream_item specify the size of the items in each stream + */ + static sptr make(int min_streams, int max_streams, + int sizeof_stream_item); + + /*! + * \brief Create an i/o signature + * + * \param min_streams specify minimum number of streams (>= 0) + * \param max_streams specify maximum number of streams (>= min_streams or -1 -> infinite) + * \param sizeof_stream_item1 specify the size of the items in the first stream + * \param sizeof_stream_item2 specify the size of the items in the second and subsequent streams + */ + static sptr make2(int min_streams, int max_streams, + int sizeof_stream_item1, + int sizeof_stream_item2); + + /*! + * \brief Create an i/o signature + * + * \param min_streams specify minimum number of streams (>= 0) + * \param max_streams specify maximum number of streams (>= min_streams or -1 -> infinite) + * \param sizeof_stream_item1 specify the size of the items in the first stream + * \param sizeof_stream_item2 specify the size of the items in the second stream + * \param sizeof_stream_item3 specify the size of the items in the third and subsequent streams + */ + static sptr make3(int min_streams, int max_streams, + int sizeof_stream_item1, + int sizeof_stream_item2, + int sizeof_stream_item3); + + /*! + * \brief Create an i/o signature + * + * \param min_streams specify minimum number of streams (>= 0) + * \param max_streams specify maximum number of streams (>= min_streams or -1 -> infinite) + * \param sizeof_stream_items specify the size of the items in the streams + * + * If there are more streams than there are entries in + * sizeof_stream_items, the value of the last entry in + * sizeof_stream_items is used for the missing values. + * sizeof_stream_items must contain at least 1 entry. + */ + static sptr makev(int min_streams, int max_streams, + const std::vector &sizeof_stream_items); + + int min_streams() const { return d_min_streams; } + int max_streams() const { return d_max_streams; } + int sizeof_stream_item(int index) const; + std::vector sizeof_stream_items() const; + }; + +} /* namespace gr */ + +#endif /* INCLUDED_IO_SIGNATURE_H */ diff --git a/include/osmosdr/runtime/gnuradio/logger.h b/include/osmosdr/runtime/gnuradio/logger.h new file mode 100644 index 0000000..37ba9bf --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/logger.h @@ -0,0 +1,733 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/******************************************************************************* +* Author: Mark Plett +* Description: +* The gr::logger module wraps the log4cpp library for logging in gnuradio +*******************************************************************************/ + +#ifndef INCLUDED_GR_LOGGER_H +#define INCLUDED_GR_LOGGER_H + +/*! +* \ingroup logging +* \brief GNU Radio logging wrapper for log4cpp library (C++ port of log4j) +* +*/ + +#ifndef ENABLE_GR_LOG +//#cmakedefine ENABLE_GR_LOG +#endif +#ifndef HAVE_LOG4CPP +//#cmakedefine HAVE_LOG4CPP +#endif + +#ifdef _MSC_VER +typedef unsigned short mode_t; +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_GR_LOG + +// We have three configurations... first logging to stdout/stderr +#ifndef HAVE_LOG4CPP + +namespace gr { + //#warning GR logging Enabled and using std::cout + typedef std::string logger_ptr; +} /* namespace gr */ + +#define GR_LOG_DECLARE_LOGPTR(logger) +#define GR_LOG_ASSIGN_LOGPTR(logger,name) +#define GR_CONFIG_LOGGER(config) +#define GR_CONFIG_AND_WATCH_LOGGER(config,period) +#define GR_LOG_GETLOGGER(logger, name) +#define GR_SET_LEVEL(name, level) +#define GR_LOG_SET_LEVEL(logger, level) +#define GR_GET_LEVEL(name, level) +#define GR_LOG_GET_LEVEL(logger, level) +#define GR_ADD_APPENDER(name,appender) +#define GR_LOG_ADD_APPENDER(logger,appender) +#define GR_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_ADD_FILE_APPENDER(name,filename,append,pattern) +#define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern) +#define GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern) +#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern) +#define GR_GET_LOGGER_NAMES(names) +#define GR_RESET_CONFIGURATION() +#define GR_DEBUG(name, msg) std::cout<<"DEBUG: "< +#include +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief GR_LOG macros + * \ingroup logging + * + * These macros wrap the standard LOG4CPP_LEVEL macros. The availablie macros + * are: + * LOG_DEBUG + * LOG_INFO + * LOG_WARN + * LOG_TRACE + * LOG_ERROR + * LOG_ALERT + * LOG_CRIT + * LOG_FATAL + * LOG_EMERG + */ + typedef log4cpp::Category* logger_ptr; + +} /* namespace gr */ + + + /* Macros for Programmatic Configuration */ +#define GR_LOG_DECLARE_LOGPTR(logger) \ + gr::logger_ptr logger; + +#define GR_LOG_ASSIGN_LOGPTR(logger,name) \ + logger = gr::logger_get_logger(name); + +#define GR_CONFIG_LOGGER(config) \ + gr::logger_config::load_config(config) + +#define GR_CONFIG_AND_WATCH_LOGGER(config,period) \ + gr::logger_config::load_config(config,period) + +#define GR_LOG_GETLOGGER(logger, name) \ + gr::logger_ptr logger = gr::logger_get_logger(name); + +#define GR_SET_LEVEL(name, level) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_set_level(logger,level);} + +#define GR_LOG_SET_LEVEL(logger, level) \ + gr::logger_set_level(logger, level); + +#define GR_GET_LEVEL(name, level) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_get_level(logger,level);} + +#define GR_LOG_GET_LEVEL(logger, level) \ + gr::logger_get_level(logger,level); + +#define GR_ADD_APPENDER(name, appender) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_appender(logger,appender);} + +#define GR_LOG_ADD_APPENDER(logger, appender) { \ + gr::logger_add_appender(logger, appender);} + +#define GR_ADD_CONSOLE_APPENDER(name, target, pattern) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_console_appender(logger,target,pattern);} + +#define GR_LOG_ADD_CONSOLE_APPENDER(logger, target, pattern) { \ + gr::logger_add_console_appender(logger,target,pattern);} + +#define GR_ADD_FILE_APPENDER(name, filename, append, pattern) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_file_appender(logger,filename,append,pattern);} + +#define GR_LOG_ADD_FILE_APPENDER(logger, filename, append, pattern) { \ + gr::logger_add_file_appender(logger,filename,append,pattern);} + +#define GR_ADD_ROLLINGFILE_APPENDER(name, filename, filesize, bkup_index, append, mode, pattern) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);} + +#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger, filename, filesize, bkup_index, append, mode, pattern) { \ + gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);} + +#define GR_GET_LOGGER_NAMES(names) { \ + names = gr::logger_get_logger_names();} + +#define GR_RESET_CONFIGURATION() \ + gr::logger_config::reset_config(); + + /* Logger name referenced macros */ +#define GR_DEBUG(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::DEBUG << msg << log4cpp::eol;} + +#define GR_INFO(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::INFO << msg << log4cpp::eol;} + +#define GR_NOTICE(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger << log4cpp::Priority::NOTICE << msg;} + +#define GR_WARN(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::WARN << msg << log4cpp::eol;} + +#define GR_ERROR(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} + +#define GR_CRIT(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::CRIT << msg << log4cpp::eol;} + +#define GR_ALERT(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::ALERT << msg << log4cpp::eol;} + +#define GR_FATAL(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::FATAL << msg << log4cpp::eol;} + +#define GR_EMERG(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol;} + +#define GR_ERRORIF(name, cond, msg) { \ + if((cond)) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} \ + } + +#define GR_ASSERT(name, cond, msg) { \ + if(!(cond)) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol;} \ + assert(0); \ + } + + /* LoggerPtr Referenced Macros */ +#define GR_LOG_DEBUG(logger, msg) { \ + *logger << log4cpp::Priority::DEBUG << msg << log4cpp::eol;} + +#define GR_LOG_INFO(logger, msg) { \ + *logger << log4cpp::Priority::INFO << msg << log4cpp::eol;} + +#define GR_LOG_NOTICE(logger, msg) { \ + *logger << log4cpp::Priority::NOTICE << msg << log4cpp::eol;} + +#define GR_LOG_WARN(logger, msg) { \ + *logger << log4cpp::Priority::WARN << msg << log4cpp::eol;} + +#define GR_LOG_ERROR(logger, msg) { \ + *logger << log4cpp::Priority::ERROR << msg << log4cpp::eol;} + +#define GR_LOG_CRIT(logger, msg) { \ + *logger << log4cpp::Priority::CRIT << msg << log4cpp::eol;} + +#define GR_LOG_ALERT(logger, msg) { \ + *logger << log4cpp::Priority::ALERT << msg << log4cpp::eol;} + +#define GR_LOG_FATAL(logger, msg) { \ + *logger << log4cpp::Priority::FATAL << msg << log4cpp::eol;} + +#define GR_LOG_EMERG(logger, msg) { \ + *logger << log4cpp::Priority::EMERG << msg << log4cpp::eol;} + +#define GR_LOG_ERRORIF(logger,cond, msg) { \ + if((cond)) { \ + *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} \ + } + +#define GR_LOG_ASSERT(logger, cond, msg) { \ + if(!(cond)) { \ + *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol; \ + assert(0);} \ + } + +namespace gr { + + /*! + * \brief Class to control configuration of logger. + * This is a singleton that cna launch a thread to wathc a config file for changes + * \ingroup logging + */ + class logger_config + { + private: + /*! \brief filename of logger config file */ + std::string filename; + /*! \brief Period (seconds) over which watcher thread checks config file for changes */ + unsigned int watch_period; + /*! \brief Pointer to watch thread for config file changes */ + boost::thread *watch_thread; + + /*! \brief Watcher thread method + * /param filename Name of configuration file + * /param watch_period Seconds between checks for changes in config file + */ + static void watch_file(std::string filename,unsigned int watch_period); + + static bool logger_configured; + + logger_config() + { + } //!< Constructor + + /* + rpcbasic_register_get rpc_get_filename; + rpcbasic_register_get rpc_get_watchperiod; + rpcbasic_register_get rpc_get_config; + rpcbasic_register_set rpc_set_config; + */ + + logger_config(logger_config const&); //! logger_get_logger_names(void); + +} /* namespace gr */ + +#endif /* HAVE_LOG4CPP */ + + // If Logger disable do nothing +#else /* ENABLE_GR_LOG */ + +namespace gr { + typedef void* logger_ptr; +} /* namespace gr */ + +#define GR_LOG_DECLARE_LOGPTR(logger) +#define GR_LOG_ASSIGN_LOGPTR(logger,name) +#define GR_CONFIG_LOGGER(config) +#define GR_CONFIG_AND_WATCH_LOGGER(config,period) +#define GR_LOG_GETLOGGER(logger, name) +#define GR_SET_LEVEL(name, level) +#define GR_LOG_SET_LEVEL(logger, level) +#define GR_GET_LEVEL(name, level) +#define GR_LOG_GET_LEVEL(logger, level) +#define GR_ADD_APPENDER(name,appender) +#define GR_LOG_ADD_APPENDER(logger,appender) +#define GR_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_ADD_FILE_APPENDER(name,filename,append,pattern) +#define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern) +#define GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern) +#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern) +#define GR_GET_LOGGER_NAMES(names) +#define GR_RESET_CONFIGURATION() +#define GR_DEBUG(name, msg) +#define GR_INFO(name, msg) +#define GR_NOTICE(name, msg) +#define GR_WARN(name, msg) +#define GR_ERROR(name, msg) +#define GR_ALERT(name, msg) +#define GR_CRIT(name, msg) +#define GR_FATAL(name, msg) +#define GR_EMERG(name, msg) +#define GR_ERRORIF(name, cond, msg) +#define GR_ASSERT(name, cond, msg) +#define GR_LOG_DEBUG(logger, msg) +#define GR_LOG_INFO(logger, msg) +#define GR_LOG_NOTICE(logger, msg) +#define GR_LOG_WARN(logger, msg) +#define GR_LOG_ERROR(logger, msg) +#define GR_LOG_ALERT(logger, msg) +#define GR_LOG_CRIT(logger, msg) +#define GR_LOG_FATAL(logger, msg) +#define GR_LOG_EMERG(logger, msg) +#define GR_LOG_ERRORIF(logger, cond, msg) +#define GR_LOG_ASSERT(logger, cond, msg) + +#endif /* ENABLE_GR_LOG */ + +namespace gr { + + // Even if logger is disabled we'll need for methods below to exist in python. + // The macros these call will be disabled if ENABLE_GR_LOG is undefined + + /********************* Start Classes and Methods for Python ******************/ + /*! + * \brief Logger class for referencing loggers in python. Not + * needed in C++ (use macros) Wraps and manipulates loggers for + * python as python has no macros + * \ingroup logging + * + */ + class logger + { + private: + /*! \brief logger pointer to logger associated wiith this wrapper class */ + logger_ptr d_logger; + public: + /*! + * \brief contructor Provide name of logger to associate with this class + * \param logger_name Name of logger associated with class + */ + logger(std::string logger_name) { + GR_LOG_ASSIGN_LOGPTR(d_logger,logger_name); + }; + + /*! \brief Destructor */ + ~logger(){;} + + // Wrappers for logging macros + /*! \brief inline function, wrapper to set the logger level */ + void set_level(std::string level){GR_LOG_SET_LEVEL(d_logger,level);} + + /*! \brief inline function, wrapper to get the logger level */ + void get_level(std::string &level){GR_LOG_GET_LEVEL(d_logger,level);} + + /*! \brief inline function, wrapper for LOG4CPP_DEBUG for DEBUG message */ + void debug(std::string msg){GR_LOG_DEBUG(d_logger,msg);}; + + /*! \brief inline function, wrapper for LOG4CPP_INFO for INFO message */ + void info(std::string msg){GR_LOG_INFO(d_logger,msg);} + + /*! \brief inline function, wrapper for NOTICE message */ + void notice(std::string msg){GR_LOG_NOTICE(d_logger,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_WARN for WARN message */ + void warn(std::string msg){GR_LOG_WARN(d_logger,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_ERROR for ERROR message */ + void error(std::string msg){GR_LOG_ERROR(d_logger,msg);} + + /*! \brief inline function, wrapper for NOTICE message */ + void crit(std::string msg){GR_LOG_CRIT(d_logger,msg);} + + /*! \brief inline function, wrapper for ALERT message */ + void alert(std::string msg){GR_LOG_ALERT(d_logger,msg);} + + /*! \brief inline function, wrapper for FATAL message */ + void fatal(std::string msg){GR_LOG_FATAL(d_logger,msg);} + + /*! \brief inline function, wrapper for EMERG message */ + void emerg(std::string msg){GR_LOG_EMERG(d_logger,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message */ + void errorIF(bool cond,std::string msg){GR_LOG_ERRORIF(d_logger,cond,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message */ + void log_assert(bool cond,std::string msg){GR_LOG_ASSERT(d_logger,cond,msg);} + + /*! \brief inline function, Method to add appender to logger by + name (define appender in conf file) */ + void add_appender(std::string appender) { + GR_LOG_ADD_APPENDER(d_logger, appender); + } + + /*! \brief inline function, Method to add console appender to logger */ + void add_console_appender(std::string target,std::string pattern) { + GR_LOG_ADD_CONSOLE_APPENDER(d_logger, target, pattern); + } + + /*! \brief inline function, Method to add file appender to logger */ + void add_file_appender(std::string filename, bool append, std::string pattern) { + GR_LOG_ADD_FILE_APPENDER(d_logger, filename, append, pattern); + } + + /*! \brief inline function, Method to add rolling file appender to logger */ + void add_rollingfile_appender(std::string filename, size_t filesize, + int bkup_index, bool append, mode_t mode, + std::string pattern) { + GR_LOG_ADD_ROLLINGFILE_APPENDER(d_logger,filename,filesize, + bkup_index,append,mode,pattern); + } + }; + +} /* namespace gr */ + +/**************** Start Configuration Class and Methods for Python ************/ +/*! + * \brief Function to call configuration macro from python. + * Note: Configuration is only updated if filename or watch_period has changed. + * \param config_filename Name of configuration file + * \param watch_period Seconds to wait between checking for changes in conf file. + * Watch_period defaults to 0 in which case the file is not watched for changes + */ +GR_RUNTIME_API void gr_logger_config(const std::string config_filename, + unsigned int watch_period = 0); + +/*! + * \brief Function to return logger names to python + * \return Vector of name strings + * + */ +GR_RUNTIME_API std::vector gr_logger_get_logger_names(void); + +/*! + * \brief Function to reset logger configuration from python + * + */ +GR_RUNTIME_API void gr_logger_reset_config(void); + +#endif /* INCLUDED_GR_LOGGER_H */ diff --git a/include/osmosdr/runtime/gnuradio/logger.h.in b/include/osmosdr/runtime/gnuradio/logger.h.in new file mode 100644 index 0000000..c67bcac --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/logger.h.in @@ -0,0 +1,733 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/******************************************************************************* +* Author: Mark Plett +* Description: +* The gr::logger module wraps the log4cpp library for logging in gnuradio +*******************************************************************************/ + +#ifndef INCLUDED_GR_LOGGER_H +#define INCLUDED_GR_LOGGER_H + +/*! +* \ingroup logging +* \brief GNU Radio logging wrapper for log4cpp library (C++ port of log4j) +* +*/ + +#ifndef ENABLE_GR_LOG +#cmakedefine ENABLE_GR_LOG +#endif +#ifndef HAVE_LOG4CPP +#cmakedefine HAVE_LOG4CPP +#endif + +#ifdef _MSC_VER +typedef unsigned short mode_t; +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_GR_LOG + +// We have three configurations... first logging to stdout/stderr +#ifndef HAVE_LOG4CPP + +namespace gr { + //#warning GR logging Enabled and using std::cout + typedef std::string logger_ptr; +} /* namespace gr */ + +#define GR_LOG_DECLARE_LOGPTR(logger) +#define GR_LOG_ASSIGN_LOGPTR(logger,name) +#define GR_CONFIG_LOGGER(config) +#define GR_CONFIG_AND_WATCH_LOGGER(config,period) +#define GR_LOG_GETLOGGER(logger, name) +#define GR_SET_LEVEL(name, level) +#define GR_LOG_SET_LEVEL(logger, level) +#define GR_GET_LEVEL(name, level) +#define GR_LOG_GET_LEVEL(logger, level) +#define GR_ADD_APPENDER(name,appender) +#define GR_LOG_ADD_APPENDER(logger,appender) +#define GR_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_ADD_FILE_APPENDER(name,filename,append,pattern) +#define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern) +#define GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern) +#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern) +#define GR_GET_LOGGER_NAMES(names) +#define GR_RESET_CONFIGURATION() +#define GR_DEBUG(name, msg) std::cout<<"DEBUG: "< +#include +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief GR_LOG macros + * \ingroup logging + * + * These macros wrap the standard LOG4CPP_LEVEL macros. The availablie macros + * are: + * LOG_DEBUG + * LOG_INFO + * LOG_WARN + * LOG_TRACE + * LOG_ERROR + * LOG_ALERT + * LOG_CRIT + * LOG_FATAL + * LOG_EMERG + */ + typedef log4cpp::Category* logger_ptr; + +} /* namespace gr */ + + + /* Macros for Programmatic Configuration */ +#define GR_LOG_DECLARE_LOGPTR(logger) \ + gr::logger_ptr logger; + +#define GR_LOG_ASSIGN_LOGPTR(logger,name) \ + logger = gr::logger_get_logger(name); + +#define GR_CONFIG_LOGGER(config) \ + gr::logger_config::load_config(config) + +#define GR_CONFIG_AND_WATCH_LOGGER(config,period) \ + gr::logger_config::load_config(config,period) + +#define GR_LOG_GETLOGGER(logger, name) \ + gr::logger_ptr logger = gr::logger_get_logger(name); + +#define GR_SET_LEVEL(name, level) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_set_level(logger,level);} + +#define GR_LOG_SET_LEVEL(logger, level) \ + gr::logger_set_level(logger, level); + +#define GR_GET_LEVEL(name, level) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_get_level(logger,level);} + +#define GR_LOG_GET_LEVEL(logger, level) \ + gr::logger_get_level(logger,level); + +#define GR_ADD_APPENDER(name, appender) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_appender(logger,appender);} + +#define GR_LOG_ADD_APPENDER(logger, appender) { \ + gr::logger_add_appender(logger, appender);} + +#define GR_ADD_CONSOLE_APPENDER(name, target, pattern) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_console_appender(logger,target,pattern);} + +#define GR_LOG_ADD_CONSOLE_APPENDER(logger, target, pattern) { \ + gr::logger_add_console_appender(logger,target,pattern);} + +#define GR_ADD_FILE_APPENDER(name, filename, append, pattern) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_file_appender(logger,filename,append,pattern);} + +#define GR_LOG_ADD_FILE_APPENDER(logger, filename, append, pattern) { \ + gr::logger_add_file_appender(logger,filename,append,pattern);} + +#define GR_ADD_ROLLINGFILE_APPENDER(name, filename, filesize, bkup_index, append, mode, pattern) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);} + +#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger, filename, filesize, bkup_index, append, mode, pattern) { \ + gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);} + +#define GR_GET_LOGGER_NAMES(names) { \ + names = gr::logger_get_logger_names();} + +#define GR_RESET_CONFIGURATION() \ + gr::logger_config::reset_config(); + + /* Logger name referenced macros */ +#define GR_DEBUG(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::DEBUG << msg << log4cpp::eol;} + +#define GR_INFO(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::INFO << msg << log4cpp::eol;} + +#define GR_NOTICE(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger << log4cpp::Priority::NOTICE << msg;} + +#define GR_WARN(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::WARN << msg << log4cpp::eol;} + +#define GR_ERROR(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} + +#define GR_CRIT(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::CRIT << msg << log4cpp::eol;} + +#define GR_ALERT(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::ALERT << msg << log4cpp::eol;} + +#define GR_FATAL(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::FATAL << msg << log4cpp::eol;} + +#define GR_EMERG(name, msg) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol;} + +#define GR_ERRORIF(name, cond, msg) { \ + if((cond)) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} \ + } + +#define GR_ASSERT(name, cond, msg) { \ + if(!(cond)) { \ + gr::logger_ptr logger = gr::logger_get_logger(name); \ + *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol;} \ + assert(0); \ + } + + /* LoggerPtr Referenced Macros */ +#define GR_LOG_DEBUG(logger, msg) { \ + *logger << log4cpp::Priority::DEBUG << msg << log4cpp::eol;} + +#define GR_LOG_INFO(logger, msg) { \ + *logger << log4cpp::Priority::INFO << msg << log4cpp::eol;} + +#define GR_LOG_NOTICE(logger, msg) { \ + *logger << log4cpp::Priority::NOTICE << msg << log4cpp::eol;} + +#define GR_LOG_WARN(logger, msg) { \ + *logger << log4cpp::Priority::WARN << msg << log4cpp::eol;} + +#define GR_LOG_ERROR(logger, msg) { \ + *logger << log4cpp::Priority::ERROR << msg << log4cpp::eol;} + +#define GR_LOG_CRIT(logger, msg) { \ + *logger << log4cpp::Priority::CRIT << msg << log4cpp::eol;} + +#define GR_LOG_ALERT(logger, msg) { \ + *logger << log4cpp::Priority::ALERT << msg << log4cpp::eol;} + +#define GR_LOG_FATAL(logger, msg) { \ + *logger << log4cpp::Priority::FATAL << msg << log4cpp::eol;} + +#define GR_LOG_EMERG(logger, msg) { \ + *logger << log4cpp::Priority::EMERG << msg << log4cpp::eol;} + +#define GR_LOG_ERRORIF(logger,cond, msg) { \ + if((cond)) { \ + *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} \ + } + +#define GR_LOG_ASSERT(logger, cond, msg) { \ + if(!(cond)) { \ + *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol; \ + assert(0);} \ + } + +namespace gr { + + /*! + * \brief Class to control configuration of logger. + * This is a singleton that cna launch a thread to wathc a config file for changes + * \ingroup logging + */ + class logger_config + { + private: + /*! \brief filename of logger config file */ + std::string filename; + /*! \brief Period (seconds) over which watcher thread checks config file for changes */ + unsigned int watch_period; + /*! \brief Pointer to watch thread for config file changes */ + boost::thread *watch_thread; + + /*! \brief Watcher thread method + * /param filename Name of configuration file + * /param watch_period Seconds between checks for changes in config file + */ + static void watch_file(std::string filename,unsigned int watch_period); + + static bool logger_configured; + + logger_config() + { + } //!< Constructor + + /* + rpcbasic_register_get rpc_get_filename; + rpcbasic_register_get rpc_get_watchperiod; + rpcbasic_register_get rpc_get_config; + rpcbasic_register_set rpc_set_config; + */ + + logger_config(logger_config const&); //! logger_get_logger_names(void); + +} /* namespace gr */ + +#endif /* HAVE_LOG4CPP */ + + // If Logger disable do nothing +#else /* ENABLE_GR_LOG */ + +namespace gr { + typedef void* logger_ptr; +} /* namespace gr */ + +#define GR_LOG_DECLARE_LOGPTR(logger) +#define GR_LOG_ASSIGN_LOGPTR(logger,name) +#define GR_CONFIG_LOGGER(config) +#define GR_CONFIG_AND_WATCH_LOGGER(config,period) +#define GR_LOG_GETLOGGER(logger, name) +#define GR_SET_LEVEL(name, level) +#define GR_LOG_SET_LEVEL(logger, level) +#define GR_GET_LEVEL(name, level) +#define GR_LOG_GET_LEVEL(logger, level) +#define GR_ADD_APPENDER(name,appender) +#define GR_LOG_ADD_APPENDER(logger,appender) +#define GR_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern) +#define GR_ADD_FILE_APPENDER(name,filename,append,pattern) +#define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern) +#define GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern) +#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern) +#define GR_GET_LOGGER_NAMES(names) +#define GR_RESET_CONFIGURATION() +#define GR_DEBUG(name, msg) +#define GR_INFO(name, msg) +#define GR_NOTICE(name, msg) +#define GR_WARN(name, msg) +#define GR_ERROR(name, msg) +#define GR_ALERT(name, msg) +#define GR_CRIT(name, msg) +#define GR_FATAL(name, msg) +#define GR_EMERG(name, msg) +#define GR_ERRORIF(name, cond, msg) +#define GR_ASSERT(name, cond, msg) +#define GR_LOG_DEBUG(logger, msg) +#define GR_LOG_INFO(logger, msg) +#define GR_LOG_NOTICE(logger, msg) +#define GR_LOG_WARN(logger, msg) +#define GR_LOG_ERROR(logger, msg) +#define GR_LOG_ALERT(logger, msg) +#define GR_LOG_CRIT(logger, msg) +#define GR_LOG_FATAL(logger, msg) +#define GR_LOG_EMERG(logger, msg) +#define GR_LOG_ERRORIF(logger, cond, msg) +#define GR_LOG_ASSERT(logger, cond, msg) + +#endif /* ENABLE_GR_LOG */ + +namespace gr { + + // Even if logger is disabled we'll need for methods below to exist in python. + // The macros these call will be disabled if ENABLE_GR_LOG is undefined + + /********************* Start Classes and Methods for Python ******************/ + /*! + * \brief Logger class for referencing loggers in python. Not + * needed in C++ (use macros) Wraps and manipulates loggers for + * python as python has no macros + * \ingroup logging + * + */ + class logger + { + private: + /*! \brief logger pointer to logger associated wiith this wrapper class */ + logger_ptr d_logger; + public: + /*! + * \brief contructor Provide name of logger to associate with this class + * \param logger_name Name of logger associated with class + */ + logger(std::string logger_name) { + GR_LOG_ASSIGN_LOGPTR(d_logger,logger_name); + }; + + /*! \brief Destructor */ + ~logger(){;} + + // Wrappers for logging macros + /*! \brief inline function, wrapper to set the logger level */ + void set_level(std::string level){GR_LOG_SET_LEVEL(d_logger,level);} + + /*! \brief inline function, wrapper to get the logger level */ + void get_level(std::string &level){GR_LOG_GET_LEVEL(d_logger,level);} + + /*! \brief inline function, wrapper for LOG4CPP_DEBUG for DEBUG message */ + void debug(std::string msg){GR_LOG_DEBUG(d_logger,msg);}; + + /*! \brief inline function, wrapper for LOG4CPP_INFO for INFO message */ + void info(std::string msg){GR_LOG_INFO(d_logger,msg);} + + /*! \brief inline function, wrapper for NOTICE message */ + void notice(std::string msg){GR_LOG_NOTICE(d_logger,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_WARN for WARN message */ + void warn(std::string msg){GR_LOG_WARN(d_logger,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_ERROR for ERROR message */ + void error(std::string msg){GR_LOG_ERROR(d_logger,msg);} + + /*! \brief inline function, wrapper for NOTICE message */ + void crit(std::string msg){GR_LOG_CRIT(d_logger,msg);} + + /*! \brief inline function, wrapper for ALERT message */ + void alert(std::string msg){GR_LOG_ALERT(d_logger,msg);} + + /*! \brief inline function, wrapper for FATAL message */ + void fatal(std::string msg){GR_LOG_FATAL(d_logger,msg);} + + /*! \brief inline function, wrapper for EMERG message */ + void emerg(std::string msg){GR_LOG_EMERG(d_logger,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message */ + void errorIF(bool cond,std::string msg){GR_LOG_ERRORIF(d_logger,cond,msg);} + + /*! \brief inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message */ + void log_assert(bool cond,std::string msg){GR_LOG_ASSERT(d_logger,cond,msg);} + + /*! \brief inline function, Method to add appender to logger by + name (define appender in conf file) */ + void add_appender(std::string appender) { + GR_LOG_ADD_APPENDER(d_logger, appender); + } + + /*! \brief inline function, Method to add console appender to logger */ + void add_console_appender(std::string target,std::string pattern) { + GR_LOG_ADD_CONSOLE_APPENDER(d_logger, target, pattern); + } + + /*! \brief inline function, Method to add file appender to logger */ + void add_file_appender(std::string filename, bool append, std::string pattern) { + GR_LOG_ADD_FILE_APPENDER(d_logger, filename, append, pattern); + } + + /*! \brief inline function, Method to add rolling file appender to logger */ + void add_rollingfile_appender(std::string filename, size_t filesize, + int bkup_index, bool append, mode_t mode, + std::string pattern) { + GR_LOG_ADD_ROLLINGFILE_APPENDER(d_logger,filename,filesize, + bkup_index,append,mode,pattern); + } + }; + +} /* namespace gr */ + +/**************** Start Configuration Class and Methods for Python ************/ +/*! + * \brief Function to call configuration macro from python. + * Note: Configuration is only updated if filename or watch_period has changed. + * \param config_filename Name of configuration file + * \param watch_period Seconds to wait between checking for changes in conf file. + * Watch_period defaults to 0 in which case the file is not watched for changes + */ +GR_RUNTIME_API void gr_logger_config(const std::string config_filename, + unsigned int watch_period = 0); + +/*! + * \brief Function to return logger names to python + * \return Vector of name strings + * + */ +GR_RUNTIME_API std::vector gr_logger_get_logger_names(void); + +/*! + * \brief Function to reset logger configuration from python + * + */ +GR_RUNTIME_API void gr_logger_reset_config(void); + +#endif /* INCLUDED_GR_LOGGER_H */ diff --git a/include/osmosdr/runtime/gnuradio/math.h b/include/osmosdr/runtime/gnuradio/math.h new file mode 100644 index 0000000..d611c98 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/math.h @@ -0,0 +1,227 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2005,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/* + * mathematical odds and ends. + */ + +#ifndef _GR_MATH_H_ +#define _GR_MATH_H_ + +#include +#include +#include + +namespace gr { + + static inline bool + is_power_of_2(long x) + { + return x != 0 && (x & (x-1)) == 0; + } + + /*! + * \brief Fast arc tangent using table lookup and linear interpolation + * \ingroup misc + * + * \param y component of input vector + * \param x component of input vector + * \returns float angle angle of vector (x, y) in radians + * + * This function calculates the angle of the vector (x,y) based on a + * table lookup and linear interpolation. The table uses a 256 point + * table covering -45 to +45 degrees and uses symetry to determine + * the final angle value in the range of -180 to 180 degrees. Note + * that this function uses the small angle approximation for values + * close to zero. This routine calculates the arc tangent with an + * average error of +/- 0.045 degrees. + */ + GR_RUNTIME_API float fast_atan2f(float y, float x); + + static inline float + fast_atan2f(gr_complex z) + { + return fast_atan2f(z.imag(), z.real()); + } + + /* This bounds x by +/- clip without a branch */ + static inline float + branchless_clip(float x, float clip) + { + float x1 = fabsf(x+clip); + float x2 = fabsf(x-clip); + x1 -= x2; + return 0.5*x1; + } + + static inline float + clip(float x, float clip) + { + float y = x; + if(x > clip) + y = clip; + else if(x < -clip) + y = -clip; + return y; + } + + // Slicer Functions + static inline unsigned int + binary_slicer(float x) + { + if(x >= 0) + return 1; + else + return 0; + } + + static inline unsigned int + quad_45deg_slicer(float r, float i) + { + unsigned int ret = 0; + if((r >= 0) && (i >= 0)) + ret = 0; + else if((r < 0) && (i >= 0)) + ret = 1; + else if((r < 0) && (i < 0)) + ret = 2; + else + ret = 3; + return ret; + } + + static inline unsigned int + quad_0deg_slicer(float r, float i) + { + unsigned int ret = 0; + if(fabsf(r) > fabsf(i)) { + if(r > 0) + ret = 0; + else + ret = 2; + } + else { + if(i > 0) + ret = 1; + else + ret = 3; + } + + return ret; + } + + static inline unsigned int + quad_45deg_slicer(gr_complex x) + { + return quad_45deg_slicer(x.real(), x.imag()); + } + + static inline unsigned int + quad_0deg_slicer(gr_complex x) + { + return quad_0deg_slicer(x.real(), x.imag()); + } + + // Branchless Slicer Functions + static inline unsigned int + branchless_binary_slicer(float x) + { + return (x >= 0); + } + + static inline unsigned int + branchless_quad_0deg_slicer(float r, float i) + { + unsigned int ret = 0; + ret = (fabsf(r) > fabsf(i)) * (((r < 0) << 0x1)); // either 0 (00) or 2 (10) + ret |= (fabsf(i) > fabsf(r)) * (((i < 0) << 0x1) | 0x1); // either 1 (01) or 3 (11) + + return ret; + } + + static inline unsigned int + branchless_quad_0deg_slicer(gr_complex x) + { + return branchless_quad_0deg_slicer(x.real(), x.imag()); + } + + static inline unsigned int + branchless_quad_45deg_slicer(float r, float i) + { + char ret = (r <= 0); + ret |= ((i <= 0) << 1); + return (ret ^ ((ret & 0x2) >> 0x1)); + } + + static inline unsigned int + branchless_quad_45deg_slicer(gr_complex x) + { + return branchless_quad_45deg_slicer(x.real(), x.imag()); + } + + /*! + * \param x any value + * \param pow2 must be a power of 2 + * \returns \p x rounded down to a multiple of \p pow2. + */ + static inline size_t + p2_round_down(size_t x, size_t pow2) + { + return x & -pow2; + } + + /*! + * \param x any value + * \param pow2 must be a power of 2 + * \returns \p x rounded up to a multiple of \p pow2. + */ + static inline size_t + p2_round_up(size_t x, size_t pow2) + { + return p2_round_down(x + pow2 - 1, pow2); + } + + /*! + * \param x any value + * \param pow2 must be a power of 2 + * \returns \p x modulo \p pow2. + */ + static inline size_t + p2_modulo(size_t x, size_t pow2) + { + return x & (pow2 - 1); + } + + /*! + * \param x any value + * \param pow2 must be a power of 2 + * \returns \p pow2 - (\p x modulo \p pow2). + */ + static inline size_t + p2_modulo_neg(size_t x, size_t pow2) + { + return pow2 - p2_modulo(x, pow2); + } + +} /* namespace gr */ + +#endif /* _GR_MATH_H_ */ diff --git a/include/osmosdr/runtime/gnuradio/misc.h b/include/osmosdr/runtime/gnuradio/misc.h new file mode 100644 index 0000000..290e39b --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/misc.h @@ -0,0 +1,39 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_MISC_H +#define INCLUDED_GR_MISC_H + +#include +#include + +GR_RUNTIME_API unsigned int +gr_rounduppow2(unsigned int n); + +// FIXME should be template +GR_RUNTIME_API void gr_zero_vector(std::vector &v); +GR_RUNTIME_API void gr_zero_vector(std::vector &v); +GR_RUNTIME_API void gr_zero_vector(std::vector &v); +GR_RUNTIME_API void gr_zero_vector(std::vector &v); + + +#endif /* INCLUDED_GR_MISC_H */ diff --git a/include/osmosdr/runtime/gnuradio/prefs.h b/include/osmosdr/runtime/gnuradio/prefs.h new file mode 100644 index 0000000..b675c83 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/prefs.h @@ -0,0 +1,152 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_PREFS_H +#define INCLUDED_GR_PREFS_H + +#include +#include +#include +#include + +namespace gr { + + typedef std::map< std::string, std::map > config_map_t; + typedef std::map< std::string, std::map >::iterator config_map_itr; + typedef std::map config_map_elem_t; + typedef std::map::iterator config_map_elem_itr; + + /*! + * \brief Base class for representing user preferences a la windows INI files. + * \ingroup misc + * + * The real implementation is in Python, and is accessable from C++ + * via the magic of SWIG directors. + */ + class GR_RUNTIME_API prefs + { + public: + static prefs *singleton(); + static void set_singleton(prefs *p); + + prefs(); + virtual ~prefs(); + + /*! + * \brief Returns the configuration options as a string. + */ + std::string to_string(); + + /*! + * \brief Saves the configuration settings to + * ${HOME}/.gnuradio/config.conf. + * + * WARNING: this will overwrite your current config.conf file. + */ + void save(); + + /*! + * \brief Does \p section exist? + */ + virtual bool has_section(const std::string §ion); + + /*! + * \brief Does \p option exist? + */ + virtual bool has_option(const std::string §ion, + const std::string &option); + + /*! + * \brief If option exists return associated value; else + * default_val. + */ + virtual const std::string get_string(const std::string §ion, + const std::string &option, + const std::string &default_val); + + /*! + * \brief Set or add a string \p option to \p section with value + * \p val. + */ + virtual void set_string(const std::string §ion, + const std::string &option, + const std::string &val); + + /*! + * \brief If option exists and value can be converted to bool, + * return it; else default_val. + */ + virtual bool get_bool(const std::string §ion, + const std::string &option, + bool default_val); + + /*! + * \brief Set or add a bool \p option to \p section with value \p val. + */ + virtual void set_bool(const std::string §ion, + const std::string &option, + bool val); + + /*! + * \brief If option exists and value can be converted to long, + * return it; else default_val. + */ + virtual long get_long(const std::string §ion, + const std::string &option, + long default_val); + + /*! + * \brief Set or add a long \p option to \p section with value \p val. + */ + virtual void set_long(const std::string §ion, + const std::string &option, + long val); + + /*! + * \brief If option exists and value can be converted to double, + * return it; else default_val. + */ + virtual double get_double(const std::string §ion, + const std::string &option, + double default_val); + + /*! + * \brief Set or add a double \p option to \p section with value \p val. + */ + virtual void set_double(const std::string §ion, + const std::string &option, + double val); + + protected: + virtual std::vector _sys_prefs_filenames(); + virtual void _read_files(); + virtual void _convert_to_map(const std::string &conf); + virtual char * option_to_env(std::string section, std::string option); + + private: + gr::thread::mutex d_mutex; + config_map_t d_config_map; + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_PREFS_H */ diff --git a/include/osmosdr/runtime/gnuradio/random.h b/include/osmosdr/runtime/gnuradio/random.h new file mode 100644 index 0000000..e01fcb7 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/random.h @@ -0,0 +1,81 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RANDOM_H +#define INCLUDED_GR_RANDOM_H + +#include +#include + +// While rand(3) specifies RAND_MAX, random(3) says that the output +// ranges from 0 to 2^31-1 but does not specify a macro to denote +// this. We define RANDOM_MAX for cleanliness. We must omit the +// definition for systems that have made the same choice. (Note that +// random(3) is from 4.2BSD, and not specified by POSIX.) + +#ifndef RANDOM_MAX +static const int RANDOM_MAX = 2147483647; // 2^31-1 +#endif /* RANDOM_MAX */ + +#include + +namespace gr { + + /*! + * \brief pseudo random number generator + * \ingroup math_blk + */ + class GR_RUNTIME_API random + { + protected: + static const int NTAB = 32; + long d_seed; + long d_iy; + long d_iv[NTAB]; + int d_iset; + float d_gset; + + public: + random(long seed=3021); + + void reseed(long seed); + + /*! + * \brief uniform random deviate in the range [0.0, 1.0) + */ + float ran1(); + + /*! + * \brief normally distributed deviate with zero mean and variance 1 + */ + float gasdev(); + + float laplacian(); + float impulse(float factor); + float rayleigh(); + gr_complex rayleigh_complex(); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RANDOM_H */ + diff --git a/include/osmosdr/runtime/gnuradio/realtime.h b/include/osmosdr/runtime/gnuradio/realtime.h new file mode 100644 index 0000000..b07caf6 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/realtime.h @@ -0,0 +1,39 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_REALTIME_H +#define INCLUDED_GR_REALTIME_H + +#include +#include + +namespace gr { + + /*! + * \brief If possible, enable high-priority "real time" scheduling. + * \ingroup misc + */ + GR_RUNTIME_API rt_status_t enable_realtime_scheduling(); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_REALTIME_H */ diff --git a/include/osmosdr/runtime/gnuradio/realtime_impl.h b/include/osmosdr/runtime/gnuradio/realtime_impl.h new file mode 100644 index 0000000..bc22502 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/realtime_impl.h @@ -0,0 +1,96 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GNURADIO_REALTIME_H +#define INCLUDED_GNURADIO_REALTIME_H + +#include +#include + +/*! + * \brief System independent way to ask for realtime scheduling + * + * \sa sys_pri.h + */ +namespace gr { + + typedef enum { + RT_OK = 0, + RT_NOT_IMPLEMENTED, + RT_NO_PRIVS, + RT_OTHER_ERROR + } rt_status_t; + + enum rt_sched_policy { + RT_SCHED_RR = 0, // round robin + RT_SCHED_FIFO = 1, // first in first out + }; + + namespace impl { + /* + * Define the range for our virtual priorities (don't change + * these) + * + * Processes (or threads) with numerically higher priority values + * are scheduled before processes with numerically lower priority + * values. Thus, the value returned by rt_priority_max() will be + * greater than the value returned by rt_priority_min(). + */ + static inline int rt_priority_min() { return 0; } + static inline int rt_priority_max() { return 15; } + static inline int rt_priority_default() { return 1; } + + struct GR_RUNTIME_API rt_sched_param { + int priority; + rt_sched_policy policy; + + rt_sched_param() + : priority(rt_priority_default()), policy(RT_SCHED_RR){} + + rt_sched_param(int priority_, rt_sched_policy policy_ = RT_SCHED_RR) + { + if(priority_ < rt_priority_min() || priority_ > rt_priority_max()) + throw std::invalid_argument("rt_sched_param: priority out of range"); + + priority = priority_; + policy = policy_; + } + }; + + /*! + * \brief If possible, enable "realtime" scheduling. + * \ingroup misc + * + * In general, this means that the code will be scheduled before + * any non-realtime (normal) processes. Note that if your code + * contains an non-blocking infinite loop and you enable realtime + * scheduling, it's possible to hang the system. + */ + + // NOTE: If you change this, you need to change the code in + // gnuradio-runtime/swig/realtime.i, see note there. + GR_RUNTIME_API rt_status_t enable_realtime_scheduling(rt_sched_param = rt_sched_param()); + + } /* namespace impl */ +} /* namespace gr */ + +#endif /* INCLUDED_GNURADIO_REALTIME_H */ diff --git a/include/osmosdr/runtime/gnuradio/runtime_types.h b/include/osmosdr/runtime/gnuradio/runtime_types.h new file mode 100644 index 0000000..f4674c0 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/runtime_types.h @@ -0,0 +1,56 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_TYPES_H +#define INCLUDED_GR_RUNTIME_TYPES_H + +#include +#include + +namespace gr { + + /* + * typedefs for smart pointers we use throughout the runtime system + */ + class basic_block; + class block; + class block_detail; + class buffer; + class buffer_reader; + class hier_block2; + class flat_flowgraph; + class flowgraph; + class top_block; + + typedef boost::shared_ptr basic_block_sptr; + typedef boost::shared_ptr block_sptr; + typedef boost::shared_ptr block_detail_sptr; + typedef boost::shared_ptr buffer_sptr; + typedef boost::shared_ptr buffer_reader_sptr; + typedef boost::shared_ptr hier_block2_sptr; + typedef boost::shared_ptr flat_flowgraph_sptr; + typedef boost::shared_ptr flowgraph_sptr; + typedef boost::shared_ptr top_block_sptr; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_TYPES_H */ diff --git a/include/osmosdr/runtime/gnuradio/sincos.h b/include/osmosdr/runtime/gnuradio/sincos.h new file mode 100644 index 0000000..f162f6e --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/sincos.h @@ -0,0 +1,35 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2004 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_SINCOS_H +#define INCLUDED_GR_SINCOS_H + +#include + +namespace gr { + + // compute sine and cosine at the same time + GR_RUNTIME_API void sincos (double x, double *sin, double *cos); + GR_RUNTIME_API void sincosf (float x, float *sin, float *cos); +} + +#endif /* INCLUDED_GR_SINCOS_H */ diff --git a/include/osmosdr/runtime/gnuradio/sptr_magic.h b/include/osmosdr/runtime/gnuradio/sptr_magic.h new file mode 100644 index 0000000..898edc8 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/sptr_magic.h @@ -0,0 +1,57 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_SPTR_MAGIC_H +#define INCLUDED_GR_RUNTIME_SPTR_MAGIC_H + +#include +#include + +namespace gr { + class basic_block; + class hier_block2; +} + +namespace gnuradio { + namespace detail { + + class GR_RUNTIME_API sptr_magic + { + public: + static boost::shared_ptr fetch_initial_sptr(gr::basic_block *p); + static void create_and_stash_initial_sptr(gr::hier_block2 *p); + }; + }; + + /* + * \brief New! Improved! Standard method to get/create the + * boost::shared_ptr for a block. + */ + template + boost::shared_ptr + get_initial_sptr(T *p) + { + return boost::dynamic_pointer_cast + (detail::sptr_magic::fetch_initial_sptr(p)); + } +} + +#endif /* INCLUDED_GR_RUNTIME_SPTR_MAGIC_H */ diff --git a/include/osmosdr/runtime/gnuradio/sync_block.h b/include/osmosdr/runtime/gnuradio/sync_block.h new file mode 100644 index 0000000..4b0022a --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/sync_block.h @@ -0,0 +1,69 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_SYNC_BLOCK_H +#define INCLUDED_GR_RUNTIME_SYNC_BLOCK_H + +#include +#include + +namespace gr { + + /*! + * \brief synchronous 1:1 input to output with history + * \ingroup base_blk + * + * Override work to provide the signal processing implementation. + */ + class GR_RUNTIME_API sync_block : public block + { + protected: + sync_block(void) {} // allows pure virtual interface sub-classes + sync_block(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature); + + public: + /*! + * \brief just like gr::block::general_work, only this arranges to + * call consume_each for you + * + * The user must override work to define the signal processing code + */ + virtual int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) = 0; + + // gr::sync_block overrides these to assist work + void forecast(int noutput_items, gr_vector_int &ninput_items_required); + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + int fixed_rate_ninput_to_noutput(int ninput); + int fixed_rate_noutput_to_ninput(int noutput); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_SYNC_BLOCK_H */ diff --git a/include/osmosdr/runtime/gnuradio/sync_decimator.h b/include/osmosdr/runtime/gnuradio/sync_decimator.h new file mode 100644 index 0000000..129abdc --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/sync_decimator.h @@ -0,0 +1,72 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004, 2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_SYNC_DECIMATOR_H +#define INCLUDED_GR_RUNTIME_SYNC_DECIMATOR_H + +#include +#include + +namespace gr { + + /*! + * \brief synchronous N:1 input to output with history + * \ingroup base_blk + * + * Override work to provide the signal processing implementation. + */ + class GR_RUNTIME_API sync_decimator : public sync_block + { + private: + unsigned d_decimation; + + protected: + sync_decimator(void) {} // allows pure virtual interface sub-classes + sync_decimator(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature, + unsigned decimation); + + public: + unsigned decimation() const { return d_decimation; } + void set_decimation(unsigned decimation) + { + d_decimation = decimation; + set_relative_rate(1.0 / decimation); + } + + // gr::sync_decimator overrides these to assist work + void forecast(int noutput_items, gr_vector_int &ninput_items_required); + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + // derived classes should override work + + int fixed_rate_ninput_to_noutput(int ninput); + int fixed_rate_noutput_to_ninput(int noutput); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_SYNC_DECIMATOR_H */ diff --git a/include/osmosdr/runtime/gnuradio/sync_interpolator.h b/include/osmosdr/runtime/gnuradio/sync_interpolator.h new file mode 100644 index 0000000..bfe79f9 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/sync_interpolator.h @@ -0,0 +1,74 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_SYNC_INTERPOLATOR_H +#define INCLUDED_GR_RUNTIME_SYNC_INTERPOLATOR_H + +#include +#include + +namespace gr { + + /*! + * \brief synchronous 1:N input to output with history + * \ingroup base_blk + * + * Override work to provide the signal processing implementation. + */ + class GR_RUNTIME_API sync_interpolator : public sync_block + { + private: + unsigned d_interpolation; + + protected: + sync_interpolator(void) {} // allows pure virtual interface sub-classes + sync_interpolator(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature, + unsigned interpolation); + + public: + unsigned interpolation() const { return d_interpolation; } + void set_interpolation(unsigned interpolation) + { + d_interpolation = interpolation; + set_relative_rate(1.0 * interpolation); + set_output_multiple(interpolation); + } + + // gr::sync_interpolator overrides these to assist work + void forecast(int noutput_items, + gr_vector_int &ninput_items_required); + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + // derived classes should override work + + int fixed_rate_ninput_to_noutput(int ninput); + int fixed_rate_noutput_to_ninput(int noutput); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_SYNC_INTERPOLATOR_H */ diff --git a/include/osmosdr/runtime/gnuradio/sys_paths.h b/include/osmosdr/runtime/gnuradio/sys_paths.h new file mode 100644 index 0000000..1bd2e0d --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/sys_paths.h @@ -0,0 +1,37 @@ +/* + * Copyright 2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_SYS_PATHS_H +#define GR_SYS_PATHS_H + +#include + +namespace gr { + + //! directory to create temporary files + GR_RUNTIME_API const char *tmp_path(); + + //! directory to store application data + GR_RUNTIME_API const char *appdata_path(); + +} /* namespace gr */ + +#endif /* GR_SYS_PATHS_H */ diff --git a/include/osmosdr/runtime/gnuradio/thread/thread.h b/include/osmosdr/runtime/gnuradio/thread/thread.h new file mode 100644 index 0000000..a0c9e4f --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/thread/thread.h @@ -0,0 +1,156 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_THREAD_H +#define INCLUDED_THREAD_H + +#include +#include +#include +#include +#include +#include + +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include + +#endif + +namespace gr { + namespace thread { + + typedef boost::thread thread; + typedef boost::mutex mutex; + typedef boost::unique_lock scoped_lock; + typedef boost::condition_variable condition_variable; + + /*! \brief a system-dependent typedef for the underlying thread type. + */ +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + typedef HANDLE gr_thread_t; +#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) + typedef pthread_t gr_thread_t; +#else + typedef pthread_t gr_thread_t; +#endif + + /*! \brief Get the current thread's ID as a gr_thread_t + * + * We use this when setting the thread affinity or any other + * low-level thread settings. Can be called withing a GNU Radio + * block to get a reference to its current thread ID. + */ + GR_RUNTIME_API gr_thread_t get_current_thread_id(); + + /*! \brief Bind the current thread to a set of cores. + * + * Wrapper for system-dependent calls to set the affinity of the + * current thread to the processor mask. The mask is simply a + * 1-demensional vector containing the processor or core number + * from 0 to N-1 for N cores. + * + * Note: this does not work on OSX; it is a nop call since OSX + * does not support the concept of thread affinity (and what they + * do support in this way since 10.5 is not what we want or can + * use in this fashion). + */ + GR_RUNTIME_API void thread_bind_to_processor(const std::vector &mask); + + /*! \brief Convineince function to bind the current thread to a single core. + * + * Wrapper for system-dependent calls to set the affinity of the + * current thread to a given core from 0 to N-1 for N cores. + * + * Note: this does not work on OSX; it is a nop call since OSX + * does not support the concept of thread affinity (and what they + * do support in this way since 10.5 is not what we want or can + * use in this fashion). + */ + GR_RUNTIME_API void thread_bind_to_processor(int n); + + /*! \brief Bind a thread to a set of cores. + * + * Wrapper for system-dependent calls to set the affinity of the + * given thread ID to the processor mask. The mask is simply a + * 1-demensional vector containing the processor or core number + * from 0 to N-1 for N cores. + * + * Note: this does not work on OSX; it is a nop call since OSX + * does not support the concept of thread affinity (and what they + * do support in this way since 10.5 is not what we want or can + * use in this fashion). + */ + GR_RUNTIME_API void thread_bind_to_processor(gr_thread_t thread, + const std::vector &mask); + + + /*! \brief Convineince function to bind the a thread to a single core. + * + * Wrapper for system-dependent calls to set the affinity of the + * given thread ID to a given core from 0 to N-1 for N cores. + * + * Note: this does not work on OSX; it is a nop call since OSX + * does not support the concept of thread affinity (and what they + * do support in this way since 10.5 is not what we want or can + * use in this fashion). + */ + GR_RUNTIME_API void thread_bind_to_processor(gr_thread_t thread, + unsigned int n); + + /*! \brief Remove any thread-processor affinity for the current thread. + * + * Note: this does not work on OSX; it is a nop call since OSX + * does not support the concept of thread affinity (and what they + * do support in this way since 10.5 is not what we want or can + * use in this fashion). + */ + GR_RUNTIME_API void thread_unbind(); + + /*! \brief Remove any thread-processor affinity for a given thread ID. + * + * Note: this does not work on OSX; it is a nop call since OSX + * does not support the concept of thread affinity (and what they + * do support in this way since 10.5 is not what we want or can + * use in this fashion). + */ + GR_RUNTIME_API void thread_unbind(gr_thread_t thread); + + /*! \brief get current thread priority for a given thread ID + * + * Note: this does not work on OSX + */ + GR_RUNTIME_API int thread_priority(gr_thread_t thread); + + /*! \brief get current thread priority for a given thread ID + * + * Note: this does not work on OSX + */ + GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority); + + } /* namespace thread */ +} /* namespace gr */ + +#endif /* INCLUDED_THREAD_H */ diff --git a/include/osmosdr/runtime/gnuradio/thread/thread_body_wrapper.h b/include/osmosdr/runtime/gnuradio/thread/thread_body_wrapper.h new file mode 100644 index 0000000..9761d3f --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/thread/thread_body_wrapper.h @@ -0,0 +1,72 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_THREAD_BODY_WRAPPER_H +#define INCLUDED_THREAD_BODY_WRAPPER_H + +#include +#include +#include +#include + +namespace gr { + namespace thread { + + GR_RUNTIME_API void mask_signals(); + + template + class thread_body_wrapper + { + private: + F d_f; + std::string d_name; + + public: + explicit thread_body_wrapper(F f, const std::string &name="") + : d_f(f), d_name(name) {} + + void operator()() + { + mask_signals(); + + try { + d_f(); + } + catch(boost::thread_interrupted const &) + { + } + catch(std::exception const &e) + { + std::cerr << "thread[" << d_name << "]: " + << e.what() << std::endl; + } + catch(...) + { + std::cerr << "thread[" << d_name << "]: " + << "caught unrecognized exception\n"; + } + } + }; + + } /* namespace thread */ +} /* namespace gr */ + +#endif /* INCLUDED_THREAD_BODY_WRAPPER_H */ diff --git a/include/osmosdr/runtime/gnuradio/thread/thread_group.h b/include/osmosdr/runtime/gnuradio/thread/thread_group.h new file mode 100644 index 0000000..830017d --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/thread/thread_group.h @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2001-2003 William E. Kempf + * Copyright (C) 2007 Anthony Williams + * Copyright 2008,2009 Free Software Foundation, Inc. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ + +/* + * This was extracted from Boost 1.35.0 and fixed. + */ + +#ifndef INCLUDED_THREAD_GROUP_H +#define INCLUDED_THREAD_GROUP_H + +#include +#include +#include +#include +#include + +namespace gr { + namespace thread { + + class GR_RUNTIME_API thread_group : public boost::noncopyable + { + public: + thread_group(); + ~thread_group(); + + boost::thread* create_thread(const boost::function0& threadfunc); + void add_thread(boost::thread* thrd); + void remove_thread(boost::thread* thrd); + void join_all(); + void interrupt_all(); + size_t size() const; + + private: + std::list m_threads; + mutable boost::shared_mutex m_mutex; + }; + + } /* namespace thread */ +} /* namespace gr */ + +#endif /* INCLUDED_THREAD_GROUP_H */ diff --git a/include/osmosdr/runtime/gnuradio/top_block.h b/include/osmosdr/runtime/gnuradio/top_block.h new file mode 100644 index 0000000..b3692e0 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/top_block.h @@ -0,0 +1,146 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007-2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_TOP_BLOCK_H +#define INCLUDED_GR_TOP_BLOCK_H + +#include +#include + +namespace gr { + + class top_block_impl; + + GR_RUNTIME_API top_block_sptr make_top_block(const std::string &name); + + /*! + *\brief Top-level hierarchical block representing a flowgraph + * \ingroup container_blk + */ + class GR_RUNTIME_API top_block : public hier_block2 + { + private: + friend GR_RUNTIME_API top_block_sptr + make_top_block(const std::string &name); + + top_block_impl *d_impl; + + protected: + top_block(const std::string &name); + + public: + ~top_block(); + + /*! + * \brief The simple interface to running a flowgraph. + * + * Calls start() then wait(). Used to run a flowgraph that will + * stop on its own, or when another thread will call stop(). + * + * \param max_noutput_items the maximum number of output items + * allowed for any block in the flowgraph. This passes through to + * the start function; see that function for more details. + */ + void run(int max_noutput_items=100000000); + + /*! + * Start the contained flowgraph. Creates one or more threads to + * execute the flow graph. Returns to the caller once the threads + * are created. Calling start() on a top_block that is already + * started IS an error. + * + * \param max_noutput_items the maximum number of output items + * allowed for any block in the flowgraph; the noutput_items can + * always be less than this, but this will cap it as a + * maximum. Use this to adjust the maximum latency a flowgraph can + * exhibit. + */ + void start(int max_noutput_items=100000000); + + /*! + * Stop the running flowgraph. Notifies each thread created by the + * scheduler to shutdown, then returns to caller. Calling stop() + * on a top_block that is already stopped IS NOT an error. + */ + void stop(); + + /*! + * Wait for a flowgraph to complete. Flowgraphs complete when + * either (1) all blocks indicate that they are done (typically + * only when using blocks.file_source, or blocks.head, or (2) + * after stop() has been called to request shutdown. Calling wait + * on a top_block that is not running IS NOT an error (wait + * returns w/o blocking). + */ + void wait(); + + /*! + * Lock a flowgraph in preparation for reconfiguration. When an + * equal number of calls to lock() and unlock() have occurred, the + * flowgraph will be reconfigured. + * + * N.B. lock() and unlock() may not be called from a flowgraph + * thread (E.g., block::work method) or deadlock will occur + * when reconfiguration happens. + */ + virtual void lock(); + + /*! + * Unlock a flowgraph in preparation for reconfiguration. When an + * equal number of calls to lock() and unlock() have occurred, the + * flowgraph will be reconfigured. + * + * N.B. lock() and unlock() may not be called from a flowgraph thread + * (E.g., block::work method) or deadlock will occur when + * reconfiguration happens. + */ + virtual void unlock(); + + /*! + * Returns a string that lists the edge connections in the + * flattened flowgraph. + */ + std::string edge_list(); + + /*! + * Displays flattened flowgraph edges and block connectivity + */ + void dump(); + + //! Get the number of max noutput_items in the flowgraph + int max_noutput_items(); + + //! Set the maximum number of noutput_items in the flowgraph + void set_max_noutput_items(int nmax); + + top_block_sptr to_top_block(); // Needed for Python type coercion + + void setup_rpc(); + }; + + inline top_block_sptr cast_to_top_block_sptr(basic_block_sptr block) { + return boost::dynamic_pointer_cast(block); + } + +} /* namespce gr */ + +#endif /* INCLUDED_GR_TOP_BLOCK_H */ diff --git a/include/osmosdr/runtime/gnuradio/tpb_detail.h b/include/osmosdr/runtime/gnuradio/tpb_detail.h new file mode 100644 index 0000000..d3be97b --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/tpb_detail.h @@ -0,0 +1,92 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_GR_TPB_DETAIL_H +#define INCLUDED_GR_TPB_DETAIL_H + +#include +#include +#include + +namespace gr { + + class block_detail; + + /*! + * \brief used by thread-per-block scheduler + */ + struct GR_RUNTIME_API tpb_detail { + gr::thread::mutex mutex; //< protects all vars + bool input_changed; + gr::thread::condition_variable input_cond; + bool output_changed; + gr::thread::condition_variable output_cond; + + public: + tpb_detail() + : input_changed(false), output_changed(false) { } + + //! Called by us to tell all our upstream blocks that their output + //! may have changed. + void notify_upstream(block_detail *d); + + //! Called by us to tell all our downstream blocks that their + //! input may have changed. + void notify_downstream(block_detail *d); + + //! Called by us to notify both upstream and downstream + void notify_neighbors(block_detail *d); + + //! Called by pmt msg posters + void notify_msg() { + input_cond.notify_one(); + output_cond.notify_one(); + } + + //! Called by us + void clear_changed() + { + gr::thread::scoped_lock guard(mutex); + input_changed = false; + output_changed = false; + } + + private: + //! Used by notify_downstream + void set_input_changed() + { + gr::thread::scoped_lock guard(mutex); + input_changed = true; + input_cond.notify_one(); + } + + //! Used by notify_upstream + void set_output_changed() + { + gr::thread::scoped_lock guard(mutex); + output_changed = true; + output_cond.notify_one(); + } + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_TPB_DETAIL_H */ diff --git a/include/osmosdr/runtime/gnuradio/types.h b/include/osmosdr/runtime/gnuradio/types.h new file mode 100644 index 0000000..6cb0f72 --- /dev/null +++ b/include/osmosdr/runtime/gnuradio/types.h @@ -0,0 +1,65 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_TYPES_H +#define INCLUDED_GR_TYPES_H + +#include +#include +#include +#include // size_t + +#include + +typedef std::vector gr_vector_int; +typedef std::vector gr_vector_uint; +typedef std::vector gr_vector_float; +typedef std::vector gr_vector_double; +typedef std::vector gr_vector_void_star; +typedef std::vector gr_vector_const_void_star; + +/* + * #include must be placed beforehand + * in the source file including gnuradio/types.h for + * the following to work correctly + */ +#ifdef HAVE_STDINT_H +#include +typedef int16_t gr_int16; +typedef int32_t gr_int32; +typedef int64_t gr_int64; +typedef uint16_t gr_uint16; +typedef uint32_t gr_uint32; +typedef uint64_t gr_uint64; +#else +/* + * Note: these defaults may be wrong on 64-bit systems + */ +typedef short gr_int16; +typedef int gr_int32; +typedef long long gr_int64; +typedef unsigned short gr_uint16; +typedef unsigned int gr_uint32; +typedef unsigned long long gr_uint64; +#endif /* HAVE_STDINT_H */ + +#endif /* INCLUDED_GR_TYPES_H */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ad0ec96..5963b7e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -49,6 +49,144 @@ GR_OSMOSDR_APPEND_LIBS( ${GNURADIO_BLOCKS_LIBRARIES} ) +######################################################################## +# Set up built-in GNU Radio runtime component +######################################################################## +GR_REGISTER_COMPONENT("Built-in GNU Radio runtime" ENABLE_RUNTIME RUNTIME_MODE) +if(ENABLE_RUNTIME) + +message(STATUS "") +INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/ConfigChecks.cmake) + +include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/runtime) + +GR_OSMOSDR_APPEND_SRCS( + runtime/blocks/throttle_impl.cc + runtime/blocks/null_sink_impl.cc + runtime/blocks/null_source_impl.cc + + runtime/math/fast_atan2f.cc + runtime/math//fxpt.cc + runtime/math/random.cc + runtime/math/sincos.cc + + runtime/thread/thread.cc + runtime/thread/thread_body_wrapper.cc + runtime/thread/thread_group.cc + + runtime/basic_block.cc + runtime/block.cc + runtime/block_detail.cc + runtime/block_executor.cc + runtime/block_gateway_impl.cc + runtime/block_registry.cc + runtime/buffer.cc + runtime/circular_file.cc + runtime/feval.cc + runtime/flat_flowgraph.cc + runtime/flowgraph.cc + runtime/hier_block2.cc + runtime/hier_block2_detail.cc + runtime/high_res_timer.cc + runtime/io_signature.cc + runtime/local_sighandler.cc + runtime/logger.cc + runtime/misc.cc + runtime/pagesize.cc + runtime/prefs.cc + runtime/realtime.cc + runtime/realtime_impl.cc + runtime/scheduler.cc + runtime/scheduler_sts.cc + runtime/scheduler_tpb.cc + runtime/single_threaded_scheduler.cc + runtime/sptr_magic.cc + runtime/sync_block.cc + runtime/sys_paths.cc + runtime/top_block.cc + runtime/top_block_impl.cc + runtime/tpb_detail.cc + runtime/tpb_thread_body.cc + runtime/vmcircbuf.cc + runtime/vmcircbuf_createfilemapping.cc + runtime/vmcircbuf_mmap_shm_open.cc + runtime/vmcircbuf_mmap_tmpfile.cc + runtime/vmcircbuf_prefs.cc + runtime/vmcircbuf_sysv_shm.cc +) + +######################################################################## +# Handle the generated constants +######################################################################## +execute_process(COMMAND ${PYTHON_EXECUTABLE} -c + "import time;print time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime())" + OUTPUT_VARIABLE BUILD_DATE OUTPUT_STRIP_TRAILING_WHITESPACE +) + +set(GR_VERSION "built-in") + +message(STATUS "Loading prefix ${prefix} into constants...") +message(STATUS "Loading SYSCONFDIR ${SYSCONFDIR} into constants...") +message(STATUS "Loading GR_PREFSDIR ${GR_PREFSDIR} into constants...") +message(STATUS "Loading build date ${BUILD_DATE} into constants...") +message(STATUS "Loading version ${GR_VERSION} into constants...") + +#double escape for windows backslash path separators +string(REPLACE "\\" "\\\\" prefix ${prefix}) +string(REPLACE "\\" "\\\\" SYSCONFDIR ${SYSCONFDIR}) +string(REPLACE "\\" "\\\\" GR_PREFSDIR ${GR_PREFSDIR}) + +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/runtime/constants.cc.in + ${CMAKE_CURRENT_BINARY_DIR}/constants.cc +@ONLY) + +GR_OSMOSDR_APPEND_SRCS(${CMAKE_CURRENT_BINARY_DIR}/constants.cc) + +# PowerPC workaround for posix_memalign +# Might not be needed, but we'll keep it for now. +if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)") + GR_OSMOSDR_APPEND_SRCS(runtime/posix_memalign.cc) +endif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)") + +######################################################################## +# Will set TRY_SHM_VMCIRCBUF to 1 by default except on Windows machines. +# Can manually set with -DTRY_SHM_VMCIRCBUF=0|1 +######################################################################## +if(WIN32) + OPTION(TRY_SHM_VMCIRCBUF "Try SHM VMCIRCBUF" OFF) +else(WIN32) + OPTION(TRY_SHM_VMCIRCBUF "Try SHM VMCIRCBUF" ON) +endif(WIN32) + +message(STATUS "TRY_SHM_VMCIRCBUF set to ${TRY_SHM_VMCIRCBUF}.") + +if(TRY_SHM_VMCIRCBUF) + ADD_DEFINITIONS( -DTRY_SHM_VMCIRCBUF ) +endif(TRY_SHM_VMCIRCBUF) + +######################################################################## +# Add libraries for winsock2.h on Windows +######################################################################## +CHECK_INCLUDE_FILE_CXX(windows.h HAVE_WINDOWS_H) +if(HAVE_WINDOWS_H) + include_directories(${CMAKE_SOURCE_DIR}/cmake/msvc) + ADD_DEFINITIONS(-DHAVE_WINDOWS_H -DUSING_WINSOCK -DWIN32_LEAN_AND_MEAN) + ADD_DEFINITIONS(-DNOMINMAX -D_USE_MATH_DEFINES) + ADD_DEFINITIONS(/MP -D_CRT_SECURE_NO_WARNINGS -DD_CRT_SECURE_NO_DEPRECATE) + MESSAGE(STATUS "Adding windows libs to gnuradio osmosdr lib...") + GR_OSMOSDR_APPEND_LIBS(WS2_32.lib WSock32.lib) +endif(HAVE_WINDOWS_H) + +######################################################################## +# Need to link with librt for shm_* +######################################################################## +if(LINUX) + GR_OSMOSDR_APPEND_LIBS(rt) +endif() + +endif(ENABLE_RUNTIME) + ######################################################################## # Setup IQBalance component ######################################################################## @@ -169,3 +307,8 @@ ADD_LIBRARY(gnuradio-osmosdr SHARED ${gr_osmosdr_srcs}) TARGET_LINK_LIBRARIES(gnuradio-osmosdr ${gr_osmosdr_libs}) SET_TARGET_PROPERTIES(gnuradio-osmosdr PROPERTIES DEFINE_SYMBOL "gnuradio_osmosdr_EXPORTS") GR_LIBRARY_FOO(gnuradio-osmosdr) + +if(ENABLE_RUNTIME) + SET_TARGET_PROPERTIES(gnuradio-osmosdr PROPERTIES DEFINE_SYMBOL "gnuradio_runtime_EXPORTS") + SET_TARGET_PROPERTIES(gnuradio-osmosdr PROPERTIES DEFINE_SYMBOL "gnuradio_blocks_EXPORTS") +endif(ENABLE_RUNTIME) diff --git a/lib/ConfigChecks.cmake b/lib/ConfigChecks.cmake new file mode 100644 index 0000000..0a7d0d8 --- /dev/null +++ b/lib/ConfigChecks.cmake @@ -0,0 +1,204 @@ +# Copyright 2010-2013 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +######################################################################## +INCLUDE(GrMiscUtils) +INCLUDE(CheckCXXSourceCompiles) + +IF(MSVC) #add this directory for our provided headers +LIST(APPEND CMAKE_REQUIRED_INCLUDES ${CMAKE_SOURCE_DIR}/msvc) +ENDIF(MSVC) + +#GR_CHECK_HDR_N_DEF(netdb.h HAVE_NETDB_H) +#GR_CHECK_HDR_N_DEF(sys/time.h HAVE_SYS_TIME_H) +GR_CHECK_HDR_N_DEF(sys/types.h HAVE_SYS_TYPES_H) +#GR_CHECK_HDR_N_DEF(sys/select.h HAVE_SYS_SELECT_H) +#GR_CHECK_HDR_N_DEF(sys/socket.h HAVE_SYS_SOCKET_H) +#GR_CHECK_HDR_N_DEF(sys/resource.h HAVE_SYS_RESOURCE_H) +GR_CHECK_HDR_N_DEF(io.h HAVE_IO_H) +GR_CHECK_HDR_N_DEF(sys/mman.h HAVE_SYS_MMAN_H) +GR_CHECK_HDR_N_DEF(sys/ipc.h HAVE_SYS_IPC_H) +GR_CHECK_HDR_N_DEF(sys/shm.h HAVE_SYS_SHM_H) +GR_CHECK_HDR_N_DEF(signal.h HAVE_SIGNAL_H) +#GR_CHECK_HDR_N_DEF(netinet/in.h HAVE_NETINET_IN_H) +#GR_CHECK_HDR_N_DEF(arpa/inet.h HAVE_ARPA_INET_H) +#GR_CHECK_HDR_N_DEF(byteswap.h HAVE_BYTESWAP_H) +#GR_CHECK_HDR_N_DEF(linux/ppdev.h HAVE_LINUX_PPDEV_H) +#GR_CHECK_HDR_N_DEF(dev/ppbus/ppi.h HAVE_DEV_PPBUS_PPI_H) +GR_CHECK_HDR_N_DEF(unistd.h HAVE_UNISTD_H) +#GR_CHECK_HDR_N_DEF(malloc.h HAVE_MALLOC_H) + + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){snprintf(0, 0, 0); return 0;} + " HAVE_SNPRINTF +) +GR_ADD_COND_DEF(HAVE_SNPRINTF) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){sigaction(0, 0, 0); return 0;} + " HAVE_SIGACTION +) +GR_ADD_COND_DEF(HAVE_SIGACTION) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){select(0, 0, 0, 0, 0); return 0;} + " HAVE_SELECT +) +GR_ADD_COND_DEF(HAVE_SELECT) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){sysconf(0); return 0;} + " HAVE_SYSCONF +) +GR_ADD_COND_DEF(HAVE_SYSCONF) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){getpagesize(); return 0;} + " HAVE_GETPAGESIZE +) +GR_ADD_COND_DEF(HAVE_GETPAGESIZE) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){Sleep(0); return 0;} + " HAVE_SSLEEP +) +GR_ADD_COND_DEF(HAVE_SSLEEP) + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){nanosleep(0, 0); return 0;} + " HAVE_NANOSLEEP +) +GR_ADD_COND_DEF(HAVE_NANOSLEEP) + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){gettimeofday(0, 0); return 0;} + " HAVE_GETTIMEOFDAY +) +GR_ADD_COND_DEF(HAVE_GETTIMEOFDAY) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){posix_memalign(0, 0, 0); return 0;} + " HAVE_POSIX_MEMALIGN +) +GR_ADD_COND_DEF(HAVE_POSIX_MEMALIGN) + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){valloc(0); return 0;} + " HAVE_VALLOC +) +GR_ADD_COND_DEF(HAVE_VALLOC) + +ADD_DEFINITIONS(-DALIGNED_MALLOC=0) + +######################################################################## +SET(CMAKE_REQUIRED_LIBRARIES -lpthread) +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){pthread_sigmask(0, 0, 0); return 0;} + " HAVE_PTHREAD_SIGMASK +) +GR_ADD_COND_DEF(HAVE_PTHREAD_SIGMASK) +SET(CMAKE_REQUIRED_LIBRARIES) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){ + HANDLE handle; + int size; + LPCTSTR lpName; + handle = CreateFileMapping( + INVALID_HANDLE_VALUE, // use paging file + NULL, // default security + PAGE_READWRITE, // read/write access + 0, // max. object size + size, // buffer size + lpName); // name of mapping object + return 0; + } " HAVE_CREATEFILEMAPPING +) +GR_ADD_COND_DEF(HAVE_CREATEFILEMAPPING) + +######################################################################## +SET(CMAKE_REQUIRED_LIBRARIES -lrt) +CHECK_CXX_SOURCE_COMPILES(" + #include + #include + int main(){shm_open(0, 0, 0); return 0;} + " HAVE_SHM_OPEN +) +GR_ADD_COND_DEF(HAVE_SHM_OPEN) +SET(CMAKE_REQUIRED_LIBRARIES) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #define _GNU_SOURCE + #include + int main(){double x, sin, cos; sincos(x, &sin, &cos); return 0;} + " HAVE_SINCOS +) +GR_ADD_COND_DEF(HAVE_SINCOS) + +CHECK_CXX_SOURCE_COMPILES(" + #define _GNU_SOURCE + #include + int main(){float x, sin, cos; sincosf(x, &sin, &cos); return 0;} + " HAVE_SINCOSF +) +GR_ADD_COND_DEF(HAVE_SINCOSF) + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){sinf(0); return 0;} + " HAVE_SINF +) +GR_ADD_COND_DEF(HAVE_SINF) + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){cosf(0); return 0;} + " HAVE_COSF +) +GR_ADD_COND_DEF(HAVE_COSF) + +######################################################################## +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){mmap(0, 0, 0, 0, 0, 0); return 0;} + " HAVE_MMAP +) +GR_ADD_COND_DEF(HAVE_MMAP) diff --git a/lib/runtime/basic_block.cc b/lib/runtime/basic_block.cc new file mode 100644 index 0000000..3fa97b1 --- /dev/null +++ b/lib/runtime/basic_block.cc @@ -0,0 +1,77 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2012-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +namespace gr { + + static long s_next_id = 0; + static long s_ncurrently_allocated = 0; + + long + basic_block_ncurrently_allocated() + { + return s_ncurrently_allocated; + } + + basic_block::basic_block(const std::string &name, + io_signature::sptr input_signature, + io_signature::sptr output_signature) + : d_name(name), + d_input_signature(input_signature), + d_output_signature(output_signature), + d_unique_id(s_next_id++), + d_symbolic_id(global_block_registry.block_register(this)), + d_symbol_name(global_block_registry.register_symbolic_name(this)), + d_color(WHITE), + d_rpc_set(false) + { + s_ncurrently_allocated++; + } + + basic_block::~basic_block() + { + s_ncurrently_allocated--; + global_block_registry.block_unregister(this); + } + + basic_block_sptr + basic_block::to_basic_block() + { + return shared_from_this(); + } + + void + basic_block::set_block_alias(std::string name) + { + global_block_registry.register_symbolic_name(this, name); + } + +} /* namespace gr */ diff --git a/lib/runtime/block.cc b/lib/runtime/block.cc new file mode 100644 index 0000000..c2f32e4 --- /dev/null +++ b/lib/runtime/block.cc @@ -0,0 +1,626 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2009,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include + +namespace gr { + + block::block(const std::string &name, + io_signature::sptr input_signature, + io_signature::sptr output_signature) + : basic_block(name, input_signature, output_signature), + d_output_multiple (1), + d_output_multiple_set(false), + d_unaligned(0), + d_is_unaligned(false), + d_relative_rate (1.0), + d_history(1), + d_fixed_rate(false), + d_max_noutput_items_set(false), + d_max_noutput_items(0), + d_min_noutput_items(0), + d_priority(-1), + d_pc_rpc_set(false), + d_max_output_buffer(std::max(output_signature->max_streams(),1), -1), + d_min_output_buffer(std::max(output_signature->max_streams(),1), -1) + { + global_block_registry.register_primitive(alias(), this); + +#ifdef ENABLE_GR_LOG +#ifdef HAVE_LOG4CPP + prefs *p = prefs::singleton(); + std::string config_file = p->get_string("LOG", "log_config", ""); + std::string log_level = p->get_string("LOG", "log_level", "off"); + std::string log_file = p->get_string("LOG", "log_file", ""); + std::string debug_level = p->get_string("LOG", "debug_level", "off"); + std::string debug_file = p->get_string("LOG", "debug_file", ""); + + GR_CONFIG_LOGGER(config_file); + + GR_LOG_GETLOGGER(LOG, "gr_log." + alias()); + GR_LOG_SET_LEVEL(LOG, log_level); + if(log_file.size() > 0) { + if(log_file == "stdout") { + GR_LOG_ADD_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n"); + } + else if(log_file == "stderr") { + GR_LOG_ADD_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n"); + } + else { + GR_LOG_ADD_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n"); + } + } + d_logger = LOG; + + GR_LOG_GETLOGGER(DLOG, "gr_log_debug." + alias()); + GR_LOG_SET_LEVEL(DLOG, debug_level); + if(debug_file.size() > 0) { + if(debug_file == "stdout") { + GR_LOG_ADD_CONSOLE_APPENDER(DLOG, "cout","gr::debug :%p: %c{1} - %m%n"); + } + else if(debug_file == "stderr") { + GR_LOG_ADD_CONSOLE_APPENDER(DLOG, "cerr", "gr::debug :%p: %c{1} - %m%n"); + } + else { + GR_LOG_ADD_FILE_APPENDER(DLOG, debug_file, true, "%r :%p: %c{1} - %m%n"); + } + } + d_debug_logger = DLOG; +#endif /* HAVE_LOG4CPP */ +#else /* ENABLE_GR_LOG */ + d_logger = NULL; + d_debug_logger = NULL; +#endif /* ENABLE_GR_LOG */ + } + + block::~block() + { + global_block_registry.unregister_primitive(alias()); + } + + // stub implementation: 1:1 + + void + block::forecast(int noutput_items, gr_vector_int &ninput_items_required) + { + unsigned ninputs = ninput_items_required.size (); + for(unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = noutput_items + history() - 1; + } + + // default implementation + + bool + block::start() + { + return true; + } + + bool + block::stop() + { + return true; + } + + void + block::set_output_multiple(int multiple) + { + if(multiple < 1) + throw std::invalid_argument("block::set_output_multiple"); + + d_output_multiple_set = true; + d_output_multiple = multiple; + } + + void + block::set_alignment(int multiple) + { + if(multiple < 1) + throw std::invalid_argument("block::set_alignment_multiple"); + + d_output_multiple = multiple; + } + + void + block::set_unaligned(int na) + { + // unaligned value must be less than 0 and it doesn't make sense + // that it's larger than the alignment value. + if((na < 0) || (na > d_output_multiple)) + throw std::invalid_argument("block::set_unaligned"); + + d_unaligned = na; + } + + void + block::set_is_unaligned(bool u) + { + d_is_unaligned = u; + } + + void + block::set_relative_rate(double relative_rate) + { + if(relative_rate < 0.0) + throw std::invalid_argument("block::set_relative_rate"); + + d_relative_rate = relative_rate; + } + + void + block::consume(int which_input, int how_many_items) + { + d_detail->consume(which_input, how_many_items); + } + + void + block::consume_each(int how_many_items) + { + d_detail->consume_each(how_many_items); + } + + void + block::produce(int which_output, int how_many_items) + { + d_detail->produce(which_output, how_many_items); + } + + int + block::fixed_rate_ninput_to_noutput(int ninput) + { + throw std::runtime_error("Unimplemented"); + } + + int + block::fixed_rate_noutput_to_ninput(int noutput) + { + throw std::runtime_error("Unimplemented"); + } + + uint64_t + block::nitems_read(unsigned int which_input) + { + if(d_detail) { + return d_detail->nitems_read(which_input); + } + else { + //throw std::runtime_error("No block_detail associated with block yet"); + return 0; + } + } + + uint64_t + block::nitems_written(unsigned int which_output) + { + if(d_detail) { + return d_detail->nitems_written(which_output); + } + else { + //throw std::runtime_error("No block_detail associated with block yet"); + return 0; + } + } + + int + block::max_noutput_items() + { + return d_max_noutput_items; + } + + void + block::set_max_noutput_items(int m) + { + if(m <= 0) + throw std::runtime_error("block::set_max_noutput_items: value for max_noutput_items must be greater than 0.\n"); + + d_max_noutput_items = m; + d_max_noutput_items_set = true; + } + + void + block::unset_max_noutput_items() + { + d_max_noutput_items_set = false; + } + + bool + block::is_set_max_noutput_items() + { + return d_max_noutput_items_set; + } + + void + block::set_processor_affinity(const std::vector &mask) + { + d_affinity = mask; + if(d_detail) { + d_detail->set_processor_affinity(d_affinity); + } + } + + void + block::unset_processor_affinity() + { + d_affinity.clear(); + if(d_detail) { + d_detail->unset_processor_affinity(); + } + } + + int + block::active_thread_priority() + { + if(d_detail) { + return d_detail->thread_priority(); + } + return -1; + } + + int + block::thread_priority() + { + return d_priority; + } + + int + block::set_thread_priority(int priority) + { + d_priority = priority; + if(d_detail) { + return d_detail->set_thread_priority(priority); + } + return d_priority; + } + + void + block::expand_minmax_buffer(int port) + { + if((size_t)port >= d_max_output_buffer.size()) + set_max_output_buffer(port, -1); + if((size_t)port >= d_min_output_buffer.size()) + set_min_output_buffer(port, -1); + } + + long + block::max_output_buffer(size_t i) + { + if(i >= d_max_output_buffer.size()) + throw std::invalid_argument("basic_block::max_output_buffer: port out of range."); + return d_max_output_buffer[i]; + } + + void + block::set_max_output_buffer(long max_output_buffer) + { + for(int i = 0; i < output_signature()->max_streams(); i++) { + set_max_output_buffer(i, max_output_buffer); + } + } + + void + block::set_max_output_buffer(int port, long max_output_buffer) + { + if((size_t)port >= d_max_output_buffer.size()) + d_max_output_buffer.push_back(max_output_buffer); + else + d_max_output_buffer[port] = max_output_buffer; + } + + long + block::min_output_buffer(size_t i) + { + if(i >= d_min_output_buffer.size()) + throw std::invalid_argument("basic_block::min_output_buffer: port out of range."); + return d_min_output_buffer[i]; + } + + void + block::set_min_output_buffer(long min_output_buffer) + { + std::cout << "set_min_output_buffer on block " << unique_id() << " to " << min_output_buffer << std::endl; + for(int i=0; imax_streams(); i++) { + set_min_output_buffer(i, min_output_buffer); + } + } + + void + block::set_min_output_buffer(int port, long min_output_buffer) + { + if((size_t)port >= d_min_output_buffer.size()) + d_min_output_buffer.push_back(min_output_buffer); + else + d_min_output_buffer[port] = min_output_buffer; + } + + float + block::pc_noutput_items() + { + if(d_detail) { + return d_detail->pc_noutput_items(); + } + else { + return 0; + } + } + + float + block::pc_noutput_items_avg() + { + if(d_detail) { + return d_detail->pc_noutput_items_avg(); + } + else { + return 0; + } + } + + float + block::pc_noutput_items_var() + { + if(d_detail) { + return d_detail->pc_noutput_items_var(); + } + else { + return 0; + } + } + + float + block::pc_nproduced() + { + if(d_detail) { + return d_detail->pc_nproduced(); + } + else { + return 0; + } + } + + float + block::pc_nproduced_avg() + { + if(d_detail) { + return d_detail->pc_nproduced_avg(); + } + else { + return 0; + } + } + + float + block::pc_nproduced_var() + { + if(d_detail) { + return d_detail->pc_nproduced_var(); + } + else { + return 0; + } + } + + float + block::pc_input_buffers_full(int which) + { + if(d_detail) { + return d_detail->pc_input_buffers_full(static_cast(which)); + } + else { + return 0; + } + } + + float + block::pc_input_buffers_full_avg(int which) + { + if(d_detail) { + return d_detail->pc_input_buffers_full_avg(static_cast(which)); + } + else { + return 0; + } + } + + float + block::pc_input_buffers_full_var(int which) + { + if(d_detail) { + return d_detail->pc_input_buffers_full_var(static_cast(which)); + } + else { + return 0; + } + } + + std::vector + block::pc_input_buffers_full() + { + if(d_detail) { + return d_detail->pc_input_buffers_full(); + } + else { + return std::vector(1,0); + } + } + + std::vector + block::pc_input_buffers_full_avg() + { + if(d_detail) { + return d_detail->pc_input_buffers_full_avg(); + } + else { + return std::vector(1,0); + } + } + + std::vector + block::pc_input_buffers_full_var() + { + if(d_detail) { + return d_detail->pc_input_buffers_full_var(); + } + else { + return std::vector(1,0); + } + } + + float + block::pc_output_buffers_full(int which) + { + if(d_detail) { + return d_detail->pc_output_buffers_full(static_cast(which)); + } + else { + return 0; + } + } + + float + block::pc_output_buffers_full_avg(int which) + { + if(d_detail) { + return d_detail->pc_output_buffers_full_avg(static_cast(which)); + } + else { + return 0; + } + } + + float + block::pc_output_buffers_full_var(int which) + { + if(d_detail) { + return d_detail->pc_output_buffers_full_var(static_cast(which)); + } + else { + return 0; + } + } + + std::vector + block::pc_output_buffers_full() + { + if(d_detail) { + return d_detail->pc_output_buffers_full(); + } + else { + return std::vector(1,0); + } + } + + std::vector + block::pc_output_buffers_full_avg() + { + if(d_detail) { + return d_detail->pc_output_buffers_full_avg(); + } + else { + return std::vector(1,0); + } + } + + std::vector + block::pc_output_buffers_full_var() + { + if(d_detail) { + return d_detail->pc_output_buffers_full_var(); + } + else { + return std::vector(1,0); + } + } + + float + block::pc_work_time() + { + if(d_detail) { + return d_detail->pc_work_time(); + } + else { + return 0; + } + } + + float + block::pc_work_time_avg() + { + if(d_detail) { + return d_detail->pc_work_time_avg(); + } + else { + return 0; + } + } + + float + block::pc_work_time_var() + { + if(d_detail) { + return d_detail->pc_work_time_var(); + } + else { + return 0; + } + } + + void + block::reset_perf_counters() + { + if(d_detail) { + d_detail->reset_perf_counters(); + } + } + + void + block::setup_pc_rpc() + { + d_pc_rpc_set = true; + + } + + std::ostream& + operator << (std::ostream& os, const block *m) + { + os << "name() << " (" << m->unique_id() << ")>"; + return os; + } + + int + block::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + throw std::runtime_error("block::general_work() not implemented"); + return 0; + } + +} /* namespace gr */ diff --git a/lib/runtime/block_detail.cc b/lib/runtime/block_detail.cc new file mode 100644 index 0000000..7e3e1a2 --- /dev/null +++ b/lib/runtime/block_detail.cc @@ -0,0 +1,435 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2009,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +namespace gr { + + static long s_ncurrently_allocated = 0; + + long + block_detail_ncurrently_allocated() + { + return s_ncurrently_allocated; + } + + block_detail::block_detail(unsigned int ninputs, unsigned int noutputs) + : d_produce_or(0), + d_ninputs(ninputs), d_noutputs(noutputs), + d_input(ninputs), d_output(noutputs), + d_done(false), + d_ins_noutput_items(0), + d_avg_noutput_items(0), + d_var_noutput_items(0), + d_ins_nproduced(0), + d_avg_nproduced(0), + d_var_nproduced(0), + d_ins_input_buffers_full(ninputs, 0), + d_avg_input_buffers_full(ninputs, 0), + d_var_input_buffers_full(ninputs, 0), + d_ins_output_buffers_full(noutputs, 0), + d_avg_output_buffers_full(noutputs, 0), + d_var_output_buffers_full(noutputs, 0), + d_ins_work_time(0), + d_avg_work_time(0), + d_var_work_time(0), + d_pc_counter(0) + { + s_ncurrently_allocated++; + } + + block_detail::~block_detail() + { + // should take care of itself + s_ncurrently_allocated--; + } + + void + block_detail::set_input(unsigned int which, buffer_reader_sptr reader) + { + if(which >= d_ninputs) + throw std::invalid_argument("block_detail::set_input"); + + d_input[which] = reader; + } + + void + block_detail::set_output(unsigned int which, buffer_sptr buffer) + { + if(which >= d_noutputs) + throw std::invalid_argument("block_detail::set_output"); + + d_output[which] = buffer; + } + + block_detail_sptr + make_block_detail(unsigned int ninputs, unsigned int noutputs) + { + return block_detail_sptr (new block_detail(ninputs, noutputs)); + } + + void + block_detail::set_done(bool done) + { + d_done = done; + for(unsigned int i = 0; i < d_noutputs; i++) + d_output[i]->set_done(done); + + for(unsigned int i = 0; i < d_ninputs; i++) + d_input[i]->set_done(done); + } + + void + block_detail::consume(int which_input, int how_many_items) + { + if(how_many_items > 0) { + input(which_input)->update_read_pointer(how_many_items); + } + } + + void + block_detail::consume_each(int how_many_items) + { + if(how_many_items > 0) { + for(int i = 0; i < ninputs (); i++) { + d_input[i]->update_read_pointer(how_many_items); + } + } + } + + void + block_detail::produce(int which_output, int how_many_items) + { + if(how_many_items > 0) { + d_output[which_output]->update_write_pointer(how_many_items); + d_produce_or |= how_many_items; + } + } + + void + block_detail::produce_each(int how_many_items) + { + if(how_many_items > 0) { + for(int i = 0; i < noutputs (); i++) { + d_output[i]->update_write_pointer (how_many_items); + } + d_produce_or |= how_many_items; + } + } + + uint64_t + block_detail::nitems_read(unsigned int which_input) + { + if(which_input >= d_ninputs) + throw std::invalid_argument ("block_detail::n_input_items"); + return d_input[which_input]->nitems_read(); + } + + uint64_t + block_detail::nitems_written(unsigned int which_output) + { + if(which_output >= d_noutputs) + throw std::invalid_argument ("block_detail::n_output_items"); + return d_output[which_output]->nitems_written(); + } + + void + block_detail::set_processor_affinity(const std::vector &mask) + { + if(threaded) { + try { + gr::thread::thread_bind_to_processor(thread, mask); + } + catch (std::runtime_error e) { + std::cerr << "set_processor_affinity: invalid mask." << std::endl;; + } + } + } + + void + block_detail::unset_processor_affinity() + { + if(threaded) { + gr::thread::thread_unbind(thread); + } + } + + int + block_detail::thread_priority(){ + if(threaded) { + return gr::thread::thread_priority(thread); + } + return -1; + } + + int + block_detail::set_thread_priority(int priority){ + if(threaded) { + return gr::thread::set_thread_priority(thread,priority); + } + return -1; + } + + void + block_detail::start_perf_counters() + { + d_start_of_work = gr::high_res_timer_now_perfmon(); + } + + void + block_detail::stop_perf_counters(int noutput_items, int nproduced) + { + d_end_of_work = gr::high_res_timer_now_perfmon(); + gr::high_res_timer_type diff = d_end_of_work - d_start_of_work; + + if(d_pc_counter == 0) { + d_ins_work_time = diff; + d_avg_work_time = diff; + d_var_work_time = 0; + d_ins_nproduced = nproduced; + d_avg_nproduced = nproduced; + d_var_nproduced = 0; + d_ins_noutput_items = noutput_items; + d_avg_noutput_items = noutput_items; + d_var_noutput_items = 0; + for(size_t i=0; i < d_input.size(); i++) { + gr::thread::scoped_lock guard(*d_input[i]->mutex()); + float pfull = static_cast(d_input[i]->items_available()) / + static_cast(d_input[i]->max_possible_items_available()); + d_ins_input_buffers_full[i] = pfull; + d_avg_input_buffers_full[i] = pfull; + d_var_input_buffers_full[i] = 0; + } + for(size_t i=0; i < d_output.size(); i++) { + gr::thread::scoped_lock guard(*d_output[i]->mutex()); + float pfull = 1.0f - static_cast(d_output[i]->space_available()) / + static_cast(d_output[i]->bufsize()); + d_ins_output_buffers_full[i] = pfull; + d_avg_output_buffers_full[i] = pfull; + d_var_output_buffers_full[i] = 0; + } + } + else { + float d = diff - d_avg_work_time; + d_ins_work_time = diff; + d_avg_work_time = d_avg_work_time + d/d_pc_counter; + d_var_work_time = d_var_work_time + d*d; + + d = nproduced - d_avg_nproduced; + d_ins_nproduced = nproduced; + d_avg_nproduced = d_avg_nproduced + d/d_pc_counter; + d_var_nproduced = d_var_nproduced + d*d; + + d = noutput_items - d_avg_noutput_items; + d_ins_noutput_items = noutput_items; + d_avg_noutput_items = d_avg_noutput_items + d/d_pc_counter; + d_var_noutput_items = d_var_noutput_items + d*d; + + for(size_t i=0; i < d_input.size(); i++) { + gr::thread::scoped_lock guard(*d_input[i]->mutex()); + float pfull = static_cast(d_input[i]->items_available()) / + static_cast(d_input[i]->max_possible_items_available()); + + d = pfull - d_avg_input_buffers_full[i]; + d_ins_input_buffers_full[i] = pfull; + d_avg_input_buffers_full[i] = d_avg_input_buffers_full[i] + d/d_pc_counter; + d_var_input_buffers_full[i] = d_var_input_buffers_full[i] + d*d; + } + + for(size_t i=0; i < d_output.size(); i++) { + gr::thread::scoped_lock guard(*d_output[i]->mutex()); + float pfull = 1.0f - static_cast(d_output[i]->space_available()) / + static_cast(d_output[i]->bufsize()); + + d = pfull - d_avg_output_buffers_full[i]; + d_ins_output_buffers_full[i] = pfull; + d_avg_output_buffers_full[i] = d_avg_output_buffers_full[i] + d/d_pc_counter; + d_var_output_buffers_full[i] = d_var_output_buffers_full[i] + d*d; + } + } + + d_pc_counter++; + } + + void + block_detail::reset_perf_counters() + { + d_pc_counter = 0; + } + + float + block_detail::pc_noutput_items() + { + return d_ins_noutput_items; + } + + float + block_detail::pc_nproduced() + { + return d_ins_nproduced; + } + + float + block_detail::pc_input_buffers_full(size_t which) + { + if(which < d_ins_input_buffers_full.size()) + return d_ins_input_buffers_full[which]; + else + return 0; + } + + std::vector + block_detail::pc_input_buffers_full() + { + return d_ins_input_buffers_full; + } + + float + block_detail::pc_output_buffers_full(size_t which) + { + if(which < d_ins_output_buffers_full.size()) + return d_ins_output_buffers_full[which]; + else + return 0; + } + + std::vector + block_detail::pc_output_buffers_full() + { + return d_ins_output_buffers_full; + } + + float + block_detail::pc_work_time() + { + return d_ins_work_time; + } + + float + block_detail::pc_noutput_items_avg() + { + return d_avg_noutput_items; + } + + float + block_detail::pc_nproduced_avg() + { + return d_avg_nproduced; + } + + float + block_detail::pc_input_buffers_full_avg(size_t which) + { + if(which < d_avg_input_buffers_full.size()) + return d_avg_input_buffers_full[which]; + else + return 0; + } + + std::vector + block_detail::pc_input_buffers_full_avg() + { + return d_avg_input_buffers_full; + } + + float + block_detail::pc_output_buffers_full_avg(size_t which) + { + if(which < d_avg_output_buffers_full.size()) + return d_avg_output_buffers_full[which]; + else + return 0; + } + + std::vector + block_detail::pc_output_buffers_full_avg() + { + return d_avg_output_buffers_full; + } + + float + block_detail::pc_work_time_avg() + { + return d_avg_work_time; + } + + float + block_detail::pc_noutput_items_var() + { + return d_var_noutput_items/(d_pc_counter-1); + } + + float + block_detail::pc_nproduced_var() + { + return d_var_nproduced/(d_pc_counter-1); + } + + float + block_detail::pc_input_buffers_full_var(size_t which) + { + if(which < d_avg_input_buffers_full.size()) + return d_var_input_buffers_full[which]/(d_pc_counter-1); + else + return 0; + } + + std::vector + block_detail::pc_input_buffers_full_var() + { + std::vector var(d_avg_input_buffers_full.size(), 0); + for(size_t i = 0; i < d_avg_input_buffers_full.size(); i++) + var[i] = d_avg_input_buffers_full[i]/(d_pc_counter-1); + return var; + } + + float + block_detail::pc_output_buffers_full_var(size_t which) + { + if(which < d_avg_output_buffers_full.size()) + return d_var_output_buffers_full[which]/(d_pc_counter-1); + else + return 0; + } + + std::vector + block_detail::pc_output_buffers_full_var() + { + std::vector var(d_avg_output_buffers_full.size(), 0); + for(size_t i = 0; i < d_avg_output_buffers_full.size(); i++) + var[i] = d_avg_output_buffers_full[i]/(d_pc_counter-1); + return var; + } + + float + block_detail::pc_work_time_var() + { + return d_var_work_time/(d_pc_counter-1); + } + +} /* namespace gr */ diff --git a/lib/runtime/block_executor.cc b/lib/runtime/block_executor.cc new file mode 100644 index 0000000..c1ac04c --- /dev/null +++ b/lib/runtime/block_executor.cc @@ -0,0 +1,419 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2008-2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gr { + +// must be defined to either 0 or 1 +#define ENABLE_LOGGING 0 + +#if (ENABLE_LOGGING) +#define LOG(x) do { x; } while(0) +#else +#define LOG(x) do {;} while(0) +#endif + + static int which_scheduler = 0; + + inline static unsigned int + round_up(unsigned int n, unsigned int multiple) + { + return ((n + multiple - 1) / multiple) * multiple; + } + + inline static unsigned int + round_down(unsigned int n, unsigned int multiple) + { + return (n / multiple) * multiple; + } + + // + // Return minimum available write space in all our downstream + // buffers or -1 if we're output blocked and the output we're + // blocked on is done. + // + static int + min_available_space(block_detail *d, int output_multiple, int min_noutput_items) + { + int min_space = std::numeric_limits::max(); + if(min_noutput_items == 0) + min_noutput_items = 1; + for(int i = 0; i < d->noutputs (); i++) { + gr::thread::scoped_lock guard(*d->output(i)->mutex()); + int avail_n = round_down(d->output(i)->space_available(), output_multiple); + int best_n = round_down(d->output(i)->bufsize()/2, output_multiple); + if(best_n < min_noutput_items) + throw std::runtime_error("Buffer too small for min_noutput_items"); + int n = std::min(avail_n, best_n); + if(n < min_noutput_items){ // We're blocked on output. + if(d->output(i)->done()){ // Downstream is done, therefore we're done. + return -1; + } + return 0; + } + min_space = std::min(min_space, n); + } + return min_space; + } + + block_executor::block_executor(block_sptr block, int max_noutput_items) + : d_block(block), d_log(0), d_max_noutput_items(max_noutput_items) + { + if(ENABLE_LOGGING) { + std::string name = str(boost::format("sst-%03d.log") % which_scheduler++); + d_log = new std::ofstream(name.c_str()); + std::unitbuf(*d_log); // make it unbuffered... + *d_log << "block_executor: " + << d_block << std::endl; + } + +#ifdef GR_PERFORMANCE_COUNTERS + prefs *prefs = prefs::singleton(); + d_use_pc = prefs->get_bool("PerfCounters", "on", false); +#endif /* GR_PERFORMANCE_COUNTERS */ + + d_block->start(); // enable any drivers, etc. + } + + block_executor::~block_executor() + { + if(ENABLE_LOGGING) + delete d_log; + + d_block->stop(); // stop any drivers, etc. + } + + block_executor::state + block_executor::run_one_iteration() + { + int noutput_items; + int max_items_avail; + int max_noutput_items; + int new_alignment = 0; + int alignment_state = -1; + + block *m = d_block.get(); + block_detail *d = m->detail().get(); + + LOG(*d_log << std::endl << m); + + max_noutput_items = round_down(d_max_noutput_items, m->output_multiple()); + + if(d->done()){ + assert(0); + return DONE; + } + + if(d->source_p ()) { + d_ninput_items_required.resize(0); + d_ninput_items.resize(0); + d_input_items.resize(0); + d_input_done.resize(0); + d_output_items.resize(d->noutputs()); + d_start_nitems_read.resize(0); + + // determine the minimum available output space + noutput_items = min_available_space(d, m->output_multiple (), m->min_noutput_items ()); + noutput_items = std::min(noutput_items, max_noutput_items); + LOG(*d_log << " source\n noutput_items = " << noutput_items << std::endl); + if(noutput_items == -1) // we're done + goto were_done; + + if(noutput_items == 0){ // we're output blocked + LOG(*d_log << " BLKD_OUT\n"); + return BLKD_OUT; + } + + goto setup_call_to_work; // jump to common code + } + + else if(d->sink_p ()) { + d_ninput_items_required.resize(d->ninputs ()); + d_ninput_items.resize(d->ninputs ()); + d_input_items.resize(d->ninputs ()); + d_input_done.resize(d->ninputs()); + d_output_items.resize (0); + d_start_nitems_read.resize(d->ninputs()); + LOG(*d_log << " sink\n"); + + max_items_avail = 0; + for(int i = 0; i < d->ninputs (); i++) { + { + /* + * Acquire the mutex and grab local copies of items_available and done. + */ + gr::thread::scoped_lock guard(*d->input(i)->mutex()); + d_ninput_items[i] = d->input(i)->items_available(); + d_input_done[i] = d->input(i)->done(); + } + + LOG(*d_log << " d_ninput_items[" << i << "] = " << d_ninput_items[i] << std::endl); + LOG(*d_log << " d_input_done[" << i << "] = " << d_input_done[i] << std::endl); + + if (d_ninput_items[i] < m->output_multiple() && d_input_done[i]) + goto were_done; + + max_items_avail = std::max (max_items_avail, d_ninput_items[i]); + } + + // take a swag at how much output we can sink + noutput_items = (int)(max_items_avail * m->relative_rate ()); + noutput_items = round_down(noutput_items, m->output_multiple ()); + noutput_items = std::min(noutput_items, max_noutput_items); + LOG(*d_log << " max_items_avail = " << max_items_avail << std::endl); + LOG(*d_log << " noutput_items = " << noutput_items << std::endl); + + if(noutput_items == 0) { // we're blocked on input + LOG(*d_log << " BLKD_IN\n"); + return BLKD_IN; + } + + goto try_again; // Jump to code shared with regular case. + } + + else { + // do the regular thing + d_ninput_items_required.resize (d->ninputs ()); + d_ninput_items.resize (d->ninputs ()); + d_input_items.resize (d->ninputs ()); + d_input_done.resize(d->ninputs()); + d_output_items.resize (d->noutputs ()); + d_start_nitems_read.resize(d->ninputs()); + + max_items_avail = 0; + for(int i = 0; i < d->ninputs (); i++) { + { + /* + * Acquire the mutex and grab local copies of items_available and done. + */ + gr::thread::scoped_lock guard(*d->input(i)->mutex()); + d_ninput_items[i] = d->input(i)->items_available (); + d_input_done[i] = d->input(i)->done(); + } + max_items_avail = std::max(max_items_avail, d_ninput_items[i]); + } + + // determine the minimum available output space + noutput_items = min_available_space(d, m->output_multiple(), m->min_noutput_items()); + if(ENABLE_LOGGING) { + *d_log << " regular "; + if(m->relative_rate() >= 1.0) + *d_log << "1:" << m->relative_rate() << std::endl; + else + *d_log << 1.0/m->relative_rate() << ":1\n"; + *d_log << " max_items_avail = " << max_items_avail << std::endl; + *d_log << " noutput_items = " << noutput_items << std::endl; + } + if(noutput_items == -1) // we're done + goto were_done; + + if(noutput_items == 0) { // we're output blocked + LOG(*d_log << " BLKD_OUT\n"); + return BLKD_OUT; + } + + try_again: + if(m->fixed_rate()) { + // try to work it forward starting with max_items_avail. + // We want to try to consume all the input we've got. + int reqd_noutput_items = m->fixed_rate_ninput_to_noutput(max_items_avail); + + // only test this if we specifically set the output_multiple + if(m->output_multiple_set()) + reqd_noutput_items = round_down(reqd_noutput_items, m->output_multiple()); + + if(reqd_noutput_items > 0 && reqd_noutput_items <= noutput_items) + noutput_items = reqd_noutput_items; + + // if we need this many outputs, overrule the max_noutput_items setting + max_noutput_items = std::max(m->output_multiple(), max_noutput_items); + } + noutput_items = std::min(noutput_items, max_noutput_items); + + // Check if we're still unaligned; use up items until we're + // aligned again. Otherwise, make sure we set the alignment + // requirement. + if(!m->output_multiple_set()) { + if(m->is_unaligned()) { + // When unaligned, don't just set noutput_items to the remaining + // samples to meet alignment; this causes too much overhead in + // requiring a premature call back here. Set the maximum amount + // of samples to handle unalignment and get us back aligned. + if(noutput_items >= m->unaligned()) { + noutput_items = round_up(noutput_items, m->alignment()) \ + - (m->alignment() - m->unaligned()); + new_alignment = 0; + } + else { + new_alignment = m->unaligned() - noutput_items; + } + alignment_state = 0; + } + else if(noutput_items < m->alignment()) { + // if we don't have enough for an aligned call, keep track of + // misalignment, set unaligned flag, and proceed. + new_alignment = m->alignment() - noutput_items; + m->set_unaligned(new_alignment); + m->set_is_unaligned(true); + alignment_state = 1; + } + else { + // enough to round down to the nearest alignment and process. + noutput_items = round_down(noutput_items, m->alignment()); + m->set_is_unaligned(false); + alignment_state = 2; + } + } + + // ask the block how much input they need to produce noutput_items + m->forecast (noutput_items, d_ninput_items_required); + + // See if we've got sufficient input available + int i; + for(i = 0; i < d->ninputs (); i++) + if(d_ninput_items_required[i] > d_ninput_items[i]) // not enough + break; + + if(i < d->ninputs()) { // not enough input on input[i] + // if we can, try reducing the size of our output request + if(noutput_items > m->output_multiple()) { + noutput_items /= 2; + noutput_items = round_up(noutput_items, m->output_multiple()); + goto try_again; + } + + // We're blocked on input + LOG(*d_log << " BLKD_IN\n"); + if(d_input_done[i]) // If the upstream block is done, we're done + goto were_done; + + // Is it possible to ever fulfill this request? + if(d_ninput_items_required[i] > d->input(i)->max_possible_items_available()) { + // Nope, never going to happen... + std::cerr << "\nsched: name() + << " (" << m->unique_id() << ")>" + << " is requesting more input data\n" + << " than we can provide.\n" + << " ninput_items_required = " + << d_ninput_items_required[i] << "\n" + << " max_possible_items_available = " + << d->input(i)->max_possible_items_available() << "\n" + << " If this is a filter, consider reducing the number of taps.\n"; + goto were_done; + } + + // If we were made unaligned in this round but return here without + // processing; reset the unalignment claim before next entry. + if(alignment_state == 1) { + m->set_unaligned(0); + m->set_is_unaligned(false); + } + return BLKD_IN; + } + + // We've got enough data on each input to produce noutput_items. + // Finish setting up the call to work. + for(int i = 0; i < d->ninputs (); i++) + d_input_items[i] = d->input(i)->read_pointer(); + + setup_call_to_work: + + d->d_produce_or = 0; + for(int i = 0; i < d->noutputs (); i++) + d_output_items[i] = d->output(i)->write_pointer(); + + // determine where to start looking for new tags + for(int i = 0; i < d->ninputs(); i++) + d_start_nitems_read[i] = d->nitems_read(i); + +#ifdef GR_PERFORMANCE_COUNTERS + if(d_use_pc) + d->start_perf_counters(); +#endif /* GR_PERFORMANCE_COUNTERS */ + + // Do the actual work of the block + int n = m->general_work(noutput_items, d_ninput_items, + d_input_items, d_output_items); + +#ifdef GR_PERFORMANCE_COUNTERS + if(d_use_pc) + d->stop_perf_counters(noutput_items, n); +#endif /* GR_PERFORMANCE_COUNTERS */ + + LOG(*d_log << " general_work: noutput_items = " << noutput_items + << " result = " << n << std::endl); + + // Adjust number of unaligned items left to process + if(m->is_unaligned()) { + m->set_unaligned(new_alignment); + m->set_is_unaligned(m->unaligned() != 0); + } + + if(n == block::WORK_DONE) + goto were_done; + + if(n != block::WORK_CALLED_PRODUCE) + d->produce_each (n); // advance write pointers + + if(d->d_produce_or > 0) // block produced something + return READY; + + // We didn't produce any output even though we called general_work. + // We have (most likely) consumed some input. + + /* + // If this is a source, it's broken. + if(d->source_p()) { + std::cerr << "block_executor: source " << m + << " produced no output. We're marking it DONE.\n"; + // FIXME maybe we ought to raise an exception... + goto were_done; + } + */ + + // Have the caller try again... + return READY_NO_OUTPUT; + } + assert(0); + + were_done: + LOG(*d_log << " were_done\n"); + d->set_done (true); + return DONE; + } + +} /* namespace gr */ diff --git a/lib/runtime/block_executor.h b/lib/runtime/block_executor.h new file mode 100644 index 0000000..49d12c4 --- /dev/null +++ b/lib/runtime/block_executor.h @@ -0,0 +1,77 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_BLOCK_EXECUTOR_H +#define INCLUDED_GR_RUNTIME_BLOCK_EXECUTOR_H + +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief Manage the execution of a single block. + * \ingroup internal + */ + class GR_RUNTIME_API block_executor + { + protected: + block_sptr d_block; // The block we're trying to run + std::ofstream *d_log; + + // These are allocated here so we don't have to on each iteration + + gr_vector_int d_ninput_items_required; + gr_vector_int d_ninput_items; + gr_vector_const_void_star d_input_items; + std::vector d_input_done; + gr_vector_void_star d_output_items; + std::vector d_start_nitems_read; //stores where tag counts are before work + int d_max_noutput_items; + +#ifdef GR_PERFORMANCE_COUNTERS + bool d_use_pc; +#endif /* GR_PERFORMANCE_COUNTERS */ + + public: + block_executor(block_sptr block, int max_noutput_items=100000); + ~block_executor(); + + enum state { + READY, // We made progress; everything's cool. + READY_NO_OUTPUT, // We consumed some input, but produced no output. + BLKD_IN, // no progress; we're blocked waiting for input data. + BLKD_OUT, // no progress; we're blocked waiting for output buffer space. + DONE, // we're done; don't call me again. + }; + + /* + * \brief Run one iteration. + */ + state run_one_iteration(); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_BLOCK_EXECUTOR_H */ diff --git a/lib/runtime/block_gateway_impl.cc b/lib/runtime/block_gateway_impl.cc new file mode 100644 index 0000000..13f4326 --- /dev/null +++ b/lib/runtime/block_gateway_impl.cc @@ -0,0 +1,186 @@ +/* + * Copyright 2011-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "block_gateway_impl.h" +#include +#include +#include + +namespace gr { + + /*********************************************************************** + * Helper routines + **********************************************************************/ + template + void + copy_pointers(OutType &out, const InType &in) + { + out.resize(in.size()); + for(size_t i = 0; i < in.size(); i++) { + out[i] = (void *)(in[i]); + } + } + + + block_gateway::sptr + block_gateway::make(feval_ll *handler, + const std::string &name, + gr::io_signature::sptr in_sig, + gr::io_signature::sptr out_sig, + const block_gw_work_type work_type, + const unsigned factor) + { + return block_gateway::sptr + (new block_gateway_impl(handler, name, in_sig, out_sig, + work_type, factor)); + } + + block_gateway_impl::block_gateway_impl(feval_ll *handler, + const std::string &name, + gr::io_signature::sptr in_sig, + gr::io_signature::sptr out_sig, + const block_gw_work_type work_type, + const unsigned factor) + : block(name, in_sig, out_sig), + _handler(handler), + _work_type(work_type) + { + switch(_work_type) { + case GR_BLOCK_GW_WORK_GENERAL: + _decim = 1; //not relevant, but set anyway + _interp = 1; //not relevant, but set anyway + break; + + case GR_BLOCK_GW_WORK_SYNC: + _decim = 1; + _interp = 1; + this->set_fixed_rate(true); + break; + + case GR_BLOCK_GW_WORK_DECIM: + _decim = factor; + _interp = 1; + break; + + case GR_BLOCK_GW_WORK_INTERP: + _decim = 1; + _interp = factor; + this->set_output_multiple(_interp); + break; + } + } + + void + block_gateway_impl::forecast(int noutput_items, + gr_vector_int &ninput_items_required) + { + switch(_work_type) { + case GR_BLOCK_GW_WORK_GENERAL: + _message.action = block_gw_message_type::ACTION_FORECAST; + _message.forecast_args_noutput_items = noutput_items; + _message.forecast_args_ninput_items_required = ninput_items_required; + _handler->calleval(0); + ninput_items_required = _message.forecast_args_ninput_items_required; + return; + + default: + unsigned ninputs = ninput_items_required.size(); + for(unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = fixed_rate_noutput_to_ninput(noutput_items); + return; + } + } + + int + block_gateway_impl::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + switch(_work_type) { + case GR_BLOCK_GW_WORK_GENERAL: + _message.action = block_gw_message_type::ACTION_GENERAL_WORK; + _message.general_work_args_noutput_items = noutput_items; + _message.general_work_args_ninput_items = ninput_items; + copy_pointers(_message.general_work_args_input_items, input_items); + _message.general_work_args_output_items = output_items; + _handler->calleval(0); + return _message.general_work_args_return_value; + + default: + int r = work (noutput_items, input_items, output_items); + if(r > 0) + consume_each(r*_decim/_interp); + return r; + } + } + + int + block_gateway_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + _message.action = block_gw_message_type::ACTION_WORK; + _message.work_args_ninput_items = fixed_rate_noutput_to_ninput(noutput_items); + if(_message.work_args_ninput_items == 0) + return -1; + _message.work_args_noutput_items = noutput_items; + copy_pointers(_message.work_args_input_items, input_items); + _message.work_args_output_items = output_items; + _handler->calleval(0); + return _message.work_args_return_value; + } + + int + block_gateway_impl::fixed_rate_noutput_to_ninput(int noutput_items) + { + return (noutput_items*_decim/_interp) + history() - 1; + } + + int + block_gateway_impl::fixed_rate_ninput_to_noutput(int ninput_items) + { + return std::max(0, ninput_items - (int)history() + 1)*_interp/_decim; + } + + bool + block_gateway_impl::start(void) + { + _message.action = block_gw_message_type::ACTION_START; + _handler->calleval(0); + return _message.start_args_return_value; + } + + bool + block_gateway_impl::stop(void) + { + _message.action = block_gw_message_type::ACTION_STOP; + _handler->calleval(0); + return _message.stop_args_return_value; + } + + block_gw_message_type& + block_gateway_impl::block_message(void) + { + return _message; + } + +} /* namespace gr */ diff --git a/lib/runtime/block_gateway_impl.h b/lib/runtime/block_gateway_impl.h new file mode 100644 index 0000000..1707ecf --- /dev/null +++ b/lib/runtime/block_gateway_impl.h @@ -0,0 +1,75 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_RUNTIME_BLOCK_GATEWAY_IMPL_H +#define INCLUDED_RUNTIME_BLOCK_GATEWAY_IMPL_H + +#include + +namespace gr { + + /*********************************************************************** + * The gr::block gateway implementation class + **********************************************************************/ + class block_gateway_impl : public block_gateway + { + public: + block_gateway_impl(feval_ll *handler, + const std::string &name, + gr::io_signature::sptr in_sig, + gr::io_signature::sptr out_sig, + const block_gw_work_type work_type, + const unsigned factor); + + /******************************************************************* + * Overloads for various scheduler-called functions + ******************************************************************/ + void forecast(int noutput_items, + gr_vector_int &ninput_items_required); + + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + int fixed_rate_noutput_to_ninput(int noutput_items); + int fixed_rate_ninput_to_noutput(int ninput_items); + + bool start(void); + bool stop(void); + + block_gw_message_type& block_message(void); + + private: + feval_ll *_handler; + block_gw_message_type _message; + const block_gw_work_type _work_type; + unsigned _decim, _interp; + }; + +} /* namespace gr */ + +#endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */ diff --git a/lib/runtime/block_registry.cc b/lib/runtime/block_registry.cc new file mode 100644 index 0000000..cdfa90e --- /dev/null +++ b/lib/runtime/block_registry.cc @@ -0,0 +1,111 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include + +gr::block_registry global_block_registry; + +namespace gr { + + block_registry::block_registry() + { + } + + long + block_registry::block_register(basic_block* block) + { + gr::thread::scoped_lock guard(d_mutex); + + if(d_map.find(block->name()) == d_map.end()) { + d_map[block->name()] = blocksubmap_t(); + d_map[block->name()][0] = block; + return 0; + } + else { + for(size_t i=0; i<=d_map[block->name()].size(); i++){ + if(d_map[block->name()].find(i) == d_map[block->name()].end()){ + d_map[block->name()][i] = block; + return i; + } + } + } + throw std::runtime_error("should not reach this"); + } + + void + block_registry::block_unregister(basic_block* block) + { + gr::thread::scoped_lock guard(d_mutex); + + d_map[block->name()].erase( d_map[block->name()].find(block->symbolic_id())); + } + + std::string + block_registry::register_symbolic_name(basic_block* block) + { + std::stringstream ss; + ss << block->name() << block->symbolic_id(); + //std::cout << "register_symbolic_name: " << ss.str() << std::endl; + register_symbolic_name(block, ss.str()); + return ss.str(); + } + + void + block_registry::register_symbolic_name(basic_block* block, std::string name) + { + gr::thread::scoped_lock guard(d_mutex); + } + + void + block_registry::register_primitive(std::string blk, block* ref) + { + gr::thread::scoped_lock guard(d_mutex); + + primitive_map[blk] = ref; + } + + void + block_registry::unregister_primitive(std::string blk) + { + gr::thread::scoped_lock guard(d_mutex); + + primitive_map.erase(primitive_map.find(blk)); + } + + void + block_registry::notify_blk(std::string blk) + { + gr::thread::scoped_lock guard(d_mutex); + + if(primitive_map.find(blk) == primitive_map.end()) { + return; + } + if(primitive_map[blk]->detail().get()) + primitive_map[blk]->detail()->d_tpb.notify_msg(); + } + +} /* namespace gr */ diff --git a/lib/runtime/blocks/null_sink_impl.cc b/lib/runtime/blocks/null_sink_impl.cc new file mode 100644 index 0000000..41adeea --- /dev/null +++ b/lib/runtime/blocks/null_sink_impl.cc @@ -0,0 +1,60 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "null_sink_impl.h" +#include + +namespace gr { + namespace blocks { + + null_sink::sptr + null_sink::make(size_t sizeof_stream_item) + { + return gnuradio::get_initial_sptr + (new null_sink_impl(sizeof_stream_item)); + } + + null_sink_impl::null_sink_impl(size_t sizeof_stream_item) + : sync_block("null_sink", + io_signature::make(1, 1, sizeof_stream_item), + io_signature::make(0, 0, 0)) + { + } + + null_sink_impl::~null_sink_impl() + { + } + + int + null_sink_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + return noutput_items; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/lib/runtime/blocks/null_sink_impl.h b/lib/runtime/blocks/null_sink_impl.h new file mode 100644 index 0000000..948bc19 --- /dev/null +++ b/lib/runtime/blocks/null_sink_impl.h @@ -0,0 +1,45 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_NULL_SINK_IMPL_H +#define INCLUDED_GR_NULL_SINK_IMPL_H + +#include + +namespace gr { + namespace blocks { + + class null_sink_impl : public null_sink + { + public: + null_sink_impl(size_t sizeof_stream_item); + ~null_sink_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_NULL_SINK_IMPL_H */ diff --git a/lib/runtime/blocks/null_source_impl.cc b/lib/runtime/blocks/null_source_impl.cc new file mode 100644 index 0000000..edf0104 --- /dev/null +++ b/lib/runtime/blocks/null_source_impl.cc @@ -0,0 +1,63 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "null_source_impl.h" +#include +#include + +namespace gr { + namespace blocks { + + null_source::sptr + null_source::make(size_t sizeof_stream_item) + { + return gnuradio::get_initial_sptr + (new null_source_impl(sizeof_stream_item)); + } + + null_source_impl::null_source_impl (size_t sizeof_stream_item) + : sync_block("null_source", + io_signature::make(0, 0, 0), + io_signature::make(1, 1, sizeof_stream_item)) + { + } + + null_source_impl::~null_source_impl() + { + } + + int + null_source_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + void *optr = (void*)output_items[0]; + memset(optr, 0, noutput_items * output_signature()->sizeof_stream_item(0)); + return noutput_items; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/lib/runtime/blocks/null_source_impl.h b/lib/runtime/blocks/null_source_impl.h new file mode 100644 index 0000000..887c77d --- /dev/null +++ b/lib/runtime/blocks/null_source_impl.h @@ -0,0 +1,45 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_NULL_SOURCE_IMPL_H +#define INCLUDED_GR_NULL_SOURCE_IMPL_H + +#include + +namespace gr { + namespace blocks { + + class null_source_impl : public null_source + { + public: + null_source_impl(size_t sizeof_stream_item); + ~null_source_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_NULL_SOURCE_IMPL_H */ diff --git a/lib/runtime/blocks/throttle_impl.cc b/lib/runtime/blocks/throttle_impl.cc new file mode 100644 index 0000000..b1b1c84 --- /dev/null +++ b/lib/runtime/blocks/throttle_impl.cc @@ -0,0 +1,102 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "throttle_impl.h" +#include +#include +#include + +namespace gr { + namespace blocks { + + throttle::sptr + throttle::make(size_t itemsize, double samples_per_sec) + { + return gnuradio::get_initial_sptr + (new throttle_impl(itemsize, samples_per_sec)); + } + + throttle_impl::throttle_impl(size_t itemsize, + double samples_per_second) + : sync_block("throttle", + io_signature::make(1, 1, itemsize), + io_signature::make(1, 1, itemsize)), + d_itemsize(itemsize) + { + set_sample_rate(samples_per_second); + } + + throttle_impl::~throttle_impl() + { + } + + void + throttle_impl::set_sample_rate(double rate) + { + //changing the sample rate performs a reset of state params + d_start = boost::get_system_time(); + d_total_samples = 0; + d_samps_per_tick = rate/boost::posix_time::time_duration::ticks_per_second(); + d_samps_per_us = rate/1e6; + } + + double + throttle_impl::sample_rate() const + { + return d_samps_per_us * 1e6; + } + + int + throttle_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + //calculate the expected number of samples to have passed through + boost::system_time now = boost::get_system_time(); + boost::int64_t ticks = (now - d_start).ticks(); + uint64_t expected_samps = uint64_t(d_samps_per_tick*ticks); + + //if the expected samples was less, we need to throttle back + if(d_total_samples > expected_samps) { + boost::this_thread::sleep(boost::posix_time::microseconds + (long((d_total_samples - expected_samps)/d_samps_per_us))); + } + + //copy all samples output[i] <= input[i] + const char *in = (const char *)input_items[0]; + char *out = (char *)output_items[0]; + std::memcpy(out, in, noutput_items * d_itemsize); + d_total_samples += noutput_items; + return noutput_items; + } + + void + throttle_impl::setup_rpc() + { + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/lib/runtime/blocks/throttle_impl.h b/lib/runtime/blocks/throttle_impl.h new file mode 100644 index 0000000..797cb7d --- /dev/null +++ b/lib/runtime/blocks/throttle_impl.h @@ -0,0 +1,56 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_THROTTLE_IMPL_H +#define INCLUDED_GR_THROTTLE_IMPL_H + +#include + +namespace gr { + namespace blocks { + + class throttle_impl : public throttle + { + private: + boost::system_time d_start; + size_t d_itemsize; + uint64_t d_total_samples; + double d_samps_per_tick, d_samps_per_us; + + public: + throttle_impl(size_t itemsize, double samples_per_sec); + ~throttle_impl(); + + void setup_rpc(); + + void set_sample_rate(double rate); + double sample_rate() const; + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_THROTTLE_IMPL_H */ diff --git a/lib/runtime/buffer.cc b/lib/runtime/buffer.cc new file mode 100644 index 0000000..7446799 --- /dev/null +++ b/lib/runtime/buffer.cc @@ -0,0 +1,269 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2009,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include "vmcircbuf.h" +#include +#include +#include +#include +#include + +namespace gr { + + static long s_buffer_count = 0; // counts for debugging storage mgmt + static long s_buffer_reader_count = 0; + + /* ---------------------------------------------------------------------------- + Notes on storage management + + Pretty much all the fundamental classes are now using the + shared_ptr stuff for automatic reference counting. To ensure that + no mistakes are made, we make the constructors for classes private, + and then provide a free factory function that returns a smart + pointer to the desired class. + + gr::buffer and gr::buffer_reader are no exceptions. However, they + both want pointers to each other, and unless we do something, we'll + never delete any of them because of the circular structure. + They'll always have a reference count of at least one. We could + use boost::weak_ptr's from gr::buffer to gr::buffer_reader but that + introduces it's own problems. (gr::buffer_reader's destructor needs + to call gr::buffer::drop_reader, but has no easy way to get a + shared_ptr to itself.) + + Instead, we solve this problem by having gr::buffer hold a raw + pointer to gr::buffer_reader in its d_reader vector. + gr::buffer_reader's destructor calls gr::buffer::drop_reader, so + we're never left with an dangling pointer. gr::buffer_reader still + has a shared_ptr to the buffer ensuring that the buffer doesn't go + away under it. However, when the reference count of a + gr::buffer_reader goes to zero, we can successfully reclaim it. + ---------------------------------------------------------------------------- */ + + /* + * Compute the minimum number of buffer items that work (i.e., + * address space wrap-around works). To work is to satisfy this + * contraint for integer buffer_size and k: + * + * type_size * nitems == k * page_size + */ + static long + minimum_buffer_items(long type_size, long page_size) + { + return page_size / boost::math::gcd (type_size, page_size); + } + + + buffer::buffer(int nitems, size_t sizeof_item, block_sptr link) + : d_base(0), d_bufsize(0), d_vmcircbuf(0), + d_sizeof_item(sizeof_item), d_link(link), + d_write_index(0), d_abs_write_offset(0), d_done(false), + d_last_min_items_read(0) + { + if(!allocate_buffer (nitems, sizeof_item)) + throw std::bad_alloc (); + + s_buffer_count++; + } + + buffer_sptr + make_buffer(int nitems, size_t sizeof_item, block_sptr link) + { + return buffer_sptr(new buffer(nitems, sizeof_item, link)); + } + + buffer::~buffer() + { + delete d_vmcircbuf; + assert(d_readers.size() == 0); + s_buffer_count--; + } + + /*! + * sets d_vmcircbuf, d_base, d_bufsize. + * returns true iff successful. + */ + bool + buffer::allocate_buffer(int nitems, size_t sizeof_item) + { + int orig_nitems = nitems; + + // Any buffersize we come up with must be a multiple of min_nitems. + int granularity = gr::vmcircbuf_sysconfig::granularity(); + int min_nitems = minimum_buffer_items(sizeof_item, granularity); + + // Round-up nitems to a multiple of min_nitems. + if(nitems % min_nitems != 0) + nitems = ((nitems / min_nitems) + 1) * min_nitems; + + // If we rounded-up a whole bunch, give the user a heads up. + // This only happens if sizeof_item is not a power of two. + + if(nitems > 2 * orig_nitems && nitems * (int) sizeof_item > granularity){ + std::cerr << "gr::buffer::allocate_buffer: warning: tried to allocate\n" + << " " << orig_nitems << " items of size " + << sizeof_item << ". Due to alignment requirements\n" + << " " << nitems << " were allocated. If this isn't OK, consider padding\n" + << " your structure to a power-of-two bytes.\n" + << " On this platform, our allocation granularity is " << granularity << " bytes.\n"; + } + + d_bufsize = nitems; + d_vmcircbuf = gr::vmcircbuf_sysconfig::make(d_bufsize * d_sizeof_item); + if(d_vmcircbuf == 0){ + std::cerr << "gr::buffer::allocate_buffer: failed to allocate buffer of size " + << d_bufsize * d_sizeof_item / 1024 << " KB\n"; + return false; + } + + d_base = (char*)d_vmcircbuf->pointer_to_first_copy(); + return true; + } + + int + buffer::space_available() + { + if(d_readers.empty()) + return d_bufsize - 1; // See comment below + + else { + // Find out the maximum amount of data available to our readers + + int most_data = d_readers[0]->items_available(); + uint64_t min_items_read = d_readers[0]->nitems_read(); + for(size_t i = 1; i < d_readers.size (); i++) { + most_data = std::max(most_data, d_readers[i]->items_available()); + min_items_read = std::min(min_items_read, d_readers[i]->nitems_read()); + } + + if(min_items_read != d_last_min_items_read) { + d_last_min_items_read = min_items_read; + } + + // The -1 ensures that the case d_write_index == d_read_index is + // unambiguous. It indicates that there is no data for the reader + return d_bufsize - most_data - 1; + } + } + + void * + buffer::write_pointer() + { + return &d_base[d_write_index * d_sizeof_item]; + } + + void + buffer::update_write_pointer(int nitems) + { + gr::thread::scoped_lock guard(*mutex()); + d_write_index = index_add(d_write_index, nitems); + d_abs_write_offset += nitems; + } + + void + buffer::set_done(bool done) + { + gr::thread::scoped_lock guard(*mutex()); + d_done = done; + } + + buffer_reader_sptr + buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link) + { + if(nzero_preload < 0) + throw std::invalid_argument("buffer_add_reader: nzero_preload must be >= 0"); + + buffer_reader_sptr r(new buffer_reader(buf, + buf->index_sub(buf->d_write_index, + nzero_preload), + link)); + buf->d_readers.push_back(r.get ()); + + return r; + } + + void + buffer::drop_reader(buffer_reader *reader) + { + std::vector::iterator result = + std::find(d_readers.begin(), d_readers.end(), reader); + + if(result == d_readers.end()) + throw std::invalid_argument("buffer::drop_reader"); // we didn't find it... + + d_readers.erase(result); + } + + long + buffer_ncurrently_allocated() + { + return s_buffer_count; + } + + // ---------------------------------------------------------------------------- + + buffer_reader::buffer_reader(buffer_sptr buffer, unsigned int read_index, + block_sptr link) + : d_buffer(buffer), d_read_index(read_index), d_abs_read_offset(0), d_link(link) + { + s_buffer_reader_count++; + } + + buffer_reader::~buffer_reader() + { + d_buffer->drop_reader(this); + s_buffer_reader_count--; + } + + int + buffer_reader::items_available() const + { + return d_buffer->index_sub(d_buffer->d_write_index, d_read_index); + } + + const void * + buffer_reader::read_pointer() + { + return &d_buffer->d_base[d_read_index * d_buffer->d_sizeof_item]; + } + + void + buffer_reader::update_read_pointer(int nitems) + { + gr::thread::scoped_lock guard(*mutex()); + d_read_index = d_buffer->index_add (d_read_index, nitems); + d_abs_read_offset += nitems; + } + + long + buffer_reader_ncurrently_allocated() + { + return s_buffer_reader_count; + } + +} /* namespace gr */ diff --git a/lib/runtime/circular_file.cc b/lib/runtime/circular_file.cc new file mode 100644 index 0000000..4d7d060 --- /dev/null +++ b/lib/runtime/circular_file.cc @@ -0,0 +1,208 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "circular_file.h" + +#include +#ifdef HAVE_SYS_MMAN_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef HAVE_IO_H +#include +#endif + +namespace gr { + + static const int HEADER_SIZE = 4096; + static const int HEADER_MAGIC = 0xEB021026; + + static const int HD_MAGIC = 0; + static const int HD_HEADER_SIZE = 1; // integer offsets into header + static const int HD_BUFFER_SIZE = 2; + static const int HD_BUFFER_BASE = 3; + static const int HD_BUFFER_CURRENT = 4; + + circular_file::circular_file(const char *filename, + bool writable, int size) + : d_fd(-1), d_header(0), d_buffer(0), d_mapped_size(0), d_bytes_read(0) + { + int mm_prot; + if(writable) { +#ifdef HAVE_MMAP + mm_prot = PROT_READ | PROT_WRITE; +#endif + d_fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0664); + if(d_fd < 0) { + perror(filename); + exit(1); + } +#ifdef HAVE_MMAP /* FIXME */ + if(ftruncate(d_fd, size + HEADER_SIZE) != 0) { + perror(filename); + exit(1); + } +#endif + } + else { +#ifdef HAVE_MMAP + mm_prot = PROT_READ; +#endif + d_fd = open (filename, O_RDONLY); + if(d_fd < 0) { + perror(filename); + exit(1); + } + } + + struct stat statbuf; + if(fstat (d_fd, &statbuf) < 0) { + perror(filename); + exit(1); + } + + if(statbuf.st_size < HEADER_SIZE) { + fprintf(stderr, "%s: file too small to be circular buffer\n", filename); + exit(1); + } + + d_mapped_size = statbuf.st_size; +#ifdef HAVE_MMAP + void *p = mmap (0, d_mapped_size, mm_prot, MAP_SHARED, d_fd, 0); + if(p == MAP_FAILED) { + perror("gr::circular_file: mmap failed"); + exit(1); + } + + d_header = (int*)p; +#else + perror("gr::circular_file: mmap unsupported by this system"); + exit(1); +#endif + + if(writable) { // init header + + if(size < 0) { + fprintf(stderr, "gr::circular_buffer: size must be > 0 when writable\n"); + exit(1); + } + + d_header[HD_MAGIC] = HEADER_MAGIC; + d_header[HD_HEADER_SIZE] = HEADER_SIZE; + d_header[HD_BUFFER_SIZE] = size; + d_header[HD_BUFFER_BASE] = HEADER_SIZE; // right after header + d_header[HD_BUFFER_CURRENT] = 0; + } + + // sanity check (the asserts are a bit unforgiving...) + + assert(d_header[HD_MAGIC] == HEADER_MAGIC); + assert(d_header[HD_HEADER_SIZE] == HEADER_SIZE); + assert(d_header[HD_BUFFER_SIZE] > 0); + assert(d_header[HD_BUFFER_BASE] >= d_header[HD_HEADER_SIZE]); + assert(d_header[HD_BUFFER_BASE] + d_header[HD_BUFFER_SIZE] <= d_mapped_size); + assert(d_header[HD_BUFFER_CURRENT] >= 0 && + d_header[HD_BUFFER_CURRENT] < d_header[HD_BUFFER_SIZE]); + + d_bytes_read = 0; + d_buffer = (unsigned char*)d_header + d_header[HD_BUFFER_BASE]; + } + + circular_file::~circular_file() + { +#ifdef HAVE_MMAP + if(munmap ((char *) d_header, d_mapped_size) < 0) { + perror("gr::circular_file: munmap"); + exit(1); + } +#endif + close(d_fd); + } + + bool + circular_file::write(void *vdata, int nbytes) + { + unsigned char *data = (unsigned char*)vdata; + int buffer_size = d_header[HD_BUFFER_SIZE]; + int buffer_current = d_header[HD_BUFFER_CURRENT]; + + while(nbytes > 0) { + int n = std::min(nbytes, buffer_size - buffer_current); + memcpy(d_buffer + buffer_current, data, n); + + buffer_current += n; + if(buffer_current >= buffer_size) + buffer_current = 0; + + data += n; + nbytes -= n; + } + + d_header[HD_BUFFER_CURRENT] = buffer_current; + return true; + } + + int + circular_file::read(void *vdata, int nbytes) + { + unsigned char *data = (unsigned char *) vdata; + int buffer_current = d_header[HD_BUFFER_CURRENT]; + int buffer_size = d_header[HD_BUFFER_SIZE]; + int total = 0; + + nbytes = std::min(nbytes, buffer_size - d_bytes_read); + + while(nbytes > 0) { + int offset = (buffer_current + d_bytes_read) % buffer_size; + int n = std::min (nbytes, buffer_size - offset); + memcpy(data, d_buffer + offset, n); + data += n; + d_bytes_read += n; + total += n; + nbytes -= n; + } + return total; + } + + void + circular_file::reset_read_pointer() + { + d_bytes_read = 0; + } + +} /* namespace gr */ diff --git a/lib/runtime/circular_file.h b/lib/runtime/circular_file.h new file mode 100644 index 0000000..1dc431d --- /dev/null +++ b/lib/runtime/circular_file.h @@ -0,0 +1,65 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_CIRCULAR_FILE_H +#define GR_CIRCULAR_FILE_H + +#include + +namespace gr { + + /* + * writes input data into a circular buffer on disk. + * + * the file contains a fixed header: + * 0x0000: int32 magic (0xEB021026) + * 0x0004: int32 size in bytes of header (constant 4096) + * 0x0008: int32 size in bytes of circular buffer (not including header) + * 0x000C: int32 file offset to beginning of circular buffer + * 0x0010: int32 byte offset from beginning of circular buffer to + * current start of data + */ + class GR_RUNTIME_API circular_file + { + private: + int d_fd; + int *d_header; + unsigned char *d_buffer; + int d_mapped_size; + int d_bytes_read; + + public: + circular_file(const char *filename, bool writable = false, int size = 0); + ~circular_file(); + + bool write(void *data, int nbytes); + + // returns # of bytes actually read or 0 if end of buffer, or -1 on error. + int read(void *data, int nbytes); + + // reset read pointer to beginning of buffer. + void reset_read_pointer(); + }; + +} /* namespace gr */ + +#endif /* GR_CIRCULAR_FILE_H */ diff --git a/lib/runtime/constants.cc.in b/lib/runtime/constants.cc.in new file mode 100644 index 0000000..dcb1ed6 --- /dev/null +++ b/lib/runtime/constants.cc.in @@ -0,0 +1,61 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#if HAVE_CONFIG_H +#include +#endif + +#include + +namespace gr { + + const std::string + prefix() + { + return "@prefix@"; + } + + const std::string + sysconfdir() + { + return "@SYSCONFDIR@"; + } + + const std::string + prefsdir() + { + return "@GR_PREFSDIR@"; + } + + const std::string + build_date() + { + return "@BUILD_DATE@"; + } + + const std::string + version() + { + return "@GR_VERSION@"; + } + +} /* namespace gr */ diff --git a/lib/runtime/feval.cc b/lib/runtime/feval.cc new file mode 100644 index 0000000..c9242f5 --- /dev/null +++ b/lib/runtime/feval.cc @@ -0,0 +1,136 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +namespace gr { + + feval_dd::~feval_dd(){} + + double + feval_dd::eval(double x) + { + return 0; + } + + double + feval_dd::calleval(double x) + { + return eval(x); + } + + // ---------------------------------------------------------------- + + feval_cc::~feval_cc(){} + + gr_complex + feval_cc::eval(gr_complex x) + { + return 0; + } + + gr_complex + feval_cc::calleval(gr_complex x) + { + return eval(x); + } + + // ---------------------------------------------------------------- + + feval_ll::~feval_ll(){} + + long + feval_ll::eval(long x) + { + return 0; + } + + long + feval_ll::calleval(long x) + { + return eval(x); + } + + // ---------------------------------------------------------------- + + feval::~feval(){} + + void + feval::eval(void) + { + // nop + } + + void + feval::calleval(void) + { + eval(); + } + + // ---------------------------------------------------------------- + + feval_p::~feval_p(){} + + void + feval_p::eval(double x) + { + // nop + } + + void + feval_p::calleval(double x) + { + eval(x); + } + + /* + * Trivial examples showing C++ (transparently) calling Python + */ + double + feval_dd_example(feval_dd *f, double x) + { + return f->calleval(x); + } + + gr_complex + feval_cc_example(feval_cc *f, gr_complex x) + { + return f->calleval(x); + } + + long + feval_ll_example(feval_ll *f, long x) + { + return f->calleval(x); + } + + void + feval_example(feval *f) + { + f->calleval(); + } + +} /* namespace gr */ diff --git a/lib/runtime/flat_flowgraph.cc b/lib/runtime/flat_flowgraph.cc new file mode 100644 index 0000000..38be56a --- /dev/null +++ b/lib/runtime/flat_flowgraph.cc @@ -0,0 +1,389 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "flat_flowgraph.h" +#include +#include +#include +//#include // FIXME: +#include +#include +#include + +namespace gr { + +#define FLAT_FLOWGRAPH_DEBUG 0 + +// 32Kbyte buffer size between blocks +#define GR_FIXED_BUFFER_SIZE (32*(1L<<10)) + + static const unsigned int s_fixed_buffer_size = GR_FIXED_BUFFER_SIZE; + + flat_flowgraph_sptr + make_flat_flowgraph() + { + return flat_flowgraph_sptr(new flat_flowgraph()); + } + + flat_flowgraph::flat_flowgraph() + { + } + + flat_flowgraph::~flat_flowgraph() + { + } + + void + flat_flowgraph::setup_connections() + { + basic_block_vector_t blocks = calc_used_blocks(); + + // Assign block details to blocks + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) + cast_to_block_sptr(*p)->set_detail(allocate_block_detail(*p)); + + // Connect inputs to outputs for each block + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) { + connect_block_inputs(*p); + + block_sptr block = cast_to_block_sptr(*p); + block->set_unaligned(0); + block->set_is_unaligned(false); + } + } + + block_detail_sptr + flat_flowgraph::allocate_block_detail(basic_block_sptr block) + { + int ninputs = calc_used_ports(block, true).size(); + int noutputs = calc_used_ports(block, false).size(); + block_detail_sptr detail = make_block_detail(ninputs, noutputs); + + block_sptr grblock = cast_to_block_sptr(block); + if(!grblock) + throw std::runtime_error("allocate_block_detail found non-gr::block"); + + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "Creating block detail for " << block << std::endl; + + for(int i = 0; i < noutputs; i++) { + grblock->expand_minmax_buffer(i); + + buffer_sptr buffer = allocate_buffer(block, i); + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "Allocated buffer for output " << block << ":" << i << std::endl; + detail->set_output(i, buffer); + + // Update the block's max_output_buffer based on what was actually allocated. + grblock->set_max_output_buffer(i, buffer->bufsize()); + } + + return detail; + } + + buffer_sptr + flat_flowgraph::allocate_buffer(basic_block_sptr block, int port) + { + block_sptr grblock = cast_to_block_sptr(block); + if(!grblock) + throw std::runtime_error("allocate_buffer found non-gr::block"); + int item_size = block->output_signature()->sizeof_stream_item(port); + + // *2 because we're now only filling them 1/2 way in order to + // increase the available parallelism when using the TPB scheduler. + // (We're double buffering, where we used to single buffer) + int nitems = s_fixed_buffer_size * 2 / item_size; + + // Make sure there are at least twice the output_multiple no. of items + if(nitems < 2*grblock->output_multiple()) // Note: this means output_multiple() + nitems = 2*grblock->output_multiple(); // can't be changed by block dynamically + + // If any downstream blocks are decimators and/or have a large output_multiple, + // ensure we have a buffer at least twice their decimation factor*output_multiple + basic_block_vector_t blocks = calc_downstream_blocks(block, port); + + // limit buffer size if indicated + if(grblock->max_output_buffer(port) > 0) { + //std::cout << "constraining output items to " << block->max_output_buffer(port) << "\n"; + nitems = std::min((long)nitems, (long)grblock->max_output_buffer(port)); + nitems -= nitems%grblock->output_multiple(); + if(nitems < 1) + throw std::runtime_error("problems allocating a buffer with the given max output buffer constraint!"); + } + else if(grblock->min_output_buffer(port) > 0) { + nitems = std::max((long)nitems, (long)grblock->min_output_buffer(port)); + nitems -= nitems%grblock->output_multiple(); + if(nitems < 1) + throw std::runtime_error("problems allocating a buffer with the given min output buffer constraint!"); + } + + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) { + block_sptr dgrblock = cast_to_block_sptr(*p); + if(!dgrblock) + throw std::runtime_error("allocate_buffer found non-gr::block"); + + double decimation = (1.0/dgrblock->relative_rate()); + int multiple = dgrblock->output_multiple(); + int history = dgrblock->history(); + nitems = std::max(nitems, static_cast(2*(decimation*multiple+history))); + } + + // std::cout << "make_buffer(" << nitems << ", " << item_size << ", " << grblock << "\n"; + // We're going to let this fail once and retry. If that fails, + // throw and exit. + buffer_sptr b; + try { + b = make_buffer(nitems, item_size, grblock); + } + catch(std::bad_alloc&) { + b = make_buffer(nitems, item_size, grblock); + } + return b; + } + + void + flat_flowgraph::connect_block_inputs(basic_block_sptr block) + { + block_sptr grblock = cast_to_block_sptr(block); + if (!grblock) + throw std::runtime_error("connect_block_inputs found non-gr::block"); + + // Get its detail and edges that feed into it + block_detail_sptr detail = grblock->detail(); + edge_vector_t in_edges = calc_upstream_edges(block); + + // For each edge that feeds into it + for(edge_viter_t e = in_edges.begin(); e != in_edges.end(); e++) { + // Set the buffer reader on the destination port to the output + // buffer on the source port + int dst_port = e->dst().port(); + int src_port = e->src().port(); + basic_block_sptr src_block = e->src().block(); + block_sptr src_grblock = cast_to_block_sptr(src_block); + if(!src_grblock) + throw std::runtime_error("connect_block_inputs found non-gr::block"); + buffer_sptr src_buffer = src_grblock->detail()->output(src_port); + + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "Setting input " << dst_port << " from edge " << (*e) << std::endl; + + detail->set_input(dst_port, buffer_add_reader(src_buffer, grblock->history()-1, grblock)); + } + } + + void + flat_flowgraph::merge_connections(flat_flowgraph_sptr old_ffg) + { + // Allocate block details if needed. Only new blocks that aren't pruned out + // by flattening will need one; existing blocks still in the new flowgraph will + // already have one. + for(basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) { + block_sptr block = cast_to_block_sptr(*p); + + if(!block->detail()) { + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "merge: allocating new detail for block " << (*p) << std::endl; + block->set_detail(allocate_block_detail(block)); + } + else + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "merge: reusing original detail for block " << (*p) << std::endl; + } + + // Calculate the old edges that will be going away, and clear the + // buffer readers on the RHS. + for(edge_viter_t old_edge = old_ffg->d_edges.begin(); old_edge != old_ffg->d_edges.end(); old_edge++) { + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "merge: testing old edge " << (*old_edge) << "..."; + + edge_viter_t new_edge; + for(new_edge = d_edges.begin(); new_edge != d_edges.end(); new_edge++) + if(new_edge->src() == old_edge->src() && + new_edge->dst() == old_edge->dst()) + break; + + if(new_edge == d_edges.end()) { // not found in new edge list + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "not in new edge list" << std::endl; + // zero the buffer reader on RHS of old edge + block_sptr block(cast_to_block_sptr(old_edge->dst().block())); + int port = old_edge->dst().port(); + block->detail()->set_input(port, buffer_reader_sptr()); + } + else { + if (FLAT_FLOWGRAPH_DEBUG) + std::cout << "found in new edge list" << std::endl; + } + } + + // Now connect inputs to outputs, reusing old buffer readers if they exist + for(basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) { + block_sptr block = cast_to_block_sptr(*p); + + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "merge: merging " << (*p) << "..."; + + if(old_ffg->has_block_p(*p)) { + // Block exists in old flow graph + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "used in old flow graph" << std::endl; + block_detail_sptr detail = block->detail(); + + // Iterate through the inputs and see what needs to be done + int ninputs = calc_used_ports(block, true).size(); // Might be different now + for(int i = 0; i < ninputs; i++) { + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "Checking input " << block << ":" << i << "..."; + edge edge = calc_upstream_edge(*p, i); + + // Fish out old buffer reader and see if it matches correct buffer from edge list + block_sptr src_block = cast_to_block_sptr(edge.src().block()); + block_detail_sptr src_detail = src_block->detail(); + buffer_sptr src_buffer = src_detail->output(edge.src().port()); + buffer_reader_sptr old_reader; + if(i < detail->ninputs()) // Don't exceed what the original detail has + old_reader = detail->input(i); + + // If there's a match, use it + if(old_reader && (src_buffer == old_reader->buffer())) { + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "matched, reusing" << std::endl; + } + else { + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "needs a new reader" << std::endl; + + // Create new buffer reader and assign + detail->set_input(i, buffer_add_reader(src_buffer, block->history()-1, block)); + } + } + } + else { + // Block is new, it just needs buffer readers at this point + if(FLAT_FLOWGRAPH_DEBUG) + std::cout << "new block" << std::endl; + connect_block_inputs(block); + + // Make sure all buffers are aligned + setup_buffer_alignment(block); + } + + // Now deal with the fact that the block details might have + // changed numbers of inputs and outputs vs. in the old + // flowgraph. + } + } + + void + flat_flowgraph::setup_buffer_alignment(block_sptr block) + { + const int alignment = 4; //volk_get_alignment(); // FIXME: check alignment + for(int i = 0; i < block->detail()->ninputs(); i++) { + void *r = (void*)block->detail()->input(i)->read_pointer(); + unsigned long int ri = (unsigned long int)r % alignment; + //std::cerr << "reader: " << r << " alignment: " << ri << std::endl; + if(ri != 0) { + size_t itemsize = block->detail()->input(i)->get_sizeof_item(); + block->detail()->input(i)->update_read_pointer(alignment-ri/itemsize); + } + block->set_unaligned(0); + block->set_is_unaligned(false); + } + + for(int i = 0; i < block->detail()->noutputs(); i++) { + void *w = (void*)block->detail()->output(i)->write_pointer(); + unsigned long int wi = (unsigned long int)w % alignment; + //std::cerr << "writer: " << w << " alignment: " << wi << std::endl; + if(wi != 0) { + size_t itemsize = block->detail()->output(i)->get_sizeof_item(); + block->detail()->output(i)->update_write_pointer(alignment-wi/itemsize); + } + block->set_unaligned(0); + block->set_is_unaligned(false); + } + } + + std::string + flat_flowgraph::edge_list() + { + std::stringstream s; + for(edge_viter_t e = d_edges.begin(); e != d_edges.end(); e++) + s << (*e) << std::endl; + return s.str(); + } + + void flat_flowgraph::dump() + { + for(edge_viter_t e = d_edges.begin(); e != d_edges.end(); e++) + std::cout << " edge: " << (*e) << std::endl; + + for(basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) { + std::cout << " block: " << (*p) << std::endl; + block_detail_sptr detail = cast_to_block_sptr(*p)->detail(); + std::cout << " detail @" << detail << ":" << std::endl; + + int ni = detail->ninputs(); + int no = detail->noutputs(); + for(int i = 0; i < no; i++) { + buffer_sptr buffer = detail->output(i); + std::cout << " output " << i << ": " << buffer << std::endl; + } + + for(int i = 0; i < ni; i++) { + buffer_reader_sptr reader = detail->input(i); + std::cout << " reader " << i << ": " << reader + << " reading from buffer=" << reader->buffer() << std::endl; + } + } + } + + block_vector_t + flat_flowgraph::make_block_vector(basic_block_vector_t &blocks) + { + block_vector_t result; + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) { + result.push_back(cast_to_block_sptr(*p)); + } + + return result; + } + + void + flat_flowgraph::enable_pc_rpc() + { +#ifdef GR_PERFORMANCE_COUNTERS + if(prefs::singleton()->get_bool("PerfCounters", "on", false)) { + basic_block_viter_t p; + for(p = d_blocks.begin(); p != d_blocks.end(); p++) { + block_sptr block = cast_to_block_sptr(*p); + if(!block->is_pc_rpc_set()) + block->setup_pc_rpc(); + } + } +#endif /* GR_PERFORMANCE_COUNTERS */ + } + +} /* namespace gr */ diff --git a/lib/runtime/flat_flowgraph.h b/lib/runtime/flat_flowgraph.h new file mode 100644 index 0000000..2bdcf5f --- /dev/null +++ b/lib/runtime/flat_flowgraph.h @@ -0,0 +1,90 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2007,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_FLAT_FLOWGRAPH_H +#define INCLUDED_GR_RUNTIME_FLAT_FLOWGRAPH_H + +#include +#include +#include + +namespace gr { + + // Create a shared pointer to a heap allocated gr::flat_flowgraph + // (types defined in gr_runtime_types.h) + GR_RUNTIME_API flat_flowgraph_sptr make_flat_flowgraph(); + + /*! + *\brief Class specializing gr_flat_flowgraph that has all nodes + * as blocks, with no hierarchy + * \ingroup internal + */ + class GR_RUNTIME_API flat_flowgraph : public flowgraph + { + public: + friend GR_RUNTIME_API flat_flowgraph_sptr make_flat_flowgraph(); + + // Destruct an arbitrary gr::flat_flowgraph + ~flat_flowgraph(); + + // Wire list of gr::block together in new flat_flowgraph + void setup_connections(); + + // Merge applicable connections from existing flat flowgraph + void merge_connections(flat_flowgraph_sptr sfg); + + // Return a string list of edges + std::string edge_list(); + + void dump(); + + /*! + * Make a vector of gr::block from a vector of gr::basic_block + */ + static block_vector_t make_block_vector(basic_block_vector_t &blocks); + + /*! + * Enables export of perf. counters to ControlPort on all blocks in + * the flowgraph. + */ + void enable_pc_rpc(); + + private: + flat_flowgraph(); + + block_detail_sptr allocate_block_detail(basic_block_sptr block); + buffer_sptr allocate_buffer(basic_block_sptr block, int port); + void connect_block_inputs(basic_block_sptr block); + + /* When reusing a flowgraph's blocks, this call makes sure all of + * the buffer's are aligned at the machine's alignment boundary + * and tells the blocks that they are aligned. + * + * Called from both setup_connections and merge_connections for + * start and restarts. + */ + void setup_buffer_alignment(block_sptr block); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_FLAT_FLOWGRAPH_H */ diff --git a/lib/runtime/flowgraph.cc b/lib/runtime/flowgraph.cc new file mode 100644 index 0000000..6bf04f1 --- /dev/null +++ b/lib/runtime/flowgraph.cc @@ -0,0 +1,475 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include + +namespace gr { + +#define FLOWGRAPH_DEBUG 0 + + edge::~edge() + { + } + + flowgraph_sptr make_flowgraph() + { + return flowgraph_sptr(new flowgraph()); + } + + flowgraph::flowgraph() + { + } + + flowgraph::~flowgraph() + { + } + + template + static + std::vector + unique_vector(std::vector v) + { + std::vector result; + std::insert_iterator > inserter(result, result.begin()); + + sort(v.begin(), v.end()); + unique_copy(v.begin(), v.end(), inserter); + return result; + } + + void + flowgraph::connect(const endpoint &src, const endpoint &dst) + { + check_valid_port(src.block()->output_signature(), src.port()); + check_valid_port(dst.block()->input_signature(), dst.port()); + check_dst_not_used(dst); + check_type_match(src, dst); + + // All ist klar, Herr Kommisar + d_edges.push_back(edge(src,dst)); + } + + void + flowgraph::disconnect(const endpoint &src, const endpoint &dst) + { + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) { + if(src == p->src() && dst == p->dst()) { + d_edges.erase(p); + return; + } + } + + std::stringstream msg; + msg << "cannot disconnect edge " << edge(src, dst) << ", not found"; + throw std::invalid_argument(msg.str()); + } + + void + flowgraph::validate() + { + d_blocks = calc_used_blocks(); + + for(basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) { + std::vector used_ports; + int ninputs, noutputs; + + if(FLOWGRAPH_DEBUG) + std::cout << "Validating block: " << (*p) << std::endl; + + used_ports = calc_used_ports(*p, true); // inputs + ninputs = used_ports.size(); + check_contiguity(*p, used_ports, true); // inputs + + used_ports = calc_used_ports(*p, false); // outputs + noutputs = used_ports.size(); + check_contiguity(*p, used_ports, false); // outputs + + if(!((*p)->check_topology(ninputs, noutputs))) { + std::stringstream msg; + msg << "check topology failed on " << (*p) + << " using ninputs=" << ninputs + << ", noutputs=" << noutputs; + throw std::runtime_error(msg.str()); + } + } + } + + void + flowgraph::clear() + { + // Boost shared pointers will deallocate as needed + d_blocks.clear(); + d_edges.clear(); + } + + void + flowgraph::check_valid_port(gr::io_signature::sptr sig, int port) + { + std::stringstream msg; + + if(port < 0) { + msg << "negative port number " << port << " is invalid"; + throw std::invalid_argument(msg.str()); + } + + int max = sig->max_streams(); + if(max != io_signature::IO_INFINITE && port >= max) { + msg << "port number " << port << " exceeds max of "; + if(max == 0) + msg << "(none)"; + else + msg << max-1; + throw std::invalid_argument(msg.str()); + } + } + + void + flowgraph::check_dst_not_used(const endpoint &dst) + { + // A destination is in use if it is already on the edge list + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) + if(p->dst() == dst) { + std::stringstream msg; + msg << "destination already in use by edge " << (*p); + throw std::invalid_argument(msg.str()); + } + } + + void + flowgraph::check_type_match(const endpoint &src, const endpoint &dst) + { + int src_size = src.block()->output_signature()->sizeof_stream_item(src.port()); + int dst_size = dst.block()->input_signature()->sizeof_stream_item(dst.port()); + + if(src_size != dst_size) { + std::stringstream msg; + msg << "itemsize mismatch: " << src << " using " << src_size + << ", " << dst << " using " << dst_size; + throw std::invalid_argument(msg.str()); + } + } + + basic_block_vector_t + flowgraph::calc_used_blocks() + { + basic_block_vector_t tmp; + + // Collect all blocks in the edge list + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) { + tmp.push_back(p->src().block()); + tmp.push_back(p->dst().block()); + } + + return unique_vector(tmp); + } + + std::vector + flowgraph::calc_used_ports(basic_block_sptr block, bool check_inputs) + { + std::vector tmp; + + // Collect all seen ports + edge_vector_t edges = calc_connections(block, check_inputs); + for(edge_viter_t p = edges.begin(); p != edges.end(); p++) { + if(check_inputs == true) + tmp.push_back(p->dst().port()); + else + tmp.push_back(p->src().port()); + } + + return unique_vector(tmp); + } + + edge_vector_t + flowgraph::calc_connections(basic_block_sptr block, bool check_inputs) + { + edge_vector_t result; + + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) { + if(check_inputs) { + if(p->dst().block() == block) + result.push_back(*p); + } + else { + if(p->src().block() == block) + result.push_back(*p); + } + } + + return result; // assumes no duplicates + } + + void + flowgraph::check_contiguity(basic_block_sptr block, + const std::vector &used_ports, + bool check_inputs) + { + std::stringstream msg; + + gr::io_signature::sptr sig = + check_inputs ? block->input_signature() : block->output_signature(); + + int nports = used_ports.size(); + int min_ports = sig->min_streams(); + int max_ports = sig->max_streams(); + + if(nports == 0 && min_ports == 0) + return; + + if(nports < min_ports) { + msg << block << ": insufficient connected " + << (check_inputs ? "input ports " : "output ports ") + << "(" << min_ports << " needed, " << nports << " connected)"; + throw std::runtime_error(msg.str()); + } + + if(nports > max_ports && max_ports != io_signature::IO_INFINITE) { + msg << block << ": too many connected " + << (check_inputs ? "input ports " : "output ports ") + << "(" << max_ports << " allowed, " << nports << " connected)"; + throw std::runtime_error(msg.str()); + } + + if(used_ports[nports-1]+1 != nports) { + for(int i = 0; i < nports; i++) { + if(used_ports[i] != i) { + msg << block << ": missing connection " + << (check_inputs ? "to input port " : "from output port ") + << i; + throw std::runtime_error(msg.str()); + } + } + } + } + + basic_block_vector_t + flowgraph::calc_downstream_blocks(basic_block_sptr block, int port) + { + basic_block_vector_t tmp; + + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) + if(p->src() == endpoint(block, port)) + tmp.push_back(p->dst().block()); + + return unique_vector(tmp); + } + + basic_block_vector_t + flowgraph::calc_downstream_blocks(basic_block_sptr block) + { + basic_block_vector_t tmp; + + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) + if(p->src().block() == block) + tmp.push_back(p->dst().block()); + + return unique_vector(tmp); + } + + edge_vector_t + flowgraph::calc_upstream_edges(basic_block_sptr block) + { + edge_vector_t result; + + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) + if(p->dst().block() == block) + result.push_back(*p); + + return result; // Assume no duplicates + } + + bool + flowgraph::has_block_p(basic_block_sptr block) + { + basic_block_viter_t result; + result = std::find(d_blocks.begin(), d_blocks.end(), block); + return (result != d_blocks.end()); + } + + edge + flowgraph::calc_upstream_edge(basic_block_sptr block, int port) + { + edge result; + + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) { + if(p->dst() == endpoint(block, port)) { + result = (*p); + break; + } + } + + return result; + } + + std::vector + flowgraph::partition() + { + std::vector result; + basic_block_vector_t blocks = calc_used_blocks(); + basic_block_vector_t graph; + + while(blocks.size() > 0) { + graph = calc_reachable_blocks(blocks[0], blocks); + assert(graph.size()); + result.push_back(topological_sort(graph)); + + for(basic_block_viter_t p = graph.begin(); p != graph.end(); p++) + blocks.erase(find(blocks.begin(), blocks.end(), *p)); + } + + return result; + } + + basic_block_vector_t + flowgraph::calc_reachable_blocks(basic_block_sptr block, basic_block_vector_t &blocks) + { + basic_block_vector_t result; + + // Mark all blocks as unvisited + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) + (*p)->set_color(basic_block::WHITE); + + // Recursively mark all reachable blocks + reachable_dfs_visit(block, blocks); + + // Collect all the blocks that have been visited + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) + if((*p)->color() == basic_block::BLACK) + result.push_back(*p); + + return result; + } + + // Recursively mark all reachable blocks from given block and block list + void + flowgraph::reachable_dfs_visit(basic_block_sptr block, basic_block_vector_t &blocks) + { + // Mark the current one as visited + block->set_color(basic_block::BLACK); + + // Recurse into adjacent vertices + basic_block_vector_t adjacent = calc_adjacent_blocks(block, blocks); + + for(basic_block_viter_t p = adjacent.begin(); p != adjacent.end(); p++) + if((*p)->color() == basic_block::WHITE) + reachable_dfs_visit(*p, blocks); + } + + // Return a list of block adjacent to a given block along any edge + basic_block_vector_t + flowgraph::calc_adjacent_blocks(basic_block_sptr block, basic_block_vector_t &blocks) + { + basic_block_vector_t tmp; + + // Find any blocks that are inputs or outputs + for(edge_viter_t p = d_edges.begin(); p != d_edges.end(); p++) { + if(p->src().block() == block) + tmp.push_back(p->dst().block()); + if(p->dst().block() == block) + tmp.push_back(p->src().block()); + } + + return unique_vector(tmp); + } + + basic_block_vector_t + flowgraph::topological_sort(basic_block_vector_t &blocks) + { + basic_block_vector_t tmp; + basic_block_vector_t result; + tmp = sort_sources_first(blocks); + + // Start 'em all white + for(basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) + (*p)->set_color(basic_block::WHITE); + + for(basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) { + if((*p)->color() == basic_block::WHITE) + topological_dfs_visit(*p, result); + } + + reverse(result.begin(), result.end()); + return result; + } + + basic_block_vector_t + flowgraph::sort_sources_first(basic_block_vector_t &blocks) + { + basic_block_vector_t sources, nonsources, result; + + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) { + if(source_p(*p)) + sources.push_back(*p); + else + nonsources.push_back(*p); + } + + for(basic_block_viter_t p = sources.begin(); p != sources.end(); p++) + result.push_back(*p); + + for(basic_block_viter_t p = nonsources.begin(); p != nonsources.end(); p++) + result.push_back(*p); + + return result; + } + + bool + flowgraph::source_p(basic_block_sptr block) + { + return (calc_upstream_edges(block).size() == 0); + } + + void + flowgraph::topological_dfs_visit(basic_block_sptr block, basic_block_vector_t &output) + { + block->set_color(basic_block::GREY); + basic_block_vector_t blocks(calc_downstream_blocks(block)); + + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) { + switch((*p)->color()) { + case basic_block::WHITE: + topological_dfs_visit(*p, output); + break; + + case basic_block::GREY: + throw std::runtime_error("flow graph has loops!"); + + case basic_block::BLACK: + continue; + + default: + throw std::runtime_error("invalid color on block!"); + } + } + + block->set_color(basic_block::BLACK); + output.push_back(block); + } + +} /* namespace gr */ diff --git a/lib/runtime/hier_block2.cc b/lib/runtime/hier_block2.cc new file mode 100644 index 0000000..e136465 --- /dev/null +++ b/lib/runtime/hier_block2.cc @@ -0,0 +1,142 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include "hier_block2_detail.h" +#include + +namespace gr { + +#define GR_HIER_BLOCK2_DEBUG 0 + + hier_block2_sptr + make_hier_block2(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature) + { + return gnuradio::get_initial_sptr + (new hier_block2(name, input_signature, output_signature)); + } + + hier_block2::hier_block2(const std::string &name, + gr::io_signature::sptr input_signature, + gr::io_signature::sptr output_signature) + : basic_block(name, input_signature, output_signature), + d_detail(new hier_block2_detail(this)) + { + // This bit of magic ensures that self() works in the constructors of derived classes. + gnuradio::detail::sptr_magic::create_and_stash_initial_sptr(this); + } + + hier_block2::~hier_block2() + { + delete d_detail; + } + + hier_block2::opaque_self + hier_block2::self() + { + return shared_from_this(); + } + + hier_block2_sptr + hier_block2::to_hier_block2() + { + return cast_to_hier_block2_sptr(shared_from_this()); + } + + void + hier_block2::connect(basic_block_sptr block) + { + d_detail->connect(block); + } + + void + hier_block2::connect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port) + { + d_detail->connect(src, src_port, dst, dst_port); + } + + void + hier_block2::disconnect(basic_block_sptr block) + { + d_detail->disconnect(block); + } + + void + hier_block2::disconnect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port) + { + d_detail->disconnect(src, src_port, dst, dst_port); + } + + void + hier_block2::disconnect_all() + { + d_detail->disconnect_all(); + } + + void + hier_block2::lock() + { + d_detail->lock(); + } + + void + hier_block2::unlock() + { + d_detail->unlock(); + } + + flat_flowgraph_sptr + hier_block2::flatten() const + { + flat_flowgraph_sptr new_ffg = make_flat_flowgraph(); + d_detail->flatten_aux(new_ffg); + return new_ffg; + } + + void + hier_block2::set_processor_affinity(const std::vector &mask) + { + d_detail->set_processor_affinity(mask); + } + + void + hier_block2::unset_processor_affinity() + { + d_detail->unset_processor_affinity(); + } + + std::vector + hier_block2::processor_affinity() + { + return d_detail->processor_affinity(); + } + +} /* namespace gr */ diff --git a/lib/runtime/hier_block2_detail.cc b/lib/runtime/hier_block2_detail.cc new file mode 100644 index 0000000..9e7ac82 --- /dev/null +++ b/lib/runtime/hier_block2_detail.cc @@ -0,0 +1,566 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2007,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "hier_block2_detail.h" +#include +#include +#include +#include +#include + +namespace gr { + +#define HIER_BLOCK2_DETAIL_DEBUG 0 + + hier_block2_detail::hier_block2_detail(hier_block2 *owner) + : d_owner(owner), + d_parent_detail(0), + d_fg(make_flowgraph()) + { + int min_inputs = owner->input_signature()->min_streams(); + int max_inputs = owner->input_signature()->max_streams(); + int min_outputs = owner->output_signature()->min_streams(); + int max_outputs = owner->output_signature()->max_streams(); + + if(max_inputs == io_signature::IO_INFINITE || + max_outputs == io_signature::IO_INFINITE || + (min_inputs != max_inputs) ||(min_outputs != max_outputs) ) { + std::stringstream msg; + msg << "Hierarchical blocks do not yet support arbitrary or" + << " variable numbers of inputs or outputs (" << d_owner->name() << ")"; + throw std::runtime_error(msg.str()); + } + + d_inputs = std::vector(max_inputs); + d_outputs = endpoint_vector_t(max_outputs); + } + + hier_block2_detail::~hier_block2_detail() + { + d_owner = 0; // Don't use delete, we didn't allocate + } + + void + hier_block2_detail::connect(basic_block_sptr block) + { + std::stringstream msg; + + // Check if duplicate + if(std::find(d_blocks.begin(), d_blocks.end(), block) != d_blocks.end()) { + msg << "Block " << block << " already connected."; + throw std::invalid_argument(msg.str()); + } + + // Check if has inputs or outputs + if(block->input_signature()->max_streams() != 0 || + block->output_signature()->max_streams() != 0) { + msg << "Block " << block << " must not have any input or output ports"; + throw std::invalid_argument(msg.str()); + } + + hier_block2_sptr hblock(cast_to_hier_block2_sptr(block)); + + if(hblock && hblock.get() != d_owner) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "connect: block is hierarchical, setting parent to " << this << std::endl; + hblock->d_detail->d_parent_detail = this; + } + + d_blocks.push_back(block); + } + + void + hier_block2_detail::connect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port) + { + std::stringstream msg; + + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "connecting: " << endpoint(src, src_port) + << " -> " << endpoint(dst, dst_port) << std::endl; + + if(src.get() == dst.get()) + throw std::invalid_argument("connect: src and destination blocks cannot be the same"); + + hier_block2_sptr src_block(cast_to_hier_block2_sptr(src)); + hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst)); + + if(src_block && src.get() != d_owner) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "connect: src is hierarchical, setting parent to " << this << std::endl; + src_block->d_detail->d_parent_detail = this; + } + + if(dst_block && dst.get() != d_owner) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "connect: dst is hierarchical, setting parent to " << this << std::endl; + dst_block->d_detail->d_parent_detail = this; + } + + // Connections to block inputs or outputs + int max_port; + if(src.get() == d_owner) { + max_port = src->input_signature()->max_streams(); + if((max_port != -1 && (src_port >= max_port)) || src_port < 0) { + msg << "source port " << src_port << " out of range for " << src; + throw std::invalid_argument(msg.str()); + } + + return connect_input(src_port, dst_port, dst); + } + + if(dst.get() == d_owner) { + max_port = dst->output_signature()->max_streams(); + if((max_port != -1 && (dst_port >= max_port)) || dst_port < 0) { + msg << "destination port " << dst_port << " out of range for " << dst; + throw std::invalid_argument(msg.str()); + } + + return connect_output(dst_port, src_port, src); + } + + // Internal connections + d_fg->connect(src, src_port, dst, dst_port); + + // TODO: connects to NC + } + + void + hier_block2_detail::disconnect(basic_block_sptr block) + { + // Check on singleton list + for(basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) { + if(*p == block) { + d_blocks.erase(p); + + hier_block2_sptr hblock(cast_to_hier_block2_sptr(block)); + if(block && block.get() != d_owner) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "disconnect: block is hierarchical, clearing parent" << std::endl; + hblock->d_detail->d_parent_detail = 0; + } + + return; + } + } + + // Otherwise find all edges containing block + edge_vector_t edges, tmp = d_fg->edges(); + edge_vector_t::iterator p; + for(p = tmp.begin(); p != tmp.end(); p++) { + if((*p).src().block() == block || (*p).dst().block() == block) { + edges.push_back(*p); + + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "disconnect: block found in edge " << (*p) << std::endl; + } + } + + if(edges.size() == 0) { + std::stringstream msg; + msg << "cannot disconnect block " << block << ", not found"; + throw std::invalid_argument(msg.str()); + } + + for(p = edges.begin(); p != edges.end(); p++) { + disconnect((*p).src().block(), (*p).src().port(), + (*p).dst().block(), (*p).dst().port()); + } + } + + void + hier_block2_detail::disconnect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port) + { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "disconnecting: " << endpoint(src, src_port) + << " -> " << endpoint(dst, dst_port) << std::endl; + + if(src.get() == dst.get()) + throw std::invalid_argument("disconnect: source and destination blocks cannot be the same"); + + hier_block2_sptr src_block(cast_to_hier_block2_sptr(src)); + hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst)); + + if(src_block && src.get() != d_owner) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "disconnect: src is hierarchical, clearing parent" << std::endl; + src_block->d_detail->d_parent_detail = 0; + } + + if(dst_block && dst.get() != d_owner) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "disconnect: dst is hierarchical, clearing parent" << std::endl; + dst_block->d_detail->d_parent_detail = 0; + } + + if(src.get() == d_owner) + return disconnect_input(src_port, dst_port, dst); + + if(dst.get() == d_owner) + return disconnect_output(dst_port, src_port, src); + + // Internal connections + d_fg->disconnect(src, src_port, dst, dst_port); + } + + void + hier_block2_detail::connect_input(int my_port, int port, + basic_block_sptr block) + { + std::stringstream msg; + + if(my_port < 0 || my_port >= (signed)d_inputs.size()) { + msg << "input port " << my_port << " out of range for " << block; + throw std::invalid_argument(msg.str()); + } + + endpoint_vector_t &endps = d_inputs[my_port]; + endpoint endp(block, port); + + endpoint_viter_t p = std::find(endps.begin(), endps.end(), endp); + if(p != endps.end()) { + msg << "external input port " << my_port << " already wired to " << endp; + throw std::invalid_argument(msg.str()); + } + + endps.push_back(endp); + } + + void + hier_block2_detail::connect_output(int my_port, int port, + basic_block_sptr block) + { + std::stringstream msg; + + if(my_port < 0 || my_port >= (signed)d_outputs.size()) { + msg << "output port " << my_port << " out of range for " << block; + throw std::invalid_argument(msg.str()); + } + + if(d_outputs[my_port].block()) { + msg << "external output port " << my_port << " already connected from " + << d_outputs[my_port]; + throw std::invalid_argument(msg.str()); + } + + d_outputs[my_port] = endpoint(block, port); + } + + void + hier_block2_detail::disconnect_input(int my_port, int port, + basic_block_sptr block) + { + std::stringstream msg; + + if(my_port < 0 || my_port >= (signed)d_inputs.size()) { + msg << "input port number " << my_port << " out of range for " << block; + throw std::invalid_argument(msg.str()); + } + + endpoint_vector_t &endps = d_inputs[my_port]; + endpoint endp(block, port); + + endpoint_viter_t p = std::find(endps.begin(), endps.end(), endp); + if(p == endps.end()) { + msg << "external input port " << my_port << " not connected to " << endp; + throw std::invalid_argument(msg.str()); + } + + endps.erase(p); + } + + void + hier_block2_detail::disconnect_output(int my_port, int port, + basic_block_sptr block) + { + std::stringstream msg; + + if(my_port < 0 || my_port >= (signed)d_outputs.size()) { + msg << "output port number " << my_port << " out of range for " << block; + throw std::invalid_argument(msg.str()); + } + + if(d_outputs[my_port].block() != block) { + msg << "block " << block << " not assigned to output " + << my_port << ", can't disconnect"; + throw std::invalid_argument(msg.str()); + } + + d_outputs[my_port] = endpoint(); + } + + endpoint_vector_t + hier_block2_detail::resolve_port(int port, bool is_input) + { + std::stringstream msg; + + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "Resolving port " << port << " as an " + << (is_input ? "input" : "output") + << " of " << d_owner->name() << std::endl; + + endpoint_vector_t result; + + if(is_input) { + if(port < 0 || port >= (signed)d_inputs.size()) { + msg << "resolve_port: hierarchical block '" << d_owner->name() + << "': input " << port << " is out of range"; + throw std::runtime_error(msg.str()); + } + + if(d_inputs[port].empty()) { + msg << "resolve_port: hierarchical block '" << d_owner->name() + << "': input " << port << " is not connected internally"; + throw std::runtime_error(msg.str()); + } + + endpoint_vector_t &endps = d_inputs[port]; + endpoint_viter_t p; + for(p = endps.begin(); p != endps.end(); p++) { + endpoint_vector_t tmp = resolve_endpoint(*p, true); + std::copy(tmp.begin(), tmp.end(), back_inserter(result)); + } + } + else { + if(port < 0 || port >= (signed)d_outputs.size()) { + msg << "resolve_port: hierarchical block '" << d_owner->name() + << "': output " << port << " is out of range"; + throw std::runtime_error(msg.str()); + } + + if(d_outputs[port] == endpoint()) { + msg << "resolve_port: hierarchical block '" << d_owner->name() + << "': output " << port << " is not connected internally"; + throw std::runtime_error(msg.str()); + } + + result = resolve_endpoint(d_outputs[port], false); + } + + if(result.empty()) { + msg << "resolve_port: hierarchical block '" << d_owner->name() + << "': unable to resolve " + << (is_input ? "input port " : "output port ") + << port; + throw std::runtime_error(msg.str()); + } + + return result; + } + + void + hier_block2_detail::disconnect_all() + { + d_fg->clear(); + d_blocks.clear(); + d_inputs.clear(); + d_outputs.clear(); + } + + endpoint_vector_t + hier_block2_detail::resolve_endpoint(const endpoint &endp, bool is_input) const + { + std::stringstream msg; + endpoint_vector_t result; + + // Check if endpoint is a leaf node + if(cast_to_block_sptr(endp.block())) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "Block " << endp.block() << " is a leaf node, returning." << std::endl; + result.push_back(endp); + return result; + } + + // Check if endpoint is a hierarchical block + hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(endp.block())); + if(hier_block2) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "Resolving endpoint " << endp << " as an " + << (is_input ? "input" : "output") + << ", recursing" << std::endl; + return hier_block2->d_detail->resolve_port(endp.port(), is_input); + } + + msg << "unable to resolve" << (is_input ? " input " : " output ") + << "endpoint " << endp; + throw std::runtime_error(msg.str()); + } + + void + hier_block2_detail::flatten_aux(flat_flowgraph_sptr sfg) const + { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << " ** Flattening " << d_owner->name() << std::endl; + + // Add my edges to the flow graph, resolving references to actual endpoints + edge_vector_t edges = d_fg->edges(); + edge_viter_t p; + + // Only run setup_rpc if ControlPort config param is enabled. + bool ctrlport_on = prefs::singleton()->get_bool("ControlPort", "on", false); + + // For every block (gr::block and gr::hier_block2), set up the RPC + // interface. + for(p = edges.begin(); p != edges.end(); p++) { + basic_block_sptr b; + b = p->src().block(); + + if(ctrlport_on) { + if(!b->is_rpc_set()) { + b->setup_rpc(); + b->rpc_set(); + } + } + + b = p->dst().block(); + if(ctrlport_on) { + if(!b->is_rpc_set()) { + b->setup_rpc(); + b->rpc_set(); + } + } + } + + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "Flattening stream connections: " << std::endl; + + for(p = edges.begin(); p != edges.end(); p++) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "Flattening edge " << (*p) << std::endl; + + endpoint_vector_t src_endps = resolve_endpoint(p->src(), false); + endpoint_vector_t dst_endps = resolve_endpoint(p->dst(), true); + + endpoint_viter_t s, d; + for(s = src_endps.begin(); s != src_endps.end(); s++) { + for(d = dst_endps.begin(); d != dst_endps.end(); d++) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << (*s) << "->" << (*d) << std::endl; + sfg->connect(*s, *d); + } + } + } + + // Construct unique list of blocks used either in edges, inputs, + // outputs, or by themselves. I still hate STL. + basic_block_vector_t blocks; // unique list of used blocks + basic_block_vector_t tmp = d_fg->calc_used_blocks(); + + // First add the list of singleton blocks + std::vector::const_iterator b; // Because flatten_aux is const + for(b = d_blocks.begin(); b != d_blocks.end(); b++) + tmp.push_back(*b); + + // Now add the list of connected input blocks + std::stringstream msg; + for(unsigned int i = 0; i < d_inputs.size(); i++) { + if(d_inputs[i].size() == 0) { + msg << "In hierarchical block " << d_owner->name() << ", input " << i + << " is not connected internally"; + throw std::runtime_error(msg.str()); + } + + for(unsigned int j = 0; j < d_inputs[i].size(); j++) + tmp.push_back(d_inputs[i][j].block()); + } + + for(unsigned int i = 0; i < d_outputs.size(); i++) { + basic_block_sptr blk = d_outputs[i].block(); + if(!blk) { + msg << "In hierarchical block " << d_owner->name() << ", output " << i + << " is not connected internally"; + throw std::runtime_error(msg.str()); + } + tmp.push_back(blk); + } + sort(tmp.begin(), tmp.end()); + + std::insert_iterator inserter(blocks, blocks.begin()); + unique_copy(tmp.begin(), tmp.end(), inserter); + + // Recurse hierarchical children + for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) { + hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(*p)); + if(hier_block2 && (hier_block2.get() != d_owner)) { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "flatten_aux: recursing into hierarchical block " + << hier_block2 << std::endl; + hier_block2->d_detail->flatten_aux(sfg); + } + } + } + + void + hier_block2_detail::lock() + { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "lock: entered in " << this << std::endl; + + if(d_parent_detail) + d_parent_detail->lock(); + else + d_owner->lock(); + } + + void + hier_block2_detail::unlock() + { + if(HIER_BLOCK2_DETAIL_DEBUG) + std::cout << "unlock: entered in " << this << std::endl; + + if(d_parent_detail) + d_parent_detail->unlock(); + else + d_owner->unlock(); + } + + void + hier_block2_detail::set_processor_affinity(const std::vector &mask) + { + basic_block_vector_t tmp = d_fg->calc_used_blocks(); + for(basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) { + (*p)->set_processor_affinity(mask); + } + } + + void + hier_block2_detail::unset_processor_affinity() + { + basic_block_vector_t tmp = d_fg->calc_used_blocks(); + for(basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) { + (*p)->unset_processor_affinity(); + } + } + + std::vector + hier_block2_detail::processor_affinity() + { + basic_block_vector_t tmp = d_fg->calc_used_blocks(); + return tmp[0]->processor_affinity(); + } + +} /* namespace gr */ diff --git a/lib/runtime/hier_block2_detail.h b/lib/runtime/hier_block2_detail.h new file mode 100644 index 0000000..27c1fd0 --- /dev/null +++ b/lib/runtime/hier_block2_detail.h @@ -0,0 +1,77 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2007,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_RUNTIME_HIER_BLOCK2_DETAIL_H +#define INCLUDED_GR_RUNTIME_HIER_BLOCK2_DETAIL_H + +#include +#include +#include +#include + +namespace gr { + + /*! + * \ingroup internal + */ + class GR_RUNTIME_API hier_block2_detail : boost::noncopyable + { + public: + hier_block2_detail(hier_block2 *owner); + ~hier_block2_detail(); + + void connect(basic_block_sptr block); + void connect(basic_block_sptr src, int src_port, + basic_block_sptr dst, int dst_port); + void disconnect(basic_block_sptr block); + void disconnect(basic_block_sptr, int src_port, + basic_block_sptr, int dst_port); + void disconnect_all(); + void lock(); + void unlock(); + void flatten_aux(flat_flowgraph_sptr sfg) const; + + void set_processor_affinity(const std::vector &mask); + void unset_processor_affinity(); + std::vector processor_affinity(); + + private: + // Private implementation data + hier_block2 *d_owner; + hier_block2_detail *d_parent_detail; + flowgraph_sptr d_fg; + std::vector d_inputs; // Multiple internal endpoints per external input + endpoint_vector_t d_outputs; // Single internal endpoint per external output + basic_block_vector_t d_blocks; + + void connect_input(int my_port, int port, basic_block_sptr block); + void connect_output(int my_port, int port, basic_block_sptr block); + void disconnect_input(int my_port, int port, basic_block_sptr block); + void disconnect_output(int my_port, int port, basic_block_sptr block); + + endpoint_vector_t resolve_port(int port, bool is_input); + endpoint_vector_t resolve_endpoint(const endpoint &endp, bool is_input) const; + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_RUNTIME_HIER_BLOCK2_DETAIL_H */ diff --git a/lib/runtime/high_res_timer.cc b/lib/runtime/high_res_timer.cc new file mode 100644 index 0000000..37e7e32 --- /dev/null +++ b/lib/runtime/high_res_timer.cc @@ -0,0 +1,8 @@ +#include + +#ifdef GNURADIO_HRT_USE_CLOCK_GETTIME +clockid_t gr::high_res_timer_source = CLOCK_THREAD_CPUTIME_ID; +#endif + + + diff --git a/lib/runtime/io_signature.cc b/lib/runtime/io_signature.cc new file mode 100644 index 0000000..ccfdf3c --- /dev/null +++ b/lib/runtime/io_signature.cc @@ -0,0 +1,117 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +namespace gr { + + gr::io_signature::sptr + io_signature::makev(int min_streams, int max_streams, + const std::vector &sizeof_stream_items) + { + return gr::io_signature::sptr + (new io_signature(min_streams, max_streams, + sizeof_stream_items)); + } + + gr::io_signature::sptr + io_signature::make(int min_streams, int max_streams, + int sizeof_stream_item) + { + std::vector sizeof_items(1); + sizeof_items[0] = sizeof_stream_item; + return io_signature::makev(min_streams, max_streams, sizeof_items); + } + + gr::io_signature::sptr + io_signature::make2(int min_streams, int max_streams, + int sizeof_stream_item1, + int sizeof_stream_item2) + { + std::vector sizeof_items(2); + sizeof_items[0] = sizeof_stream_item1; + sizeof_items[1] = sizeof_stream_item2; + return io_signature::makev(min_streams, max_streams, sizeof_items); + } + + gr::io_signature::sptr + io_signature::make3(int min_streams, int max_streams, + int sizeof_stream_item1, + int sizeof_stream_item2, + int sizeof_stream_item3) + { + std::vector sizeof_items(3); + sizeof_items[0] = sizeof_stream_item1; + sizeof_items[1] = sizeof_stream_item2; + sizeof_items[2] = sizeof_stream_item3; + return io_signature::makev(min_streams, max_streams, sizeof_items); + } + + // ------------------------------------------------------------------------ + + io_signature::io_signature(int min_streams, int max_streams, + const std::vector &sizeof_stream_items) + { + if(min_streams < 0 + || (max_streams != IO_INFINITE && max_streams < min_streams)) + throw std::invalid_argument ("gr::io_signature(1)"); + + if(sizeof_stream_items.size() < 1) + throw std::invalid_argument("gr::io_signature(2)"); + + for(size_t i = 0; i < sizeof_stream_items.size(); i++) { + if(max_streams != 0 && sizeof_stream_items[i] < 1) + throw std::invalid_argument("gr::io_signature(3)"); + } + + d_min_streams = min_streams; + d_max_streams = max_streams; + d_sizeof_stream_item = sizeof_stream_items; + } + + io_signature::~io_signature() + { + } + + int + io_signature::sizeof_stream_item(int _index) const + { + if(_index < 0) + throw std::invalid_argument("gr::io_signature::sizeof_stream_item"); + + size_t index = _index; + return d_sizeof_stream_item[std::min(index, d_sizeof_stream_item.size() - 1)]; + } + + std::vector + io_signature::sizeof_stream_items() const + { + return d_sizeof_stream_item; + } + +} /* namespace gr */ diff --git a/lib/runtime/local_sighandler.cc b/lib/runtime/local_sighandler.cc new file mode 100644 index 0000000..ebd9bb1 --- /dev/null +++ b/lib/runtime/local_sighandler.cc @@ -0,0 +1,189 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "local_sighandler.h" +#include +#include +#include + +namespace gr { + + local_sighandler::local_sighandler(int signum, + void (*new_handler)(int)) + : d_signum(signum) + { +#ifdef HAVE_SIGACTION + struct sigaction new_action; + memset(&new_action, 0, sizeof(new_action)); + + new_action.sa_handler = new_handler; + sigemptyset(&new_action.sa_mask); + new_action.sa_flags = 0; + + if(sigaction (d_signum, &new_action, &d_old_action) < 0) { + perror("sigaction (install new)"); + throw std::runtime_error("sigaction"); + } +#endif + } + + local_sighandler::~local_sighandler() + { +#ifdef HAVE_SIGACTION + if(sigaction (d_signum, &d_old_action, 0) < 0) { + perror("sigaction (restore old)"); + throw std::runtime_error("sigaction"); + } +#endif + } + + void + local_sighandler::throw_signal(int signum) + { + throw signal(signum); + } + + /* + * Semi-hideous way to may a signal number into a signal name + */ + #define SIGNAME(x) case x: return #x + + std::string + signal::name() const + { + char tmp[128]; + + switch(signum()) { +#ifdef SIGHUP + SIGNAME(SIGHUP); +#endif +#ifdef SIGINT + SIGNAME(SIGINT); +#endif +#ifdef SIGQUIT + SIGNAME(SIGQUIT); +#endif +#ifdef SIGILL + SIGNAME(SIGILL); +#endif +#ifdef SIGTRAP + SIGNAME(SIGTRAP); +#endif +#ifdef SIGABRT + SIGNAME(SIGABRT); +#endif +#ifdef SIGBUS + SIGNAME(SIGBUS); +#endif +#ifdef SIGFPE + SIGNAME(SIGFPE); +#endif +#ifdef SIGKILL + SIGNAME(SIGKILL); +#endif +#ifdef SIGUSR1 + SIGNAME(SIGUSR1); +#endif +#ifdef SIGSEGV + SIGNAME(SIGSEGV); +#endif +#ifdef SIGUSR2 + SIGNAME(SIGUSR2); +#endif +#ifdef SIGPIPE + SIGNAME(SIGPIPE); +#endif +#ifdef SIGALRM + SIGNAME(SIGALRM); +#endif +#ifdef SIGTERM + SIGNAME(SIGTERM); +#endif +#ifdef SIGSTKFLT + SIGNAME(SIGSTKFLT); +#endif +#ifdef SIGCHLD + SIGNAME(SIGCHLD); +#endif +#ifdef SIGCONT + SIGNAME(SIGCONT); +#endif +#ifdef SIGSTOP + SIGNAME(SIGSTOP); +#endif +#ifdef SIGTSTP + SIGNAME(SIGTSTP); +#endif +#ifdef SIGTTIN + SIGNAME(SIGTTIN); +#endif +#ifdef SIGTTOU + SIGNAME(SIGTTOU); +#endif +#ifdef SIGURG + SIGNAME(SIGURG); +#endif +#ifdef SIGXCPU + SIGNAME(SIGXCPU); +#endif +#ifdef SIGXFSZ + SIGNAME(SIGXFSZ); +#endif +#ifdef SIGVTALRM + SIGNAME(SIGVTALRM); +#endif +#ifdef SIGPROF + SIGNAME(SIGPROF); +#endif +#ifdef SIGWINCH + SIGNAME(SIGWINCH); +#endif +#ifdef SIGIO + SIGNAME(SIGIO); +#endif +#ifdef SIGPWR + SIGNAME(SIGPWR); +#endif +#ifdef SIGSYS + SIGNAME(SIGSYS); +#endif + default: +#if defined (HAVE_SNPRINTF) +#if defined (SIGRTMIN) && defined (SIGRTMAX) + if(signum() >= SIGRTMIN && signum() <= SIGRTMAX) { + snprintf(tmp, sizeof(tmp), "SIGRTMIN + %d", signum()); + return tmp; + } +#endif + snprintf(tmp, sizeof(tmp), "SIGNAL %d", signum()); + return tmp; +#else + return "Unknown signal"; +#endif + } + } + +} /* namespace gr */ diff --git a/lib/runtime/local_sighandler.h b/lib/runtime/local_sighandler.h new file mode 100644 index 0000000..bd322e5 --- /dev/null +++ b/lib/runtime/local_sighandler.h @@ -0,0 +1,74 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_LOCAL_SIGHANDLER_H +#define INCLUDED_GR_LOCAL_SIGHANDLER_H + +#ifdef HAVE_SIGNAL_H +#include +#endif + +#include +#include + +namespace gr { + + /*! + * \brief Get and set signal handler. + * + * \ingroup internal + * Constructor installs new handler, destructor reinstalls + * original value. + */ + class GR_RUNTIME_API local_sighandler + { + private: + int d_signum; +#ifdef HAVE_SIGACTION + struct sigaction d_old_action; +#endif + + public: + local_sighandler(int signum, void (*new_handler)(int)); + ~local_sighandler(); + + /* throw gr_signal (signum) */ + static void throw_signal(int signum); + }; + + /*! + * \brief Representation of signal. + */ + class GR_RUNTIME_API signal + { + private: + int d_signum; + + public: + signal(int signum) : d_signum(signum) {} + int signum() const { return d_signum; } + std::string name() const; + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_LOCAL_SIGHANDLER_H */ diff --git a/lib/runtime/logger.cc b/lib/runtime/logger.cc new file mode 100644 index 0000000..ce14abd --- /dev/null +++ b/lib/runtime/logger.cc @@ -0,0 +1,323 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/******************************************************************************* +* Author: Mark Plett +* Description: +* The gr_log module wraps the log4cpp library for logging in gnuradio. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + + +#ifdef ENABLE_GR_LOG +#ifdef HAVE_LOG4CPP + +namespace gr { + + bool logger_config::logger_configured(false); + + /************************ BEGIN LOG4CPP HELPERS ***********************/ + /* Logger config class. This is a singleton that controls how + * log4cpp is configured If watch_period>0 a thread is started to + * watch teh config file for changes. + */ + + // Getters of logger_config + logger_config& + logger_config::get_instance(void) + { + static logger_config instance; + return instance; + } + + std::string + logger_config::get_filename() + { + logger_config& in=get_instance(); + return in.filename; + } + + unsigned int + logger_config::get_watch_period() + { + logger_config& in=get_instance(); + return in.watch_period; + } + + // Method to watch config file for changes + void + logger_config::watch_file(std::string filename, unsigned int watch_period) + { + std::time_t last_write(boost::filesystem::last_write_time(filename)); + std::time_t current_time(0); + while(true) { + try { + current_time = boost::filesystem::last_write_time(filename); + if(current_time>last_write) { + //std::cout<<"GNURadio Reloading logger configuration:"<0) { + instance.watch_thread = new boost::thread(watch_file, instance.filename, + instance.watch_period); + } + } + } + + // Method to stop the watcher thread + void + logger_config::stop_watch() + { + logger_config& instance = get_instance(); + if(instance.watch_thread) { + instance.watch_thread->interrupt(); + instance.watch_thread->join(); + delete(instance.watch_thread); + instance.watch_thread=NULL; + } + } + + // Method to reset logger configuration + void + logger_config::reset_config(void) + { + logger_config& instance = get_instance(); + stop_watch(); + std::vector *loggers = log4cpp::Category::getCurrentCategories(); + std::vector::iterator logger = loggers->begin(); + // We can't destroy categories but we can neuter them by removing all appenders. + for(;logger!=loggers->end();logger++) { + (*logger)->removeAllAppenders(); + } + instance.filename = std::string(""); + instance.watch_period = 0; + logger_configured = false; + } + + /***************** Functions to call log4cpp methods *************************/ + + logger_ptr + logger_get_logger(std::string name) + { + if(log4cpp::Category::exists(name)) { + logger_ptr logger = &log4cpp::Category::getInstance(name); + return logger; + } + else { + logger_ptr logger = &log4cpp::Category::getInstance(name); + logger->setPriority(log4cpp::Priority::NOTSET); + return logger; + } + } + + bool + logger_load_config(const std::string &config_filename) + { + if(config_filename.size() != 0) { + try { + log4cpp::PropertyConfigurator::configure(config_filename); + return true; + } + catch(log4cpp::ConfigureFailure &e) { + std::cerr << "Logger config failed :" << e.what() << std::endl; + } + } + return false; + } + + void + logger_set_level(logger_ptr logger, const std::string &level) + { + std::string nocase = level; + std::transform(level.begin(), level.end(), nocase.begin(), ::tolower); + + if(nocase == "off" || nocase == "notset") + logger_set_level(logger, log4cpp::Priority::NOTSET); + else if(nocase == "all" || nocase == "debug") + logger_set_level(logger, log4cpp::Priority::DEBUG); + else if(nocase == "info") + logger_set_level(logger, log4cpp::Priority::INFO); + else if(nocase == "notice") + logger_set_level(logger, log4cpp::Priority::NOTICE); + else if(nocase == "warn") + logger_set_level(logger, log4cpp::Priority::WARN); + else if(nocase == "error") + logger_set_level(logger, log4cpp::Priority::ERROR); + else if(nocase == "crit") + logger_set_level(logger, log4cpp::Priority::CRIT); + else if(nocase == "alert") + logger_set_level(logger, log4cpp::Priority::ALERT); + else if(nocase=="fatal") + logger_set_level(logger, log4cpp::Priority::FATAL); + else if(nocase == "emerg") + logger_set_level(logger, log4cpp::Priority::EMERG); + else + throw std::runtime_error("logger_set_level: Bad level type.\n"); + } + + void + logger_set_level(logger_ptr logger, log4cpp::Priority::Value level) + { + logger->setPriority(level); + } + + void + logger_get_level(logger_ptr logger, std::string &level) + { + log4cpp::Priority::Value levelPtr = logger->getPriority(); + if(levelPtr == log4cpp::Priority::NOTSET) level = "noset"; + if(levelPtr == log4cpp::Priority::DEBUG) level = "debug"; + if(levelPtr == log4cpp::Priority::INFO) level = "info"; + if(levelPtr == log4cpp::Priority::NOTICE) level = "notice"; + if(levelPtr == log4cpp::Priority::WARN) level = "warn"; + if(levelPtr == log4cpp::Priority::ERROR) level = "error"; + if(levelPtr == log4cpp::Priority::CRIT) level = "crit"; + if(levelPtr == log4cpp::Priority::ALERT) level = "alert"; + if(levelPtr == log4cpp::Priority::FATAL) level = "fatal"; + if(levelPtr == log4cpp::Priority::EMERG) level = "emerg"; + } + + void + logger_get_level(logger_ptr logger,log4cpp::Priority::Value level) + { + level = logger->getPriority(); + } + + void + logger_add_console_appender(logger_ptr logger, std::string target, std::string pattern) + { + log4cpp::PatternLayout* layout = new log4cpp::PatternLayout(); + log4cpp::Appender* app; + if(target=="stdout") + app = new log4cpp::OstreamAppender("ConsoleAppender::",&std::cout); + else + app = new log4cpp::OstreamAppender("ConsoleAppender::",&std::cerr); + + layout->setConversionPattern(pattern); + app->setLayout(layout); + logger->setAppender(*app); + } + + void + logger_add_file_appender(logger_ptr logger, std::string filename, + bool append, std::string pattern) + { + log4cpp::PatternLayout* layout = new log4cpp::PatternLayout(); + log4cpp::Appender* app = new + log4cpp::FileAppender("FileAppender::"+filename, + filename); + layout->setConversionPattern(pattern); + app->setLayout(layout); + logger->setAppender(app); + } + + void + logger_add_rollingfile_appender(logger_ptr logger, std::string filename, + size_t filesize, int bkup_index, bool append, + mode_t mode, std::string pattern) + { + log4cpp::PatternLayout* layout = new log4cpp::PatternLayout(); + log4cpp::Appender* app = new + log4cpp::RollingFileAppender("RollFileAppender::" + filename, filename, + filesize, bkup_index, append, mode); + layout->setConversionPattern(pattern); + app->setLayout(layout); + logger->setAppender(app); + } + + std::vector + logger_get_logger_names(void) + { + std::vector names; + std::vector *loggers = log4cpp::Category::getCurrentCategories(); + std::vector::iterator logger = loggers->begin(); + + for(;logger!=loggers->end();logger++) { + names.push_back((*logger)->getName()); + } + return names; + } + +} /* namespace gr */ + +#endif /* HAVE_LOG4CPP */ + +/****** Start Methods to provide Python the capabilities of the macros ********/ +void +gr_logger_config(const std::string config_filename, unsigned int watch_period) +{ + GR_CONFIG_AND_WATCH_LOGGER(config_filename, watch_period); +} + +std::vector +gr_logger_get_logger_names(void) +{ + std::vector names; + GR_GET_LOGGER_NAMES(names); + return names; +} + +void +gr_logger_reset_config(void) +{ + GR_RESET_CONFIGURATION(); +} + +// Remaining capability provided by gr::logger class in gnuradio/logger.h + +#endif /* ENABLE_GR_LOGGER */ diff --git a/lib/runtime/math/fast_atan2f.cc b/lib/runtime/math/fast_atan2f.cc new file mode 100644 index 0000000..3555cf5 --- /dev/null +++ b/lib/runtime/math/fast_atan2f.cc @@ -0,0 +1,202 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include // declaration is in here +#include + +namespace gr { + + /***************************************************************************/ + /* Constant definitions */ + /***************************************************************************/ + + #define TAN_MAP_RES 0.003921569 /* (smallest non-zero value in table) */ + #define RAD_PER_DEG 0.017453293 + #define TAN_MAP_SIZE 256 + + /* arctangents from 0 to pi/4 radians */ + static float + fast_atan_table[257] = { + 0.000000e+00, 3.921549e-03, 7.842976e-03, 1.176416e-02, + 1.568499e-02, 1.960533e-02, 2.352507e-02, 2.744409e-02, + 3.136226e-02, 3.527947e-02, 3.919560e-02, 4.311053e-02, + 4.702413e-02, 5.093629e-02, 5.484690e-02, 5.875582e-02, + 6.266295e-02, 6.656816e-02, 7.047134e-02, 7.437238e-02, + 7.827114e-02, 8.216752e-02, 8.606141e-02, 8.995267e-02, + 9.384121e-02, 9.772691e-02, 1.016096e-01, 1.054893e-01, + 1.093658e-01, 1.132390e-01, 1.171087e-01, 1.209750e-01, + 1.248376e-01, 1.286965e-01, 1.325515e-01, 1.364026e-01, + 1.402496e-01, 1.440924e-01, 1.479310e-01, 1.517652e-01, + 1.555948e-01, 1.594199e-01, 1.632403e-01, 1.670559e-01, + 1.708665e-01, 1.746722e-01, 1.784728e-01, 1.822681e-01, + 1.860582e-01, 1.898428e-01, 1.936220e-01, 1.973956e-01, + 2.011634e-01, 2.049255e-01, 2.086818e-01, 2.124320e-01, + 2.161762e-01, 2.199143e-01, 2.236461e-01, 2.273716e-01, + 2.310907e-01, 2.348033e-01, 2.385093e-01, 2.422086e-01, + 2.459012e-01, 2.495869e-01, 2.532658e-01, 2.569376e-01, + 2.606024e-01, 2.642600e-01, 2.679104e-01, 2.715535e-01, + 2.751892e-01, 2.788175e-01, 2.824383e-01, 2.860514e-01, + 2.896569e-01, 2.932547e-01, 2.968447e-01, 3.004268e-01, + 3.040009e-01, 3.075671e-01, 3.111252e-01, 3.146752e-01, + 3.182170e-01, 3.217506e-01, 3.252758e-01, 3.287927e-01, + 3.323012e-01, 3.358012e-01, 3.392926e-01, 3.427755e-01, + 3.462497e-01, 3.497153e-01, 3.531721e-01, 3.566201e-01, + 3.600593e-01, 3.634896e-01, 3.669110e-01, 3.703234e-01, + 3.737268e-01, 3.771211e-01, 3.805064e-01, 3.838825e-01, + 3.872494e-01, 3.906070e-01, 3.939555e-01, 3.972946e-01, + 4.006244e-01, 4.039448e-01, 4.072558e-01, 4.105574e-01, + 4.138496e-01, 4.171322e-01, 4.204054e-01, 4.236689e-01, + 4.269229e-01, 4.301673e-01, 4.334021e-01, 4.366272e-01, + 4.398426e-01, 4.430483e-01, 4.462443e-01, 4.494306e-01, + 4.526070e-01, 4.557738e-01, 4.589307e-01, 4.620778e-01, + 4.652150e-01, 4.683424e-01, 4.714600e-01, 4.745676e-01, + 4.776654e-01, 4.807532e-01, 4.838312e-01, 4.868992e-01, + 4.899573e-01, 4.930055e-01, 4.960437e-01, 4.990719e-01, + 5.020902e-01, 5.050985e-01, 5.080968e-01, 5.110852e-01, + 5.140636e-01, 5.170320e-01, 5.199904e-01, 5.229388e-01, + 5.258772e-01, 5.288056e-01, 5.317241e-01, 5.346325e-01, + 5.375310e-01, 5.404195e-01, 5.432980e-01, 5.461666e-01, + 5.490251e-01, 5.518738e-01, 5.547124e-01, 5.575411e-01, + 5.603599e-01, 5.631687e-01, 5.659676e-01, 5.687566e-01, + 5.715357e-01, 5.743048e-01, 5.770641e-01, 5.798135e-01, + 5.825531e-01, 5.852828e-01, 5.880026e-01, 5.907126e-01, + 5.934128e-01, 5.961032e-01, 5.987839e-01, 6.014547e-01, + 6.041158e-01, 6.067672e-01, 6.094088e-01, 6.120407e-01, + 6.146630e-01, 6.172755e-01, 6.198784e-01, 6.224717e-01, + 6.250554e-01, 6.276294e-01, 6.301939e-01, 6.327488e-01, + 6.352942e-01, 6.378301e-01, 6.403565e-01, 6.428734e-01, + 6.453808e-01, 6.478788e-01, 6.503674e-01, 6.528466e-01, + 6.553165e-01, 6.577770e-01, 6.602282e-01, 6.626701e-01, + 6.651027e-01, 6.675261e-01, 6.699402e-01, 6.723452e-01, + 6.747409e-01, 6.771276e-01, 6.795051e-01, 6.818735e-01, + 6.842328e-01, 6.865831e-01, 6.889244e-01, 6.912567e-01, + 6.935800e-01, 6.958943e-01, 6.981998e-01, 7.004964e-01, + 7.027841e-01, 7.050630e-01, 7.073330e-01, 7.095943e-01, + 7.118469e-01, 7.140907e-01, 7.163258e-01, 7.185523e-01, + 7.207701e-01, 7.229794e-01, 7.251800e-01, 7.273721e-01, + 7.295557e-01, 7.317307e-01, 7.338974e-01, 7.360555e-01, + 7.382053e-01, 7.403467e-01, 7.424797e-01, 7.446045e-01, + 7.467209e-01, 7.488291e-01, 7.509291e-01, 7.530208e-01, + 7.551044e-01, 7.571798e-01, 7.592472e-01, 7.613064e-01, + 7.633576e-01, 7.654008e-01, 7.674360e-01, 7.694633e-01, + 7.714826e-01, 7.734940e-01, 7.754975e-01, 7.774932e-01, + 7.794811e-01, 7.814612e-01, 7.834335e-01, 7.853983e-01, + 7.853983e-01 + }; + + + /***************************************************************************** + Function: Arc tangent + + Syntax: angle = fast_atan2(y, x); + float y y component of input vector + float x x component of input vector + float angle angle of vector (x, y) in radians + + Description: This function calculates the angle of the vector (x,y) + based on a table lookup and linear interpolation. The table uses a + 256 point table covering -45 to +45 degrees and uses symetry to + determine the final angle value in the range of -180 to 180 + degrees. Note that this function uses the small angle approximation + for values close to zero. This routine calculates the arc tangent + with an average error of +/- 0.045 degrees. + *****************************************************************************/ + + float + fast_atan2f(float y, float x) + { + float x_abs, y_abs, z; + float alpha, angle, base_angle; + int index; + + /* don't divide by zero! */ // FIXME could get hosed with -0.0 + if((y == 0.0) && (x == 0.0)) + return 0.0; + + /* normalize to +/- 45 degree range */ + y_abs = fabsf(y); + x_abs = fabsf(x); + //z = (y_abs < x_abs ? y_abs / x_abs : x_abs / y_abs); + if(y_abs < x_abs) + z = y_abs / x_abs; + else + z = x_abs / y_abs; + + /* when ratio approaches the table resolution, the angle is */ + /* best approximated with the argument itself... */ + if(z < TAN_MAP_RES) + base_angle = z; + else { + /* find index and interpolation value */ + alpha = z * (float)TAN_MAP_SIZE - .5; + index = (int)alpha; + alpha -= (float)index; + /* determine base angle based on quadrant and */ + /* add or subtract table value from base angle based on quadrant */ + base_angle = fast_atan_table[index]; + base_angle += + (fast_atan_table[index + 1] - fast_atan_table[index]) * alpha; + } + + if(x_abs > y_abs) { /* -45 -> 45 or 135 -> 225 */ + if(x >= 0.0) { /* -45 -> 45 */ + if(y >= 0.0) + angle = base_angle; /* 0 -> 45, angle OK */ + else + angle = -base_angle; /* -45 -> 0, angle = -angle */ + } + else { /* 135 -> 180 or 180 -> -135 */ + angle = 3.14159265358979323846; + if(y >= 0.0) + angle -= base_angle; /* 135 -> 180, angle = 180 - angle */ + else + angle = base_angle - angle; /* 180 -> -135, angle = angle - 180 */ + } + } + else { /* 45 -> 135 or -135 -> -45 */ + if(y >= 0.0) { /* 45 -> 135 */ + angle = 1.57079632679489661923; + if(x >= 0.0) + angle -= base_angle; /* 45 -> 90, angle = 90 - angle */ + else + angle += base_angle; /* 90 -> 135, angle = 90 + angle */ + } + else { /* -135 -> -45 */ + angle = -1.57079632679489661923; + if(x >= 0.0) + angle += base_angle; /* -90 -> -45, angle = -90 + angle */ + else + angle -= base_angle; /* -135 -> -90, angle = -90 - angle */ + } + } + + #ifdef ZERO_TO_TWOPI + if (angle < 0) + return (angle + TWOPI); + else + return (angle); + #else + return (angle); + #endif + } + +} /* namespace gr */ diff --git a/lib/runtime/math/fxpt.cc b/lib/runtime/math/fxpt.cc new file mode 100644 index 0000000..23fdda1 --- /dev/null +++ b/lib/runtime/math/fxpt.cc @@ -0,0 +1,38 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +namespace gr { + + const float fxpt::s_sine_table[1 << NBITS][2] = { + #include "sine_table.h" + }; + + const float fxpt::PI = 3.14159265358979323846; + const float fxpt::TWO_TO_THE_31 = 2147483648.0; + +} /* namespace gr */ diff --git a/lib/runtime/math/random.cc b/lib/runtime/math/random.cc new file mode 100644 index 0000000..7170f27 --- /dev/null +++ b/lib/runtime/math/random.cc @@ -0,0 +1,188 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/* + * Copyright 1997 Massachusetts Institute of Technology + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of M.I.T. not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. M.I.T. makes no representations about the + * suitability of this software for any purpose. It is provided "as is" + * without express or implied warranty. + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +namespace gr { + +#define IA 16807 +#define IM 2147483647 +#define AM (1.0/IM) +#define IQ 127773 +#define IR 2836 +#define NDIV (1+(IM-1)/NTAB) +#define EPS 1.2e-7 +#define RNMX (1.0-EPS) + + random::random(long seed) + { + reseed(seed); + } + + void + random::reseed(long seed) + { + d_seed = seed; + d_iy = 0; + for(int i = 0; i < NTAB; i++) + d_iv[i] = 0; + d_iset = 0; + d_gset = 0; + } + + /* + * This looks like it returns a uniform random deviate between 0.0 and 1.0 + * It looks similar to code from "Numerical Recipes in C". + */ + float + random::ran1() + { + int j; + long k; + float temp; + + if(d_seed <= 0 || !d_iy) { + if(-d_seed < 1) + d_seed=1; + else + d_seed = -d_seed; + for(j=NTAB+7;j>=0;j--) { + k=d_seed/IQ; + d_seed=IA*(d_seed-k*IQ)-IR*k; + if(d_seed < 0) + d_seed += IM; + if(j < NTAB) + d_iv[j] = d_seed; + } + d_iy=d_iv[0]; + } + k=(d_seed)/IQ; + d_seed=IA*(d_seed-k*IQ)-IR*k; + if(d_seed < 0) + d_seed += IM; + j=d_iy/NDIV; + d_iy=d_iv[j]; + d_iv[j] = d_seed; + temp=AM * d_iy; + if(temp > RNMX) + temp = RNMX; + return temp; + } + + /* + * Returns a normally distributed deviate with zero mean and variance 1. + * Also looks like it's from "Numerical Recipes in C". + */ + float + random::gasdev() + { + float fac,rsq,v1,v2; + d_iset = 1 - d_iset; + if(d_iset) { + do { + v1=2.0*ran1()-1.0; + v2=2.0*ran1()-1.0; + rsq=v1*v1+v2*v2; + } while(rsq >= 1.0 || rsq == 0.0); + fac= sqrt(-2.0*log(rsq)/rsq); + d_gset=v1*fac; + return v2*fac; + } + return d_gset; + } + + /* + * Copied from The KC7WW / OH2BNS Channel Simulator + * FIXME Need to check how good this is at some point + */ + float + random::laplacian() + { + float z = ran1(); + if(z < 0.5) + return log(2.0 * z) / M_SQRT2; + else + return -log(2.0 * (1.0 - z)) / M_SQRT2; + } + + /* + * Copied from The KC7WW / OH2BNS Channel Simulator + * FIXME Need to check how good this is at some point + */ + // 5 => scratchy, 8 => Geiger + float + random::impulse(float factor = 5) + { + float z = -M_SQRT2 * log(ran1()); + if(fabsf(z) <= factor) + return 0.0; + else + return z; + } + + /* + * Complex rayleigh is really gaussian I and gaussian Q + * It can also be generated by real rayleigh magnitude and + * uniform random angle + * Adapted from The KC7WW / OH2BNS Channel Simulator + * FIXME Need to check how good this is at some point + */ + gr_complex + random::rayleigh_complex() + { + return gr_complex(gasdev(),gasdev()); + } + + /* Other option + mag = rayleigh(); + ang = 2.0 * M_PI * RNG(); + *Rx = rxx * cos(z); + *Iy = rxx * sin(z); + */ + + float + random::rayleigh() + { + return sqrt(-2.0 * log(ran1())); + } + +} /* namespace gr */ diff --git a/lib/runtime/math/sincos.cc b/lib/runtime/math/sincos.cc new file mode 100644 index 0000000..49a90d2 --- /dev/null +++ b/lib/runtime/math/sincos.cc @@ -0,0 +1,85 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE // ask for GNU extensions if available +#endif + +#include +#include + +namespace gr { + +#if defined (HAVE_SINCOS) + + void + sincos(double x, double *sinx, double *cosx) + { + ::sincos(x, sinx, cosx); + } + +#else + + void + sincos(double x, double *sinx, double *cosx) + { + *sinx = ::sin(x); + *cosx = ::cos(x); + } + +#endif + + // ---------------------------------------------------------------- + +#if defined (HAVE_SINCOSF) + + void + sincosf(float x, float *sinx, float *cosx) + { + ::sincosf(x, sinx, cosx); + } + +#elif defined (HAVE_SINF) && defined (HAVE_COSF) + + void + sincosf(float x, float *sinx, float *cosx) + { + *sinx = ::sinf(x); + *cosx = ::cosf(x); + } + +#else + + void + sincosf(float x, float *sinx, float *cosx) + { + *sinx = ::sin(x); + *cosx = ::cos(x); + } + +#endif + +} /* namespace gr */ diff --git a/lib/runtime/math/sine_table.h b/lib/runtime/math/sine_table.h new file mode 100644 index 0000000..6983494 --- /dev/null +++ b/lib/runtime/math/sine_table.h @@ -0,0 +1,1025 @@ + // max_error = 2.353084136763606e-06 + { 2.925817799165007e-09, 7.219194364267018e-09 }, + { 2.925707643778599e-09, 2.526699001579799e-07 }, + { 2.925487337153070e-09, 1.191140162167675e-06 }, + { 2.925156887582842e-09, 3.284585035595589e-06 }, + { 2.924716307509151e-09, 6.994872605695784e-06 }, + { 2.924165613519592e-09, 1.278374920658798e-05 }, + { 2.923504826347475e-09, 2.111280464718590e-05 }, + { 2.922733970871080e-09, 3.244343744537165e-05 }, + { 2.921853076112655e-09, 4.723682007436170e-05 }, + { 2.920862175237416e-09, 6.595386421935634e-05 }, + { 2.919761305552202e-09, 8.905518605213658e-05 }, + { 2.918550508504146e-09, 1.170010715193098e-04 }, + { 2.917229829679050e-09, 1.502514416517192e-04 }, + { 2.915799318799769e-09, 1.892658178912071e-04 }, + { 2.914259029724184e-09, 2.345032874456615e-04 }, + { 2.912609020443340e-09, 2.864224686607020e-04 }, + { 2.910849353079123e-09, 3.454814764261432e-04 }, + { 2.908980093882049e-09, 4.121378876027343e-04 }, + { 2.907001313228646e-09, 4.868487064877691e-04 }, + { 2.904913085618902e-09, 5.700703303049837e-04 }, + { 2.902715489673383e-09, 6.622585147355725e-04 }, + { 2.900408608130373e-09, 7.638683394782519e-04 }, + { 2.897992527842612e-09, 8.753541738578119e-04 }, + { 2.895467339774186e-09, 9.971696424604937e-04 }, + { 2.892833138996999e-09, 1.129767590823255e-03 }, + { 2.890090024687216e-09, 1.273600051161478e-03 }, + { 2.887238100121550e-09, 1.429118208142094e-03 }, + { 2.884277472673313e-09, 1.596772364709564e-03 }, + { 2.881208253808507e-09, 1.777011907950626e-03 }, + { 2.878030559081432e-09, 1.970285275029487e-03 }, + { 2.874744508130554e-09, 2.177039919152579e-03 }, + { 2.871350224673798e-09, 2.397722275614272e-03 }, + { 2.867847836504030e-09, 2.632777727878843e-03 }, + { 2.864237475484149e-09, 2.882650573737405e-03 }, + { 2.860519277542297e-09, 3.147783991507308e-03 }, + { 2.856693382666432e-09, 3.428620006328931e-03 }, + { 2.852759934899389e-09, 3.725599456482154e-03 }, + { 2.848719082333207e-09, 4.039161959812243e-03 }, + { 2.844570977103752e-09, 4.369745880190706e-03 }, + { 2.840315775384800e-09, 4.717788294077374e-03 }, + { 2.835953637382310e-09, 5.083724957128360e-03 }, + { 2.831484727328322e-09, 5.467990270896617e-03 }, + { 2.826909213474759e-09, 5.871017249604038e-03 }, + { 2.822227268087134e-09, 6.293237486988512e-03 }, + { 2.817439067438018e-09, 6.735081123237729e-03 }, + { 2.812544791800534e-09, 7.196976811989608e-03 }, + { 2.807544625441273e-09, 7.679351687456759e-03 }, + { 2.802438756613836e-09, 8.182631331563162e-03 }, + { 2.797227377551135e-09, 8.707239741274575e-03 }, + { 2.791910684458716e-09, 9.253599295902304e-03 }, + { 2.786488877507140e-09, 9.822130724578715e-03 }, + { 2.780962160824228e-09, 1.041325307382490e-02 }, + { 2.775330742487884e-09, 1.102738367513773e-02 }, + { 2.769594834517682e-09, 1.166493811278924e-02 }, + { 2.763754652867477e-09, 1.232633019159818e-02 }, + { 2.757810417416620e-09, 1.301197190494069e-02 }, + { 2.751762351962413e-09, 1.372227340270610e-02 }, + { 2.745610684210923e-09, 1.445764295952962e-02 }, + { 2.739355645769094e-09, 1.521848694296229e-02 }, + { 2.732997472135539e-09, 1.600520978188769e-02 }, + { 2.726536402691907e-09, 1.681821393496225e-02 }, + { 2.719972680693777e-09, 1.765789985920713e-02 }, + { 2.713306553261610e-09, 1.852466597868779e-02 }, + { 2.706538271371373e-09, 1.941890865333146e-02 }, + { 2.699668089844909e-09, 2.034102214787814e-02 }, + { 2.692696267340880e-09, 2.129139860085272e-02 }, + { 2.685623066344263e-09, 2.227042799383416e-02 }, + { 2.678448753157212e-09, 2.327849812064098e-02 }, + { 2.671173597888530e-09, 2.431599455681316e-02 }, + { 2.663797874443630e-09, 2.538330062913108e-02 }, + { 2.656321860514457e-09, 2.648079738524795e-02 }, + { 2.648745837568575e-09, 2.760886356354952e-02 }, + { 2.641070090839117e-09, 2.876787556300114e-02 }, + { 2.633294909313421e-09, 2.995820741329835e-02 }, + { 2.625420585722845e-09, 3.118023074495535e-02 }, + { 2.617447416531143e-09, 3.243431475972608e-02 }, + { 2.609375701923643e-09, 3.372082620101990e-02 }, + { 2.601205745795833e-09, 3.504012932452527e-02 }, + { 2.592937855741933e-09, 3.639258586895711e-02 }, + { 2.584572343043400e-09, 3.777855502693250e-02 }, + { 2.576109522656942e-09, 3.919839341605197e-02 }, + { 2.567549713203028e-09, 4.065245505002102e-02 }, + { 2.558893236953688e-09, 4.214109131001403e-02 }, + { 2.550140419820252e-09, 4.366465091617666e-02 }, + { 2.541291591341445e-09, 4.522347989919473e-02 }, + { 2.532347084670572e-09, 4.681792157215026e-02 }, + { 2.523307236563343e-09, 4.844831650239501e-02 }, + { 2.514172387364900e-09, 5.011500248369893e-02 }, + { 2.504942880997064e-09, 5.181831450849345e-02 }, + { 2.495619064945627e-09, 5.355858474024022e-02 }, + { 2.486201290246928e-09, 5.533614248606705e-02 }, + { 2.476689911475047e-09, 5.715131416942842e-02 }, + { 2.467085286727668e-09, 5.900442330315692e-02 }, + { 2.457387777613798e-09, 6.089579046229943e-02 }, + { 2.447597749239101e-09, 6.282573325755320e-02 }, + { 2.437715570192557e-09, 6.479456630859221e-02 }, + { 2.427741612532542e-09, 6.680260121764925e-02 }, + { 2.417676251773166e-09, 6.885014654319160e-02 }, + { 2.407519866869294e-09, 7.093750777401114e-02 }, + { 2.397272840203310e-09, 7.306498730310884e-02 }, + { 2.386935557569868e-09, 7.523288440214027e-02 }, + { 2.376508408161815e-09, 7.744149519577415e-02 }, + { 2.365991784555363e-09, 7.969111263635709e-02 }, + { 2.355386082695641e-09, 8.198202647865405e-02 }, + { 2.344691701881232e-09, 8.431452325495814e-02 }, + { 2.333909044749407e-09, 8.668888625021409e-02 }, + { 2.323038517261246e-09, 8.910539547731611e-02 }, + { 2.312080528685971e-09, 9.156432765274414e-02 }, + { 2.301035491585642e-09, 9.406595617227698e-02 }, + { 2.289903821799651e-09, 9.661055108691619e-02 }, + { 2.278685938428940e-09, 9.919837907903295e-02 }, + { 2.267382263820762e-09, 1.018297034385580e-01 }, + { 2.255993223551837e-09, 1.045047840397028e-01 }, + { 2.244519246413220e-09, 1.072238773174577e-01 }, + { 2.232960764393620e-09, 1.099872362446146e-01 }, + { 2.221318212663309e-09, 1.127951103088245e-01 }, + { 2.209592029557811e-09, 1.156477454898748e-01 }, + { 2.197782656561395e-09, 1.185453842371912e-01 }, + { 2.185890538290176e-09, 1.214882654476019e-01 }, + { 2.173916122475606e-09, 1.244766244431883e-01 }, + { 2.161859859947797e-09, 1.275106929493488e-01 }, + { 2.149722204618256e-09, 1.305906990731841e-01 }, + { 2.137503613462743e-09, 1.337168672820376e-01 }, + { 2.125204546504321e-09, 1.368894183821595e-01 }, + { 2.112825466795944e-09, 1.401085694976751e-01 }, + { 2.100366840402933e-09, 1.433745340497602e-01 }, + { 2.087829136385612e-09, 1.466875217359607e-01 }, + { 2.075212826781308e-09, 1.500477385098620e-01 }, + { 2.062518386587093e-09, 1.534553865607503e-01 }, + { 2.049746293741359e-09, 1.569106642937665e-01 }, + { 2.036897029106193e-09, 1.604137663100403e-01 }, + { 2.023971076449323e-09, 1.639648833871233e-01 }, + { 2.010968922425217e-09, 1.675642024598467e-01 }, + { 1.997891056557933e-09, 1.712119066008896e-01 }, + { 1.984737971221581e-09, 1.749081750021970e-01 }, + { 1.971510161622434e-09, 1.786531829561379e-01 }, + { 1.958208125780130e-09, 1.824471018371070e-01 }, + { 1.944832364508511e-09, 1.862900990834311e-01 }, + { 1.931383381397782e-09, 1.901823381790926e-01 }, + { 1.917861682794392e-09, 1.941239786363039e-01 }, + { 1.904267777782611e-09, 1.981151759777950e-01 }, + { 1.890602178165317e-09, 2.021560817195309e-01 }, + { 1.876865398444616e-09, 2.062468433536743e-01 }, + { 1.863057955802572e-09, 2.103876043317229e-01 }, + { 1.849180370081465e-09, 2.145785040479915e-01 }, + { 1.835233163764673e-09, 2.188196778231083e-01 }, + { 1.821216861956509e-09, 2.231112568880342e-01 }, + { 1.807131992362945e-09, 2.274533683680190e-01 }, + { 1.792979085271234e-09, 2.318461352671018e-01 }, + { 1.778758673530482e-09, 2.362896764525300e-01 }, + { 1.764471292530943e-09, 2.407841066397789e-01 }, + { 1.750117480184598e-09, 2.453295363773890e-01 }, + { 1.735697776904342e-09, 2.499260720324433e-01 }, + { 1.721212725583874e-09, 2.545738157760434e-01 }, + { 1.706662871577097e-09, 2.592728655691494e-01 }, + { 1.692048762677849e-09, 2.640233151485341e-01 }, + { 1.677370949099090e-09, 2.688252540131204e-01 }, + { 1.662629983452104e-09, 2.736787674105404e-01 }, + { 1.647826420726167e-09, 2.785839363237506e-01 }, + { 1.632960818266680e-09, 2.835408374583758e-01 }, + { 1.618033735755429e-09, 2.885495432295704e-01 }, + { 1.603045735188609e-09, 2.936101217498361e-01 }, + { 1.587997380855918e-09, 2.987226368167127e-01 }, + { 1.572889239319430e-09, 3.038871479007593e-01 }, + { 1.557721879392051e-09, 3.091037101339017e-01 }, + { 1.542495872116447e-09, 3.143723742978435e-01 }, + { 1.527211790743024e-09, 3.196931868130269e-01 }, + { 1.511870210708909e-09, 3.250661897274744e-01 }, + { 1.496471709615926e-09, 3.304914207062036e-01 }, + { 1.481016867208896e-09, 3.359689130207621e-01 }, + { 1.465506265353924e-09, 3.414986955389885e-01 }, + { 1.449940488016384e-09, 3.470807927151147e-01 }, + { 1.434320121238994e-09, 3.527152245800635e-01 }, + { 1.418645753119802e-09, 3.584020067320109e-01 }, + { 1.402917973789838e-09, 3.641411503272979e-01 }, + { 1.387137375391042e-09, 3.699326620714776e-01 }, + { 1.371304552054134e-09, 3.757765442106153e-01 }, + { 1.355420099875958e-09, 3.816727945230153e-01 }, + { 1.339484616897137e-09, 3.876214063110671e-01 }, + { 1.323498703079580e-09, 3.936223683933865e-01 }, + { 1.307462960283922e-09, 3.996756650972121e-01 }, + { 1.291377992246768e-09, 4.057812762511174e-01 }, + { 1.275244404558188e-09, 4.119391771778626e-01 }, + { 1.259062804638585e-09, 4.181493386877248e-01 }, + { 1.242833801715929e-09, 4.244117270719281e-01 }, + { 1.226558006803155e-09, 4.307263040962509e-01 }, + { 1.210236032674760e-09, 4.370930269951803e-01 }, + { 1.193868493843725e-09, 4.435118484661861e-01 }, + { 1.177456006538695e-09, 4.499827166641340e-01 }, + { 1.160999188680582e-09, 4.565055751961679e-01 }, + { 1.144498659859216e-09, 4.630803631168164e-01 }, + { 1.127955041310214e-09, 4.697070149232604e-01 }, + { 1.111368955891417e-09, 4.763854605510119e-01 }, + { 1.094741028059551e-09, 4.831156253697562e-01 }, + { 1.078071883846871e-09, 4.898974301794375e-01 }, + { 1.061362150836978e-09, 4.967307912069362e-01 }, + { 1.044612458142151e-09, 5.036156201023686e-01 }, + { 1.027823436378632e-09, 5.105518239364775e-01 }, + { 1.010995717643647e-09, 5.175393051975563e-01 }, + { 9.941299354913699e-10, 5.245779617890562e-01 }, + { 9.772267249089968e-10, 5.316676870274011e-01 }, + { 9.602867222926046e-10, 5.388083696401416e-01 }, + { 9.433105654240147e-10, 5.459998937639375e-01 }, + { 9.262988934458084e-10, 5.532421389435711e-01 }, + { 9.092523468378193e-10, 5.605349801305876e-01 }, + { 8.921715673928355e-10, 5.678782876825250e-01 }, + { 8.750571981926701e-10, 5.752719273622372e-01 }, + { 8.579098835836508e-10, 5.827157603377209e-01 }, + { 8.407302691522673e-10, 5.902096431821322e-01 }, + { 8.235190017016133e-10, 5.977534278737073e-01 }, + { 8.062767292259225e-10, 6.053469617967722e-01 }, + { 7.890041008871165e-10, 6.129900877421282e-01 }, + { 7.717017669898175e-10, 6.206826439083659e-01 }, + { 7.543703789572603e-10, 6.284244639030392e-01 }, + { 7.370105893063053e-10, 6.362153767444958e-01 }, + { 7.196230516231919e-10, 6.440552068636356e-01 }, + { 7.022084205389746e-10, 6.519437741060674e-01 }, + { 6.847673517046416e-10, 6.598808937346672e-01 }, + { 6.673005017664976e-10, 6.678663764322770e-01 }, + { 6.498085283416530e-10, 6.759000283046127e-01 }, + { 6.322920899929834e-10, 6.839816508836737e-01 }, + { 6.147518462045659e-10, 6.921110411311926e-01 }, + { 5.971884573565851e-10, 7.002879914425926e-01 }, + { 5.796025847007168e-10, 7.085122896509806e-01 }, + { 5.619948903351406e-10, 7.167837190315758e-01 }, + { 5.443660371796048e-10, 7.251020583063744e-01 }, + { 5.267166889504394e-10, 7.334670816491009e-01 }, + { 5.090475101356742e-10, 7.418785586903696e-01 }, + { 4.913591659698399e-10, 7.503362545232619e-01 }, + { 4.736523224091392e-10, 7.588399297089872e-01 }, + { 4.559276461062478e-10, 7.673893402829834e-01 }, + { 4.381858043851147e-10, 7.759842377612828e-01 }, + { 4.204274652161870e-10, 7.846243691469355e-01 }, + { 4.026532971908398e-10, 7.933094769370790e-01 }, + { 3.848639694963359e-10, 8.020392991300200e-01 }, + { 3.670601518910503e-10, 8.108135692324444e-01 }, + { 3.492425146784233e-10, 8.196320162675177e-01 }, + { 3.314117286825031e-10, 8.284943647824689e-01 }, + { 3.135684652223755e-10, 8.374003348569865e-01 }, + { 2.957133960867535e-10, 8.463496421118015e-01 }, + { 2.778471935089361e-10, 8.553419977173513e-01 }, + { 2.599705301412391e-10, 8.643771084029740e-01 }, + { 2.420840790301135e-10, 8.734546764660205e-01 }, + { 2.241885135902046e-10, 8.825743997817682e-01 }, + { 2.062845075795238e-10, 8.917359718130367e-01 }, + { 1.883727350736140e-10, 9.009390816205823e-01 }, + { 1.704538704408269e-10, 9.101834138731877e-01 }, + { 1.525285883160648e-10, 9.194686488588080e-01 }, + { 1.345975635762696e-10, 9.287944624950824e-01 }, + { 1.166614713141648e-10, 9.381605263410157e-01 }, + { 9.872098681369190e-11, 9.475665076080466e-01 }, + { 8.077678552380464e-11, 9.570120691722380e-01 }, + { 6.282954303364090e-11, 9.664968695860140e-01 }, + { 4.487993504668797e-11, 9.760205630906909e-01 }, + { 2.692863735553042e-11, 9.855827996289697e-01 }, + { 8.976325816439114e-12, 9.951832248577780e-01 }, + { -8.976323676304494e-12, 1.004821480161519e+00 }, + { -2.692863521550168e-11, 1.014497202665280e+00 }, + { -4.487993290681805e-11, 1.024210025248670e+00 }, + { -6.282954089398273e-11, 1.033959576559617e+00 }, + { -8.077678338451706e-11, 1.043745481028715e+00 }, + { -9.872098467477489e-11, 1.053567358883467e+00 }, + { -1.166614691757772e-10, 1.063424826163223e+00 }, + { -1.345975614383584e-10, 1.073317494734013e+00 }, + { -1.525285861788948e-10, 1.083244972303963e+00 }, + { -1.704538683042922e-10, 1.093206862438572e+00 }, + { -1.883727329379793e-10, 1.103202764576806e+00 }, + { -2.062845054446831e-10, 1.113232274046796e+00 }, + { -2.241885114563697e-10, 1.123294982082432e+00 }, + { -2.420840768973375e-10, 1.133390475839767e+00 }, + { -2.599705280096278e-10, 1.143518338413855e+00 }, + { -2.778471913784365e-10, 1.153678148855860e+00 }, + { -2.957133939575774e-10, 1.163869482190458e+00 }, + { -3.135684630945758e-10, 1.174091909433296e+00 }, + { -3.314117265561857e-10, 1.184344997608959e+00 }, + { -3.492425125535882e-10, 1.194628309769018e+00 }, + { -3.670601497678034e-10, 1.204941405010466e+00 }, + { -3.848639673748360e-10, 1.215283838494269e+00 }, + { -4.026532950710339e-10, 1.225655161464298e+00 }, + { -4.204274630982869e-10, 1.236054921266445e+00 }, + { -4.381858022691734e-10, 1.246482661367958e+00 }, + { -4.559276439922654e-10, 1.256937921377146e+00 }, + { -4.736523202972214e-10, 1.267420237063216e+00 }, + { -4.913591638600925e-10, 1.277929140376502e+00 }, + { -5.090475080282032e-10, 1.288464159468706e+00 }, + { -5.267166868452449e-10, 1.299024818713528e+00 }, + { -5.443660350768455e-10, 1.309610638727845e+00 }, + { -5.619948882348695e-10, 1.320221136392390e+00 }, + { -5.796025826029868e-10, 1.330855824873457e+00 }, + { -5.971884552615020e-10, 1.341514213644420e+00 }, + { -6.147518441122357e-10, 1.352195808507556e+00 }, + { -6.322920879034590e-10, 1.362900111616144e+00 }, + { -6.498085262549874e-10, 1.373626621496939e+00 }, + { -6.673004996827436e-10, 1.384374833072571e+00 }, + { -6.847673496239581e-10, 1.395144237684605e+00 }, + { -7.022084184613616e-10, 1.405934323116231e+00 }, + { -7.196230495488082e-10, 1.416744573616104e+00 }, + { -7.370105872352039e-10, 1.427574469921397e+00 }, + { -7.543703768894941e-10, 1.438423489281758e+00 }, + { -7.717017649255453e-10, 1.449291105483472e+00 }, + { -7.890040988262324e-10, 1.460176788873383e+00 }, + { -8.062767271686383e-10, 1.471080006383765e+00 }, + { -8.235189996479819e-10, 1.482000221556656e+00 }, + { -8.407302671024475e-10, 1.492936894569018e+00 }, + { -8.579098815375368e-10, 1.503889482257845e+00 }, + { -8.750571961505266e-10, 1.514857438145604e+00 }, + { -8.921715653546624e-10, 1.525840212465756e+00 }, + { -9.092523448036167e-10, 1.536837252188703e+00 }, + { -9.262988914157881e-10, 1.547848001047890e+00 }, + { -9.433105633981766e-10, 1.558871899565883e+00 }, + { -9.602867202711075e-10, 1.569908385081254e+00 }, + { -9.772267228916820e-10, 1.580956891774897e+00 }, + { -9.941299334786078e-10, 1.592016850697478e+00 }, + { -1.010995715635332e-09, 1.603087689796053e+00 }, + { -1.027823434374870e-09, 1.614168833942028e+00 }, + { -1.044612456143047e-09, 1.625259704958335e+00 }, + { -1.061362148842745e-09, 1.636359721647526e+00 }, + { -1.078071881857297e-09, 1.647468299819543e+00 }, + { -1.094741026074900e-09, 1.658584852320419e+00 }, + { -1.111368953911690e-09, 1.669708789060341e+00 }, + { -1.127955039335462e-09, 1.680839517042381e+00 }, + { -1.144498657889600e-09, 1.691976440391624e+00 }, + { -1.160999186716154e-09, 1.703118960383971e+00 }, + { -1.177456004579561e-09, 1.714266475475616e+00 }, + { -1.193868491889832e-09, 1.725418381332405e+00 }, + { -1.210236030726319e-09, 1.736574070859850e+00 }, + { -1.226558004860220e-09, 1.747732934232508e+00 }, + { -1.242833799778447e-09, 1.758894358924547e+00 }, + { -1.259062802706714e-09, 1.770057729740021e+00 }, + { -1.275244402631982e-09, 1.781222428842935e+00 }, + { -1.291377990326492e-09, 1.792387835788660e+00 }, + { -1.307462958369363e-09, 1.803553327553897e+00 }, + { -1.323498701170897e-09, 1.814718278568759e+00 }, + { -1.339484614994490e-09, 1.825882060747428e+00 }, + { -1.355420097979292e-09, 1.837044043519582e+00 }, + { -1.371304550163662e-09, 1.848203593862598e+00 }, + { -1.387137373506711e-09, 1.859360076332671e+00 }, + { -1.402917971911754e-09, 1.870512853097495e+00 }, + { -1.418645751248018e-09, 1.881661283967967e+00 }, + { -1.434320119373722e-09, 1.892804726431080e+00 }, + { -1.449940486157623e-09, 1.903942535681972e+00 }, + { -1.465506263501516e-09, 1.915074064656886e+00 }, + { -1.481016865363264e-09, 1.926198664066737e+00 }, + { -1.496471707776859e-09, 1.937315682428795e+00 }, + { -1.511870208876724e-09, 1.948424466101625e+00 }, + { -1.527211788917509e-09, 1.959524359317042e+00 }, + { -1.542495870297867e-09, 1.970614704215133e+00 }, + { -1.557721877580406e-09, 1.981694840876775e+00 }, + { -1.572889237514880e-09, 1.992764107358707e+00 }, + { -1.587997379058514e-09, 2.003821839726753e+00 }, + { -1.603045733398246e-09, 2.014867372090665e+00 }, + { -1.618033733972424e-09, 2.025900036638798e+00 }, + { -1.632960816490822e-09, 2.036919163671778e+00 }, + { -1.647826418957721e-09, 2.047924081638631e+00 }, + { -1.662629981691070e-09, 2.058914117170269e+00 }, + { -1.677370947345626e-09, 2.069888595116115e+00 }, + { -1.692048760931849e-09, 2.080846838577820e+00 }, + { -1.706662869838827e-09, 2.091788168946183e+00 }, + { -1.721212723853279e-09, 2.102711905935372e+00 }, + { -1.735697775181424e-09, 2.113617367619504e+00 }, + { -1.750117478469621e-09, 2.124503870468520e+00 }, + { -1.764471290823748e-09, 2.135370729383332e+00 }, + { -1.778758671831281e-09, 2.146217257733207e+00 }, + { -1.792979083579974e-09, 2.157042767390815e+00 }, + { -1.807131990679890e-09, 2.167846568770014e+00 }, + { -1.821216860281448e-09, 2.178627970860822e+00 }, + { -1.835233162097977e-09, 2.189386281268046e+00 }, + { -1.849180368423027e-09, 2.200120806246095e+00 }, + { -1.863057954152340e-09, 2.210830850737588e+00 }, + { -1.876865396802907e-09, 2.221515718409926e+00 }, + { -1.890602176531920e-09, 2.232174711691990e+00 }, + { -1.904267776157843e-09, 2.242807131812679e+00 }, + { -1.917861681178094e-09, 2.253412278837029e+00 }, + { -1.931383379790273e-09, 2.263989451705295e+00 }, + { -1.944832362909578e-09, 2.274537948269257e+00 }, + { -1.958208124189984e-09, 2.285057065331676e+00 }, + { -1.971510160041235e-09, 2.295546098682665e+00 }, + { -1.984737969649064e-09, 2.306004343138794e+00 }, + { -1.997891054994522e-09, 2.316431092581699e+00 }, + { -2.010968920870647e-09, 2.326825639994779e+00 }, + { -2.023971074903858e-09, 2.337187277503834e+00 }, + { -2.036897027569834e-09, 2.347515296413520e+00 }, + { -2.049746292214264e-09, 2.357808987247877e+00 }, + { -2.062518385069210e-09, 2.368067639787542e+00 }, + { -2.075212825272584e-09, 2.378290543109652e+00 }, + { -2.087829134886364e-09, 2.388476985626922e+00 }, + { -2.100366838912949e-09, 2.398626255125417e+00 }, + { -2.112825465315542e-09, 2.408737638805759e+00 }, + { -2.125204545033289e-09, 2.418810423320288e+00 }, + { -2.137503612001452e-09, 2.428843894814472e+00 }, + { -2.149722203166389e-09, 2.438837338964302e+00 }, + { -2.161859858505829e-09, 2.448790041018174e+00 }, + { -2.173916121043380e-09, 2.458701285834241e+00 }, + { -2.185890536867478e-09, 2.468570357921585e+00 }, + { -2.197782655148702e-09, 2.478396541480230e+00 }, + { -2.209592028154913e-09, 2.488179120439544e+00 }, + { -2.221318211270522e-09, 2.497917378500214e+00 }, + { -2.232960763010574e-09, 2.507610599172123e+00 }, + { -2.244519245040444e-09, 2.517258065817044e+00 }, + { -2.255993222189014e-09, 2.526859061686102e+00 }, + { -2.267382262468209e-09, 2.536412869962689e+00 }, + { -2.278685937086658e-09, 2.545918773800664e+00 }, + { -2.289903820467374e-09, 2.555376056366064e+00 }, + { -2.301035490263848e-09, 2.564784000877677e+00 }, + { -2.312080527374447e-09, 2.574141890646339e+00 }, + { -2.323038515960257e-09, 2.583449009117307e+00 }, + { -2.333909043458635e-09, 2.592704639909166e+00 }, + { -2.344691700601153e-09, 2.601908066856634e+00 }, + { -2.355386081425938e-09, 2.611058574048749e+00 }, + { -2.365991783296513e-09, 2.620155445872768e+00 }, + { -2.376508406913500e-09, 2.629197967052127e+00 }, + { -2.386935556332088e-09, 2.638185422689490e+00 }, + { -2.397272838976436e-09, 2.647117098307332e+00 }, + { -2.407519865653114e-09, 2.655992279887846e+00 }, + { -2.417676250567891e-09, 2.664810253915885e+00 }, + { -2.427741611338014e-09, 2.673570307418169e+00 }, + { -2.437715569009093e-09, 2.682271728006635e+00 }, + { -2.447597748066437e-09, 2.690913803917100e+00 }, + { -2.457387776452357e-09, 2.699495824053297e+00 }, + { -2.467085285577292e-09, 2.708017078025636e+00 }, + { -2.476689910335470e-09, 2.716476856194105e+00 }, + { -2.486201289118733e-09, 2.724874449709689e+00 }, + { -2.495619063828443e-09, 2.733209150554255e+00 }, + { -2.504942879891263e-09, 2.741480251583985e+00 }, + { -2.514172386270163e-09, 2.749687046568741e+00 }, + { -2.523307235480146e-09, 2.757828830235740e+00 }, + { -2.532347083598520e-09, 2.765904898308531e+00 }, + { -2.541291590280960e-09, 2.773914547551261e+00 }, + { -2.550140418771202e-09, 2.781857075807392e+00 }, + { -2.558893235915887e-09, 2.789731782043156e+00 }, + { -2.567549712176927e-09, 2.797537966388929e+00 }, + { -2.576109521642196e-09, 2.805274930179221e+00 }, + { -2.584572342040407e-09, 2.812941975996573e+00 }, + { -2.592937854750428e-09, 2.820538407710556e+00 }, + { -2.601205744816134e-09, 2.828063530521908e+00 }, + { -2.609375700955458e-09, 2.835516651001539e+00 }, + { -2.617447415574869e-09, 2.842897077134583e+00 }, + { -2.625420584778350e-09, 2.850204118359573e+00 }, + { -2.633294908380520e-09, 2.857437085611509e+00 }, + { -2.641070089918234e-09, 2.864595291363663e+00 }, + { -2.648745836659391e-09, 2.871678049666939e+00 }, + { -2.656321859617343e-09, 2.878684676194483e+00 }, + { -2.663797873558322e-09, 2.885614488280000e+00 }, + { -2.671173597015318e-09, 2.892466804962122e+00 }, + { -2.678448752295859e-09, 2.899240947023252e+00 }, + { -2.685623065495139e-09, 2.905936237033475e+00 }, + { -2.692696266503800e-09, 2.912551999389617e+00 }, + { -2.699668089019767e-09, 2.919087560358171e+00 }, + { -2.706538270558513e-09, 2.925542248116882e+00 }, + { -2.713306552460767e-09, 2.931915392794031e+00 }, + { -2.719972679905295e-09, 2.938206326512581e+00 }, + { -2.726536401915442e-09, 2.944414383428562e+00 }, + { -2.732997471371516e-09, 2.950538899775061e+00 }, + { -2.739355645017194e-09, 2.956579213900666e+00 }, + { -2.745610683471516e-09, 2.962534666313284e+00 }, + { -2.751762351235315e-09, 2.968404599718795e+00 }, + { -2.757810416701751e-09, 2.974188359063684e+00 }, + { -2.763754652165128e-09, 2.979885291576143e+00 }, + { -2.769594833827588e-09, 2.985494746805227e+00 }, + { -2.775330741810390e-09, 2.991016076664491e+00 }, + { -2.780962160159068e-09, 2.996448635469842e+00 }, + { -2.786488876854607e-09, 3.001791779983262e+00 }, + { -2.791910683818570e-09, 3.007044869450794e+00 }, + { -2.797227376923695e-09, 3.012207265645876e+00 }, + { -2.802438755998943e-09, 3.017278332907412e+00 }, + { -2.807544624838820e-09, 3.022257438182037e+00 }, + { -2.812544791210840e-09, 3.027143951064684e+00 }, + { -2.817439066860792e-09, 3.031937243837070e+00 }, + { -2.822227267522746e-09, 3.036636691510884e+00 }, + { -2.826909212922864e-09, 3.041241671864994e+00 }, + { -2.831484726789317e-09, 3.045751565488710e+00 }, + { -2.835953636855826e-09, 3.050165755818853e+00 }, + { -2.840315774871260e-09, 3.054483629182857e+00 }, + { -2.844570976602957e-09, 3.058704574835744e+00 }, + { -2.848719081844986e-09, 3.062827985002047e+00 }, + { -2.852759934424164e-09, 3.066853254915581e+00 }, + { -2.856693382203833e-09, 3.070779782857041e+00 }, + { -2.860519277092708e-09, 3.074606970196721e+00 }, + { -2.864237475047239e-09, 3.078334221430809e+00 }, + { -2.867847836080156e-09, 3.081960944223928e+00 }, + { -2.871350224262603e-09, 3.085486549445314e+00 }, + { -2.874744507732462e-09, 3.088910451211251e+00 }, + { -2.878030558696270e-09, 3.092232066921130e+00 }, + { -2.881208253436038e-09, 3.095450817298478e+00 }, + { -2.884277472313999e-09, 3.098566126429974e+00 }, + { -2.887238099774968e-09, 3.101577421802070e+00 }, + { -2.890090024353816e-09, 3.104484134342861e+00 }, + { -2.892833138676371e-09, 3.107285698457308e+00 }, + { -2.895467339466766e-09, 3.109981552069083e+00 }, + { -2.897992527547963e-09, 3.112571136655481e+00 }, + { -2.900408607848946e-09, 3.115053897289195e+00 }, + { -2.902715489404992e-09, 3.117429282673042e+00 }, + { -2.904913085363323e-09, 3.119696745180238e+00 }, + { -2.907001312986328e-09, 3.121855740892224e+00 }, + { -2.908980093652563e-09, 3.123905729634218e+00 }, + { -2.910849352862924e-09, 3.125846175016163e+00 }, + { -2.912609020239985e-09, 3.127676544466606e+00 }, + { -2.914259029534118e-09, 3.129396309273659e+00 }, + { -2.915799318622574e-09, 3.131004944618667e+00 }, + { -2.917229829515169e-09, 3.132501929616775e+00 }, + { -2.918550508353347e-09, 3.133886747350606e+00 }, + { -2.919761305414294e-09, 3.135158884909254e+00 }, + { -2.920862175112829e-09, 3.136317833424958e+00 }, + { -2.921853076000972e-09, 3.137363088107359e+00 }, + { -2.922733970772719e-09, 3.138294148283254e+00 }, + { -2.923504826262027e-09, 3.139110517429204e+00 }, + { -2.924165613447473e-09, 3.139811703211207e+00 }, + { -2.924716307449950e-09, 3.140397217517018e+00 }, + { -2.925156887536978e-09, 3.140866576495489e+00 }, + { -2.925487337120335e-09, 3.141219300588825e+00 }, + { -2.925707643758784e-09, 3.141454914570261e+00 }, + { -2.925817799158535e-09, 3.141572947579352e+00 }, + { -2.925817799171455e-09, 3.141572933154836e+00 }, + { -2.925707643798390e-09, 3.141454409272987e+00 }, + { -2.925487337185779e-09, 3.141216918378770e+00 }, + { -2.925156887628892e-09, 3.140860007424112e+00 }, + { -2.924716307568119e-09, 3.140383227898687e+00 }, + { -2.924165613591896e-09, 3.139786135867868e+00 }, + { -2.923504826432903e-09, 3.139068292003385e+00 }, + { -2.922733970969412e-09, 3.138229261619561e+00 }, + { -2.921853076224321e-09, 3.137268614707029e+00 }, + { -2.920862175361976e-09, 3.136185925964038e+00 }, + { -2.919761305690083e-09, 3.134980774833275e+00 }, + { -2.918550508654911e-09, 3.133652745531368e+00 }, + { -2.917229829843137e-09, 3.132201427085629e+00 }, + { -2.915799318976726e-09, 3.130626413363146e+00 }, + { -2.914259029914435e-09, 3.128927303107136e+00 }, + { -2.912609020646661e-09, 3.127103699965947e+00 }, + { -2.910849353295315e-09, 3.125155212527586e+00 }, + { -2.908980094111509e-09, 3.123081454351802e+00 }, + { -2.907001313470937e-09, 3.120882043999591e+00 }, + { -2.904913085874448e-09, 3.118556605068443e+00 }, + { -2.902715489941767e-09, 3.116104766219928e+00 }, + { -2.900408608411958e-09, 3.113526161214776e+00 }, + { -2.897992528137022e-09, 3.110820428940251e+00 }, + { -2.895467340081818e-09, 3.107987213444579e+00 }, + { -2.892833139317615e-09, 3.105026163964191e+00 }, + { -2.890090025020589e-09, 3.101936934956479e+00 }, + { -2.887238100468092e-09, 3.098719186130021e+00 }, + { -2.884277473032614e-09, 3.095372582472161e+00 }, + { -2.881208254180937e-09, 3.091896794282404e+00 }, + { -2.878030559466594e-09, 3.088291497198199e+00 }, + { -2.874744508528832e-09, 3.084556372228054e+00 }, + { -2.871350225084755e-09, 3.080691105776848e+00 }, + { -2.867847836928063e-09, 3.076695389678615e+00 }, + { -2.864237475921086e-09, 3.072568921221621e+00 }, + { -2.860519277991847e-09, 3.068311403179147e+00 }, + { -2.856693383129018e-09, 3.063922543837792e+00 }, + { -2.852759935374575e-09, 3.059402057023109e+00 }, + { -2.848719082821403e-09, 3.054749662130841e+00 }, + { -2.844570977604520e-09, 3.049965084150782e+00 }, + { -2.840315775898525e-09, 3.045048053697736e+00 }, + { -2.835953637908582e-09, 3.039998307034967e+00 }, + { -2.831484727867511e-09, 3.034815586104635e+00 }, + { -2.826909214026628e-09, 3.029499638550941e+00 }, + { -2.822227268651470e-09, 3.024050217748861e+00 }, + { -2.817439068015245e-09, 3.018467082830179e+00 }, + { -2.812544792390175e-09, 3.012749998707001e+00 }, + { -2.807544626043751e-09, 3.006898736100911e+00 }, + { -2.802438757228650e-09, 3.000913071564665e+00 }, + { -2.797227378178760e-09, 2.994792787510961e+00 }, + { -2.791910685098702e-09, 2.988537672233504e+00 }, + { -2.786488878159805e-09, 2.982147519935565e+00 }, + { -2.780962161489413e-09, 2.975622130750641e+00 }, + { -2.775330743165298e-09, 2.968961310769028e+00 }, + { -2.769594835207775e-09, 2.962164872061613e+00 }, + { -2.763754653569747e-09, 2.955232632701135e+00 }, + { -2.757810418131543e-09, 2.948164416789036e+00 }, + { -2.751762352689432e-09, 2.940960054474719e+00 }, + { -2.745610684950541e-09, 2.933619381982341e+00 }, + { -2.739355646520809e-09, 2.926142241629213e+00 }, + { -2.732997472899722e-09, 2.918528481852205e+00 }, + { -2.726536403468318e-09, 2.910777957226018e+00 }, + { -2.719972681482232e-09, 2.902890528487386e+00 }, + { -2.713306554062453e-09, 2.894866062556452e+00 }, + { -2.706538272184154e-09, 2.886704432555728e+00 }, + { -2.699668090670078e-09, 2.878405517834426e+00 }, + { -2.692696268177908e-09, 2.869969203985464e+00 }, + { -2.685623067193599e-09, 2.861395382869544e+00 }, + { -2.678448754018380e-09, 2.852683952631486e+00 }, + { -2.671173598761847e-09, 2.843834817723832e+00 }, + { -2.663797875328991e-09, 2.834847888922988e+00 }, + { -2.656321861411517e-09, 2.825723083350459e+00 }, + { -2.648745838477759e-09, 2.816460324492298e+00 }, + { -2.641070091759922e-09, 2.807059542215146e+00 }, + { -2.633294910246296e-09, 2.797520672788269e+00 }, + { -2.625420586667340e-09, 2.787843658897949e+00 }, + { -2.617447417487602e-09, 2.778028449668942e+00 }, + { -2.609375702891616e-09, 2.768075000678399e+00 }, + { -2.601205746775692e-09, 2.757983273976943e+00 }, + { -2.592937856733464e-09, 2.747753238101915e+00 }, + { -2.584572344046340e-09, 2.737384868096553e+00 }, + { -2.576109523671634e-09, 2.726878145526201e+00 }, + { -2.567549714229129e-09, 2.716233058492422e+00 }, + { -2.558893237991435e-09, 2.705449601651722e+00 }, + { -2.550140420869302e-09, 2.694527776227857e+00 }, + { -2.541291592402089e-09, 2.683467590030445e+00 }, + { -2.532347085742440e-09, 2.672269057466213e+00 }, + { -2.523307237646751e-09, 2.660932199557362e+00 }, + { -2.514172388459584e-09, 2.649457043952206e+00 }, + { -2.504942882102813e-09, 2.637843624941622e+00 }, + { -2.495619066062810e-09, 2.626091983472908e+00 }, + { -2.486201291375123e-09, 2.614202167160335e+00 }, + { -2.476689912614465e-09, 2.602174230302269e+00 }, + { -2.467085287878098e-09, 2.590008233889805e+00 }, + { -2.457387778775451e-09, 2.577704245623143e+00 }, + { -2.447597750411553e-09, 2.565262339920002e+00 }, + { -2.437715571376127e-09, 2.552682597931055e+00 }, + { -2.427741613727123e-09, 2.539965107548168e+00 }, + { -2.417676252978335e-09, 2.527109963417675e+00 }, + { -2.407519868085581e-09, 2.514117266951687e+00 }, + { -2.397272841430131e-09, 2.500987126335739e+00 }, + { -2.386935558807595e-09, 2.487719656543254e+00 }, + { -2.376508409410024e-09, 2.474314979341178e+00 }, + { -2.365991785814531e-09, 2.460773223303822e+00 }, + { -2.355386083965131e-09, 2.447094523817833e+00 }, + { -2.344691703161363e-09, 2.433279023095734e+00 }, + { -2.333909046040126e-09, 2.419326870180582e+00 }, + { -2.323038518562289e-09, 2.405238220956597e+00 }, + { -2.312080529997549e-09, 2.391013238157397e+00 }, + { -2.301035492907384e-09, 2.376652091371587e+00 }, + { -2.289903823131822e-09, 2.362154957053137e+00 }, + { -2.278685939771276e-09, 2.347522018525197e+00 }, + { -2.267382265173420e-09, 2.332753465990296e+00 }, + { -2.255993224914501e-09, 2.317849496533128e+00 }, + { -2.244519247786155e-09, 2.302810314130351e+00 }, + { -2.232960765776561e-09, 2.287636129652823e+00 }, + { -2.221318214056095e-09, 2.272327160873552e+00 }, + { -2.209592030960763e-09, 2.256883632472565e+00 }, + { -2.197782657974034e-09, 2.241305776039511e+00 }, + { -2.185890539712767e-09, 2.225593830081461e+00 }, + { -2.173916123907886e-09, 2.209748040023618e+00 }, + { -2.161859861389976e-09, 2.193768658216360e+00 }, + { -2.149722206070124e-09, 2.177655943935795e+00 }, + { -2.137503614923981e-09, 2.161410163388424e+00 }, + { -2.125204547975352e-09, 2.145031589714984e+00 }, + { -2.112825468276292e-09, 2.128520502989477e+00 }, + { -2.100366841892917e-09, 2.111877190225612e+00 }, + { -2.087829137884807e-09, 2.095101945374541e+00 }, + { -2.075212828290086e-09, 2.078195069329960e+00 }, + { -2.062518388104923e-09, 2.061156869925600e+00 }, + { -2.049746295268559e-09, 2.043987661939897e+00 }, + { -2.036897030642658e-09, 2.026687767092888e+00 }, + { -2.023971077994576e-09, 2.009257514048162e+00 }, + { -2.010968923979840e-09, 1.991697238413571e+00 }, + { -1.997891058121344e-09, 1.974007282737320e+00 }, + { -1.984737972794098e-09, 1.956187996511354e+00 }, + { -1.971510163203686e-09, 1.938239736166060e+00 }, + { -1.958208127370276e-09, 1.920162865072273e+00 }, + { -1.944832366107339e-09, 1.901957753535934e+00 }, + { -1.931383383005451e-09, 1.883624778799427e+00 }, + { -1.917861684410531e-09, 1.865164325035177e+00 }, + { -1.904267779407432e-09, 1.846576783346324e+00 }, + { -1.890602179798714e-09, 1.827862551760622e+00 }, + { -1.876865400086483e-09, 1.809022035228338e+00 }, + { -1.863057957452539e-09, 1.790055645617624e+00 }, + { -1.849180371740008e-09, 1.770963801711725e+00 }, + { -1.835233165431475e-09, 1.751746929201178e+00 }, + { -1.821216863631569e-09, 1.732405460681919e+00 }, + { -1.807131994045840e-09, 1.712939835648088e+00 }, + { -1.792979086962494e-09, 1.693350500488565e+00 }, + { -1.778758675229683e-09, 1.673637908477153e+00 }, + { -1.764471294238191e-09, 1.653802519770021e+00 }, + { -1.750117481899733e-09, 1.633844801396848e+00 }, + { -1.735697778626995e-09, 1.613765227254186e+00 }, + { -1.721212727314574e-09, 1.593564278099856e+00 }, + { -1.706662873315474e-09, 1.573242441540939e+00 }, + { -1.692048764423848e-09, 1.552800212030258e+00 }, + { -1.677370950852395e-09, 1.532238090855187e+00 }, + { -1.662629985213192e-09, 1.511556586131055e+00 }, + { -1.647826422494560e-09, 1.490756212788764e+00 }, + { -1.632960820042537e-09, 1.469837492568651e+00 }, + { -1.618033737538645e-09, 1.448800954008929e+00 }, + { -1.603045736978760e-09, 1.427647132435469e+00 }, + { -1.587997382653428e-09, 1.406376569953373e+00 }, + { -1.572889241124034e-09, 1.384989815432507e+00 }, + { -1.557721881203696e-09, 1.363487424499449e+00 }, + { -1.542495873934815e-09, 1.341869959524515e+00 }, + { -1.527211792568486e-09, 1.320137989611176e+00 }, + { -1.511870212541253e-09, 1.298292090581491e+00 }, + { -1.496471711454994e-09, 1.276332844965754e+00 }, + { -1.481016869054634e-09, 1.254260841988828e+00 }, + { -1.465506267206068e-09, 1.232076677556547e+00 }, + { -1.449940489875303e-09, 1.209780954243628e+00 }, + { -1.434320123104372e-09, 1.187374281276747e+00 }, + { -1.418645754991533e-09, 1.164857274523495e+00 }, + { -1.402917975667710e-09, 1.142230556475749e+00 }, + { -1.387137377275425e-09, 1.119494756236361e+00 }, + { -1.371304553944712e-09, 1.096650509501278e+00 }, + { -1.355420101772623e-09, 1.073698458546610e+00 }, + { -1.339484618799891e-09, 1.050639252211352e+00 }, + { -1.323498704988051e-09, 1.027473545880543e+00 }, + { -1.307462962198534e-09, 1.004202001471034e+00 }, + { -1.291377994167204e-09, 9.808252874104182e-01 }, + { -1.275244406484394e-09, 9.573440786237052e-01 }, + { -1.259062806570190e-09, 9.337590565128454e-01 }, + { -1.242833803653464e-09, 9.100709089414796e-01 }, + { -1.226558008746195e-09, 8.862803302125812e-01 }, + { -1.210236034623253e-09, 8.623880210538113e-01 }, + { -1.193868495797618e-09, 8.383946885959868e-01 }, + { -1.177456008497777e-09, 8.143010463544786e-01 }, + { -1.160999190645010e-09, 7.901078142102129e-01 }, + { -1.144498661828833e-09, 7.658157183877095e-01 }, + { -1.127955043284965e-09, 7.414254914366063e-01 }, + { -1.111368957870986e-09, 7.169378722095157e-01 }, + { -1.094741030044308e-09, 6.923536058430697e-01 }, + { -1.078071885836393e-09, 6.676734437331688e-01 }, + { -1.061362152831423e-09, 6.428981435165511e-01 }, + { -1.044612460141255e-09, 6.180284690466404e-01 }, + { -1.027823438382183e-09, 5.930651903718045e-01 }, + { -1.010995719652015e-09, 5.680090837138436e-01 }, + { -9.941299375042378e-10, 5.428609314418970e-01 }, + { -9.772267269262058e-10, 5.176215220520872e-01 }, + { -9.602867243141016e-10, 4.922916501421032e-01 }, + { -9.433105674499058e-10, 4.668721163885412e-01 }, + { -9.262988954758817e-10, 4.413637275202624e-01 }, + { -9.092523488719689e-10, 4.157672962958654e-01 }, + { -8.921715694311144e-10, 3.900836414778084e-01 }, + { -8.750572002347607e-10, 3.643135878065193e-01 }, + { -8.579098856296589e-10, 3.384579659762392e-01 }, + { -8.407302712022458e-10, 3.125176126069478e-01 }, + { -8.235190037551917e-10, 2.864933702193017e-01 }, + { -8.062767312831008e-10, 2.603860872080448e-01 }, + { -7.890041029479477e-10, 2.341966178147619e-01 }, + { -7.717017690542486e-10, 2.079258220999725e-01 }, + { -7.543703810250266e-10, 1.815745659161734e-01 }, + { -7.370105913774597e-10, 1.551437208801425e-01 }, + { -7.196230536974697e-10, 1.286341643433767e-01 }, + { -7.022084226165876e-10, 1.020467793657360e-01 }, + { -6.847673537853251e-10, 7.538245468350446e-02 }, + { -6.673005038502516e-10, 4.864208468284503e-02 }, + { -6.498085304282128e-10, 2.182656936863137e-02 }, + { -6.322920920826137e-10, -5.063185663820913e-03 }, + { -6.147518482969490e-10, -3.202626926150343e-02 }, + { -5.971884594516681e-10, -5.906176474160862e-02 }, + { -5.796025867984469e-10, -8.616874992366363e-02 }, + { -5.619948924353588e-10, -1.133462971605448e-01 }, + { -5.443660392823640e-10, -1.405934733692621e-01 }, + { -5.267166910556339e-10, -1.679093400638023e-01 }, + { -5.090475122431451e-10, -1.952929533862739e-01 }, + { -4.913591680795342e-10, -2.227433641394564e-01 }, + { -4.736523245210571e-10, -2.502596178194491e-01 }, + { -4.559276482202303e-10, -2.778407546490776e-01 }, + { -4.381858065011618e-10, -3.054858096104932e-01 }, + { -4.204274673340870e-10, -3.331938124792702e-01 }, + { -4.026532993105397e-10, -3.609637878577768e-01 }, + { -3.848639716178888e-10, -3.887947552098022e-01 }, + { -3.670601540142443e-10, -4.166857288948674e-01 }, + { -3.492425168032583e-10, -4.446357182029681e-01 }, + { -3.314117308088734e-10, -4.726437273896633e-01 }, + { -3.135684673501752e-10, -5.007087557112619e-01 }, + { -2.957133982159296e-10, -5.288297974607742e-01 }, + { -2.778471956393828e-10, -5.570058420037128e-01 }, + { -2.599705322729564e-10, -5.852358738143247e-01 }, + { -2.420840811628366e-10, -6.135188725122560e-01 }, + { -2.241885157240923e-10, -6.418538128986450e-01 }, + { -2.062845097142585e-10, -6.702396649949099e-01 }, + { -1.883727372093546e-10, -6.986753940779493e-01 }, + { -1.704538725773087e-10, -7.271599607197149e-01 }, + { -1.525285904532877e-10, -7.556923208240308e-01 }, + { -1.345975657140748e-10, -7.842714256651911e-01 }, + { -1.166614734526054e-10, -8.128962219265712e-01 }, + { -9.872098895260891e-11, -8.415656517393372e-01 }, + { -8.077678766314517e-11, -8.702786527215916e-01 }, + { -6.282954517324612e-11, -8.990341580176152e-01 }, + { -4.487993718655790e-11, -9.278310963373758e-01 }, + { -2.692863949561210e-11, -9.566683919968972e-01 }, + { -8.976327956520795e-12, -9.855449649582175e-01 }, + { 8.976321536169872e-12, -1.014459730869357e+00 }, + { 2.692863307547294e-11, -1.043411601105914e+00 }, + { 4.487993076694813e-11, -1.072399482811314e+00 }, + { 6.282953875437751e-11, -1.101422278938424e+00 }, + { 8.077678124517653e-11, -1.130478888291020e+00 }, + { 9.872098253591082e-11, -1.159568205565684e+00 }, + { 1.166614670373367e-10, -1.188689121393192e+00 }, + { 1.345975593005002e-10, -1.217840522381901e+00 }, + { 1.525285840416718e-10, -1.247021291159495e+00 }, + { 1.704538661678104e-10, -1.276230306415868e+00 }, + { 1.883727308022916e-10, -1.305466442946703e+00 }, + { 2.062845033098954e-10, -1.334728571696106e+00 }, + { 2.241885093225349e-10, -1.364015559800721e+00 }, + { 2.420840747645085e-10, -1.393326270633325e+00 }, + { 2.599705258779635e-10, -1.422659563847049e+00 }, + { 2.778471892479898e-10, -1.452014295419243e+00 }, + { 2.957133918284542e-10, -1.481389317696831e+00 }, + { 3.135684609667761e-10, -1.510783479440191e+00 }, + { 3.314117244297624e-10, -1.540195625869043e+00 }, + { 3.492425104288060e-10, -1.569624598707558e+00 }, + { 3.670601476445565e-10, -1.599069236228850e+00 }, + { 3.848639652533361e-10, -1.628528373302631e+00 }, + { 4.026532929512281e-10, -1.658000841439269e+00 }, + { 4.204274609803869e-10, -1.687485468837799e+00 }, + { 4.381858001531792e-10, -1.716981080430596e+00 }, + { 4.559276418782829e-10, -1.746486497931567e+00 }, + { 4.736523181853565e-10, -1.776000539882225e+00 }, + { 4.913591617503452e-10, -1.805522021699094e+00 }, + { 5.090475059206794e-10, -1.835049755721194e+00 }, + { 5.267166847401562e-10, -1.864582551257262e+00 }, + { 5.443660329740862e-10, -1.894119214633676e+00 }, + { 5.619948861345454e-10, -1.923658549242818e+00 }, + { 5.796025805053097e-10, -1.953199355591180e+00 }, + { 5.971884531664190e-10, -1.982740431347091e+00 }, + { 6.147518420199055e-10, -2.012280571390674e+00 }, + { 6.322920858139346e-10, -2.041818567861395e+00 }, + { 6.498085241682158e-10, -2.071353210208005e+00 }, + { 6.673004975990425e-10, -2.100883285238127e+00 }, + { 6.847673475432746e-10, -2.130407577166309e+00 }, + { 7.022084163838545e-10, -2.159924867664933e+00 }, + { 7.196230474743716e-10, -2.189433935913779e+00 }, + { 7.370105851640495e-10, -2.218933558650552e+00 }, + { 7.543703748217808e-10, -2.248422510220072e+00 }, + { 7.717017628611672e-10, -2.277899562625407e+00 }, + { 7.890040967654542e-10, -2.307363485579104e+00 }, + { 8.062767251113011e-10, -2.336813046552684e+00 }, + { 8.235189975944034e-10, -2.366247010829556e+00 }, + { 8.407302650525749e-10, -2.395664141553858e+00 }, + { 8.579098794915287e-10, -2.425063199784153e+00 }, + { 8.750571941082773e-10, -2.454442944543319e+00 }, + { 8.921715633164894e-10, -2.483802132872044e+00 }, + { 9.092523427695200e-10, -2.513139519878584e+00 }, + { 9.262988893857148e-10, -2.542453858792682e+00 }, + { 9.433105613723914e-10, -2.571743901017465e+00 }, + { 9.602867182493987e-10, -2.601008396180870e+00 }, + { 9.772267208744730e-10, -2.630246092190425e+00 }, + { 9.941299314658458e-10, -2.659455735283526e+00 }, + { 1.010995713627070e-09, -2.688636070081818e+00 }, + { 1.027823432371055e-09, -2.717785839644439e+00 }, + { 1.044612454143997e-09, -2.746903785521352e+00 }, + { 1.061362146848353e-09, -2.775988647805256e+00 }, + { 1.078071879867828e-09, -2.805039165187255e+00 }, + { 1.094741024090249e-09, -2.834054075009077e+00 }, + { 1.111368951931856e-09, -2.863032113318052e+00 }, + { 1.127955037360817e-09, -2.891972014920939e+00 }, + { 1.144498655920037e-09, -2.920872513436805e+00 }, + { 1.160999184751779e-09, -2.949732341353290e+00 }, + { 1.177456002620215e-09, -2.978550230079517e+00 }, + { 1.193868489936097e-09, -3.007324910002949e+00 }, + { 1.210236028777826e-09, -3.036055110540183e+00 }, + { 1.226558002917232e-09, -3.064739560196251e+00 }, + { 1.242833797841123e-09, -3.093376986616735e+00 }, + { 1.259062800774685e-09, -3.121966116643377e+00 }, + { 1.275244400705935e-09, -3.150505676371791e+00 }, + { 1.291377988406056e-09, -3.178994391202159e+00 }, + { 1.307462956454857e-09, -3.207430985899192e+00 }, + { 1.323498699262108e-09, -3.235814184645077e+00 }, + { 1.339484613091842e-09, -3.264142711097884e+00 }, + { 1.355420096082785e-09, -3.292415288443373e+00 }, + { 1.371304548273191e-09, -3.320630639454825e+00 }, + { 1.387137371622433e-09, -3.348787486547389e+00 }, + { 1.402917970033511e-09, -3.376884551834256e+00 }, + { 1.418645749376393e-09, -3.404920557184582e+00 }, + { 1.434320117508396e-09, -3.432894224276359e+00 }, + { 1.449940484298756e-09, -3.460804274656981e+00 }, + { 1.465506261649108e-09, -3.488649429796768e+00 }, + { 1.481016863517580e-09, -3.516428411149154e+00 }, + { 1.496471705937951e-09, -3.544139940202303e+00 }, + { 1.511870207044433e-09, -3.571782738540999e+00 }, + { 1.527211787092206e-09, -3.599355527901174e+00 }, + { 1.542495868479076e-09, -3.626857030226671e+00 }, + { 1.557721875768920e-09, -3.654285967729458e+00 }, + { 1.572889235710329e-09, -3.681641062941412e+00 }, + { 1.587997377261005e-09, -3.708921038776707e+00 }, + { 1.603045731607830e-09, -3.736124618586623e+00 }, + { 1.618033732189314e-09, -3.763250526218862e+00 }, + { 1.632960814715177e-09, -3.790297486071938e+00 }, + { 1.647826417189275e-09, -3.817264223155802e+00 }, + { 1.662629979930247e-09, -3.844149463148589e+00 }, + { 1.677370945591844e-09, -3.870951932452996e+00 }, + { 1.692048759186008e-09, -3.897670358257890e+00 }, + { 1.706662868100504e-09, -3.924303468590212e+00 }, + { 1.721212722122685e-09, -3.950849992378278e+00 }, + { 1.735697773458400e-09, -3.977308659506432e+00 }, + { 1.750117476754591e-09, -4.003678200876669e+00 }, + { 1.764471289116712e-09, -4.029957348461003e+00 }, + { 1.778758670132079e-09, -4.056144835364877e+00 }, + { 1.792979081888926e-09, -4.082239395882965e+00 }, + { 1.807131988996465e-09, -4.108239765556996e+00 }, + { 1.821216858606652e-09, -4.134144681236933e+00 }, + { 1.835233160431175e-09, -4.159952881133585e+00 }, + { 1.849180366764537e-09, -4.185663104882633e+00 }, + { 1.863057952502055e-09, -4.211274093599509e+00 }, + { 1.876865395161145e-09, -4.236784589940537e+00 }, + { 1.890602174898734e-09, -4.262193338157148e+00 }, + { 1.904267774533022e-09, -4.287499084158302e+00 }, + { 1.917861679562008e-09, -4.312700575567174e+00 }, + { 1.931383378182392e-09, -4.337796561778708e+00 }, + { 1.944832361310856e-09, -4.362785794021793e+00 }, + { 1.958208122599839e-09, -4.387667025411434e+00 }, + { 1.971510158459931e-09, -4.412439011013396e+00 }, + { 1.984737968076495e-09, -4.437100507898339e+00 }, + { 1.997891053431005e-09, -4.461650275204912e+00 }, + { 2.010968919316289e-09, -4.486087074191693e+00 }, + { 2.023971073358447e-09, -4.510409668301784e+00 }, + { 2.036897026033634e-09, -4.534616823217992e+00 }, + { 2.049746290686799e-09, -4.558707306921882e+00 }, + { 2.062518383551274e-09, -4.582679889754607e+00 }, + { 2.075212823764071e-09, -4.606533344469879e+00 }, + { 2.087829133387063e-09, -4.630266446298172e+00 }, + { 2.100366837422912e-09, -4.653877973001258e+00 }, + { 2.112825463835087e-09, -4.677366704934605e+00 }, + { 2.125204543562522e-09, -4.700731425099899e+00 }, + { 2.137503610540056e-09, -4.723970919208608e+00 }, + { 2.149722201714786e-09, -4.747083975738060e+00 }, + { 2.161859857063438e-09, -4.770069385989595e+00 }, + { 2.173916119610994e-09, -4.792925944149308e+00 }, + { 2.185890535445098e-09, -4.815652447340950e+00 }, + { 2.197782653735957e-09, -4.838247695689436e+00 }, + { 2.209592026751962e-09, -4.860710492376411e+00 }, + { 2.221318209877576e-09, -4.883039643700314e+00 }, + { 2.232960761627846e-09, -4.905233959130168e+00 }, + { 2.244519243667616e-09, -4.927292251368517e+00 }, + { 2.255993220826402e-09, -4.949213336406265e+00 }, + { 2.267382261115285e-09, -4.970996033581527e+00 }, + { 2.278685935744269e-09, -4.992639165639563e+00 }, + { 2.289903819135414e-09, -5.014141558784778e+00 }, + { 2.301035488942000e-09, -5.035502042744443e+00 }, + { 2.312080526062763e-09, -5.056719450823151e+00 }, + { 2.323038514659161e-09, -5.077792619963239e+00 }, + { 2.333909042168180e-09, -5.098720390796817e+00 }, + { 2.344691699320969e-09, -5.119501607709159e+00 }, + { 2.355386080156553e-09, -5.140135118892792e+00 }, + { 2.365991782037187e-09, -5.160619776404897e+00 }, + { 2.376508405665132e-09, -5.180954436227641e+00 }, + { 2.386935555094626e-09, -5.201137958319343e+00 }, + { 2.397272837749508e-09, -5.221169206676762e+00 }, + { 2.407519864436774e-09, -5.241047049389645e+00 }, + { 2.417676249362563e-09, -5.260770358700167e+00 }, + { 2.427741610143750e-09, -5.280338011053974e+00 }, + { 2.437715567825576e-09, -5.299748887163106e+00 }, + { 2.447597746894037e-09, -5.319001872058887e+00 }, + { 2.457387775290440e-09, -5.338095855149190e+00 }, + { 2.467085284426756e-09, -5.357029730277389e+00 }, + { 2.476689909196263e-09, -5.375802395772283e+00 }, + { 2.486201287990485e-09, -5.394412754510426e+00 }, + { 2.495619062711154e-09, -5.412859713968929e+00 }, + { 2.504942878785408e-09, -5.431142186284682e+00 }, + { 2.514172385175743e-09, -5.449259088303476e+00 }, + { 2.523307234396791e-09, -5.467209341642627e+00 }, + { 2.532347082526785e-09, -5.484991872743321e+00 }, + { 2.541291589219998e-09, -5.502605612925014e+00 }, + { 2.550140417722072e-09, -5.520049498445633e+00 }, + { 2.558893234878378e-09, -5.537322470548212e+00 }, + { 2.567549711150773e-09, -5.554423475524196e+00 }, + { 2.576109520627371e-09, -5.571351464763084e+00 }, + { 2.584572341037361e-09, -5.588105394812198e+00 }, + { 2.592937853759161e-09, -5.604684227423386e+00 }, + { 2.601205743836355e-09, -5.621086929615246e+00 }, + { 2.609375699987564e-09, -5.637312473723475e+00 }, + { 2.617447414618146e-09, -5.653359837454964e+00 }, + { 2.625420583833750e-09, -5.669228003945694e+00 }, + { 2.633294907447937e-09, -5.684915961806963e+00 }, + { 2.641070088997271e-09, -5.700422705186584e+00 }, + { 2.648745835750128e-09, -5.715747233817712e+00 }, + { 2.656321858720176e-09, -5.730888553077074e+00 }, + { 2.663797872673252e-09, -5.745845674030161e+00 }, + { 2.671173596142054e-09, -5.760617613492118e+00 }, + { 2.678448751434797e-09, -5.775203394076705e+00 }, + { 2.685623064645538e-09, -5.789602044248679e+00 }, + { 2.692696265666640e-09, -5.803812598380606e+00 }, + { 2.699668088194915e-09, -5.817834096797069e+00 }, + { 2.706538269745573e-09, -5.831665585834668e+00 }, + { 2.713306551659817e-09, -5.845306117889361e+00 }, + { 2.719972679116734e-09, -5.858754751472542e+00 }, + { 2.726536401139295e-09, -5.872010551255358e+00 }, + { 2.732997470607439e-09, -5.885072588127400e+00 }, + { 2.739355644265558e-09, -5.897939939244211e+00 }, + { 2.745610682731633e-09, -5.910611688078208e+00 }, + { 2.751762350508137e-09, -5.923086924473290e+00 }, + { 2.757810415987146e-09, -5.935364744687794e+00 }, + { 2.763754651462700e-09, -5.947444251452243e+00 }, + { 2.769594833137415e-09, -5.959324554015538e+00 }, + { 2.775330741132843e-09, -5.971004768198829e+00 }, + { 2.780962159494174e-09, -5.982484016437981e+00 }, + { 2.786488876202047e-09, -5.993761427840588e+00 }, + { 2.791910683178690e-09, -6.004836138231525e+00 }, + { 2.797227376295779e-09, -6.015707290202086e+00 }, + { 2.802438755383971e-09, -6.026374033162623e+00 }, + { 2.807544624236659e-09, -6.036835523383457e+00 }, + { 2.812544790621093e-09, -6.047090924050914e+00 }, + { 2.817439066283459e-09, -6.057139405311101e+00 }, + { 2.822227266958278e-09, -6.066980144322601e+00 }, + { 2.826909212371261e-09, -6.076612325295799e+00 }, + { 2.831484726250221e-09, -6.086035139548830e+00 }, + { 2.835953636329660e-09, -6.095247785550617e+00 }, + { 2.840315774357203e-09, -6.104249468967751e+00 }, + { 2.844570976102082e-09, -6.113039402715685e+00 }, + { 2.848719081357095e-09, -6.121616806996519e+00 }, + { 2.852759933948860e-09, -6.129980909353977e+00 }, + { 2.856693381741114e-09, -6.138130944714082e+00 }, + { 2.860519276643053e-09, -6.146066155436312e+00 }, + { 2.864237474610633e-09, -6.153785791350256e+00 }, + { 2.867847835656203e-09, -6.161289109809551e+00 }, + { 2.871350223851726e-09, -6.168575375732642e+00 }, + { 2.874744507333867e-09, -6.175643861647406e+00 }, + { 2.878030558310989e-09, -6.182493847739853e+00 }, + { 2.881208253063899e-09, -6.189124621889823e+00 }, + { 2.884277471954592e-09, -6.195535479723423e+00 }, + { 2.887238099428306e-09, -6.201725724651554e+00 }, + { 2.890090024020323e-09, -6.207694667918394e+00 }, + { 2.892833138356060e-09, -6.213441628635915e+00 }, + { 2.895467339159240e-09, -6.218965933835304e+00 }, + { 2.897992527253659e-09, -6.224266918505075e+00 }, + { 2.900408607567016e-09, -6.229343925633495e+00 }, + { 2.902715489136496e-09, -6.234196306254763e+00 }, + { 2.904913085108075e-09, -6.238823419482017e+00 }, + { 2.907001312743911e-09, -6.243224632557377e+00 }, + { 2.908980093422997e-09, -6.247399320887848e+00 }, + { 2.910849352646620e-09, -6.251346868091392e+00 }, + { 2.912609020036956e-09, -6.255066666028537e+00 }, + { 2.914259029343965e-09, -6.258558114851525e+00 }, + { 2.915799318445710e-09, -6.261820623039620e+00 }, + { 2.917229829350759e-09, -6.264853607438842e+00 }, + { 2.918550508202463e-09, -6.267656493305673e+00 }, + { 2.919761305276718e-09, -6.270228714337005e+00 }, + { 2.920862174988150e-09, -6.272569712717951e+00 }, + { 2.921853075889193e-09, -6.274678939154603e+00 }, + { 2.922733970674264e-09, -6.276555852917634e+00 }, + { 2.923504826176907e-09, -6.278199921870962e+00 }, + { 2.924165613375264e-09, -6.279610622518139e+00 }, + { 2.924716307391075e-09, -6.280787440034993e+00 }, + { 2.925156887490598e-09, -6.281729868306345e+00 }, + { 2.925487337087508e-09, -6.282437409966992e+00 }, + { 2.925707643739298e-09, -6.282909576428774e+00 }, + { 2.925817799151970e-09, -6.283145887925411e+00 }, diff --git a/lib/runtime/math/vco.h b/lib/runtime/math/vco.h new file mode 100644 index 0000000..fa11732 --- /dev/null +++ b/lib/runtime/math/vco.h @@ -0,0 +1,99 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef _GR_VCO_H_ +#define _GR_VCO_H_ + +#include +#include +#include +#include + +namespace gr { + + /*! + * \brief base class template for Voltage Controlled Oscillator (VCO) + * \ingroup misc + */ + template + class vco + { + public: + vco() : d_phase(0) {} + + virtual ~vco() {} + + // radians + void set_phase(double angle) { + d_phase = angle; + } + + void adjust_phase(double delta_phase) { + d_phase += delta_phase; + if(fabs (d_phase) > M_PI){ + + while(d_phase > M_PI) + d_phase -= 2*M_PI; + + while(d_phase < -M_PI) + d_phase += 2*M_PI; + } + } + + double get_phase() const { return d_phase; } + + // compute sin and cos for current phase angle + void sincos(float *sinx, float *cosx) const; + + // compute cos or sin for current phase angle + float cos() const { return std::cos(d_phase); } + float sin() const { return std::sin(d_phase); } + + // compute a block at a time + void cos(float *output, const float *input, + int noutput_items, double k, double ampl = 1.0); + + protected: + double d_phase; + }; + + template + void + vco::sincos(float *sinx, float *cosx) const + { + gr::sincosf(d_phase, sinx, cosx); + } + + template + void + vco::cos(float *output, const float *input, + int noutput_items, double k, double ampl) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = cos() * ampl; + adjust_phase(input[i] * k); + } + } + +} /* namespace gr */ + +#endif /* _GR_VCO_H_ */ diff --git a/lib/runtime/misc.cc b/lib/runtime/misc.cc new file mode 100644 index 0000000..f9ad6ca --- /dev/null +++ b/lib/runtime/misc.cc @@ -0,0 +1,70 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "misc.h" + +namespace gr { + + unsigned int + rounduppow2(unsigned int n) + { + int i; + for(i=0;((n-1)>>i) != 0;i++) + ; + return 1< &v) + { + for(unsigned int i=0; i < v.size(); i++) + v[i] = 0; + } + + void + zero_vector(std::vector &v) + { + for(unsigned int i=0; i < v.size(); i++) + v[i] = 0; + } + + void + zero_vector(std::vector &v) + { + for(unsigned int i=0; i < v.size(); i++) + v[i] = 0; + } + + void + zero_vector(std::vector &v) + { + for(unsigned int i=0; i < v.size(); i++) + v[i] = 0; + } + +} /* namespace gr */ diff --git a/lib/runtime/misc.h b/lib/runtime/misc.h new file mode 100644 index 0000000..833b647 --- /dev/null +++ b/lib/runtime/misc.h @@ -0,0 +1,42 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_MISC_H +#define INCLUDED_GR_MISC_H + +#include +#include + +namespace gr { + + GR_RUNTIME_API unsigned int + rounduppow2(unsigned int n); + + // FIXME should be template + GR_RUNTIME_API void zero_vector(std::vector &v); + GR_RUNTIME_API void zero_vector(std::vector &v); + GR_RUNTIME_API void zero_vector(std::vector &v); + GR_RUNTIME_API void zero_vector(std::vector &v); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_MISC_H */ diff --git a/lib/runtime/pagesize.cc b/lib/runtime/pagesize.cc new file mode 100644 index 0000000..373c0b6 --- /dev/null +++ b/lib/runtime/pagesize.cc @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pagesize.h" +#include +#include + +namespace gr { + +#if defined(_WIN32) && defined(HAVE_GETPAGESIZE) + extern "C" size_t getpagesize(void); +#endif + + int + pagesize() + { + static int s_pagesize = -1; + + if(s_pagesize == -1) { +#if defined(HAVE_GETPAGESIZE) + s_pagesize = getpagesize(); +#elif defined (HAVE_SYSCONF) + s_pagesize = sysconf(_SC_PAGESIZE); + if(s_pagesize == -1) { + perror("_SC_PAGESIZE"); + s_pagesize = 4096; + } +#else + fprintf(stderr, "gr::pagesize: no info; setting pagesize = 4096\n"); + s_pagesize = 4096; +#endif + } + return s_pagesize; + } + +} /* namespace gr */ diff --git a/lib/runtime/pagesize.h b/lib/runtime/pagesize.h new file mode 100644 index 0000000..6a16882 --- /dev/null +++ b/lib/runtime/pagesize.h @@ -0,0 +1,37 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_PAGESIZE_H_ +#define GR_PAGESIZE_H_ + +#include + +namespace gr { + + /*! + * \brief return the page size in bytes + */ + GR_RUNTIME_API int pagesize(); + +} /* namespace gr */ + +#endif /* GR_PAGESIZE_H_ */ diff --git a/lib/runtime/posix_memalign.cc b/lib/runtime/posix_memalign.cc new file mode 100644 index 0000000..f75b1d5 --- /dev/null +++ b/lib/runtime/posix_memalign.cc @@ -0,0 +1,114 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "posix_memalign.h" + +#ifdef HAVE_MALLOC_H +// for Cygwin valloc () prototype +#include +#endif + +#ifndef HAVE_POSIX_MEMALIGN + +/* emulate posix_memalign functionality, to some degree */ + +#include +#include "pagesize.h" + +int posix_memalign +(void **memptr, size_t alignment, size_t size) +{ + /* emulate posix_memalign functionality, to some degree */ + + /* make sure the return handle is valid; return "bad address" if not valid */ + if (memptr == 0) + return (EFAULT); + *memptr = (void*) 0; + + /* make sure 'alignment' is a power of 2 + * and multiple of sizeof (void*) + */ + + /* make sure 'alignment' is a multiple of sizeof (void*) */ + if ((alignment % sizeof (void*)) != 0) + return (EINVAL); + + /* make sure 'alignment' is a power of 2 */ + if ((alignment & (alignment - 1)) != 0) + return (EINVAL); + + /* good alignment */ + +#if (ALIGNED_MALLOC != 0) + + /* if 'malloc' is known to be aligned, and the desired 'alignment' + * matches is <= that provided by 'malloc', then use 'malloc'. This + * works on, e.g., Darwin 8 & 9: for which malloc is 16-byte aligned. + */ + size_t am = (size_t) ALIGNED_MALLOC; + if (alignment <= am) { + /* make sure ALIGNED_MALLOC is a power of 2, to guarantee that the + * alignment is correct (since 'alignment' must be a power of 2). + */ + if ((am & (am - 1)) != 0) + return (EINVAL); + /* good malloc alignment */ + *memptr = malloc (size); + } + +#endif /* (ALIGNED_MALLOC != 0) */ +#ifdef HAVE_VALLOC + + if (*memptr == (void*) 0) { + /* try valloc if it exists */ + /* cheap and easy way to make sure alignment is met, so long as it + * is <= pagesize () */ + if (alignment <= (size_t) gr::pagesize ()) { + *memptr = valloc (size); + } + } + +#endif /* HAVE_VALLOC */ + +#if (ALIGNED_MALLOC == 0) && !defined (HAVE_VALLOC) + /* no posix_memalign, valloc, and malloc isn't known to be aligned + * (enough for the input arguments); no idea what to do. + */ + +#error gnuradio-runtime/lib/posix_memalign.cc: Cannot find a way to alloc aligned memory. + +#endif + + /* if the pointer wasn't allocated properly, return that there was + * not enough memory to allocate; otherwise, return OK (0). + */ + if (*memptr == (void*) 0) + return (ENOMEM); + else + return (0); +}; + +#endif /* ! HAVE_POSIX_MEMALIGN */ diff --git a/lib/runtime/posix_memalign.h b/lib/runtime/posix_memalign.h new file mode 100644 index 0000000..ea79ced --- /dev/null +++ b/lib/runtime/posix_memalign.h @@ -0,0 +1,42 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef _POSIX_MEMALIGN_H_ +#define _POSIX_MEMALIGN_H_ + +#include + +#ifndef HAVE_POSIX_MEMALIGN + +#ifdef __cplusplus +extern "C" { +#endif + +extern int posix_memalign (void** memptr, size_t alignment, size_t size); + +#ifdef __cplusplus +}; +#endif + +#endif /* ! HAVE_POSIX_MEMALIGN */ + +#endif /* _POSIX_MEMALIGN_H_ */ diff --git a/lib/runtime/prefs.cc b/lib/runtime/prefs.cc new file mode 100644 index 0000000..468532f --- /dev/null +++ b/lib/runtime/prefs.cc @@ -0,0 +1,401 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include +namespace fs = boost::filesystem; + +namespace gr { + + /* + * Stub implementations + */ + static prefs s_default_singleton; + static prefs *s_singleton = &s_default_singleton; + + prefs * + prefs::singleton() + { + return s_singleton; + } + + void + prefs::set_singleton(prefs *p) + { + s_singleton = p; + } + + prefs::prefs() + { + _read_files(); + } + + prefs::~prefs() + { + // nop + } + + std::vector + prefs::_sys_prefs_filenames() + { + std::vector fnames; + + fs::path dir = prefsdir(); + if(!fs::is_directory(dir)) + return fnames; + + fs::directory_iterator diritr(dir); + while(diritr != fs::directory_iterator()) { + fs::path p = *diritr++; + if(p.extension() != ".swp") + fnames.push_back(p.string()); + } + std::sort(fnames.begin(), fnames.end()); + + // Find if there is a ~/.gnuradio/config.conf file and add this to + // the end of the file list to override any preferences in the + // installed path config files. + fs::path homedir = fs::path(gr::appdata_path()); + homedir = homedir/".gnuradio/config.conf"; + if(fs::exists(homedir)) { + fnames.push_back(homedir.string()); + } + + return fnames; + } + + void + prefs::_read_files() + { + std::string config; + + std::vector filenames = _sys_prefs_filenames(); + std::vector::iterator sitr; + char tmp[1024]; + for(sitr = filenames.begin(); sitr != filenames.end(); sitr++) { + fs::ifstream fin(*sitr); + while(!fin.eof()) { + fin.getline(tmp, 1024); + std::string t(tmp); + // ignore empty lines or lines of just comments + if((t.size() > 0) && (t[0] != '#')) { + // remove any comments in the line + size_t hash = t.find("#"); + + // Use hash marks at the end of each segment as a delimiter + config += t.substr(0, hash) + '#'; + } + } + fin.close(); + } + + // Remove all whitespace. + config.erase(std::remove_if(config.begin(), config.end(), + ::isspace), config.end()); + + // Convert the string into a map + _convert_to_map(config); + } + + void + prefs::_convert_to_map(const std::string &conf) + { + // Convert the string into an map of maps + // Map is structured as {section name: map of options} + // And options map is simply: {option name: option value} + std::string sub = conf; + size_t sec_start = sub.find("["); + while(sec_start != std::string::npos) { + sub = sub.substr(sec_start); + + size_t sec_end = sub.find("]"); + if(sec_end == std::string::npos) + throw std::runtime_error("Config file error: Mismatched section label.\n"); + + std::string sec = sub.substr(1, sec_end-1); + size_t next_sec_start = sub.find("[", sec_end); + std::string subsec = sub.substr(sec_end+1, next_sec_start-sec_end-2); + + std::transform(sec.begin(), sec.end(), sec.begin(), ::tolower); + + std::map options_map = d_config_map[sec]; + size_t next_opt = 0; + size_t next_val = 0; + next_opt = subsec.find("#"); + while(next_opt < subsec.size()-1) { + next_val = subsec.find("=", next_opt); + std::string option = subsec.substr(next_opt+1, next_val-next_opt-1); + + next_opt = subsec.find("#", next_val); + std::string value = subsec.substr(next_val+1, next_opt-next_val-1); + + std::transform(option.begin(), option.end(), option.begin(), ::tolower); + options_map[option] = value; + } + + d_config_map[sec] = options_map; + + sec_start = sub.find("[", sec_end); + } + } + + std::string + prefs::to_string() + { + config_map_itr sections; + config_map_elem_itr options; + std::stringstream s; + + for(sections = d_config_map.begin(); sections != d_config_map.end(); sections++) { + s << "[" << sections->first << "]" << std::endl; + for(options = sections->second.begin(); options != sections->second.end(); options++) { + s << options->first << " = " << options->second << std::endl; + } + s << std::endl; + } + + return s.str(); + } + + void + prefs::save() + { + std::string conf = to_string(); + + fs::path homedir = fs::path(gr::appdata_path()); + homedir = homedir/".gnuradio/config.conf"; + fs::ofstream fout(homedir); + fout << conf; + fout.close(); + } + + char * + prefs::option_to_env(std::string section, std::string option) + { + std::stringstream envname; + std::string secname=section, optname=option; + + std::transform(section.begin(), section.end(), secname.begin(), ::toupper); + std::transform(option.begin(), option.end(), optname.begin(), ::toupper); + envname << "GR_CONF_" << secname << "_" << optname; + + return getenv(envname.str().c_str()); + } + + bool + prefs::has_section(const std::string §ion) + { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + return d_config_map.count(s) > 0; + } + + bool + prefs::has_option(const std::string §ion, const std::string &option) + { + if(option_to_env(section, option)) + return true; + + if(has_section(section)) { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + + std::string o = option; + std::transform(option.begin(), option.end(), o.begin(), ::tolower); + + config_map_itr sec = d_config_map.find(s); + return sec->second.count(o) > 0; + } + else { + return false; + } + } + + const std::string + prefs::get_string(const std::string §ion, const std::string &option, + const std::string &default_val) + { + char *env = option_to_env(section, option); + if(env) + return std::string(env); + + if(has_option(section, option)) { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + + std::string o = option; + std::transform(option.begin(), option.end(), o.begin(), ::tolower); + + config_map_itr sec = d_config_map.find(s); + config_map_elem_itr opt = sec->second.find(o); + return opt->second; + } + else { + return default_val; + } + } + + void + prefs::set_string(const std::string §ion, const std::string &option, + const std::string &val) + { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + + std::string o = option; + std::transform(option.begin(), option.end(), o.begin(), ::tolower); + + std::map opt_map = d_config_map[s]; + + opt_map[o] = val; + + d_config_map[s] = opt_map; + } + + bool + prefs::get_bool(const std::string §ion, const std::string &option, + bool default_val) + { + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + if((str == "true") || (str == "on") || (str == "1")) + return true; + else if((str == "false") || (str == "off") || (str == "0")) + return false; + else + return default_val; + } + else { + return default_val; + } + } + + void + prefs::set_bool(const std::string §ion, const std::string &option, + bool val) + { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + + std::string o = option; + std::transform(option.begin(), option.end(), o.begin(), ::tolower); + + std::map opt_map = d_config_map[s]; + + std::stringstream sstr; + sstr << (val == true); + opt_map[o] = sstr.str(); + + d_config_map[s] = opt_map; + } + + long + prefs::get_long(const std::string §ion, const std::string &option, + long default_val) + { + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::stringstream sstr(str); + long n; + sstr >> n; + return n; + } + else { + return default_val; + } + } + + void + prefs::set_long(const std::string §ion, const std::string &option, + long val) + { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + + std::string o = option; + std::transform(option.begin(), option.end(), o.begin(), ::tolower); + + std::map opt_map = d_config_map[s]; + + std::stringstream sstr; + sstr << val; + opt_map[o] = sstr.str(); + + d_config_map[s] = opt_map; + } + + double + prefs::get_double(const std::string §ion, const std::string &option, + double default_val) + { + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::stringstream sstr(str); + double n; + sstr >> n; + return n; + } + else { + return default_val; + } + } + + void + prefs::set_double(const std::string §ion, const std::string &option, + double val) + { + std::string s = section; + std::transform(section.begin(), section.end(), s.begin(), ::tolower); + + std::string o = option; + std::transform(option.begin(), option.end(), o.begin(), ::tolower); + + std::map opt_map = d_config_map[s]; + + std::stringstream sstr; + sstr << val; + opt_map[o] = sstr.str(); + + d_config_map[s] = opt_map; + } + +} /* namespace gr */ diff --git a/lib/runtime/realtime.cc b/lib/runtime/realtime.cc new file mode 100644 index 0000000..99deff5 --- /dev/null +++ b/lib/runtime/realtime.cc @@ -0,0 +1,37 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2007,2008 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +namespace gr { + + rt_status_t + enable_realtime_scheduling() + { + return gr::impl::enable_realtime_scheduling(); + } + +} /* namespace gr */ diff --git a/lib/runtime/realtime_impl.cc b/lib/runtime/realtime_impl.cc new file mode 100644 index 0000000..54db9d8 --- /dev/null +++ b/lib/runtime/realtime_impl.cc @@ -0,0 +1,192 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2007,2008 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#ifdef HAVE_SCHED_H +#include +#endif + +#include +#include +#include +#include +#include + +#include + +#if defined(HAVE_PTHREAD_SETSCHEDPARAM) || defined(HAVE_SCHED_SETSCHEDULER) +#include + +namespace gr { + namespace impl { + + /*! + * Rescale our virtual priority so that it maps to the middle 1/2 of + * the priorities given by min_real_pri and max_real_pri. + */ + static int + rescale_virtual_pri(int virtual_pri, int min_real_pri, int max_real_pri) + { + float rmin = min_real_pri + (0.25 * (max_real_pri - min_real_pri)); + float rmax = min_real_pri + (0.75 * (max_real_pri - min_real_pri)); + float m = (rmax - rmin) / (rt_priority_max() - rt_priority_min()); + float y = m * (virtual_pri - rt_priority_min()) + rmin; + int y_int = static_cast(rint(y)); + return std::max(min_real_pri, std::min(max_real_pri, y_int)); + } + + } // namespace impl +} // namespace gr + +#endif + + +#if defined(HAVE_PTHREAD_SETSCHEDPARAM) + +namespace gr { + namespace impl { + + rt_status_t + enable_realtime_scheduling(rt_sched_param p) + { + int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; + int min_real_pri = sched_get_priority_min(policy); + int max_real_pri = sched_get_priority_max(policy); + int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); + + // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. + // fprintf(stderr, "pthread_setschedparam: policy = %d, pri = %d\n", policy, pri); + + struct sched_param param; + memset (¶m, 0, sizeof (param)); + param.sched_priority = pri; + int result = pthread_setschedparam (pthread_self(), policy, ¶m); + if(result != 0) { + if(result == EPERM) // N.B., return value, not errno + return RT_NO_PRIVS; + else { + fprintf(stderr, + "pthread_setschedparam: failed to set real time priority: %s\n", + strerror(result)); + return RT_OTHER_ERROR; + } + } + + //printf("SCHED_FIFO enabled with priority = %d\n", pri); + return RT_OK; + } + + } // namespace impl +} // namespace gr + + +#elif defined(HAVE_SCHED_SETSCHEDULER) + +namespace gr { + namespace impl { + + rt_status_t + enable_realtime_scheduling(rt_sched_param p) + { + int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; + int min_real_pri = sched_get_priority_min(policy); + int max_real_pri = sched_get_priority_max(policy); + int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); + + // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. + // fprintf(stderr, "sched_setscheduler: policy = %d, pri = %d\n", policy, pri); + + int pid = 0; // this process + struct sched_param param; + memset(¶m, 0, sizeof(param)); + param.sched_priority = pri; + int result = sched_setscheduler(pid, policy, ¶m); + if(result != 0){ + if(errno == EPERM) + return RT_NO_PRIVS; + else { + perror("sched_setscheduler: failed to set real time priority"); + return RT_OTHER_ERROR; + } + } + + //printf("SCHED_FIFO enabled with priority = %d\n", pri); + return RT_OK; + } + + } // namespace impl +} // namespace gr + +#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + +#include + +namespace gr { + namespace impl { + + rt_status_t enable_realtime_scheduling(rt_sched_param p) + { + //set the priority class on the process + int pri_class = (true)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; + if(SetPriorityClass(GetCurrentProcess(), pri_class) == 0) + return RT_OTHER_ERROR; + + //scale the priority value to the constants + int priorities[] = { + THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, + THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL + }; + const double priority = double(p.priority)/(rt_priority_max() - rt_priority_min()); + size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 + pri_index %= sizeof(priorities)/sizeof(*priorities); //range check + + //set the thread priority on the thread + if(SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) + return RT_OTHER_ERROR; + + //printf("SetPriorityClass + SetThreadPriority\n"); + return RT_OK; + } + + } // namespace impl +} // namespace gr + +#else + +namespace gr { + namespace impl { + + rt_status_t + enable_realtime_scheduling(rt_sched_param p) + { + return RT_NOT_IMPLEMENTED; + } + + } // namespace impl +} // namespace gr + +#endif diff --git a/lib/runtime/scheduler.cc b/lib/runtime/scheduler.cc new file mode 100644 index 0000000..38d95e6 --- /dev/null +++ b/lib/runtime/scheduler.cc @@ -0,0 +1,39 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "scheduler.h" + +namespace gr { + + scheduler::scheduler(flat_flowgraph_sptr ffg, + int max_noutput_items) + { + } + + scheduler::~scheduler() + { + } + +} /* namespace gr */ diff --git a/lib/runtime/scheduler.h b/lib/runtime/scheduler.h new file mode 100644 index 0000000..575862b --- /dev/null +++ b/lib/runtime/scheduler.h @@ -0,0 +1,68 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_GR_SCHEDULER_H +#define INCLUDED_GR_SCHEDULER_H + +#include +#include +#include +#include "flat_flowgraph.h" + +namespace gr { + + class scheduler; + typedef boost::shared_ptr scheduler_sptr; + + /*! + * \brief Abstract scheduler that takes a flattened flow graph and + * runs it. + * + * Preconditions: details, buffers and buffer readers have been + * assigned. + */ + class GR_RUNTIME_API scheduler : boost::noncopyable + { + public: + /*! + * \brief Construct a scheduler and begin evaluating the graph. + * + * The scheduler will continue running until all blocks until they + * report that they are done or the stop method is called. + */ + scheduler(flat_flowgraph_sptr ffg, int max_noutput_items); + + virtual ~scheduler(); + + /*! + * \brief Tell the scheduler to stop executing. + */ + virtual void stop() = 0; + + /*! + * \brief Block until the graph is done. + */ + virtual void wait() = 0; + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_SCHEDULER_H */ diff --git a/lib/runtime/scheduler_sts.cc b/lib/runtime/scheduler_sts.cc new file mode 100644 index 0000000..19d05b2 --- /dev/null +++ b/lib/runtime/scheduler_sts.cc @@ -0,0 +1,90 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "scheduler_sts.h" +#include "single_threaded_scheduler.h" +#include + +namespace gr { + + class sts_container + { + block_vector_t d_blocks; + + public: + sts_container(block_vector_t blocks) + : d_blocks(blocks) {} + + void operator()() + { + make_single_threaded_scheduler(d_blocks)->run(); + } + }; + + scheduler_sptr + scheduler_sts::make(flat_flowgraph_sptr ffg, int max_noutput_items) + { + return scheduler_sptr(new scheduler_sts(ffg, max_noutput_items)); + } + + scheduler_sts::scheduler_sts(flat_flowgraph_sptr ffg, int max_noutput_items) + : scheduler(ffg, max_noutput_items) + { + // Split the flattened flow graph into discrete partitions, each + // of which is topologically sorted. + + std::vector graphs = ffg->partition(); + + // For each partition, create a thread to evaluate it using + // an instance of the gr_single_threaded_scheduler + + for(std::vector::iterator p = graphs.begin(); + p != graphs.end(); p++) { + + block_vector_t blocks = flat_flowgraph::make_block_vector(*p); + d_threads.create_thread( + gr::thread::thread_body_wrapper(sts_container(blocks), + "single-threaded-scheduler")); + } + } + + scheduler_sts::~scheduler_sts() + { + stop(); + } + + void + scheduler_sts::stop() + { + d_threads.interrupt_all(); + } + + void + scheduler_sts::wait() + { + d_threads.join_all(); + } + +} /* namespace gr */ diff --git a/lib/runtime/scheduler_sts.h b/lib/runtime/scheduler_sts.h new file mode 100644 index 0000000..b4cddb4 --- /dev/null +++ b/lib/runtime/scheduler_sts.h @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_GR_SCHEDULER_STS_H +#define INCLUDED_GR_SCHEDULER_STS_H + +#include +#include +#include "scheduler.h" + +namespace gr { + + /*! + * \brief Concrete scheduler that uses the single_threaded_scheduler + */ + class GR_RUNTIME_API scheduler_sts : public scheduler + { + gr::thread::thread_group d_threads; + + protected: + /*! + * \brief Construct a scheduler and begin evaluating the graph. + * + * The scheduler will continue running until all blocks until they + * report that they are done or the stop method is called. + */ + scheduler_sts(flat_flowgraph_sptr ffg, int max_noutput_items); + + public: + static scheduler_sptr make(flat_flowgraph_sptr ffg, + int max_noutput_items); + + ~scheduler_sts(); + + /*! + * \brief Tell the scheduler to stop executing. + */ + void stop(); + + /*! + * \brief Block until the graph is done. + */ + void wait(); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_SCHEDULER_STS_H */ diff --git a/lib/runtime/scheduler_tpb.cc b/lib/runtime/scheduler_tpb.cc new file mode 100644 index 0000000..d35c37f --- /dev/null +++ b/lib/runtime/scheduler_tpb.cc @@ -0,0 +1,106 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "scheduler_tpb.h" +#include "tpb_thread_body.h" +#include +#include + +namespace gr { + + class tpb_container + { + block_sptr d_block; + int d_max_noutput_items; + + public: + tpb_container(block_sptr block, int max_noutput_items) + : d_block(block), d_max_noutput_items(max_noutput_items) {} + + void operator()() + { + tpb_thread_body body(d_block, d_max_noutput_items); + } + }; + + scheduler_sptr + scheduler_tpb::make(flat_flowgraph_sptr ffg, int max_noutput_items) + { + return scheduler_sptr(new scheduler_tpb(ffg, max_noutput_items)); + } + + scheduler_tpb::scheduler_tpb(flat_flowgraph_sptr ffg, + int max_noutput_items) + : scheduler(ffg, max_noutput_items) + { + // Get a topologically sorted vector of all the blocks in use. + // Being topologically sorted probably isn't going to matter, but + // there's a non-zero chance it might help... + + basic_block_vector_t used_blocks = ffg->calc_used_blocks(); + used_blocks = ffg->topological_sort(used_blocks); + block_vector_t blocks = flat_flowgraph::make_block_vector(used_blocks); + + // Ensure that the done flag is clear on all blocks + + for(size_t i = 0; i < blocks.size(); i++) { + blocks[i]->detail()->set_done(false); + } + + // Fire off a thead for each block + + for(size_t i = 0; i < blocks.size(); i++) { + std::stringstream name; + name << "thread-per-block[" << i << "]: " << blocks[i]; + + // If set, use internal value instead of global value + if(blocks[i]->is_set_max_noutput_items()) + max_noutput_items = blocks[i]->max_noutput_items(); + + d_threads.create_thread( + gr::thread::thread_body_wrapper + (tpb_container(blocks[i], max_noutput_items), + name.str())); + } + } + + scheduler_tpb::~scheduler_tpb() + { + stop(); + } + + void + scheduler_tpb::stop() + { + d_threads.interrupt_all(); + } + + void + scheduler_tpb::wait() + { + d_threads.join_all(); + } + +} /* namespace gr */ diff --git a/lib/runtime/scheduler_tpb.h b/lib/runtime/scheduler_tpb.h new file mode 100644 index 0000000..f5ab21f --- /dev/null +++ b/lib/runtime/scheduler_tpb.h @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_GR_SCHEDULER_TPB_H +#define INCLUDED_GR_SCHEDULER_TPB_H + +#include +#include +#include "scheduler.h" + +namespace gr { + + /*! + * \brief Concrete scheduler that uses a kernel thread-per-block + */ + class GR_RUNTIME_API scheduler_tpb : public scheduler + { + gr::thread::thread_group d_threads; + + protected: + /*! + * \brief Construct a scheduler and begin evaluating the graph. + * + * The scheduler will continue running until all blocks until they + * report that they are done or the stop method is called. + */ + scheduler_tpb(flat_flowgraph_sptr ffg, int max_noutput_items); + + public: + static scheduler_sptr make(flat_flowgraph_sptr ffg, + int max_noutput_items=100000); + + ~scheduler_tpb(); + + /*! + * \brief Tell the scheduler to stop executing. + */ + void stop(); + + /*! + * \brief Block until the graph is done. + */ + void wait(); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_SCHEDULER_TPB_H */ diff --git a/lib/runtime/single_threaded_scheduler.cc b/lib/runtime/single_threaded_scheduler.cc new file mode 100644 index 0000000..c86d26c --- /dev/null +++ b/lib/runtime/single_threaded_scheduler.cc @@ -0,0 +1,363 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "single_threaded_scheduler.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gr { + + // must be defined to either 0 or 1 +#define ENABLE_LOGGING 0 + +#if (ENABLE_LOGGING) +#define LOG(x) do { x; } while(0) +#else +#define LOG(x) do {;} while(0) +#endif + + static int which_scheduler = 0; + + single_threaded_scheduler_sptr + make_single_threaded_scheduler(const std::vector &blocks) + { + return single_threaded_scheduler_sptr + (new single_threaded_scheduler(blocks)); + } + + single_threaded_scheduler::single_threaded_scheduler(const std::vector &blocks) + : d_blocks(blocks), d_enabled(true), d_log(0) + { + if(ENABLE_LOGGING) { + std::string name = str(boost::format("sst-%d.log") % which_scheduler++); + d_log = new std::ofstream(name.c_str()); + *d_log << "single_threaded_scheduler: " + << d_blocks.size () + << " blocks\n"; + } + } + + single_threaded_scheduler::~single_threaded_scheduler() + { + if(ENABLE_LOGGING) + delete d_log; + } + + void + single_threaded_scheduler::run() + { + // d_enabled = true; // KLUDGE + main_loop (); + } + + void + single_threaded_scheduler::stop() + { + if(0) + std::cout << "gr_singled_threaded_scheduler::stop() " + << this << std::endl; + d_enabled = false; + } + + inline static unsigned int + round_up(unsigned int n, unsigned int multiple) + { + return ((n + multiple - 1) / multiple) * multiple; + } + + inline static unsigned int + round_down(unsigned int n, unsigned int multiple) + { + return (n / multiple) * multiple; + } + + // + // Return minimum available write space in all our downstream + // buffers or -1 if we're output blocked and the output we're + // blocked on is done. + // + static int + min_available_space(block_detail *d, int output_multiple) + { + int min_space = std::numeric_limits::max(); + + for(int i = 0; i < d->noutputs (); i++) { + int n = round_down (d->output(i)->space_available (), output_multiple); + if(n == 0) { // We're blocked on output. + if(d->output(i)->done()) { // Downstream is done, therefore we're done. + return -1; + } + return 0; + } + min_space = std::min (min_space, n); + } + return min_space; + } + + void + single_threaded_scheduler::main_loop() + { + static const int DEFAULT_CAPACITY = 16; + + int noutput_items; + gr_vector_int ninput_items_required(DEFAULT_CAPACITY); + gr_vector_int ninput_items(DEFAULT_CAPACITY); + gr_vector_const_void_star input_items(DEFAULT_CAPACITY); + gr_vector_void_star output_items(DEFAULT_CAPACITY); + unsigned int bi; + unsigned int nalive; + int max_items_avail; + bool made_progress_last_pass; + bool making_progress; + + for(unsigned i = 0; i < d_blocks.size (); i++) + d_blocks[i]->detail()->set_done (false); // reset any done flags + + for(unsigned i = 0; i < d_blocks.size (); i++) // enable any drivers, etc. + d_blocks[i]->start(); + + bi = 0; + made_progress_last_pass = true; + making_progress = false; + + // Loop while there are still blocks alive + + nalive = d_blocks.size (); + while(d_enabled && nalive > 0) { + if(boost::this_thread::interruption_requested()) + break; + + block *m = d_blocks[bi].get (); + block_detail *d = m->detail().get (); + + LOG(*d_log << std::endl << m); + + if(d->done ()) + goto next_block; + + if(d->source_p ()) { + // Invoke sources as a last resort. As long as the previous + // pass made progress, don't call a source. + if(made_progress_last_pass) { + LOG(*d_log << " Skipping source\n"); + goto next_block; + } + + ninput_items_required.resize (0); + ninput_items.resize (0); + input_items.resize (0); + output_items.resize (d->noutputs ()); + + // determine the minimum available output space + noutput_items = min_available_space (d, m->output_multiple ()); + LOG(*d_log << " source\n noutput_items = " << noutput_items << std::endl); + if(noutput_items == -1) // we're done + goto were_done; + + if(noutput_items == 0) { // we're output blocked + LOG(*d_log << " BLKD_OUT\n"); + goto next_block; + } + + goto setup_call_to_work; // jump to common code + } + + else if(d->sink_p ()) { + ninput_items_required.resize (d->ninputs ()); + ninput_items.resize (d->ninputs ()); + input_items.resize (d->ninputs ()); + output_items.resize (0); + LOG(*d_log << " sink\n"); + + max_items_avail = 0; + for(int i = 0; i < d->ninputs (); i++) { + ninput_items[i] = d->input(i)->items_available(); + //if (ninput_items[i] == 0 && d->input(i)->done()) + if(ninput_items[i] < m->output_multiple() && d->input(i)->done()) + goto were_done; + + max_items_avail = std::max (max_items_avail, ninput_items[i]); + } + + // take a swag at how much output we can sink + noutput_items = (int) (max_items_avail * m->relative_rate ()); + noutput_items = round_down (noutput_items, m->output_multiple ()); + LOG(*d_log << " max_items_avail = " << max_items_avail << std::endl); + LOG(*d_log << " noutput_items = " << noutput_items << std::endl); + + if(noutput_items == 0) { // we're blocked on input + LOG(*d_log << " BLKD_IN\n"); + goto next_block; + } + + goto try_again; // Jump to code shared with regular case. + } + + else { + // do the regular thing + ninput_items_required.resize(d->ninputs ()); + ninput_items.resize(d->ninputs ()); + input_items.resize(d->ninputs ()); + output_items.resize(d->noutputs ()); + + max_items_avail = 0; + for(int i = 0; i < d->ninputs (); i++) { + ninput_items[i] = d->input(i)->items_available (); + max_items_avail = std::max(max_items_avail, ninput_items[i]); + } + + // determine the minimum available output space + noutput_items = min_available_space(d, m->output_multiple ()); + if(ENABLE_LOGGING){ + *d_log << " regular "; + if(m->relative_rate() >= 1.0) + *d_log << "1:" << m->relative_rate() << std::endl; + else + *d_log << 1.0/m->relative_rate() << ":1\n"; + *d_log << " max_items_avail = " << max_items_avail << std::endl; + *d_log << " noutput_items = " << noutput_items << std::endl; + } + if(noutput_items == -1) // we're done + goto were_done; + + if(noutput_items == 0) { // we're output blocked + LOG(*d_log << " BLKD_OUT\n"); + goto next_block; + } + +#if 0 + // Compute best estimate of noutput_items that we can really use. + noutput_items = + std::min((unsigned)noutput_items, + std::max((unsigned)m->output_multiple(), + round_up((unsigned)(max_items_avail * m->relative_rate()), + m->output_multiple()))); + + LOG(*d_log << " revised noutput_items = " << noutput_items << std::endl); +#endif + + try_again: + if(m->fixed_rate()) { + // try to work it forward starting with max_items_avail. + // We want to try to consume all the input we've got. + int reqd_noutput_items = m->fixed_rate_ninput_to_noutput(max_items_avail); + reqd_noutput_items = round_up(reqd_noutput_items, m->output_multiple()); + if(reqd_noutput_items > 0 && reqd_noutput_items <= noutput_items) + noutput_items = reqd_noutput_items; + } + + // ask the block how much input they need to produce noutput_items + m->forecast(noutput_items, ninput_items_required); + + // See if we've got sufficient input available + int i; + for(i = 0; i < d->ninputs (); i++) + if(ninput_items_required[i] > ninput_items[i]) // not enough + break; + + if(i < d->ninputs()) { // not enough input on input[i] + // if we can, try reducing the size of our output request + if(noutput_items > m->output_multiple ()){ + noutput_items /= 2; + noutput_items = round_up (noutput_items, m->output_multiple ()); + goto try_again; + } + + // We're blocked on input + LOG(*d_log << " BLKD_IN\n"); + if(d->input(i)->done()) // If the upstream block is done, we're done + goto were_done; + + // Is it possible to ever fulfill this request? + if(ninput_items_required[i] > d->input(i)->max_possible_items_available ()) { + // Nope, never going to happen... + std::cerr << "\nsched: name() + << " (" << m->unique_id() << ")>" + << " is requesting more input data\n" + << " than we can provide.\n" + << " ninput_items_required = " + << ninput_items_required[i] << "\n" + << " max_possible_items_available = " + << d->input(i)->max_possible_items_available() << "\n" + << " If this is a filter, consider reducing the number of taps.\n"; + goto were_done; + } + + goto next_block; + } + + // We've got enough data on each input to produce noutput_items. + // Finish setting up the call to work. + for(int i = 0; i < d->ninputs (); i++) + input_items[i] = d->input(i)->read_pointer(); + + setup_call_to_work: + + for(int i = 0; i < d->noutputs (); i++) + output_items[i] = d->output(i)->write_pointer(); + + // Do the actual work of the block + int n = m->general_work(noutput_items, ninput_items, + input_items, output_items); + LOG(*d_log << " general_work: noutput_items = " << noutput_items + << " result = " << n << std::endl); + + if(n == -1) // block is done + goto were_done; + + d->produce_each(n); // advance write pointers + if(n > 0) + making_progress = true; + + goto next_block; + } + assert(0); + + were_done: + LOG(*d_log << " were_done\n"); + d->set_done (true); + nalive--; + + next_block: + if(++bi >= d_blocks.size ()) { + bi = 0; + made_progress_last_pass = making_progress; + making_progress = false; + } + } + + for(unsigned i = 0; i < d_blocks.size(); i++) // disable any drivers, etc. + d_blocks[i]->stop(); + } + +} /* namespace gr */ diff --git a/lib/runtime/single_threaded_scheduler.h b/lib/runtime/single_threaded_scheduler.h new file mode 100644 index 0000000..eccbf03 --- /dev/null +++ b/lib/runtime/single_threaded_scheduler.h @@ -0,0 +1,65 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_SINGLE_THREADED_SCHEDULER_H +#define INCLUDED_GR_SINGLE_THREADED_SCHEDULER_H + +#include +#include +#include + +namespace gr { + + class single_threaded_scheduler; + typedef boost::shared_ptr single_threaded_scheduler_sptr; + + /*! + * \brief Simple scheduler for stream computations. + * \ingroup internal + */ + class GR_RUNTIME_API single_threaded_scheduler + { + public: + ~single_threaded_scheduler(); + + void run(); + void stop(); + + private: + const std::vector d_blocks; + volatile bool d_enabled; + std::ofstream *d_log; + + single_threaded_scheduler(const std::vector &blocks); + + void main_loop(); + + friend GR_RUNTIME_API single_threaded_scheduler_sptr + make_single_threaded_scheduler(const std::vector &blocks); + }; + + GR_RUNTIME_API single_threaded_scheduler_sptr + make_single_threaded_scheduler(const std::vector &blocks); + +} /* namespace gr */ + +#endif /* INCLUDED_GR_SINGLE_THREADED_SCHEDULER_H */ diff --git a/lib/runtime/sptr_magic.cc b/lib/runtime/sptr_magic.cc new file mode 100644 index 0000000..70596ab --- /dev/null +++ b/lib/runtime/sptr_magic.cc @@ -0,0 +1,72 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include + +namespace gnuradio { + + static gr::thread::mutex s_mutex; + typedef std::map sptr_map; + static sptr_map s_map; + + void + detail::sptr_magic::create_and_stash_initial_sptr(gr::hier_block2 *p) + { + gr::basic_block_sptr sptr(p); + gr::thread::scoped_lock guard(s_mutex); + s_map.insert(sptr_map::value_type(static_cast(p), sptr)); + } + + gr::basic_block_sptr + detail::sptr_magic::fetch_initial_sptr(gr::basic_block *p) + { + /* + * If p isn't a subclass of gr::hier_block2, just create the + * shared ptr and return it. + */ + gr::hier_block2 *hb2 = dynamic_cast(p); + if(!hb2) { + return gr::basic_block_sptr(p); + } + + /* + * p is a subclass of gr::hier_block2, thus we've already created the shared pointer + * and stashed it away. Fish it out and return it. + */ + gr::thread::scoped_lock guard(s_mutex); + sptr_map::iterator pos = s_map.find(static_cast(p)); + if(pos == s_map.end()) + throw std::invalid_argument("sptr_magic: invalid pointer!"); + + gr::basic_block_sptr sptr = pos->second; + s_map.erase(pos); + return sptr; + } +}; diff --git a/lib/runtime/sync_block.cc b/lib/runtime/sync_block.cc new file mode 100644 index 0000000..e00b80c --- /dev/null +++ b/lib/runtime/sync_block.cc @@ -0,0 +1,72 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +namespace gr { + + sync_block::sync_block(const std::string &name, + io_signature::sptr input_signature, + io_signature::sptr output_signature) + : block(name, input_signature, output_signature) + { + set_fixed_rate(true); + } + + void + sync_block::forecast(int noutput_items, + gr_vector_int &ninput_items_required) + { + unsigned ninputs = ninput_items_required.size(); + for(unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = fixed_rate_noutput_to_ninput(noutput_items); + } + + int + sync_block::fixed_rate_noutput_to_ninput(int noutput_items) + { + return noutput_items + history() - 1; + } + + int + sync_block::fixed_rate_ninput_to_noutput(int ninput_items) + { + return std::max(0, ninput_items - (int)history() + 1); + } + + int + sync_block::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + int r = work(noutput_items, input_items, output_items); + if(r > 0) + consume_each(r); + return r; + } + +} /* namespace gr */ diff --git a/lib/runtime/sync_decimator.cc b/lib/runtime/sync_decimator.cc new file mode 100644 index 0000000..a9c2d1e --- /dev/null +++ b/lib/runtime/sync_decimator.cc @@ -0,0 +1,72 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +namespace gr { + + sync_decimator::sync_decimator(const std::string &name, + io_signature::sptr input_signature, + io_signature::sptr output_signature, + unsigned decimation) + : sync_block(name, input_signature, output_signature) + { + set_decimation(decimation); + } + + void + sync_decimator::forecast(int noutput_items, gr_vector_int &ninput_items_required) + { + unsigned ninputs = ninput_items_required.size(); + for(unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = fixed_rate_noutput_to_ninput(noutput_items); + } + + int + sync_decimator::fixed_rate_noutput_to_ninput(int noutput_items) + { + return noutput_items * decimation() + history() - 1; + } + + int + sync_decimator::fixed_rate_ninput_to_noutput(int ninput_items) + { + return std::max(0, ninput_items - (int)history() + 1) / decimation(); + } + + int + sync_decimator::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + int r = work(noutput_items, input_items, output_items); + if(r > 0) + consume_each(r * decimation ()); + return r; + } + +} /* namespace gr */ diff --git a/lib/runtime/sync_interpolator.cc b/lib/runtime/sync_interpolator.cc new file mode 100644 index 0000000..5721f24 --- /dev/null +++ b/lib/runtime/sync_interpolator.cc @@ -0,0 +1,73 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +namespace gr { + + sync_interpolator::sync_interpolator(const std::string &name, + io_signature::sptr input_signature, + io_signature::sptr output_signature, + unsigned interpolation) + : sync_block(name, input_signature, output_signature) + { + set_interpolation(interpolation); + } + + void + sync_interpolator::forecast(int noutput_items, + gr_vector_int &ninput_items_required) + { + unsigned ninputs = ninput_items_required.size(); + for(unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = fixed_rate_noutput_to_ninput(noutput_items); + } + + int + sync_interpolator::fixed_rate_noutput_to_ninput(int noutput_items) + { + return noutput_items / interpolation() + history() - 1; + } + + int + sync_interpolator::fixed_rate_ninput_to_noutput(int ninput_items) + { + return std::max(0, ninput_items - (int)history() + 1) * interpolation(); + } + + int + sync_interpolator::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + int r = work(noutput_items, input_items, output_items); + if(r > 0) + consume_each(r / interpolation()); + return r; + } + +} /* namespace gr */ diff --git a/lib/runtime/sys_paths.cc b/lib/runtime/sys_paths.cc new file mode 100644 index 0000000..64853c6 --- /dev/null +++ b/lib/runtime/sys_paths.cc @@ -0,0 +1,65 @@ +/* + * Copyright 2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include +#include //getenv +#include //P_tmpdir (maybe) + +namespace gr { + + const char *tmp_path() + { + const char *path; + + //first case, try TMP environment variable + path = getenv("TMP"); + if(path) + return path; + + //second case, try P_tmpdir when its defined + #ifdef P_tmpdir + if(P_tmpdir) + return P_tmpdir; + #endif /*P_tmpdir*/ + + //fall-through case, nothing worked + return "/tmp"; + } + + const char *appdata_path() + { + const char *path; + + //first case, try HOME environment variable (unix) + path = getenv("HOME"); + if(path) + return path; + + //second case, try APPDATA environment variable (windows) + path = getenv("APPDATA"); + if(path) + return path; + + //fall-through case, nothing worked + return tmp_path(); + } + +} /* namespace gr */ diff --git a/lib/runtime/thread/thread.cc b/lib/runtime/thread/thread.cc new file mode 100644 index 0000000..d79d1a0 --- /dev/null +++ b/lib/runtime/thread/thread.cc @@ -0,0 +1,288 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + +#include + +namespace gr { + namespace thread { + + gr_thread_t + get_current_thread_id() + { + return GetCurrentThread(); + } + + void + thread_bind_to_processor(int n) + { + std::vector mask(1, n); + thread_bind_to_processor(get_current_thread_id(), mask); + } + + void + thread_bind_to_processor(const std::vector &mask) + { + thread_bind_to_processor(get_current_thread_id(), mask); + } + + void + thread_bind_to_processor(gr_thread_t thread, int n) + { + std::vector mask(1, n); + thread_bind_to_processor(thread, mask); + } + + void + thread_bind_to_processor(gr_thread_t thread, const std::vector &mask) + { + //DWORD_PTR mask = (1 << n); + DWORD_PTR dword_mask = 0; + + std::vector _mask = mask; + std::vector::iterator itr; + for(itr = _mask.begin(); itr != _mask.end(); itr++) + dword_mask |= (1 << (*itr)); + + DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask); + if(ret == 0) { + std::stringstream s; + s << "thread_bind_to_processor failed with error: " + << GetLastError() << std::endl; + throw std::runtime_error(s.str()); + } + } + + void + thread_unbind() + { + thread_unbind(get_current_thread_id()); + } + + void + thread_unbind(gr_thread_t thread) + { + DWORD_PTR dword_mask = sizeof(DWORD_PTR) - 1; + DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask); + if(ret == 0) { + std::stringstream s; + s << "thread_unbind failed with error: " + << GetLastError() << std::endl; + throw std::runtime_error(s.str()); + } + } + + int + thread_priority(gr_thread_t thread) + { + // Not implemented on Windows + return -1; + } + + int + set_thread_priority(gr_thread_t thread, int priority) + { + // Not implemented on Windows + return -1; + } + + } /* namespace thread */ +} /* namespace gr */ + + +#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) + +namespace gr { + namespace thread { + + gr_thread_t + get_current_thread_id() + { + // Not implemented on OSX + } + + void + thread_bind_to_processor(int n) + { + // Not implemented on OSX + } + + void + thread_bind_to_processor(gr_thread_t thread, int n) + { + // Not implemented on OSX + } + + void + thread_bind_to_processor(const std::vector &mask) + { + // Not implemented on OSX + } + + void + thread_bind_to_processor(gr_thread_t thread, const std::vector &mask) + { + // Not implemented on OSX + } + + void + thread_unbind() + { + // Not implemented on OSX + } + + void + thread_unbind(gr_thread_t thread) + { + // Not implemented on OSX + } + + int + thread_priority(gr_thread_t thread) + { + // Not implemented on OSX + return -1; + } + + int + set_thread_priority(gr_thread_t thread, int priority) + { + // Not implemented on OSX + return -1; + } + + } /* namespace thread */ +} /* namespace gr */ + +#else + +#include +#include +#include + +namespace gr { + namespace thread { + + gr_thread_t + get_current_thread_id() + { + return pthread_self(); + } + + void + thread_bind_to_processor(int n) + { + std::vector mask(1, n); + thread_bind_to_processor(get_current_thread_id(), mask); + } + + void + thread_bind_to_processor(const std::vector &mask) + { + thread_bind_to_processor(get_current_thread_id(), mask); + } + + void + thread_bind_to_processor(gr_thread_t thread, int n) + { + std::vector mask(1, n); + thread_bind_to_processor(thread, mask); + } + + void + thread_bind_to_processor(gr_thread_t thread, const std::vector &mask) + { + cpu_set_t set; + size_t len = sizeof(cpu_set_t); + std::vector _mask = mask; + std::vector::iterator itr; + + CPU_ZERO(&set); + for(itr = _mask.begin(); itr != _mask.end(); itr++) + CPU_SET(*itr, &set); + + int ret = pthread_setaffinity_np(thread, len, &set); + if(ret != 0) { + std::stringstream s; + s << "thread_bind_to_processor failed with error: " << ret << std::endl; + throw std::runtime_error(s.str()); + } + } + + void + thread_unbind() + { + thread_unbind(get_current_thread_id()); + } + + void + thread_unbind(gr_thread_t thread) + { + cpu_set_t set; + size_t len = sizeof(cpu_set_t); + + CPU_ZERO(&set); + long ncpus = sysconf(_SC_NPROCESSORS_ONLN); + for(long n = 0; n < ncpus; n++) { + CPU_SET(n, &set); + } + + int ret = pthread_setaffinity_np(thread, len, &set); + if(ret != 0) { + std::stringstream s; + s << "thread_unbind failed with error: " << ret << std::endl; + throw std::runtime_error(s.str()); + } + } + + int + thread_priority(gr_thread_t thread) + { + sched_param param; + int priority; + int policy; + int ret; + ret = pthread_getschedparam (thread, &policy, ¶m); + priority = param.sched_priority; + return (ret==0)?priority:ret; + } + + int + set_thread_priority(gr_thread_t thread, int priority) + { + int policy; + struct sched_param param; + pthread_getschedparam (thread, &policy, ¶m); + param.sched_priority = priority; + return pthread_setschedparam(thread, policy, ¶m); + } + + } /* namespace thread */ +} /* namespace gr */ + +#endif diff --git a/lib/runtime/thread/thread_body_wrapper.cc b/lib/runtime/thread/thread_body_wrapper.cc new file mode 100644 index 0000000..e23b7d1 --- /dev/null +++ b/lib/runtime/thread/thread_body_wrapper.cc @@ -0,0 +1,91 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#ifdef HAVE_SIGNAL_H +#include +#endif + +#include + +namespace gr { + namespace thread { + +#if defined(HAVE_PTHREAD_SIGMASK) && defined(HAVE_SIGNAL_H) + + void mask_signals() + { + sigset_t new_set; + int r; + + sigemptyset(&new_set); + sigaddset(&new_set, SIGHUP); // block these... + sigaddset(&new_set, SIGINT); + sigaddset(&new_set, SIGPIPE); + sigaddset(&new_set, SIGALRM); + sigaddset(&new_set, SIGTERM); + sigaddset(&new_set, SIGUSR1); + sigaddset(&new_set, SIGCHLD); +#ifdef SIGPOLL + sigaddset(&new_set, SIGPOLL); +#endif +#ifdef SIGPROF + sigaddset(&new_set, SIGPROF); +#endif +#ifdef SIGSYS + sigaddset(&new_set, SIGSYS); +#endif +#ifdef SIGTRAP + sigaddset(&new_set, SIGTRAP); +#endif +#ifdef SIGURG + sigaddset(&new_set, SIGURG); +#endif +#ifdef SIGVTALRM + sigaddset(&new_set, SIGVTALRM); +#endif +#ifdef SIGXCPU + sigaddset(&new_set, SIGXCPU); +#endif +#ifdef SIGXFSZ + sigaddset(&new_set, SIGXFSZ); +#endif + r = pthread_sigmask(SIG_BLOCK, &new_set, 0); + if(r != 0) + perror("pthread_sigmask"); + } + +#else + + void mask_signals() + { + } + +#endif + + } /* namespace thread */ +} /* namespace gr */ + diff --git a/lib/runtime/thread/thread_group.cc b/lib/runtime/thread/thread_group.cc new file mode 100644 index 0000000..e467dfd --- /dev/null +++ b/lib/runtime/thread/thread_group.cc @@ -0,0 +1,98 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2001-2003 William E. Kempf + * Copyright (C) 2007 Anthony Williams + * Copyright 2008 Free Software Foundation, Inc. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ + +/* + * This was extracted from Boost 1.35.0 and fixed. + */ + +#include + +namespace gr { + namespace thread { + + thread_group::thread_group() + { + } + + thread_group::~thread_group() + { + // We shouldn't have to scoped_lock here, since referencing this + // object from another thread while we're deleting it in the + // current thread is going to lead to undefined behavior any + // way. + for(std::list::iterator it = m_threads.begin(); + it != m_threads.end(); ++it) { + delete (*it); + } + } + + boost::thread* thread_group::create_thread(const boost::function0& threadfunc) + { + // No scoped_lock required here since the only "shared data" that's + // modified here occurs inside add_thread which does scoped_lock. + std::auto_ptr thrd(new boost::thread(threadfunc)); + add_thread(thrd.get()); + return thrd.release(); + } + + void thread_group::add_thread(boost::thread* thrd) + { + boost::lock_guard guard(m_mutex); + + // For now we'll simply ignore requests to add a thread object + // multiple times. Should we consider this an error and either + // throw or return an error value? + std::list::iterator it = std::find(m_threads.begin(), + m_threads.end(), thrd); + BOOST_ASSERT(it == m_threads.end()); + if(it == m_threads.end()) + m_threads.push_back(thrd); + } + + void thread_group::remove_thread(boost::thread* thrd) + { + boost::lock_guard guard(m_mutex); + + // For now we'll simply ignore requests to remove a thread + // object that's not in the group. Should we consider this an + // error and either throw or return an error value? + std::list::iterator it = std::find(m_threads.begin(), + m_threads.end(), thrd); + BOOST_ASSERT(it != m_threads.end()); + if(it != m_threads.end()) + m_threads.erase(it); + } + + void thread_group::join_all() + { + boost::shared_lock guard(m_mutex); + for(std::list::iterator it = m_threads.begin(); + it != m_threads.end(); ++it) { + (*it)->join(); + } + } + + void thread_group::interrupt_all() + { + boost::shared_lock guard(m_mutex); + for(std::list::iterator it=m_threads.begin(),end=m_threads.end(); + it!=end; ++it) { + (*it)->interrupt(); + } + } + + size_t thread_group::size() const + { + boost::shared_lock guard(m_mutex); + return m_threads.size(); + } + + } /* namespace thread */ +} /* namespace gr */ diff --git a/lib/runtime/top_block.cc b/lib/runtime/top_block.cc new file mode 100644 index 0000000..32638af --- /dev/null +++ b/lib/runtime/top_block.cc @@ -0,0 +1,135 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "top_block_impl.h" +#include +#include +#include +#include +#include +#include + +namespace gr { + top_block_sptr + make_top_block(const std::string &name) + { + return gnuradio::get_initial_sptr + (new top_block(name)); + } + + top_block::top_block(const std::string &name) + : hier_block2(name, + io_signature::make(0,0,0), + io_signature::make(0,0,0)) + { + d_impl = new top_block_impl(this); + } + + top_block::~top_block() + { + stop(); + wait(); + + delete d_impl; + } + + void + top_block::start(int max_noutput_items) + { + d_impl->start(max_noutput_items); + + if(prefs::singleton()->get_bool("ControlPort", "on", false)) { + setup_rpc(); + } + } + + void + top_block::stop() + { + d_impl->stop(); + } + + void + top_block::wait() + { + d_impl->wait(); + } + + void + top_block::run(int max_noutput_items) + { + start(max_noutput_items); + wait(); + } + + void + top_block::lock() + { + d_impl->lock(); + } + + void + top_block::unlock() + { + d_impl->unlock(); + } + + std::string + top_block::edge_list() + { + return d_impl->edge_list(); + } + + void + top_block::dump() + { + d_impl->dump(); + } + + int + top_block::max_noutput_items() + { + return d_impl->max_noutput_items(); + } + + void + top_block::set_max_noutput_items(int nmax) + { + d_impl->set_max_noutput_items(nmax); + } + + top_block_sptr + top_block::to_top_block() + { + return cast_to_top_block_sptr(shared_from_this()); + } + + void + top_block::setup_rpc() + { + } + +} /* namespace gr */ diff --git a/lib/runtime/top_block_impl.cc b/lib/runtime/top_block_impl.cc new file mode 100644 index 0000000..9d377f4 --- /dev/null +++ b/lib/runtime/top_block_impl.cc @@ -0,0 +1,212 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "top_block_impl.h" +#include "flat_flowgraph.h" +#include "scheduler_sts.h" +#include "scheduler_tpb.h" +#include +#include + +#include +#include +#include +#include +#include + +namespace gr { + +#define GR_TOP_BLOCK_IMPL_DEBUG 0 + + typedef scheduler_sptr(*scheduler_maker)(flat_flowgraph_sptr ffg, + int max_noutput_items); + + static struct scheduler_table { + const char *name; + scheduler_maker f; + } scheduler_table[] = { + { "TPB", scheduler_tpb::make }, // first entry is default + { "STS", scheduler_sts::make } + }; + + static scheduler_sptr + make_scheduler(flat_flowgraph_sptr ffg, int max_noutput_items) + { + static scheduler_maker factory = 0; + + if(factory == 0) { + char *v = getenv("GR_SCHEDULER"); + if(!v) + factory = scheduler_table[0].f; // use default + else { + for(size_t i = 0; i < sizeof(scheduler_table)/sizeof(scheduler_table[0]); i++) { + if(strcmp(v, scheduler_table[i].name) == 0) { + factory = scheduler_table[i].f; + break; + } + } + if(factory == 0) { + std::cerr << "warning: Invalid GR_SCHEDULER environment variable value \"" + << v << "\". Using \"" << scheduler_table[0].name << "\"\n"; + factory = scheduler_table[0].f; + } + } + } + return factory(ffg, max_noutput_items); + } + + top_block_impl::top_block_impl(top_block *owner) + : d_owner(owner), d_ffg(), + d_state(IDLE), d_lock_count(0) + { + } + + top_block_impl::~top_block_impl() + { + d_owner = 0; + } + + void + top_block_impl::start(int max_noutput_items) + { + gr::thread::scoped_lock l(d_mutex); + + d_max_noutput_items = max_noutput_items; + + if(d_state != IDLE) + throw std::runtime_error("top_block::start: top block already running or wait() not called after previous stop()"); + + if(d_lock_count > 0) + throw std::runtime_error("top_block::start: can't start with flow graph locked"); + + // Create new flat flow graph by flattening hierarchy + d_ffg = d_owner->flatten(); + + // Validate new simple flow graph and wire it up + d_ffg->validate(); + d_ffg->setup_connections(); + + // Only export perf. counters if ControlPort config param is + // enabled and if the PerfCounter option 'export' is turned on. + prefs *p = prefs::singleton(); + if(p->get_bool("ControlPort", "on", false) && p->get_bool("PerfCounters", "export", false)) + d_ffg->enable_pc_rpc(); + + d_scheduler = make_scheduler(d_ffg, d_max_noutput_items); + d_state = RUNNING; + } + + void + top_block_impl::stop() + { + if(d_scheduler) + d_scheduler->stop(); + } + + void + top_block_impl::wait() + { + if(d_scheduler) + d_scheduler->wait(); + + d_state = IDLE; + } + + // N.B. lock() and unlock() cannot be called from a flow graph + // thread or deadlock will occur when reconfiguration happens + void + top_block_impl::lock() + { + gr::thread::scoped_lock lock(d_mutex); + d_lock_count++; + } + + void + top_block_impl::unlock() + { + gr::thread::scoped_lock lock(d_mutex); + + if(d_lock_count <= 0) { + d_lock_count = 0; // fix it, then complain + throw std::runtime_error("unpaired unlock() call"); + } + + d_lock_count--; + if(d_lock_count > 0 || d_state == IDLE) // nothing to do + return; + + restart(); + } + + /* + * restart is called with d_mutex held + */ + void + top_block_impl::restart() + { + stop(); // Stop scheduler and wait for completion + wait(); + + // Create new simple flow graph + flat_flowgraph_sptr new_ffg = d_owner->flatten(); + new_ffg->validate(); // check consistency, sanity, etc + new_ffg->merge_connections(d_ffg); // reuse buffers, etc + d_ffg = new_ffg; + + // Create a new scheduler to execute it + d_scheduler = make_scheduler(d_ffg, d_max_noutput_items); + d_state = RUNNING; + } + + std::string + top_block_impl::edge_list() + { + if(d_ffg) + return d_ffg->edge_list(); + else + return ""; + } + + void + top_block_impl::dump() + { + if(d_ffg) + d_ffg->dump(); + } + + int + top_block_impl::max_noutput_items() + { + return d_max_noutput_items; + } + + void + top_block_impl::set_max_noutput_items(int nmax) + { + d_max_noutput_items = nmax; + } + +} /* namespace gr */ diff --git a/lib/runtime/top_block_impl.h b/lib/runtime/top_block_impl.h new file mode 100644 index 0000000..9e0e661 --- /dev/null +++ b/lib/runtime/top_block_impl.h @@ -0,0 +1,90 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GR_TOP_BLOCK_IMPL_H +#define INCLUDED_GR_TOP_BLOCK_IMPL_H + +#include +#include "scheduler.h" +#include + +namespace gr { + + /*! + *\brief Abstract implementation details of top_block + * \ingroup internal + * + * The actual implementation of top_block. Separate class allows + * decoupling of changes from dependent classes. + */ + class GR_RUNTIME_API top_block_impl + { + public: + top_block_impl(top_block *owner); + ~top_block_impl(); + + // Create and start scheduler threads + void start(int max_noutput_items=100000000); + + // Signal scheduler threads to stop + void stop(); + + // Wait for scheduler threads to exit + void wait(); + + // Lock the top block to allow reconfiguration + void lock(); + + // Unlock the top block at end of reconfiguration + void unlock(); + + // Return a string list of edges + std::string edge_list(); + + // Dump the flowgraph to stdout + void dump(); + + // Get the number of max noutput_items in the flowgraph + int max_noutput_items(); + + // Set the maximum number of noutput_items in the flowgraph + void set_max_noutput_items(int nmax); + + protected: + enum tb_state { IDLE, RUNNING }; + + top_block *d_owner; + flat_flowgraph_sptr d_ffg; + scheduler_sptr d_scheduler; + + gr::thread::mutex d_mutex; // protects d_state and d_lock_count + tb_state d_state; + int d_lock_count; + int d_max_noutput_items; + + private: + void restart(); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_TOP_BLOCK_IMPL_H */ diff --git a/lib/runtime/tpb_detail.cc b/lib/runtime/tpb_detail.cc new file mode 100644 index 0000000..cf05c2b --- /dev/null +++ b/lib/runtime/tpb_detail.cc @@ -0,0 +1,71 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +namespace gr { + + /* + * We assume that no worker threads are ever running when the graph + * structure is being manipulated, thus it's safe for us to poke + * around in our neighbors w/o holding any locks. + */ + void + tpb_detail::notify_upstream(block_detail *d) + { + // For each of our inputs, tell the guy upstream that we've + // consumed some input, and that he most likely has more output + // buffer space available. + + for(size_t i = 0; i < d->d_input.size(); i++) { + // Can you say, "pointer chasing?" + d->d_input[i]->buffer()->link()->detail()->d_tpb.set_output_changed(); + } + } + + void + tpb_detail::notify_downstream(block_detail *d) + { + // For each of our outputs, tell the guys downstream that they + // have new input available. + + for(size_t i = 0; i < d->d_output.size(); i++) { + buffer_sptr buf = d->d_output[i]; + for(size_t j = 0, k = buf->nreaders(); j < k; j++) + buf->reader(j)->link()->detail()->d_tpb.set_input_changed(); + } + } + + void + tpb_detail::notify_neighbors(block_detail *d) + { + notify_downstream(d); + notify_upstream(d); + } + +} /* namespace gr */ diff --git a/lib/runtime/tpb_thread_body.cc b/lib/runtime/tpb_thread_body.cc new file mode 100644 index 0000000..86725a8 --- /dev/null +++ b/lib/runtime/tpb_thread_body.cc @@ -0,0 +1,121 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "tpb_thread_body.h" +#include +#include +#include +#include + +namespace gr { + + tpb_thread_body::tpb_thread_body(block_sptr block, int max_noutput_items) + : d_exec(block, max_noutput_items) + { + //std::cerr << "tpb_thread_body: " << block << std::endl; + + block_detail *d = block->detail().get(); + block_executor::state s; + + d->threaded = true; + d->thread = gr::thread::get_current_thread_id(); + + prefs *p = prefs::singleton(); + size_t max_nmsgs = static_cast(p->get_long("DEFAULT", "max_messages", 100)); + + // Set thread affinity if it was set before fg was started. + if(block->processor_affinity().size() > 0) { + gr::thread::thread_bind_to_processor(d->thread, block->processor_affinity()); + } + + // Set thread priority if it was set before fg was started + if(block->thread_priority() > 0) { + gr::thread::set_thread_priority(d->thread, block->thread_priority()); + } + + while(1) { + boost::this_thread::interruption_point(); + + d->d_tpb.clear_changed(); + // run one iteration if we are a connected stream block + if(d->noutputs() >0 || d->ninputs()>0){ + s = d_exec.run_one_iteration(); + } + else { + s = block_executor::BLKD_IN; + } + + switch(s){ + case block_executor::READY: // Tell neighbors we made progress. + d->d_tpb.notify_neighbors(d); + break; + + case block_executor::READY_NO_OUTPUT: // Notify upstream only + d->d_tpb.notify_upstream(d); + break; + + case block_executor::DONE: // Game over. + d->d_tpb.notify_neighbors(d); + return; + + case block_executor::BLKD_IN: // Wait for input. + { + gr::thread::scoped_lock guard(d->d_tpb.mutex); + while(!d->d_tpb.input_changed) { + + // wait for input or message + while(!d->d_tpb.input_changed) + d->d_tpb.input_cond.wait(guard); + + if (d->done()) { + return; + } + } + } + break; + + case block_executor::BLKD_OUT: // Wait for output buffer space. + { + gr::thread::scoped_lock guard(d->d_tpb.mutex); + while(!d->d_tpb.output_changed) { + // wait for output room or message + while(!d->d_tpb.output_changed) + d->d_tpb.output_cond.wait(guard); + } + + } + break; + + default: + throw std::runtime_error("possible memory corruption in scheduler"); + } + } + } + + tpb_thread_body::~tpb_thread_body() + { + } + +} /* namespace gr */ diff --git a/lib/runtime/tpb_thread_body.h b/lib/runtime/tpb_thread_body.h new file mode 100644 index 0000000..9859b12 --- /dev/null +++ b/lib/runtime/tpb_thread_body.h @@ -0,0 +1,49 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef INCLUDED_GR_TPB_THREAD_BODY_H +#define INCLUDED_GR_TPB_THREAD_BODY_H + +#include +#include +#include +#include "block_executor.h" + +namespace gr { + + /*! + * \brief The body of each thread-per-block thread. + * + * One of these is instantiated in its own thread for each block. + * The constructor turns into the main loop which returns when the + * block is done or is interrupted. + */ + class GR_RUNTIME_API tpb_thread_body + { + block_executor d_exec; + + public: + tpb_thread_body(block_sptr block, int max_noutput_items=100000); + ~tpb_thread_body(); + }; + +} /* namespace gr */ + +#endif /* INCLUDED_GR_TPB_THREAD_BODY_H */ diff --git a/lib/runtime/vmcircbuf.cc b/lib/runtime/vmcircbuf.cc new file mode 100644 index 0000000..ff5093d --- /dev/null +++ b/lib/runtime/vmcircbuf.cc @@ -0,0 +1,301 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include "vmcircbuf.h" +#include "vmcircbuf_prefs.h" +#include "local_sighandler.h" + +// all the factories we know about +#include "vmcircbuf_createfilemapping.h" +#include "vmcircbuf_sysv_shm.h" +#include "vmcircbuf_mmap_shm_open.h" +#include "vmcircbuf_mmap_tmpfile.h" + +gr::thread::mutex s_vm_mutex; + +namespace gr { + + static const char *FACTORY_PREF_KEY = "vmcircbuf_default_factory"; + + vmcircbuf::~vmcircbuf() + { + } + + vmcircbuf_factory::~vmcircbuf_factory() + { + } + + // ---------------------------------------------------------------- + + static vmcircbuf_factory *s_default_factory = 0; + + vmcircbuf_factory * + vmcircbuf_sysconfig::get_default_factory() + { + if(s_default_factory) + return s_default_factory; + + bool verbose = false; + + std::vector all = all_factories (); + + const char *name = gr::vmcircbuf_prefs::get(FACTORY_PREF_KEY); + + if(name) { + for(unsigned int i = 0; i < all.size (); i++) { + if(strncmp(name, all[i]->name(), strlen(all[i]->name())) == 0) { + s_default_factory = all[i]; + if(verbose) + fprintf(stderr, "gr::vmcircbuf_sysconfig: using %s\n", + s_default_factory->name()); + return s_default_factory; + } + } + } + + // either we don't have a default, or the default named is not in our + // list of factories. Find the first factory that works. + + if(verbose) + fprintf(stderr, "gr::vmcircbuf_sysconfig: finding a working factory...\n"); + + for(unsigned int i = 0; i < all.size (); i++) { + if(test_factory(all[i], verbose)) { + set_default_factory(all[i]); + return s_default_factory; + } + } + + // We're screwed! + fprintf(stderr, "gr::vmcircbuf_sysconfig: unable to find a working factory!\n"); + throw std::runtime_error("gr::vmcircbuf_sysconfig"); + } + + std::vector + vmcircbuf_sysconfig::all_factories() + { + std::vector result; + + result.push_back(gr::vmcircbuf_createfilemapping_factory::singleton()); +#ifdef TRY_SHM_VMCIRCBUF + result.push_back(gr::vmcircbuf_sysv_shm_factory::singleton()); + result.push_back(gr::vmcircbuf_mmap_shm_open_factory::singleton()); +#endif + result.push_back (gr::vmcircbuf_mmap_tmpfile_factory::singleton()); + + return result; + } + + void + vmcircbuf_sysconfig::set_default_factory(vmcircbuf_factory *f) + { + gr::vmcircbuf_prefs::set(FACTORY_PREF_KEY, f->name()); + s_default_factory = f; + } + + + // ------------------------------------------------------------------------ + // test code for vmcircbuf factories + // ------------------------------------------------------------------------ + + static void + init_buffer(vmcircbuf *c, int counter, int size) + { + unsigned int *p = (unsigned int*)c->pointer_to_first_copy(); + for(unsigned int i = 0; i < size / sizeof(int); i++) + p[i] = counter + i; + } + + static bool + check_mapping(vmcircbuf *c, int counter, int size, const char *msg, bool verbose) + { + bool ok = true; + + if(verbose) + fprintf(stderr, "... %s", msg); + + unsigned int *p1 = (unsigned int *)c->pointer_to_first_copy(); + unsigned int *p2 = (unsigned int *)c->pointer_to_second_copy(); + + // fprintf(stderr, "p1 = %p, p2 = %p\n", p1, p2); + + for(unsigned int i = 0; i < size / sizeof (int); i++) { + if(p1[i] != counter + i) { + ok = false; + if(verbose) + fprintf(stderr, " p1[%d] == %u, expected %u\n", i, p1[i], counter + i); + break; + } + if(p2[i] != counter + i) { + if(verbose) + fprintf(stderr, " p2[%d] == %u, expected %u\n", i, p2[i], counter + i); + ok = false; + break; + } + } + + if(ok && verbose) { + fprintf(stderr, " OK\n"); + } + return ok; + } + + static const char * + memsize(int size) + { + static std::string buf; + if(size >= (1 << 20)) { + buf = str(boost::format("%dMB") % (size / (1 << 20))); + } + else if(size >= (1 << 10)){ + buf = str(boost::format("%dKB") % (size / (1 << 10))); + } + else { + buf = str(boost::format("%d") % size); + } + return buf.c_str(); + } + + static bool + test_a_bunch(vmcircbuf_factory *factory, int n, int size, int *start_ptr, bool verbose) + { + bool ok = true; + std::vector counter(n); + std::vector c(n); + int cum_size = 0; + + for(int i = 0; i < n; i++) { + counter[i] = *start_ptr; + *start_ptr += size; + if((c[i] = factory->make (size)) == 0) { + if(verbose) + fprintf(stderr, + "Failed to allocate gr::vmcircbuf number %d of size %d (cum = %s)\n", + i + 1, size, memsize(cum_size)); + return false; + } + init_buffer(c[i], counter[i], size); + cum_size += size; + } + + for(int i = 0; i < n; i++) { + std::string msg = str(boost::format("test_a_bunch_%dx%s[%d]") % n % memsize(size) % i); + ok &= check_mapping(c[i], counter[i], size, msg.c_str(), verbose); + } + + for(int i = 0; i < n; i++) { + delete c[i]; + c[i] = 0; + } + + return ok; + } + + static bool + standard_tests(vmcircbuf_factory *f, int verbose) + { + if(verbose >= 1) + fprintf(stderr, "Testing %s...\n", f->name()); + + bool v = verbose >= 2; + int granularity = f->granularity(); + int start = 0; + bool ok = true; + + ok &= test_a_bunch(f, 1, 1 * granularity, &start, v); // 1 x 4KB = 4KB + + if(ok) { + ok &= test_a_bunch(f, 64, 4 * granularity, &start, v); // 64 x 16KB = 1MB + ok &= test_a_bunch(f, 4, 4 * (1L << 20), &start, v); // 4 x 4MB = 16MB + // ok &= test_a_bunch(f, 256, 256 * (1L << 10), &start, v); // 256 x 256KB = 64MB + } + + if(verbose >= 1) + fprintf(stderr, "....... %s: %s", f->name(), ok ? "OK\n" : "Doesn't work\n"); + + return ok; + } + + bool + vmcircbuf_sysconfig::test_factory(vmcircbuf_factory *f, int verbose) + { + // Install local signal handlers for SIGSEGV and SIGBUS. + // If something goes wrong, these signals may be invoked. + +#ifdef SIGSEGV + gr::local_sighandler sigsegv (SIGSEGV, gr::local_sighandler::throw_signal); +#endif +#ifdef SIGBUS + gr::local_sighandler sigbus (SIGBUS, gr::local_sighandler::throw_signal); +#endif +#ifdef SIGSYS + gr::local_sighandler sigsys (SIGSYS, gr::local_sighandler::throw_signal); +#endif + + try { + return standard_tests (f, verbose); + } + catch(gr::signal &sig) { + if(verbose) { + fprintf(stderr, "....... %s: %s", f->name(), "Doesn't work\n"); + fprintf(stderr, + "gr::vmcircbuf_factory::test_factory (%s): caught %s\n", + f->name(), sig.name().c_str()); + return false; + } + } + catch (...) { + if(verbose) { + fprintf(stderr, "....... %s: %s", f->name(), "Doesn't work\n"); + fprintf(stderr, + "gr::vmcircbuf_factory::test_factory (%s): some kind of uncaught exception\n", + f->name()); + } + return false; + } + return false; // never gets here. shut compiler up. + } + + bool + vmcircbuf_sysconfig::test_all_factories(int verbose) + { + bool ok = false; + + std::vector all = all_factories(); + + for(unsigned int i = 0; i < all.size (); i++) + ok |= test_factory(all[i], verbose); + + return ok; + } + +} /* namespace gr */ diff --git a/lib/runtime/vmcircbuf.h b/lib/runtime/vmcircbuf.h new file mode 100644 index 0000000..f9ed045 --- /dev/null +++ b/lib/runtime/vmcircbuf.h @@ -0,0 +1,128 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_VMCIRCBUF_H +#define GR_VMCIRCBUF_H + +#include +#include +#include + +extern gr::thread::mutex s_vm_mutex; + +namespace gr { + + /*! + * \brief abstract class to implement doubly mapped virtual memory circular buffers + * \ingroup internal + */ + class GR_RUNTIME_API vmcircbuf + { + protected: + int d_size; + char *d_base; + + // CREATORS + vmcircbuf(int size) : d_size(size), d_base(0) {}; + + public: + virtual ~vmcircbuf(); + + // ACCESSORS + void *pointer_to_first_copy() const{ return d_base; } + void *pointer_to_second_copy() const{ return d_base + d_size; } + }; + + /*! + * \brief abstract factory for creating circular buffers + */ + class GR_RUNTIME_API vmcircbuf_factory + { + protected: + vmcircbuf_factory() {}; + virtual ~vmcircbuf_factory(); + + public: + + /*! + * \brief return name of this factory + */ + virtual const char *name() const = 0; + + /*! + * \brief return granularity of mapping, typically equal to page size + */ + virtual int granularity() = 0; + + /*! + * \brief return a gr::vmcircbuf, or 0 if unable. + * + * Call this to create a doubly mapped circular buffer. + */ + virtual vmcircbuf *make(int size) = 0; + }; + + /* + * \brief pulls together all implementations of gr::vmcircbuf + */ + class GR_RUNTIME_API vmcircbuf_sysconfig + { + public: + /* + * \brief return the single instance of the default factory. + * + * returns the default factory to use if it's already defined, + * else find the first working factory and use it. + */ + static vmcircbuf_factory *get_default_factory(); + + static int granularity() { return get_default_factory()->granularity(); } + static vmcircbuf *make(int size) { return get_default_factory()->make(size); } + + // N.B. not all factories are guaranteed to work. + // It's too hard to check everything at config time, so we check at runtime + static std::vector all_factories(); + + // make this factory the default + static void set_default_factory(vmcircbuf_factory *f); + + /*! + * \brief Does this factory really work? + * + * verbose = 0: silent + * verbose = 1: names of factories tested and results + * verbose = 2: all intermediate results + */ + static bool test_factory(vmcircbuf_factory *f, int verbose); + + /*! + * \brief Test all factories, return true if at least one of them works + * verbose = 0: silent + * verbose = 1: names of factories tested and results + * verbose = 2: all intermediate results + */ + static bool test_all_factories(int verbose); + }; + +} /* namespace gr */ + +#endif /* GR_VMCIRCBUF_H */ diff --git a/lib/runtime/vmcircbuf_createfilemapping.cc b/lib/runtime/vmcircbuf_createfilemapping.cc new file mode 100644 index 0000000..8d4de28 --- /dev/null +++ b/lib/runtime/vmcircbuf_createfilemapping.cc @@ -0,0 +1,208 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2005,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_MMAN_H +#include +#endif +#include +#include +#include "pagesize.h" +#include "vmcircbuf_createfilemapping.h" +#include + +namespace gr { + +#ifdef HAVE_CREATEFILEMAPPING + // Print Windows error (could/should be global?) + static void + werror(char *where, DWORD last_error) + { + char buf[1024]; + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + last_error, + 0, // default language + buf, + sizeof(buf)/sizeof(TCHAR), // buffer size + NULL); + fprintf(stderr, "%s: Error %d: %s", where, last_error, buf); + return; + } +#endif + + + vmcircbuf_createfilemapping::vmcircbuf_createfilemapping(int size) + : gr::vmcircbuf(size) + { +#if !defined(HAVE_CREATEFILEMAPPING) + fprintf(stderr, "%s: createfilemapping is not available\n", __FUNCTION__); + throw std::runtime_error("gr::vmcircbuf_createfilemapping"); +#else + gr::thread::scoped_lock guard(s_vm_mutex); + + static int s_seg_counter = 0; + + if(size <= 0 || (size % gr::pagesize ()) != 0) { + fprintf(stderr, "gr::vmcircbuf_createfilemapping: invalid size = %d\n", size); + throw std::runtime_error ("gr::vmcircbuf_createfilemapping"); + } + + std::string seg_name = str(boost::format("/gnuradio-%d-%d") % getpid() % s_seg_counter); + + d_handle = CreateFileMapping(INVALID_HANDLE_VALUE, // use paging file + NULL, // default security + PAGE_READWRITE, // read/write access + 0, // max. object size + size, // buffer size + seg_name.c_str()); // name of mapping object + + s_seg_counter++; + if(d_handle == NULL || d_handle == INVALID_HANDLE_VALUE) { + std::string msg = str(boost::format( + "gr::vmcircbuf_mmap_createfilemapping: CreateFileMapping [%s]") % + seg_name); + werror((char*)msg.c_str(), GetLastError()); + throw std::runtime_error("gr::vmcircbuf_mmap_createfilemapping"); + } + + // Allocate virtual memory of the needed size, then free it so we can use it + LPVOID first_tmp; + first_tmp = VirtualAlloc( NULL, 2*size, MEM_RESERVE, PAGE_NOACCESS ); + if(first_tmp == NULL) { + werror("gr::vmcircbuf_mmap_createfilemapping: VirtualAlloc", GetLastError()); + CloseHandle(d_handle); // cleanup + throw std::runtime_error("gr::vmcircbuf_mmap_createfilemapping"); + } + + if(VirtualFree(first_tmp, 0, MEM_RELEASE) == 0) { + werror("gr::vmcircbuf_mmap_createfilemapping: VirtualFree", GetLastError()); + CloseHandle(d_handle); // cleanup + throw std::runtime_error("gr::vmcircbuf_mmap_createfilemapping"); + } + + d_first_copy = MapViewOfFileEx((HANDLE)d_handle, // handle to map object + FILE_MAP_WRITE, // read/write permission + 0, + 0, + size, + first_tmp); + if(d_first_copy != first_tmp) { + werror( "gr::vmcircbuf_mmap_createfilemapping: MapViewOfFileEx(1)", GetLastError()); + CloseHandle(d_handle); // cleanup + throw std::runtime_error ("gr::vmcircbuf_mmap_createfilemapping"); + } + + d_second_copy = MapViewOfFileEx((HANDLE)d_handle, // handle to map object + FILE_MAP_WRITE, // read/write permission + 0, + 0, + size, + (char*)first_tmp + size);//(LPVOID) ((char *)d_first_copy + size)); + + if(d_second_copy != (char *)first_tmp + size) { + werror( "gr::vmcircbuf_mmap_createfilemapping: MapViewOfFileEx(2)", GetLastError()); + UnmapViewOfFile(d_first_copy); + CloseHandle(d_handle); // cleanup + throw std::runtime_error ("gr::vmcircbuf_mmap_createfilemapping"); + } + +#ifdef DEBUG + fprintf(stderr,"gr::vmcircbuf_mmap_createfilemapping: contiguous? mmap %p %p %p %p\n", + (char*)d_first_copy, (char*)d_second_copy, size, (char*)d_first_copy + size); +#endif + + // Now remember the important stuff + d_base = (char*)d_first_copy; + d_size = size; +#endif /*HAVE_CREATEFILEMAPPING*/ + } + + vmcircbuf_createfilemapping::~vmcircbuf_createfilemapping() + { +#ifdef HAVE_CREATEFILEMAPPING + gr::thread::scoped_lock guard(s_vm_mutex); + + if(UnmapViewOfFile(d_first_copy) == 0) { + werror("gr::vmcircbuf_createfilemapping: UnmapViewOfFile(d_first_copy)", GetLastError()); + } + d_base=NULL; + if(UnmapViewOfFile(d_second_copy) == 0) { + werror("gr::vmcircbuf_createfilemapping: UnmapViewOfFile(d_second_copy)", GetLastError()); + } + //d_second=NULL; + CloseHandle(d_handle); +#endif + } + + // ---------------------------------------------------------------- + // The factory interface + // ---------------------------------------------------------------- + + gr::vmcircbuf_factory *vmcircbuf_createfilemapping_factory::s_the_factory = 0; + + gr::vmcircbuf_factory * + vmcircbuf_createfilemapping_factory::singleton() + { + if(s_the_factory) + return s_the_factory; + s_the_factory = new vmcircbuf_createfilemapping_factory(); + return s_the_factory; + } + + int + vmcircbuf_createfilemapping_factory::granularity() + { +#ifdef HAVE_CREATEFILEMAPPING + // return 65536;//TODO, check, is this needed or can we just use gr::pagesize() + SYSTEM_INFO system_info; + GetSystemInfo(&system_info); + //fprintf(stderr,"win32 AllocationGranularity %p\n",(int)system_info.dwAllocationGranularity); + return (int)system_info.dwAllocationGranularity; +#else + return gr::pagesize(); +#endif + } + + gr::vmcircbuf * + vmcircbuf_createfilemapping_factory::make(int size) + { + try { + return new vmcircbuf_createfilemapping(size); + } + catch(...) { + return 0; + } + } + +} /* namespace gr */ diff --git a/lib/runtime/vmcircbuf_createfilemapping.h b/lib/runtime/vmcircbuf_createfilemapping.h new file mode 100644 index 0000000..0513112 --- /dev/null +++ b/lib/runtime/vmcircbuf_createfilemapping.h @@ -0,0 +1,81 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2005,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_VMCIRCBUF_CREATEFILEMAPPING_H +#define GR_VMCIRCBUF_CREATEFILEMAPPING_H + +#include +#include "vmcircbuf.h" + +#ifdef HAVE_CREATEFILEMAPPING +#include +#endif + +namespace gr { + + /*! + * \brief concrete class to implement circular buffers with mmap and shm_open + * \ingroup internal + */ + class GR_RUNTIME_API vmcircbuf_createfilemapping : public gr::vmcircbuf + { + public: + // CREATORS + vmcircbuf_createfilemapping(int size); + virtual ~vmcircbuf_createfilemapping(); +#ifdef HAVE_CREATEFILEMAPPING + private: + HANDLE d_handle; + LPVOID d_first_copy; + LPVOID d_second_copy; +#endif + }; + + /*! + * \brief concrete factory for circular buffers built using mmap and shm_open + */ + class GR_RUNTIME_API vmcircbuf_createfilemapping_factory : public gr::vmcircbuf_factory + { + private: + static gr::vmcircbuf_factory *s_the_factory; + + public: + static gr::vmcircbuf_factory *singleton(); + + virtual const char *name() const { return "gr::vmcircbuf_createfilemapping_factory"; } + + /*! + * \brief return granularity of mapping, typically equal to page size + */ + virtual int granularity(); + + /*! + * \brief return a gr::vmcircbuf, or 0 if unable. + * + * Call this to create a doubly mapped circular buffer. + */ + virtual gr::vmcircbuf *make(int size); + }; + +} /* namespace gr */ + +#endif /* GR_VMCIRCBUF_CREATEFILEMAPPING_H */ diff --git a/lib/runtime/vmcircbuf_mmap_shm_open.cc b/lib/runtime/vmcircbuf_mmap_shm_open.cc new file mode 100644 index 0000000..919986c --- /dev/null +++ b/lib/runtime/vmcircbuf_mmap_shm_open.cc @@ -0,0 +1,208 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vmcircbuf_mmap_shm_open.h" +#include +#include +#include +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_MMAN_H +#include +#endif +#include +#include +#include "pagesize.h" +#include + +namespace gr { + + vmcircbuf_mmap_shm_open::vmcircbuf_mmap_shm_open(int size) + : gr::vmcircbuf(size) + { +#if !defined(HAVE_MMAP) || !defined(HAVE_SHM_OPEN) + fprintf(stderr, "gr::vmcircbuf_mmap_shm_open: mmap or shm_open is not available\n"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); +#else + gr::thread::scoped_lock guard(s_vm_mutex); + + static int s_seg_counter = 0; + + if(size <= 0 || (size % gr::pagesize ()) != 0) { + fprintf(stderr, "gr::vmcircbuf_mmap_shm_open: invalid size = %d\n", size); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + + int shm_fd = -1; + char seg_name[1024]; + static bool portable_format = true; + + // open a new named shared memory segment + while(1) { + if(portable_format) { + + // This is the POSIX recommended "portable format". + // Of course the "portable format" doesn't work on some systems... + + snprintf(seg_name, sizeof(seg_name), + "/gnuradio-%d-%d", getpid(), s_seg_counter); + } + else { + + // Where the "portable format" doesn't work, we try building + // a full filesystem pathname pointing into a suitable temporary directory. + + snprintf(seg_name, sizeof(seg_name), + "%s/gnuradio-%d-%d", gr::tmp_path(), getpid(), s_seg_counter); + } + + shm_fd = shm_open(seg_name, O_RDWR | O_CREAT | O_EXCL, 0600); + if(shm_fd == -1 && errno == EACCES && portable_format) { + portable_format = false; + continue; // try again using "non-portable format" + } + + s_seg_counter++; + + if(shm_fd == -1) { + if(errno == EEXIST) // Named segment already exists (shouldn't happen). Try again + continue; + + char msg[1024]; + snprintf(msg, sizeof(msg), "gr::vmcircbuf_mmap_shm_open: shm_open [%s]", seg_name); + perror(msg); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + break; + } + + // We've got a new shared memory segment fd open. + // Now set it's length to 2x what we really want and mmap it in. + if(ftruncate(shm_fd, (off_t)2 * size) == -1) { + close(shm_fd); // cleanup + perror("gr::vmcircbuf_mmap_shm_open: ftruncate (1)"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + + void *first_copy = mmap(0, 2 * size, + PROT_READ | PROT_WRITE, MAP_SHARED, + shm_fd, (off_t) 0); + + if(first_copy == MAP_FAILED) { + close(shm_fd); // cleanup + perror("gr::vmcircbuf_mmap_shm_open: mmap (1)"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + + // unmap the 2nd half + if(munmap ((char *) first_copy + size, size) == -1) { + close(shm_fd); // cleanup + perror("gr::vmcircbuf_mmap_shm_open: munmap (1)"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + + // map the first half into the now available hole where the + // second half used to be. + void *second_copy = mmap((char*)first_copy + size, size, + PROT_READ | PROT_WRITE, MAP_SHARED, + shm_fd, (off_t)0); + + if(second_copy == MAP_FAILED) { + close(shm_fd); // cleanup + perror("gr::vmcircbuf_mmap_shm_open: mmap (2)"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + +#if 0 // OS/X doesn't allow you to resize the segment + + // cut the shared memory segment down to size + if(ftruncate(shm_fd, (off_t)size) == -1) { + close(shm_fd); // cleanup + perror("gr::vmcircbuf_mmap_shm_open: ftruncate (2)"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } +#endif + + close(shm_fd); // fd no longer needed. The mapping is retained. + + if(shm_unlink(seg_name) == -1) { // unlink the seg_name. + perror("gr::vmcircbuf_mmap_shm_open: shm_unlink"); + throw std::runtime_error("gr::vmcircbuf_mmap_shm_open"); + } + + // Now remember the important stuff + d_base = (char*)first_copy; + d_size = size; +#endif + } + + vmcircbuf_mmap_shm_open::~vmcircbuf_mmap_shm_open() + { +#if defined(HAVE_MMAP) + gr::thread::scoped_lock guard(s_vm_mutex); + + if(munmap (d_base, 2 * d_size) == -1) { + perror("gr::vmcircbuf_mmap_shm_open: munmap (2)"); + } +#endif + } + + // ---------------------------------------------------------------- + // The factory interface + // ---------------------------------------------------------------- + + gr::vmcircbuf_factory *vmcircbuf_mmap_shm_open_factory::s_the_factory = 0; + + gr::vmcircbuf_factory * + vmcircbuf_mmap_shm_open_factory::singleton() + { + if(s_the_factory) + return s_the_factory; + + s_the_factory = new gr::vmcircbuf_mmap_shm_open_factory(); + return s_the_factory; + } + + int + vmcircbuf_mmap_shm_open_factory::granularity() + { + return gr::pagesize(); + } + + gr::vmcircbuf * + vmcircbuf_mmap_shm_open_factory::make(int size) + { + try { + return new vmcircbuf_mmap_shm_open(size); + } + catch (...) { + return 0; + } + } + +} /* namespace gr */ diff --git a/lib/runtime/vmcircbuf_mmap_shm_open.h b/lib/runtime/vmcircbuf_mmap_shm_open.h new file mode 100644 index 0000000..60654ee --- /dev/null +++ b/lib/runtime/vmcircbuf_mmap_shm_open.h @@ -0,0 +1,70 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_VMCIRCBUF_MMAP_SHM_OPEN_H +#define GR_VMCIRCBUF_MMAP_SHM_OPEN_H + +#include +#include "vmcircbuf.h" + +namespace gr { + + /*! + * \brief concrete class to implement circular buffers with mmap and shm_open + * \ingroup internal + */ + class GR_RUNTIME_API vmcircbuf_mmap_shm_open : public gr::vmcircbuf + { + public: + vmcircbuf_mmap_shm_open(int size); + virtual ~vmcircbuf_mmap_shm_open(); + }; + + /*! + * \brief concrete factory for circular buffers built using mmap and shm_open + */ + class GR_RUNTIME_API vmcircbuf_mmap_shm_open_factory : public gr::vmcircbuf_factory + { + private: + static gr::vmcircbuf_factory *s_the_factory; + + public: + static gr::vmcircbuf_factory *singleton(); + + virtual const char *name() const { return "gr::vmcircbuf_mmap_shm_open_factory"; } + + /*! + * \brief return granularity of mapping, typically equal to page size + */ + virtual int granularity(); + + /*! + * \brief return a gr::vmcircbuf, or 0 if unable. + * + * Call this to create a doubly mapped circular buffer. + */ + virtual gr::vmcircbuf *make(int size); + }; + +} /* namespace gr */ + +#endif /* GR_VMCIRCBUF_MMAP_SHM_OPEN_H */ diff --git a/lib/runtime/vmcircbuf_mmap_tmpfile.cc b/lib/runtime/vmcircbuf_mmap_tmpfile.cc new file mode 100644 index 0000000..7a02fe2 --- /dev/null +++ b/lib/runtime/vmcircbuf_mmap_tmpfile.cc @@ -0,0 +1,200 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vmcircbuf_mmap_tmpfile.h" +#include +#include +#include +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_MMAN_H +#include +#endif +#include +#include +#include +#include +#include "pagesize.h" +#include + +namespace gr { + + vmcircbuf_mmap_tmpfile::vmcircbuf_mmap_tmpfile (int size) + : gr::vmcircbuf (size) + { +#if !defined(HAVE_MMAP) + fprintf(stderr, "gr::vmcircbuf_mmap_tmpfile: mmap or mkstemp is not available\n"); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); +#else + gr::thread::scoped_lock guard(s_vm_mutex); + + if(size <= 0 || (size % gr::pagesize ()) != 0) { + fprintf(stderr, "gr::vmcircbuf_mmap_tmpfile: invalid size = %d\n", size); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + + int seg_fd = -1; + char seg_name[1024]; + + static int s_seg_counter = 0; + + // open a temporary file that we'll map in a bit later + while(1) { + snprintf(seg_name, sizeof(seg_name), + "%s/gnuradio-%d-%d-XXXXXX", gr::tmp_path(), getpid(), s_seg_counter); + s_seg_counter++; + + seg_fd = open(seg_name, O_RDWR | O_CREAT | O_EXCL, 0600); + if(seg_fd == -1) { + if(errno == EEXIST) // File already exists (shouldn't happen). Try again + continue; + + char msg[1024]; + snprintf(msg, sizeof (msg), + "gr::vmcircbuf_mmap_tmpfile: open [%s]", seg_name); + perror(msg); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + break; + } + + if(unlink (seg_name) == -1) { + perror("gr::vmcircbuf_mmap_tmpfile: unlink"); + throw std::runtime_error ("gr::vmcircbuf_mmap_tmpfile"); + } + + // We've got a valid file descriptor to a tmp file. + // Now set it's length to 2x what we really want and mmap it in. + if(ftruncate (seg_fd, (off_t) 2 * size) == -1) { + close(seg_fd); // cleanup + perror("gr::vmcircbuf_mmap_tmpfile: ftruncate (1)"); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + + void *first_copy = mmap(0, 2 * size, + PROT_READ | PROT_WRITE, MAP_SHARED, + seg_fd, (off_t)0); + + if(first_copy == MAP_FAILED) { + close(seg_fd); // cleanup + perror("gr::vmcircbuf_mmap_tmpfile: mmap (1)"); + throw std::runtime_error ("gr::vmcircbuf_mmap_tmpfile"); + } + + // unmap the 2nd half + if(munmap ((char *) first_copy + size, size) == -1) { + close(seg_fd); // cleanup + perror("gr::vmcircbuf_mmap_tmpfile: munmap (1)"); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + + // map the first half into the now available hole where the + // second half used to be. + void *second_copy = mmap((char*)first_copy + size, size, + PROT_READ | PROT_WRITE, MAP_SHARED, + seg_fd, (off_t)0); + + if(second_copy == MAP_FAILED) { + munmap(first_copy, size); // cleanup + close(seg_fd); + perror("gr::vmcircbuf_mmap_tmpfile: mmap(2)"); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + + // check for contiguity + if((char*)second_copy != (char*)first_copy + size) { + munmap(first_copy, size); // cleanup + munmap(second_copy, size); + close(seg_fd); + perror("gr::vmcircbuf_mmap_tmpfile: non-contiguous second copy"); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + + // cut the tmp file down to size + if(ftruncate (seg_fd, (off_t) size) == -1) { + munmap(first_copy, size); // cleanup + munmap(second_copy, size); + close(seg_fd); + perror("gr::vmcircbuf_mmap_tmpfile: ftruncate (2)"); + throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile"); + } + + close(seg_fd); // fd no longer needed. The mapping is retained. + + // Now remember the important stuff + + d_base = (char*)first_copy; + d_size = size; +#endif + } + + vmcircbuf_mmap_tmpfile::~vmcircbuf_mmap_tmpfile() + { +#if defined(HAVE_MMAP) + gr::thread::scoped_lock guard(s_vm_mutex); + + if(munmap(d_base, 2 * d_size) == -1) { + perror("gr::vmcircbuf_mmap_tmpfile: munmap(2)"); + } +#endif + } + + // ---------------------------------------------------------------- + // The factory interface + // ---------------------------------------------------------------- + + gr::vmcircbuf_factory *vmcircbuf_mmap_tmpfile_factory::s_the_factory = 0; + + gr::vmcircbuf_factory * + vmcircbuf_mmap_tmpfile_factory::singleton() + { + if(s_the_factory) + return s_the_factory; + + s_the_factory = new gr::vmcircbuf_mmap_tmpfile_factory(); + return s_the_factory; + } + + int + vmcircbuf_mmap_tmpfile_factory::granularity() + { + return gr::pagesize(); + } + + gr::vmcircbuf * + vmcircbuf_mmap_tmpfile_factory::make(int size) + { + try { + return new vmcircbuf_mmap_tmpfile(size); + } + catch (...) { + return 0; + } + } + +} /* namespace gr */ diff --git a/lib/runtime/vmcircbuf_mmap_tmpfile.h b/lib/runtime/vmcircbuf_mmap_tmpfile.h new file mode 100644 index 0000000..f8959c5 --- /dev/null +++ b/lib/runtime/vmcircbuf_mmap_tmpfile.h @@ -0,0 +1,70 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_VMCIRCBUF_MMAP_TMPFILE_H +#define GR_VMCIRCBUF_MMAP_TMPFILE_H + +#include +#include "vmcircbuf.h" + +namespace gr { + + /*! + * \brief concrete class to implement circular buffers with mmap and shm_open + * \ingroup internal + */ + class GR_RUNTIME_API vmcircbuf_mmap_tmpfile : public gr::vmcircbuf + { + public: + vmcircbuf_mmap_tmpfile(int size); + virtual ~vmcircbuf_mmap_tmpfile(); + }; + + /*! + * \brief concrete factory for circular buffers built using mmap and shm_open + */ + class GR_RUNTIME_API vmcircbuf_mmap_tmpfile_factory : public gr::vmcircbuf_factory + { + private: + static gr::vmcircbuf_factory *s_the_factory; + + public: + static gr::vmcircbuf_factory *singleton(); + + virtual const char *name() const { return "gr::vmcircbuf_mmap_tmpfile_factory"; } + + /*! + * \brief return granularity of mapping, typically equal to page size + */ + virtual int granularity(); + + /*! + * \brief return a gr::vmcircbuf, or 0 if unable. + * + * Call this to create a doubly mapped circular buffer. + */ + virtual gr::vmcircbuf *make(int size); + }; + +} /* namespace gr */ + +#endif /* GR_VMCIRCBUF_MMAP_TMPFILE_H */ diff --git a/lib/runtime/vmcircbuf_prefs.cc b/lib/runtime/vmcircbuf_prefs.cc new file mode 100644 index 0000000..258605c --- /dev/null +++ b/lib/runtime/vmcircbuf_prefs.cc @@ -0,0 +1,118 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2010,2011,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vmcircbuf_prefs.h" +#include "vmcircbuf.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +namespace fs = boost::filesystem; + +namespace gr { + + /* + * The simplest thing that could possibly work: + * the key is the filename; the value is the file contents. + */ + static const char * + pathname(const char *key) + { + static fs::path path; + path = fs::path(gr::appdata_path()) / ".gnuradio" / "prefs" / key; + return path.string().c_str(); + } + + static void + ensure_dir_path() + { + fs::path path = fs::path(gr::appdata_path()) / ".gnuradio"; + if(!fs::is_directory(path)) + fs::create_directory(path); + + path = path / "prefs"; + if(!fs::is_directory(path)) + fs::create_directory(path); + } + + const char * + vmcircbuf_prefs::get(const char *key) + { + static char buf[1024]; + + gr::thread::scoped_lock guard(s_vm_mutex); + + FILE *fp = fopen(pathname (key), "r"); + if(fp == 0) { + perror(pathname (key)); + return 0; + } + + memset(buf, 0, sizeof (buf)); + size_t ret = fread(buf, 1, sizeof(buf) - 1, fp); + if(ret == 0) { + if(ferror(fp) != 0) { + perror(pathname (key)); + fclose(fp); + return 0; + } + } + fclose(fp); + return buf; + } + + void + vmcircbuf_prefs::set(const char *key, const char *value) + { + gr::thread::scoped_lock guard(s_vm_mutex); + + ensure_dir_path(); + + FILE *fp = fopen(pathname(key), "w"); + if(fp == 0) { + perror(pathname (key)); + return; + } + + size_t ret = fwrite(value, 1, strlen(value), fp); + if(ret == 0) { + if(ferror(fp) != 0) { + perror(pathname (key)); + fclose(fp); + return; + } + } + fclose(fp); + }; + +} /* namespace gr */ diff --git a/lib/runtime/vmcircbuf_prefs.h b/lib/runtime/vmcircbuf_prefs.h new file mode 100644 index 0000000..709b8ff --- /dev/null +++ b/lib/runtime/vmcircbuf_prefs.h @@ -0,0 +1,39 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_PREFERENCES_H +#define GR_PREFERENCES_H + +#include + +namespace gr { + + class GR_RUNTIME_API vmcircbuf_prefs + { + public: + static const char *get(const char *key); + static void set(const char *key, const char *value); + }; + +} /* namespace gr */ + +#endif /* GR_PREFERENCES_H */ diff --git a/lib/runtime/vmcircbuf_sysv_shm.cc b/lib/runtime/vmcircbuf_sysv_shm.cc new file mode 100644 index 0000000..4d87418 --- /dev/null +++ b/lib/runtime/vmcircbuf_sysv_shm.cc @@ -0,0 +1,210 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vmcircbuf_sysv_shm.h" +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_IPC_H +#include +#endif +#ifdef HAVE_SYS_SHM_H +#include +#endif +#include +#include +#include "pagesize.h" + +#define MAX_SYSV_SHM_ATTEMPTS 3 + +namespace gr { + + vmcircbuf_sysv_shm::vmcircbuf_sysv_shm(int size) + : gr::vmcircbuf(size) + { +#if !defined(HAVE_SYS_SHM_H) + fprintf(stderr, "gr::vmcircbuf_sysv_shm: sysv shared memory is not available\n"); + throw std::runtime_error("gr::vmcircbuf_sysv_shm"); +#else + + gr::thread::scoped_lock guard(s_vm_mutex); + + int pagesize = gr::pagesize(); + + if(size <= 0 || (size % pagesize) != 0) { + fprintf(stderr, "gr::vmcircbuf_sysv_shm: invalid size = %d\n", size); + throw std::runtime_error("gr::vmcircbuf_sysv_shm"); + } + + // Attempt to allocate buffers (handle bad_alloc errors) + int attempts_remain(MAX_SYSV_SHM_ATTEMPTS); + while(attempts_remain-- > 0){ + + int shmid_guard = -1; + int shmid1 = -1; + int shmid2 = -1; + + // We use this as a guard page. We'll map it read-only on both ends of the buffer. + // Ideally we'd map it no access, but I don't think that's possible with SysV + if((shmid_guard = shmget(IPC_PRIVATE, pagesize, IPC_CREAT | 0400)) == -1) { + perror("gr::vmcircbuf_sysv_shm: shmget (0)"); + continue; + } + + if((shmid2 = shmget(IPC_PRIVATE, 2 * size + 2 * pagesize, IPC_CREAT | 0700)) == -1) { + perror("gr::vmcircbuf_sysv_shm: shmget(1)"); + shmctl(shmid_guard, IPC_RMID, 0); + continue; + } + + if((shmid1 = shmget(IPC_PRIVATE, size, IPC_CREAT | 0700)) == -1) { + perror("gr::vmcircbuf_sysv_shm: shmget (2)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid2, IPC_RMID, 0); + continue; + } + + void *first_copy = shmat (shmid2, 0, 0); + if(first_copy == (void *) -1) { + perror("gr::vmcircbuf_sysv_shm: shmat(1)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid2, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + continue; + } + + shmctl(shmid2, IPC_RMID, 0); + + // There may be a race between our detach and attach. + // + // If the system allocates all shared memory segments at the same + // virtual addresses in all processes and if the system allocates + // some other segment to first_copy or first_copoy + size between + // our detach and attach, the attaches below could fail [I've never + // seen it fail for this reason]. + shmdt(first_copy); + + // first read-only guard page + if(shmat(shmid_guard, first_copy, SHM_RDONLY) == (void *) -1) { + perror("gr::vmcircbuf_sysv_shm: shmat(2)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + continue; + } + + // first copy + if(shmat (shmid1, (char*)first_copy + pagesize, 0) == (void *) -1) { + perror("gr::vmcircbuf_sysv_shm: shmat (3)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + shmdt(first_copy); + continue; + } + + // second copy + if(shmat (shmid1, (char*)first_copy + pagesize + size, 0) == (void *) -1) { + perror("gr::vmcircbuf_sysv_shm: shmat (4)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + shmdt((char *)first_copy + pagesize); + continue; + } + + // second read-only guard page + if(shmat(shmid_guard, (char*)first_copy + pagesize + 2 * size, SHM_RDONLY) == (void *) -1) { + perror("gr::vmcircbuf_sysv_shm: shmat(5)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + shmdt(first_copy); + shmdt((char *)first_copy + pagesize); + shmdt((char *)first_copy + pagesize + size); + continue; + } + + shmctl(shmid1, IPC_RMID, 0); + shmctl(shmid_guard, IPC_RMID, 0); + + // Now remember the important stuff + d_base = (char*)first_copy + pagesize; + d_size = size; + break; + } + if(attempts_remain<0){ + throw std::runtime_error("gr_vmcircbuf_sysv_shm"); + } +#endif + } + + vmcircbuf_sysv_shm::~vmcircbuf_sysv_shm() + { +#if defined(HAVE_SYS_SHM_H) + gr::thread::scoped_lock guard(s_vm_mutex); + + if(shmdt(d_base - gr::pagesize()) == -1 + || shmdt(d_base) == -1 + || shmdt(d_base + d_size) == -1 + || shmdt(d_base + 2 * d_size) == -1){ + perror("gr::vmcircbuf_sysv_shm: shmdt(2)"); + } +#endif + } + + // ---------------------------------------------------------------- + // The factory interface + // ---------------------------------------------------------------- + + gr::vmcircbuf_factory *vmcircbuf_sysv_shm_factory::s_the_factory = 0; + + gr::vmcircbuf_factory * + vmcircbuf_sysv_shm_factory::singleton() + { + if(s_the_factory) + return s_the_factory; + + s_the_factory = new gr::vmcircbuf_sysv_shm_factory(); + return s_the_factory; + } + + int + vmcircbuf_sysv_shm_factory::granularity() + { + return gr::pagesize(); + } + + gr::vmcircbuf * + vmcircbuf_sysv_shm_factory::make(int size) + { + try { + return new vmcircbuf_sysv_shm(size); + } + catch (...) { + return 0; + } + } + +} /* namespace gr */ diff --git a/lib/runtime/vmcircbuf_sysv_shm.h b/lib/runtime/vmcircbuf_sysv_shm.h new file mode 100644 index 0000000..08e0040 --- /dev/null +++ b/lib/runtime/vmcircbuf_sysv_shm.h @@ -0,0 +1,70 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef GR_VMCIRCBUF_SYSV_SHM_H +#define GR_VMCIRCBUF_SYSV_SHM_H + +#include +#include "vmcircbuf.h" + +namespace gr { + + /*! + * \brief concrete class to implement circular buffers with mmap and shm_open + * \ingroup internal + */ + class GR_RUNTIME_API vmcircbuf_sysv_shm : public gr::vmcircbuf + { + public: + vmcircbuf_sysv_shm(int size); + virtual ~vmcircbuf_sysv_shm(); + }; + + /*! + * \brief concrete factory for circular buffers built using mmap and shm_open + */ + class GR_RUNTIME_API vmcircbuf_sysv_shm_factory : public gr::vmcircbuf_factory + { + private: + static gr::vmcircbuf_factory *s_the_factory; + + public: + static gr::vmcircbuf_factory *singleton(); + + virtual const char *name() const { return "gr::vmcircbuf_sysv_shm_factory"; } + + /*! + * \brief return granularity of mapping, typically equal to page size + */ + virtual int granularity(); + + /*! + * \brief return a gr::vmcircbuf, or 0 if unable. + * + * Call this to create a doubly mapped circular buffer. + */ + virtual gr::vmcircbuf *make(int size); + }; + +} /* namespace gr */ + +#endif /* GR_VMCIRCBUF_SYSV_SHM_H */