Skip to content

Commit

Permalink
Use foonathan memory manager for reducing allocations in SharedMemMan…
Browse files Browse the repository at this point in the history
…ager.hpp (#3833)

* Add helpers for std::list node sizes.

Signed-off-by: Matthias Schneider <[email protected]>

* Use foonathan memory pool for storing buffer nodes in shared mem manager in order to reduce dynamic heap allocations.

Signed-off-by: Matthias Schneider <[email protected]>

* Uncrustify.

Signed-off-by: Matthias Schneider <[email protected]>

* Add foonathan dependency to SharedMemTests.

Signed-off-by: Matthias Schneider <[email protected]>

* Initialize allocator with more realistic allocation assumption.

* Fix include order

Signed-off-by: Miguel Company <[email protected]>

* Removed unnecessary constexpr.

Signed-off-by: Miguel Company <[email protected]>

* Fix copyright year.

Signed-off-by: Miguel Company <[email protected]>

* Fix EOL.

Signed-off-by: Miguel Company <[email protected]>

* Fix windows build of unit test.

Signed-off-by: Miguel Company <[email protected]>

---------

Signed-off-by: Matthias Schneider <[email protected]>
Signed-off-by: Miguel Company <[email protected]>
Co-authored-by: Miguel Company <[email protected]>
  • Loading branch information
ma30002000 and MiguelCompany authored Sep 28, 2023
1 parent 4fed0bd commit df18056
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
#include <list>
#include <unordered_map>

#include <rtps/transport/shared_mem/SharedMemGlobal.hpp>
#include <foonathan/memory/container.hpp>
#include <foonathan/memory/memory_pool.hpp>

#include <utils/shared_memory/RobustSharedLock.hpp>
#include <utils/shared_memory/SharedMemWatchdog.hpp>
#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 {
Expand Down Expand Up @@ -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<pool_allocator_t>(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);
Expand Down Expand Up @@ -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&)
Expand Down Expand Up @@ -502,9 +509,15 @@ class SharedMemManager :

std::unique_ptr<RobustExclusiveLock> segment_name_lock_;

// TODO(Adolfo) : Dynamic allocations. Use foonathan to convert it to static allocation
std::list<BufferNode*> free_buffers_;
std::list<BufferNode*> allocated_buffers_;
using buffer_node_list_helper =
utilities::collections::list_size_helper<BufferNode*>;

using pool_allocator_t =
foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::heap_allocator>;
pool_allocator_t buffer_node_list_allocator_;

foonathan::memory::list<BufferNode*, pool_allocator_t> free_buffers_;
foonathan::memory::list<BufferNode*, pool_allocator_t> allocated_buffers_;

std::mutex alloc_mutex_;
std::shared_ptr<SharedMemSegment> segment_;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <typename T>
struct list_node_size : foonathan::memory::list_node_size<typename std::list<T>::value_type>
{
};

#endif /* SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ */
34 changes: 34 additions & 0 deletions src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp
Original file line number Diff line number Diff line change
@@ -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_ */
5 changes: 5 additions & 0 deletions src/cpp/utils/collections/node_size_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -100,6 +101,10 @@ struct pool_size_helper

} // namespace detail

template<typename T>
struct list_size_helper : public detail::pool_size_helper<detail::list_node_size<T>::value>
{
};

template<typename K, typename V>
struct map_size_helper : public detail::pool_size_helper<detail::map_node_size<K, V>::value>
Expand Down
2 changes: 2 additions & 0 deletions test/unittest/transport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if(WIN32)
add_definitions(
-D_WIN32_WINNT=0x0601
-D_CRT_SECURE_NO_WARNINGS
-DNOMINMAX
)
endif()

Expand Down Expand Up @@ -422,6 +423,7 @@ if(IS_THIRDPARTY_BOOST_OK)
$<$<BOOL:${ANDROID}>:${ANDROID_IFADDRS_INCLUDE_DIR}>
)
target_link_libraries(SharedMemTests
foonathan_memory
fastcdr
GTest::gtest
${MOCKS}
Expand Down

0 comments on commit df18056

Please sign in to comment.