Skip to content

Commit

Permalink
Improve max_allocations calculation on SHM transport (#5659) (#5675)
Browse files Browse the repository at this point in the history
* Improve `max_allocations` calculation on SHM transport (#5659)

* Refs #22841. Improve max_allocations calculation.

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

* Refs #22841. Consider a mean message size of half the max message size.

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

* Refs #22841. Move `shm_default_segment_size` to global scope.

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

* Refs #22841. Move `SharedMemTransportDescriptor` constants.

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

* Refs #22841. Change calculation of `mean_message_size`.

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

---------

Signed-off-by: Miguel Company <[email protected]>
(cherry picked from commit b54cb8e)

# Conflicts:
#	src/cpp/rtps/transport/shared_mem/SharedMemTransport.cpp
#	src/cpp/rtps/transport/shared_mem/SharedMemTransportDescriptor.cpp

* Fix conflicts.
Signed-off-by: Miguel Company <[email protected]>

---------

Co-authored-by: Miguel Company <[email protected]>
  • Loading branch information
mergify[bot] and MiguelCompany authored Mar 4, 2025
1 parent e6d0844 commit 0d13583
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef _FASTDDS_SHAREDMEM_TRANSPORT_DESCRIPTOR_
#define _FASTDDS_SHAREDMEM_TRANSPORT_DESCRIPTOR_

#include <cstdint>
#include <string>

#include <fastdds/rtps/attributes/ThreadSettings.hpp>
Expand Down Expand Up @@ -43,6 +44,10 @@ class TransportInterface;
*/
struct SharedMemTransportDescriptor : public PortBasedTransportDescriptor
{
static constexpr uint32_t shm_default_segment_size = 0;
static constexpr uint32_t shm_default_port_queue_capacity = 512;
static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;

//! Destructor
virtual ~SharedMemTransportDescriptor() = default;

Expand Down Expand Up @@ -149,13 +154,13 @@ struct SharedMemTransportDescriptor : public PortBasedTransportDescriptor

private:

uint32_t segment_size_;
uint32_t port_queue_capacity_;
uint32_t healthy_check_timeout_ms_;
std::string rtps_dump_file_;
uint32_t segment_size_ = shm_default_segment_size;
uint32_t port_queue_capacity_ = shm_default_port_queue_capacity;
uint32_t healthy_check_timeout_ms_ = shm_default_healthy_check_timeout_ms;
std::string rtps_dump_file_ {""};

//! Thread settings for the transport dump thread
ThreadSettings dump_thread_;
ThreadSettings dump_thread_ {};

};

Expand Down
17 changes: 13 additions & 4 deletions src/cpp/rtps/transport/shared_mem/SharedMemTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <thread>
#include <utility>
Expand All @@ -26,6 +27,7 @@
#include <fastdds/rtps/common/Locator.h>
#include <fastdds/rtps/transport/SenderResource.h>
#include <fastdds/rtps/transport/TransportInterface.h>
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastrtps/rtps/messages/CDRMessage.h>
#include <fastrtps/rtps/messages/MessageReceiver.h>

Expand All @@ -51,6 +53,9 @@ using LocatorSelectorEntry = fastrtps::rtps::LocatorSelectorEntry;
using LocatorSelector = fastrtps::rtps::LocatorSelector;
using PortParameters = fastrtps::rtps::PortParameters;

// TODO(Adolfo): Calculate this value from UDP sockets buffers size.
static constexpr uint32_t shm_default_segment_size = 512 * 1024;

TransportInterface* SharedMemTransportDescriptor::create_transport() const
{
return new SharedMemTransport(*this);
Expand Down Expand Up @@ -249,8 +254,6 @@ bool SharedMemTransport::init(
const uint32_t& max_msg_size_no_frag)
{
(void) max_msg_size_no_frag;
// TODO(Adolfo): Calculate this value from UDP sockets buffers size.
static constexpr uint32_t shm_default_segment_size = 512 * 1024;

if (configuration_.segment_size() == 0)
{
Expand Down Expand Up @@ -279,8 +282,14 @@ bool SharedMemTransport::init(
{
return false;
}
shared_mem_segment_ = shared_mem_manager_->create_segment(configuration_.segment_size(),
configuration_.port_queue_capacity());
constexpr uint32_t mean_message_size =
shm_default_segment_size / SharedMemTransportDescriptor::shm_default_port_queue_capacity;
uint32_t max_allocations = configuration_.segment_size() / mean_message_size;
if (configuration_.port_queue_capacity() > max_allocations)
{
max_allocations = configuration_.port_queue_capacity();
}
shared_mem_segment_ = shared_mem_manager_->create_segment(configuration_.segment_size(), max_allocations);

// Memset the whole segment to zero in order to force physical map of the buffer
auto buffer = shared_mem_segment_->alloc_buffer(configuration_.segment_size(),
Expand Down
16 changes: 0 additions & 16 deletions src/cpp/rtps/transport/shared_mem/SharedMemTransportDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,11 @@

using namespace eprosima::fastdds::rtps;

namespace eprosima {
namespace fastdds {
namespace rtps {

static constexpr uint32_t shm_default_segment_size = 0;
static constexpr uint32_t shm_default_port_queue_capacity = 512;
static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;

} // rtps
} // fastdds
} // eprosima

//*********************************************************
// SharedMemTransportDescriptor
//*********************************************************
SharedMemTransportDescriptor::SharedMemTransportDescriptor()
: PortBasedTransportDescriptor(shm_default_segment_size, s_maximumInitialPeersRange)
, segment_size_(shm_default_segment_size)
, port_queue_capacity_(shm_default_port_queue_capacity)
, healthy_check_timeout_ms_(shm_default_healthy_check_timeout_ms)
, rtps_dump_file_("")
{
maxMessageSize = s_maximumMessageSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ class TransportInterface;
*
* @ingroup TRANSPORT_MODULE
*/
typedef struct SharedMemTransportDescriptor : public PortBasedTransportDescriptor
struct SharedMemTransportDescriptor : public PortBasedTransportDescriptor
{
static constexpr uint32_t shm_default_segment_size = 0;
static constexpr uint32_t shm_default_port_queue_capacity = 512;
static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;

virtual ~SharedMemTransportDescriptor()
{

Expand Down Expand Up @@ -125,13 +129,13 @@ typedef struct SharedMemTransportDescriptor : public PortBasedTransportDescripto

private:

uint32_t segment_size_;
uint32_t port_queue_capacity_;
uint32_t healthy_check_timeout_ms_;
uint32_t segment_size_ = shm_default_segment_size;
uint32_t port_queue_capacity_ = shm_default_port_queue_capacity;
uint32_t healthy_check_timeout_ms_ = shm_default_healthy_check_timeout_ms;
std::string rtps_dump_file_;
ThreadSettings dump_thread_;

}SharedMemTransportDescriptor;
};

} // namespace rtps
} // namespace fastdds
Expand Down

0 comments on commit 0d13583

Please sign in to comment.