Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] zero-copy-zero-payload-shm-transfer #1683

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions ecal/core/src/io/shm/ecal_memfile_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,26 @@ namespace eCAL
// -------------------------------------------------------------------------
if (zero_copy_allowed)
{
// acquire memory file payload pointer (no copying here)
const void* buf(nullptr);
if (m_memfile.GetReadAddress(buf, mfile_hdr.data_size) > 0)
if (m_data_callback)
{
// calculate data buffer offset
const char* data_buf = static_cast<const char*>(buf) + mfile_hdr.hdr_size;
// add sample to data reader (and call user callback function)
if (m_data_callback) m_data_callback(topic_name_, topic_id_, data_buf, mfile_hdr.data_size, (long long)mfile_hdr.id, (long long)mfile_hdr.clock, (long long)mfile_hdr.time, (size_t)mfile_hdr.hash);
const char* data_buf = nullptr;
if (mfile_hdr.data_size > 0)
{
// acquire memory file payload pointer (no copying here)
const void* buf(nullptr);
if (m_memfile.GetReadAddress(buf, mfile_hdr.data_size) > 0)
{
// calculate user payload address
data_buf = static_cast<const char*>(buf) + mfile_hdr.hdr_size;
// call user callback function
m_data_callback(topic_name_, topic_id_, data_buf, mfile_hdr.data_size, (long long)mfile_hdr.id, (long long)mfile_hdr.clock, (long long)mfile_hdr.time, (size_t)mfile_hdr.hash);
}
}
else
{
// call user callback function
m_data_callback(topic_name_, topic_id_, data_buf, mfile_hdr.data_size, (long long)mfile_hdr.id, (long long)mfile_hdr.clock, (long long)mfile_hdr.time, (size_t)mfile_hdr.hash);
}
}
}
// -------------------------------------------------------------------------
Expand Down
28 changes: 21 additions & 7 deletions ecal/tests/cpp/pubsub_test/src/pubsub_test_shm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,8 +64,13 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageSHM)
pub_config.layer.udp.enable = false;
pub_config.layer.tcp.enable = false;

// create publisher for topic "A"
eCAL::CPublisher pub("A", pub_config);
// create publisher for topic "A" (no zero copy)
eCAL::CPublisher pub1("A", pub_config);

// switch on zero copy
pub_config.layer.shm.zero_copy_mode = true;
eCAL::CPublisher pub2("A", pub_config);


// add callback
EXPECT_EQ(true, sub.AddReceiveCallback(std::bind(OnReceive, std::placeholders::_1, std::placeholders::_2)));
Expand All @@ -76,21 +81,30 @@ TEST(core_cpp_pubsub, ZeroPayloadMessageSHM)
g_callback_received_bytes = 0;
g_callback_received_count = 0;

EXPECT_EQ(send_s.size(), pub.Send(send_s));
// send without zero copy
EXPECT_EQ(send_s.size(), pub1.Send(send_s));
eCAL::Process::SleepMS(DATA_FLOW_TIME_MS);

EXPECT_EQ(send_s.size(), pub1.Send(nullptr, 0));
eCAL::Process::SleepMS(DATA_FLOW_TIME_MS);

// send with zero copy
EXPECT_EQ(send_s.size(), pub2.Send(send_s));
eCAL::Process::SleepMS(DATA_FLOW_TIME_MS);

EXPECT_EQ(send_s.size(), pub.Send(nullptr, 0));
EXPECT_EQ(send_s.size(), pub2.Send(nullptr, 0));
eCAL::Process::SleepMS(DATA_FLOW_TIME_MS);

// check callback receive
EXPECT_EQ(send_s.size(), g_callback_received_bytes);
EXPECT_EQ(2, g_callback_received_count);
EXPECT_EQ(4, g_callback_received_count);

// destroy subscriber
sub.Destroy();

// destroy publisher
pub.Destroy();
pub1.Destroy();
pub2.Destroy();

// finalize eCAL API
eCAL::Finalize();
Expand Down
Loading