From 0a49a8844a65698b11fe979441a97939dad80044 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Wed, 27 Nov 2019 21:36:46 -0800 Subject: rfnoc: Create mock factory This is an API that allows creating mock block controllers, to write unit tests for block controllers. See rfnoc_blocks_test for an example how to use them. --- host/include/uhd/rfnoc/mock_block.hpp | 193 ++++++++++++++++++++++++++++++++++ host/lib/rfnoc/CMakeLists.txt | 1 + host/lib/rfnoc/mock_block.cpp | 52 +++++++++ host/lib/rfnoc/registry_factory.cpp | 8 +- host/tests/CMakeLists.txt | 1 - host/tests/client_zero_test.cpp | 3 +- host/tests/rfnoc_blocks_test.cpp | 70 +++--------- host/tests/rfnoc_mock_reg_iface.hpp | 138 ------------------------ 8 files changed, 266 insertions(+), 200 deletions(-) create mode 100644 host/include/uhd/rfnoc/mock_block.hpp create mode 100644 host/lib/rfnoc/mock_block.cpp delete mode 100644 host/tests/rfnoc_mock_reg_iface.hpp diff --git a/host/include/uhd/rfnoc/mock_block.hpp b/host/include/uhd/rfnoc/mock_block.hpp new file mode 100644 index 000000000..18c614d07 --- /dev/null +++ b/host/include/uhd/rfnoc/mock_block.hpp @@ -0,0 +1,193 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_MOCK_BLOCK_HPP +#define INCLUDED_LIBUHD_MOCK_BLOCK_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace uhd { namespace rfnoc { + +/*! Mock version of a register interface + * + * This can be used for mock blocks, usually for the sake of unit testing. + */ +class UHD_API mock_reg_iface_t : public register_iface +{ +public: + mock_reg_iface_t() = default; + virtual ~mock_reg_iface_t() = default; + + /************************************************************************** + * API + *************************************************************************/ + void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack) + { + write_memory[addr] = data; + _poke_cb(addr, data, time, ack); + } + + void multi_poke32(const std::vector addrs, + const std::vector data, + uhd::time_spec_t time, + bool ack) + { + if (addrs.size() != data.size()) { + throw uhd::value_error("addrs and data vectors must be of the same length"); + } + for (size_t i = 0; i < addrs.size(); i++) { + poke32(addrs[i], data[i], time, ack); + } + } + + void block_poke32(uint32_t first_addr, + const std::vector data, + uhd::time_spec_t timestamp, + bool ack) + { + for (size_t i = 0; i < data.size(); i++) { + poke32(first_addr + 4 * i, data[i], timestamp, ack); + } + } + + uint32_t peek32(uint32_t addr, uhd::time_spec_t time) + { + _peek_cb(addr, time); + try { + return read_memory.at(addr); + } catch (const std::out_of_range&) { + throw uhd::runtime_error( + str(boost::format("No data defined for address: 0x%04X") % addr)); + } + } + + std::vector block_peek32( + uint32_t first_addr, size_t length, uhd::time_spec_t time) + { + std::vector result(length, 0); + for (size_t i = 0; i < length; ++i) { + result[i] = peek32(first_addr + i * 4, time); + } + return result; + } + + void poll32(uint32_t addr, + uint32_t data, + uint32_t mask, + uhd::time_spec_t /* timeout */, + uhd::time_spec_t time = uhd::time_spec_t::ASAP, + bool /* ack */ = false) + { + if (force_timeout) { + throw uhd::op_timeout("timeout"); + } + + if ((peek32(addr, time) & mask) == data) { + UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() successful at addr " << addr); + } else { + UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() not successful at addr " << addr); + } + } + + void sleep(uhd::time_spec_t /*duration*/, bool /*ack*/) + { + // nop + } + + void register_async_msg_validator(async_msg_validator_t /*callback_f*/) + { + // nop + } + + void register_async_msg_handler(async_msg_callback_t /*callback_f*/) + { + // nop + } + + void set_policy(const std::string& name, const uhd::device_addr_t& args) + { + UHD_LOG_INFO("MOCK_REG_IFACE", + "Requested to set policy for " << name << " to " << args.to_string()); + } + + uint16_t get_src_epid() const + { + return 0; + } + + uint16_t get_port_num() const + { + return 0; + } + + bool force_timeout = false; + + //! All pokes end up writing to this map + std::unordered_map read_memory; + //! All peeks read from this map. A peek will fail if the address has not + // been previously set. + std::unordered_map write_memory; + +protected: + virtual void _poke_cb( + uint32_t /*addr*/, uint32_t /*data*/, uhd::time_spec_t /*time*/, bool /*ack*/) + { + } + + virtual void _peek_cb(uint32_t /*addr*/, uhd::time_spec_t /*time*/) {} +}; + +/*! Container for all the items required for running a mock block + */ +struct UHD_API mock_block_container +{ + friend class get_mock_block; + //! Reference to the register interface object + std::shared_ptr reg_iface; + + //! Reference to the prop tree object the block sees + uhd::property_tree::sptr tree; + + //! Use this to retrieve a reference to the block controller. Make sure that + // the register space is appropiately primed before doing so. + template + std::shared_ptr get_block() + { + return std::dynamic_pointer_cast(factory(std::move(make_args))); + } + + //! Factory to get the block. Use get_block() instead. + std::function factory; + + // Note: The make args would ideally be captured by the factory function, + // but std::functions need to be CopyConstructible, and this struct doesn't, + // so it needs to live out here in the open. + noc_block_base::make_args_ptr make_args; + +}; + +/*! Factory function for mock block controllers + */ +UHD_API mock_block_container get_mock_block(const noc_id_t noc_id, + const size_t num_inputs = 1, + const size_t num_outputs = 1, + const uhd::device_addr_t& args = uhd::device_addr_t(), + const size_t mtu = 8000, + const device_type_t device_id = ANY_DEVICE); + + +}}; // namespace uhd::rfnoc + +#endif /* INCLUDED_LIBUHD_MOCK_BLOCK_HPP */ diff --git a/host/lib/rfnoc/CMakeLists.txt b/host/lib/rfnoc/CMakeLists.txt index e685cef26..ea774b2c8 100644 --- a/host/lib/rfnoc/CMakeLists.txt +++ b/host/lib/rfnoc/CMakeLists.txt @@ -38,6 +38,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/rfnoc_rx_streamer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rfnoc_tx_streamer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tx_async_msg_queue.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_block.cpp # Default block control classes: ${CMAKE_CURRENT_SOURCE_DIR}/block_control.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ddc_block_control.cpp diff --git a/host/lib/rfnoc/mock_block.cpp b/host/lib/rfnoc/mock_block.cpp new file mode 100644 index 000000000..867311097 --- /dev/null +++ b/host/lib/rfnoc/mock_block.cpp @@ -0,0 +1,52 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include +#include +#include +#include +#include +#include + +using namespace uhd::rfnoc; + +uhd::rfnoc::mock_block_container uhd::rfnoc::get_mock_block(const noc_id_t noc_id, + const size_t num_inputs, + const size_t num_outputs, + const device_addr_t& args, + const size_t mtu, + const device_type_t device_id) +{ + block_factory_info_t fac_info = factory::get_block_factory(noc_id, device_id); + + mock_block_container ret_val; + ret_val.factory = fac_info.factory_fn; + ret_val.reg_iface = std::make_shared(); + ret_val.tree = uhd::property_tree::make(); + // Create make args + ret_val.make_args = std::make_unique(); + ret_val.make_args->noc_id = noc_id; + ret_val.make_args->block_id = block_id_t(fac_info.block_name); + ret_val.make_args->num_input_ports = num_inputs; + ret_val.make_args->num_output_ports = num_outputs; + ret_val.make_args->mtu = mtu; + ret_val.make_args->reg_iface = ret_val.reg_iface; + ret_val.make_args->tree = ret_val.tree; + ret_val.make_args->args = args; + ret_val.make_args->tb_clk_iface = + std::make_shared(fac_info.timebase_clk); + ret_val.make_args->ctrlport_clk_iface = + std::make_shared(fac_info.ctrlport_clk); + // TODO Make a mock mb controller too + ret_val.make_args->mb_control = nullptr; + if (fac_info.mb_access) { + UHD_LOG_WARNING("MOCK", "Mock block controllers cannot have mb_controllers."); + } + + // Make block and return + return ret_val; +} + diff --git a/host/lib/rfnoc/registry_factory.cpp b/host/lib/rfnoc/registry_factory.cpp index bf1bc60a5..b251410bf 100644 --- a/host/lib/rfnoc/registry_factory.cpp +++ b/host/lib/rfnoc/registry_factory.cpp @@ -5,13 +5,14 @@ // #include -#include -#include #include +#include +#include +#include #include #include -#include #include +#include #include #include #include @@ -106,3 +107,4 @@ block_factory_info_t factory::get_block_factory(noc_id_t noc_id, device_type_t d } return get_direct_block_registry().at(key); } + diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 017778fb8..bf47f6638 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -245,7 +245,6 @@ UHD_ADD_NONAPI_TEST( TARGET rfnoc_blocks_test.cpp EXTRA_SOURCES ${CMAKE_SOURCE_DIR}/lib/rfnoc/graph.cpp - ${CMAKE_SOURCE_DIR}/lib/rfnoc/registry_factory.cpp ) UHD_ADD_NONAPI_TEST( diff --git a/host/tests/client_zero_test.cpp b/host/tests/client_zero_test.cpp index e97dd83e2..927161418 100644 --- a/host/tests/client_zero_test.cpp +++ b/host/tests/client_zero_test.cpp @@ -5,6 +5,7 @@ // #include +#include #include #include #include @@ -14,8 +15,6 @@ #include #include -#include "rfnoc_mock_reg_iface.hpp" - using namespace uhd; using namespace uhd::rfnoc; diff --git a/host/tests/rfnoc_blocks_test.cpp b/host/tests/rfnoc_blocks_test.cpp index ea1f91b6b..b9899177e 100644 --- a/host/tests/rfnoc_blocks_test.cpp +++ b/host/tests/rfnoc_blocks_test.cpp @@ -5,14 +5,12 @@ // #include "rfnoc_graph_mock_nodes.hpp" -#include "rfnoc_mock_reg_iface.hpp" +#include #include #include #include #include #include -#include -#include #include #include #include @@ -21,47 +19,10 @@ using namespace uhd::rfnoc; -// Redeclare this here, since it's only defined outside of UHD_API -noc_block_base::make_args_t::~make_args_t() = default; - namespace { constexpr size_t DEFAULT_MTU = 8000; -noc_block_base::make_args_ptr make_make_args(noc_id_t noc_id, - const std::string& block_id, - const size_t n_inputs, - const size_t n_outputs, - const std::string& tb_clock_name = CLOCK_KEY_GRAPH, - const std::string& cp_clock_name = "MOCK_CLOCK") -{ - auto make_args = std::make_unique(); - make_args->noc_id = noc_id; - make_args->num_input_ports = n_inputs; - make_args->num_output_ports = n_outputs; - make_args->mtu = DEFAULT_MTU; - make_args->reg_iface = std::make_shared(); - make_args->block_id = block_id; - make_args->ctrlport_clk_iface = std::make_shared(cp_clock_name); - make_args->tb_clk_iface = std::make_shared(tb_clock_name); - make_args->tree = uhd::property_tree::make(); - return make_args; -} - -noc_block_base::sptr make_block(noc_block_base::make_args_ptr&& make_args) -{ - try { - auto block_factory_info = - factory::get_block_factory(make_args->noc_id, ANY_DEVICE); - return block_factory_info.factory_fn(std::move(make_args)); - } catch (std::out_of_range&) { - UHD_LOG_WARNING("TEST", - "Skipping tests due to Windows linker misconfiguration that needs to be " - "resolved."); - exit(0); - } -} - } // namespace BOOST_AUTO_TEST_CASE(test_null_block) @@ -72,17 +33,17 @@ BOOST_AUTO_TEST_CASE(test_null_block) constexpr uint32_t item_width = 32; constexpr noc_id_t noc_id = 0x00000001; - auto make_args = make_make_args(noc_id, "0/NullSrcSink#0", num_chans, num_chans); - auto reg_iface = std::dynamic_pointer_cast(make_args->reg_iface); - auto set_mem = [&](const uint32_t addr, const uint32_t data) { + auto block_container = get_mock_block(noc_id, num_chans, num_chans); + // Shorthand to save typing + auto& reg_iface = block_container.reg_iface; + auto set_mem = [&](const uint32_t addr, const uint32_t data) { reg_iface->read_memory[addr] = data; }; auto get_mem = [&](const uint32_t addr) { return reg_iface->write_memory[addr]; }; auto copy_mem = [&](const uint32_t addr) { set_mem(addr, get_mem(addr)); }; - set_mem(null_block_control::REG_CTRL_STATUS, (nipc << 24) | (item_width << 16)); - auto test_null = - std::dynamic_pointer_cast(make_block(std::move(make_args))); + + auto test_null = block_container.get_block(); BOOST_REQUIRE(test_null); using uhd::stream_cmd_t; @@ -163,15 +124,14 @@ BOOST_AUTO_TEST_CASE(test_ddc_block) constexpr noc_id_t noc_id = DDC_BLOCK; constexpr int TEST_DECIM = 20; - auto ddc_make_args = make_make_args(noc_id, "0/DDC#0", num_chans, num_chans); - ddc_make_args->args = uhd::device_addr_t("foo=bar"); - auto ddc_reg_iface = - std::dynamic_pointer_cast(ddc_make_args->reg_iface); + auto block_container = + get_mock_block(noc_id, num_chans, num_chans, uhd::device_addr_t("foo=bar")); + auto& ddc_reg_iface = block_container.reg_iface; ddc_reg_iface->read_memory[ddc_block_control::RB_COMPAT_NUM] = (ddc_block_control::MAJOR_COMPAT << 16) | ddc_block_control::MINOR_COMPAT; ddc_reg_iface->read_memory[ddc_block_control::RB_NUM_HB] = num_hb; ddc_reg_iface->read_memory[ddc_block_control::RB_CIC_MAX_DECIM] = max_cic; - auto test_ddc = make_block(std::move(ddc_make_args)); + auto test_ddc = block_container.get_block(); BOOST_REQUIRE(test_ddc); BOOST_CHECK_EQUAL(test_ddc->get_block_args().get("foo"), "bar"); @@ -266,15 +226,13 @@ BOOST_AUTO_TEST_CASE(test_duc_block) constexpr noc_id_t noc_id = DUC_BLOCK; constexpr int TEST_INTERP = 20; // 2 halfbands, CIC==5 - auto duc_make_args = make_make_args(noc_id, "0/DUC#0", num_chans, num_chans); - duc_make_args->args = uhd::device_addr_t(); - auto duc_reg_iface = - std::dynamic_pointer_cast(duc_make_args->reg_iface); + auto block_container = get_mock_block(noc_id, num_chans, num_chans); + auto& duc_reg_iface = block_container.reg_iface; duc_reg_iface->read_memory[duc_block_control::RB_COMPAT_NUM] = (duc_block_control::MAJOR_COMPAT << 16) | duc_block_control::MINOR_COMPAT; duc_reg_iface->read_memory[duc_block_control::RB_NUM_HB] = num_hb; duc_reg_iface->read_memory[duc_block_control::RB_CIC_MAX_INTERP] = max_cic; - auto test_duc = make_block(std::move(duc_make_args)); + auto test_duc = block_container.get_block(); BOOST_REQUIRE(test_duc); node_accessor.init_props(test_duc.get()); diff --git a/host/tests/rfnoc_mock_reg_iface.hpp b/host/tests/rfnoc_mock_reg_iface.hpp deleted file mode 100644 index a6e85b790..000000000 --- a/host/tests/rfnoc_mock_reg_iface.hpp +++ /dev/null @@ -1,138 +0,0 @@ -// -// Copyright 2019 Ettus Research, a National Instruments Brand -// -// SPDX-License-Identifier: GPL-3.0-or-later -// - -#ifndef INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP -#define INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP - -#include -#include -#include -#include -#include -#include - -class mock_reg_iface_t : public uhd::rfnoc::register_iface -{ -public: - mock_reg_iface_t() = default; - virtual ~mock_reg_iface_t() = default; - - /************************************************************************** - * API - *************************************************************************/ - void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack) - { - write_memory[addr] = data; - _poke_cb(addr, data, time, ack); - } - - void multi_poke32(const std::vector addrs, - const std::vector data, - uhd::time_spec_t time, - bool ack) - { - if (addrs.size() != data.size()) { - throw uhd::value_error("addrs and data vectors must be of the same length"); - } - for (size_t i = 0; i < addrs.size(); i++) { - poke32(addrs[i], data[i], time, ack); - } - } - - void block_poke32(uint32_t first_addr, - const std::vector data, - uhd::time_spec_t timestamp, - bool ack) - { - for (size_t i = 0; i < data.size(); i++) { - poke32(first_addr + 4 * i, data[i], timestamp, ack); - } - } - - uint32_t peek32(uint32_t addr, uhd::time_spec_t time) - { - _peek_cb(addr, time); - try { - return read_memory.at(addr); - } catch (const std::out_of_range&) { - throw uhd::runtime_error(str(boost::format("No data defined for address: 0x%04X") % addr)); - } - } - - std::vector block_peek32( - uint32_t first_addr, size_t length, uhd::time_spec_t time) - { - std::vector result(length, 0); - for (size_t i = 0; i < length; ++i) { - result[i] = peek32(first_addr + i * 4, time); - } - return result; - } - - void poll32(uint32_t addr, - uint32_t data, - uint32_t mask, - uhd::time_spec_t /* timeout */, - uhd::time_spec_t time = uhd::time_spec_t::ASAP, - bool /* ack */ = false) - { - if (force_timeout) { - throw uhd::op_timeout("timeout"); - } - - if ((peek32(addr, time) & mask) == data) { - UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() successful at addr " << addr); - } else { - UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() not successful at addr " << addr); - } - } - - void sleep(uhd::time_spec_t /*duration*/, bool /*ack*/) - { - // nop - } - - void register_async_msg_validator(async_msg_validator_t /*callback_f*/) - { - // nop - } - - void register_async_msg_handler(async_msg_callback_t /*callback_f*/) - { - // nop - } - - void set_policy(const std::string& name, const uhd::device_addr_t& args) - { - UHD_LOG_INFO("MOCK_REG_IFACE", - "Requested to set policy for " << name << " to " << args.to_string()); - } - - uint16_t get_src_epid() const - { - return 0; - } - - uint16_t get_port_num() const - { - return 0; - } - - bool force_timeout = false; - - std::unordered_map read_memory; - std::unordered_map write_memory; - -protected: - virtual void _poke_cb( - uint32_t /*addr*/, uint32_t /*data*/, uhd::time_spec_t /*time*/, bool /*ack*/) - { - } - virtual void _peek_cb(uint32_t /*addr*/, uhd::time_spec_t /*time*/) {} -}; - - -#endif /* INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP */ -- cgit v1.2.3