From 0f46e43c5b7970758e6f2de6cc70c09e0d9fb26d Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sat, 2 Dec 2023 13:56:16 +0100 Subject: [PATCH] Use foonathan memory manager for reducing allocations in SharedMemManager.hpp (#3833) (#3889) * Use foonathan memory manager for reducing allocations in SharedMemManager.hpp (#3833) * Add helpers for std::list node sizes. Signed-off-by: Matthias Schneider * Use foonathan memory pool for storing buffer nodes in shared mem manager in order to reduce dynamic heap allocations. Signed-off-by: Matthias Schneider * Uncrustify. Signed-off-by: Matthias Schneider * Add foonathan dependency to SharedMemTests. Signed-off-by: Matthias Schneider * Initialize allocator with more realistic allocation assumption. * Fix include order Signed-off-by: Miguel Company * Removed unnecessary constexpr. Signed-off-by: Miguel Company * Fix copyright year. Signed-off-by: Miguel Company * Fix EOL. Signed-off-by: Miguel Company * Fix windows build of unit test. Signed-off-by: Miguel Company --------- Signed-off-by: Matthias Schneider Signed-off-by: Miguel Company Co-authored-by: Miguel Company (cherry picked from commit df180560df98940fea043f776016af75f5ab9050) # Conflicts: # test/unittest/transport/CMakeLists.txt Signed-off-by: EduPonz * Fix conflicts Signed-off-by: Miguel Company Signed-off-by: EduPonz --------- Signed-off-by: Matthias Schneider Signed-off-by: Miguel Company Signed-off-by: EduPonz Co-authored-by: Matthias Schneider Co-authored-by: Miguel Company --- .../transport/shared_mem/SharedMemManager.hpp | 29 +++++++++++----- .../foonathan/list_node_size_impl.hpp | 27 +++++++++++++++ .../impl/node-sizes/list_node_size_impl.hpp | 34 +++++++++++++++++++ .../utils/collections/node_size_helpers.hpp | 5 +++ test/unittest/transport/CMakeLists.txt | 7 +++- 5 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp create mode 100644 src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp diff --git a/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp b/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp index 4dbd05171d1..2e64de6a324 100644 --- a/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp +++ b/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp @@ -19,10 +19,13 @@ #include #include -#include +#include +#include -#include -#include +#include "rtps/transport/shared_mem/SharedMemGlobal.hpp" +#include "utils/collections/node_size_helpers.hpp" +#include "utils/shared_memory/RobustSharedLock.hpp" +#include "utils/shared_memory/SharedMemWatchdog.hpp" namespace eprosima { namespace fastdds { @@ -366,7 +369,12 @@ class SharedMemManager : uint32_t payload_size, uint32_t max_allocations, const std::string& domain_name) - : segment_id_() + : buffer_node_list_allocator_( + buffer_node_list_helper::node_size, + buffer_node_list_helper::min_pool_size(max_allocations)) + , free_buffers_(buffer_node_list_allocator_) + , allocated_buffers_(buffer_node_list_allocator_) + , segment_id_() , overflows_count_(0) { generate_segment_id_and_name(domain_name); @@ -468,7 +476,6 @@ class SharedMemManager : throw std::runtime_error("alloc_buffer: out of memory"); } - // TODO(Adolfo) : Dynamic allocation. Use foonathan to convert it to static allocation allocated_buffers_.push_back(buffer_node); } catch (const std::exception&) @@ -502,9 +509,15 @@ class SharedMemManager : std::unique_ptr segment_name_lock_; - // TODO(Adolfo) : Dynamic allocations. Use foonathan to convert it to static allocation - std::list free_buffers_; - std::list allocated_buffers_; + using buffer_node_list_helper = + utilities::collections::list_size_helper; + + using pool_allocator_t = + foonathan::memory::memory_pool; + pool_allocator_t buffer_node_list_allocator_; + + foonathan::memory::list free_buffers_; + foonathan::memory::list allocated_buffers_; std::mutex alloc_mutex_; std::shared_ptr segment_; diff --git a/src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp b/src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp new file mode 100644 index 00000000000..ca9b2b51ad0 --- /dev/null +++ b/src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp @@ -0,0 +1,27 @@ +// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file list_node_size_impl.hpp + */ + +#ifndef SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ +#define SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ + +template +struct list_node_size : foonathan::memory::list_node_size::value_type> +{ +}; + +#endif /* SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ */ diff --git a/src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp b/src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp new file mode 100644 index 00000000000..901c3d6a53b --- /dev/null +++ b/src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp @@ -0,0 +1,34 @@ +// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file list_node_size_impl.hpp + */ + +#ifndef SRC_CPP_UTILS_COLLECTIONS_IMPL_LIST_NODE_SIZE_IMPL_HPP_ +#define SRC_CPP_UTILS_COLLECTIONS_IMPL_LIST_NODE_SIZE_IMPL_HPP_ + +#include "./config.hpp" + +#if defined(USE_FOONATHAN_NODE_SIZES) +#include "foonathan/list_node_size_impl.hpp" +#elif defined(USE_STD_NODE_SIZES) +#include "std/list_node_size_impl.hpp" +#elif defined(USE_CUSTOM_NODE_SIZES) +#include "custom/list_node_size_impl.hpp" +#else +#error "Don't now which list_node_size implementation to use" +#endif // NODE SIZE CONFIG + +#endif /* SRC_CPP_UTILS_COLLECTIONS_IMPL_LIST_NODE_SIZE_IMPL_HPP_ */ diff --git a/src/cpp/utils/collections/node_size_helpers.hpp b/src/cpp/utils/collections/node_size_helpers.hpp index e1d51b7cce8..0f6834fd838 100644 --- a/src/cpp/utils/collections/node_size_helpers.hpp +++ b/src/cpp/utils/collections/node_size_helpers.hpp @@ -35,6 +35,7 @@ namespace detail { namespace fm = foonathan::memory; // Include implementations for node size helpers +#include "impl/node-sizes/list_node_size_impl.hpp" #include "impl/node-sizes/map_node_size_impl.hpp" #include "impl/node-sizes/set_node_size_impl.hpp" #include "impl/node-sizes/unordered_map_node_size_impl.hpp" @@ -100,6 +101,10 @@ struct pool_size_helper } // namespace detail +template +struct list_size_helper : public detail::pool_size_helper::value> +{ +}; template struct map_size_helper : public detail::pool_size_helper::value> diff --git a/test/unittest/transport/CMakeLists.txt b/test/unittest/transport/CMakeLists.txt index a54270c19d1..3a0e26280e3 100644 --- a/test/unittest/transport/CMakeLists.txt +++ b/test/unittest/transport/CMakeLists.txt @@ -16,6 +16,7 @@ if(WIN32) add_definitions( -D_WIN32_WINNT=0x0601 -D_CRT_SECURE_NO_WARNINGS + -DNOMINMAX ) endif() @@ -406,7 +407,11 @@ if(IS_THIRDPARTY_BOOST_OK) ${THIRDPARTY_BOOST_INCLUDE_DIR} $<$:${ANDROID_IFADDRS_INCLUDE_DIR}> ) - target_link_libraries(SharedMemTests GTest::gtest ${MOCKS} + target_link_libraries(SharedMemTests + foonathan_memory + fastcdr + GTest::gtest + ${MOCKS} $<$:OpenSSL::SSL$OpenSSL::Crypto> ${THIRDPARTY_BOOST_LINK_LIBS} eProsima_atomic