Skip to content

Commit 37230ac

Browse files
Improve max_allocations calculation on SHM transport (#5659) (#5676)
* 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: # 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 * Fix conflicts. Signed-off-by: Miguel Company <[email protected]> --------- Co-authored-by: Miguel Company <[email protected]>
1 parent 657c521 commit 37230ac

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

include/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class TransportInterface;
3939
*/
4040
struct SharedMemTransportDescriptor : public TransportDescriptorInterface
4141
{
42+
static constexpr uint32_t shm_default_segment_size = 0;
43+
static constexpr uint32_t shm_default_port_queue_capacity = 512;
44+
static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;
45+
4246
//! Destructor
4347
virtual ~SharedMemTransportDescriptor() = default;
4448

@@ -132,10 +136,10 @@ struct SharedMemTransportDescriptor : public TransportDescriptorInterface
132136

133137
private:
134138

135-
uint32_t segment_size_;
136-
uint32_t port_queue_capacity_;
137-
uint32_t healthy_check_timeout_ms_;
138-
std::string rtps_dump_file_;
139+
uint32_t segment_size_ = shm_default_segment_size;
140+
uint32_t port_queue_capacity_ = shm_default_port_queue_capacity;
141+
uint32_t healthy_check_timeout_ms_ = shm_default_healthy_check_timeout_ms;
142+
std::string rtps_dump_file_ {""};
139143

140144
};
141145

src/cpp/rtps/transport/shared_mem/SharedMemTransport.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <algorithm>
16+
#include <cstdint>
1617
#include <cstring>
1718
#include <utility>
1819

@@ -25,6 +26,7 @@
2526
#include <fastdds/rtps/common/Locator.h>
2627
#include <fastdds/rtps/transport/SenderResource.h>
2728
#include <fastdds/rtps/transport/TransportInterface.h>
29+
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
2830
#include <fastrtps/rtps/messages/CDRMessage.h>
2931
#include <fastrtps/rtps/messages/MessageReceiver.h>
3032

@@ -50,6 +52,9 @@ using LocatorSelectorEntry = fastrtps::rtps::LocatorSelectorEntry;
5052
using LocatorSelector = fastrtps::rtps::LocatorSelector;
5153
using PortParameters = fastrtps::rtps::PortParameters;
5254

55+
// TODO(Adolfo): Calculate this value from UDP sockets buffers size.
56+
static constexpr uint32_t shm_default_segment_size = 512 * 1024;
57+
5358
TransportInterface* SharedMemTransportDescriptor::create_transport() const
5459
{
5560
return new SharedMemTransport(*this);
@@ -241,9 +246,6 @@ bool SharedMemTransport::DoInputLocatorsMatch(
241246
bool SharedMemTransport::init(
242247
const fastrtps::rtps::PropertyPolicy*)
243248
{
244-
// TODO(Adolfo): Calculate this value from UDP sockets buffers size.
245-
static constexpr uint32_t shm_default_segment_size = 512 * 1024;
246-
247249
if (configuration_.segment_size() == 0)
248250
{
249251
configuration_.segment_size(shm_default_segment_size);
@@ -271,8 +273,14 @@ bool SharedMemTransport::init(
271273
{
272274
return false;
273275
}
274-
shared_mem_segment_ = shared_mem_manager_->create_segment(configuration_.segment_size(),
275-
configuration_.port_queue_capacity());
276+
constexpr uint32_t mean_message_size =
277+
shm_default_segment_size / SharedMemTransportDescriptor::shm_default_port_queue_capacity;
278+
uint32_t max_allocations = configuration_.segment_size() / mean_message_size;
279+
if (configuration_.port_queue_capacity() > max_allocations)
280+
{
281+
max_allocations = configuration_.port_queue_capacity();
282+
}
283+
shared_mem_segment_ = shared_mem_manager_->create_segment(configuration_.segment_size(), max_allocations);
276284

277285
// Memset the whole segment to zero in order to force physical map of the buffer
278286
auto buffer = shared_mem_segment_->alloc_buffer(configuration_.segment_size(),

src/cpp/rtps/transport/shared_mem/SharedMemTransportDescriptor.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,11 @@
1717

1818
using namespace eprosima::fastdds::rtps;
1919

20-
namespace eprosima {
21-
namespace fastdds {
22-
namespace rtps {
23-
24-
static constexpr uint32_t shm_default_segment_size = 0;
25-
static constexpr uint32_t shm_default_port_queue_capacity = 512;
26-
static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;
27-
28-
} // rtps
29-
} // fastdds
30-
} // eprosima
31-
3220
//*********************************************************
3321
// SharedMemTransportDescriptor
3422
//*********************************************************
3523
SharedMemTransportDescriptor::SharedMemTransportDescriptor()
3624
: TransportDescriptorInterface(shm_default_segment_size, s_maximumInitialPeersRange)
37-
, segment_size_(shm_default_segment_size)
38-
, port_queue_capacity_(shm_default_port_queue_capacity)
39-
, healthy_check_timeout_ms_(shm_default_healthy_check_timeout_ms)
40-
, rtps_dump_file_("")
4125
{
4226
maxMessageSize = s_maximumMessageSize;
4327
}

test/mock/rtps/SharedMemTransportDescriptor/fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ class TransportInterface;
2828
*
2929
* @ingroup TRANSPORT_MODULE
3030
*/
31-
typedef struct SharedMemTransportDescriptor : public TransportDescriptorInterface
31+
struct SharedMemTransportDescriptor : public TransportDescriptorInterface
3232
{
33+
static constexpr uint32_t shm_default_segment_size = 0;
34+
static constexpr uint32_t shm_default_port_queue_capacity = 512;
35+
static constexpr uint32_t shm_default_healthy_check_timeout_ms = 1000;
36+
3337
virtual ~SharedMemTransportDescriptor()
3438
{
3539

@@ -108,12 +112,12 @@ typedef struct SharedMemTransportDescriptor : public TransportDescriptorInterfac
108112

109113
private:
110114

111-
uint32_t segment_size_;
112-
uint32_t port_queue_capacity_;
113-
uint32_t healthy_check_timeout_ms_;
115+
uint32_t segment_size_ = shm_default_segment_size;
116+
uint32_t port_queue_capacity_ = shm_default_port_queue_capacity;
117+
uint32_t healthy_check_timeout_ms_ = shm_default_healthy_check_timeout_ms;
114118
std::string rtps_dump_file_;
115119

116-
}SharedMemTransportDescriptor;
120+
};
117121

118122
} // namespace rtps
119123
} // namespace fastdds

0 commit comments

Comments
 (0)