Skip to content

Commit

Permalink
Improve max_allocations calculation on SHM transport (#5659)
Browse files Browse the repository at this point in the history
* 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:
#	include/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h
#	src/cpp/rtps/transport/shared_mem/SharedMemTransport.cpp
#	src/cpp/rtps/transport/shared_mem/SharedMemTransportDescriptor.cpp
#	test/mock/rtps/SharedMemTransportDescriptor/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h
  • Loading branch information
MiguelCompany authored and mergify[bot] committed Mar 3, 2025
1 parent 657c521 commit 7eb6ccd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
#ifndef _FASTDDS_SHAREDMEM_TRANSPORT_DESCRIPTOR_
#define _FASTDDS_SHAREDMEM_TRANSPORT_DESCRIPTOR_

<<<<<<< HEAD:include/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h
#include "fastdds/rtps/transport/TransportDescriptorInterface.h"
=======
#ifndef FASTDDS_RTPS_TRANSPORT_SHARED_MEM__SHAREDMEMTRANSPORTDESCRIPTOR_HPP
#define FASTDDS_RTPS_TRANSPORT_SHARED_MEM__SHAREDMEMTRANSPORTDESCRIPTOR_HPP

#include <cstdint>
#include <string>

#include <fastdds/rtps/attributes/ThreadSettings.hpp>
#include <fastdds/rtps/transport/PortBasedTransportDescriptor.hpp>
#include <fastdds/fastdds_dll.hpp>
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659)):include/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.hpp

namespace eprosima {
namespace fastdds {
Expand All @@ -39,6 +51,10 @@ class TransportInterface;
*/
struct SharedMemTransportDescriptor : public TransportDescriptorInterface
{
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 @@ -132,11 +148,17 @@ struct SharedMemTransportDescriptor : public TransportDescriptorInterface

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_ {""};

<<<<<<< HEAD:include/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h
=======
//! Thread settings for the transport dump thread
ThreadSettings dump_thread_ {};

>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659)):include/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.hpp
};

} // namespace rtps
Expand Down
27 changes: 25 additions & 2 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 <utility>

Expand All @@ -22,11 +23,18 @@
#endif // ifdef ANDROID

#include <fastdds/dds/log/Log.hpp>
<<<<<<< HEAD
#include <fastdds/rtps/common/Locator.h>
#include <fastdds/rtps/transport/SenderResource.h>
#include <fastdds/rtps/transport/TransportInterface.h>
#include <fastrtps/rtps/messages/CDRMessage.h>
#include <fastrtps/rtps/messages/MessageReceiver.h>
=======
#include <fastdds/rtps/common/Locator.hpp>
#include <fastdds/rtps/transport/SenderResource.hpp>
#include <fastdds/rtps/transport/TransportInterface.hpp>
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.hpp>
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659))

#include <rtps/network/ReceiverResource.h>
#include <rtps/transport/shared_mem/SharedMemChannelResource.hpp>
Expand All @@ -44,11 +52,16 @@ namespace eprosima {
namespace fastdds {
namespace rtps {

<<<<<<< HEAD
using octet = fastrtps::rtps::octet;
using SenderResource = fastrtps::rtps::SenderResource;
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;
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659))

TransportInterface* SharedMemTransportDescriptor::create_transport() const
{
Expand Down Expand Up @@ -241,8 +254,12 @@ bool SharedMemTransport::DoInputLocatorsMatch(
bool SharedMemTransport::init(
const fastrtps::rtps::PropertyPolicy*)
{
<<<<<<< HEAD
// TODO(Adolfo): Calculate this value from UDP sockets buffers size.
static constexpr uint32_t shm_default_segment_size = 512 * 1024;
=======
(void) max_msg_size_no_frag;
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659))

if (configuration_.segment_size() == 0)
{
Expand Down Expand Up @@ -271,8 +288,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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace eprosima {
namespace fastdds {
namespace rtps {

<<<<<<< HEAD
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;
Expand All @@ -29,15 +30,21 @@ static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;
} // fastdds
} // eprosima

=======
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659))
//*********************************************************
// SharedMemTransportDescriptor
//*********************************************************
SharedMemTransportDescriptor::SharedMemTransportDescriptor()
<<<<<<< HEAD
: TransportDescriptorInterface(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_("")
=======
: PortBasedTransportDescriptor(shm_default_segment_size, s_maximumInitialPeersRange)
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659))
{
maxMessageSize = s_maximumMessageSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ class TransportInterface;
*
* @ingroup TRANSPORT_MODULE
*/
<<<<<<< HEAD:test/mock/rtps/SharedMemTransportDescriptor/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h
typedef struct SharedMemTransportDescriptor : public TransportDescriptorInterface
=======
struct SharedMemTransportDescriptor : public PortBasedTransportDescriptor
>>>>>>> b54cb8ef (Improve `max_allocations` calculation on SHM transport (#5659)):test/mock/rtps/SharedMemTransportDescriptor/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.hpp
{
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 @@ -108,12 +116,12 @@ typedef struct SharedMemTransportDescriptor : public TransportDescriptorInterfac

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_;

}SharedMemTransportDescriptor;
};

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

0 comments on commit 7eb6ccd

Please sign in to comment.