/******************************************************************************** * Copyright (C) 2017-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace std; using namespace fair::mq; auto AsStringView(Message const& msg) -> string_view { return {static_cast(msg.GetData()), msg.GetSize()}; } auto RunPushPullWithMsgResize(string const & transport, string const & name, bool expandedShmMetadata = false) -> void { ProgOptions config; test::Configure(config, tools::Uuid(), expandedShmMetadata); auto factory(TransportFactory::CreateTransportFactory(transport, tools::Uuid(), &config)); Channel push{"Push", "push", factory}; Channel pull{"Pull", "pull", factory}; test::IpcAddress const address(tools::ToString(name, "_", transport)); ASSERT_TRUE(push.Bind(address)); ASSERT_TRUE(pull.Connect(address)); { size_t const size{6}; auto outMsg(push.NewMessage(size)); ASSERT_EQ(outMsg->GetSize(), size); memcpy(outMsg->GetData(), "ABCDEF", size); ASSERT_TRUE(outMsg->SetUsedSize(5)); ASSERT_TRUE(outMsg->SetUsedSize(5)); ASSERT_FALSE(outMsg->SetUsedSize(7)); ASSERT_EQ(outMsg->GetSize(), 5); // check if the data is still intact ASSERT_EQ(AsStringView(*outMsg)[0], 'A'); ASSERT_EQ(AsStringView(*outMsg)[1], 'B'); ASSERT_EQ(AsStringView(*outMsg)[2], 'C'); ASSERT_EQ(AsStringView(*outMsg)[3], 'D'); ASSERT_EQ(AsStringView(*outMsg)[4], 'E'); ASSERT_TRUE(outMsg->SetUsedSize(2)); ASSERT_EQ(outMsg->GetSize(), 2); ASSERT_EQ(AsStringView(*outMsg)[0], 'A'); ASSERT_EQ(AsStringView(*outMsg)[1], 'B'); auto msgCopy(push.NewMessage()); msgCopy->Copy(*outMsg); ASSERT_EQ(msgCopy->GetSize(), 2); ASSERT_EQ(push.Send(outMsg), 2); auto inMsg(pull.NewMessage()); ASSERT_EQ(pull.Receive(inMsg), 2); ASSERT_EQ(inMsg->GetSize(), 2); ASSERT_EQ(AsStringView(*inMsg)[0], 'A'); ASSERT_EQ(AsStringView(*inMsg)[1], 'B'); } { size_t const size{1000}; auto outMsg(push.NewMessage(size)); ASSERT_TRUE(outMsg->SetUsedSize(0)); ASSERT_EQ(outMsg->GetSize(), 0); auto msgCopy(push.NewMessage()); msgCopy->Copy(*outMsg); ASSERT_EQ(msgCopy->GetSize(), 0); ASSERT_EQ(push.Send(outMsg), 0); auto inMsg(pull.NewMessage()); ASSERT_EQ(pull.Receive(inMsg), 0); ASSERT_EQ(inMsg->GetSize(), 0); } } auto RunMsgRebuild(const string& transport, bool expandedShmMetadata = false) -> void { ProgOptions config; test::Configure(config, tools::Uuid(), expandedShmMetadata); auto factory(TransportFactory::CreateTransportFactory(transport, tools::Uuid(), &config)); size_t const msgSize{100}; string const expectedStr{"asdf"}; auto msg(factory->CreateMessage()); EXPECT_EQ(msg->GetSize(), 0); msg->Rebuild(msgSize); EXPECT_EQ(msg->GetSize(), msgSize); auto str(make_unique(expectedStr)); void* data(str->data()); auto const size(str->length()); msg->Rebuild( data, size, [](void* /*data*/, void* obj) { delete static_cast(obj); }, // NOLINT str.release()); EXPECT_NE(msg->GetSize(), msgSize); EXPECT_EQ(msg->GetSize(), expectedStr.length()); EXPECT_EQ(AsStringView(*msg), expectedStr); } auto CheckMsgAlignment(Message const& msg, fair::mq::Alignment alignment) -> bool { assert(static_cast(alignment) > 0); // NOLINT return (reinterpret_cast(msg.GetData()) % static_cast(alignment)) == 0; // NOLINT } auto RunPushPullWithAlignment(string const& transport, string const& name, bool expandedShmMetadata = false) -> void { ProgOptions config; test::Configure(config, tools::Uuid(), expandedShmMetadata); auto factory(TransportFactory::CreateTransportFactory(transport, tools::Uuid(), &config)); Channel push{"Push", "push", factory}; Channel pull{"Pull", "pull", factory}; test::IpcAddress const address(tools::ToString(name, "_", transport)); ASSERT_TRUE(push.Bind(address)); ASSERT_TRUE(pull.Connect(address)); { Alignment const align{64}; for (size_t const size : {100, 32}) { auto outMsg(push.NewMessage(size, align)); auto inMsg(pull.NewMessage(align)); ASSERT_TRUE(CheckMsgAlignment(*outMsg, align)); ASSERT_EQ(push.Send(outMsg), size); ASSERT_EQ(pull.Receive(inMsg), size); ASSERT_TRUE(CheckMsgAlignment(*inMsg, align)); } } { Alignment const align{0}; size_t const size{100}; auto outMsg(push.NewMessage(size, align)); auto inMsg(pull.NewMessage(align)); ASSERT_EQ(push.Send(outMsg), size); ASSERT_EQ(pull.Receive(inMsg), size); } size_t const size25{25}; size_t const size50{50}; Alignment const align64{64}; auto msg1(push.NewMessage(size25)); msg1->Rebuild(size50, align64); ASSERT_TRUE(CheckMsgAlignment(*msg1, align64)); Alignment const align32{32}; auto msg2(push.NewMessage(size25, align64)); msg2->Rebuild(size50, align32); ASSERT_TRUE(CheckMsgAlignment(*msg2, align32)); auto msgCopy(push.NewMessage()); msgCopy->Copy(*msg2); ASSERT_TRUE(CheckMsgAlignment(*msgCopy, align32)); } auto EmptyMessage(string const& transport, string const& name, bool expandedShmMetadata = false) -> void { ProgOptions config; test::Configure(config, tools::Uuid(), expandedShmMetadata); auto factory(TransportFactory::CreateTransportFactory(transport, tools::Uuid(), &config)); Channel push{"Push", "push", factory}; Channel pull{"Pull", "pull", factory}; test::IpcAddress const address(tools::ToString(name, "_", transport)); ASSERT_TRUE(push.Bind(address)); ASSERT_TRUE(pull.Connect(address)); { auto outMsg(push.NewMessage()); ASSERT_EQ(outMsg->GetData(), nullptr); ASSERT_EQ(push.Send(outMsg), 0); auto inMsg(pull.NewMessage()); ASSERT_EQ(pull.Receive(inMsg), 0); ASSERT_EQ(inMsg->GetData(), nullptr); } { auto outMsg(push.NewMessage(0)); ASSERT_EQ(outMsg->GetSize(), 0); size_t const size100{100}; outMsg->Rebuild(size100); ASSERT_EQ(outMsg->GetSize(), size100); outMsg->Rebuild(0); ASSERT_EQ(outMsg->GetSize(), 0); auto msgCopy(push.NewMessage()); msgCopy->Copy(*outMsg); ASSERT_EQ(msgCopy->GetSize(), 0); ASSERT_EQ(push.Send(outMsg), 0); ASSERT_EQ(push.Send(msgCopy), 0); auto inMsg(pull.NewMessage()); auto inMsg2(pull.NewMessage()); ASSERT_EQ(pull.Receive(inMsg), 0); ASSERT_EQ(pull.Receive(inMsg2), 0); ASSERT_EQ(inMsg->GetSize(), 0); ASSERT_EQ(inMsg2->GetSize(), 0); } } // The "zero copy" property of the Copy() method is an implementation detail and is not guaranteed. // Currently it holds true for the shmem (across devices) and for zeromq (within same device) transports. auto ZeroCopy(bool expandedShmMetadata = false) -> void { ProgOptions config; test::Configure(config, tools::Uuid(), expandedShmMetadata); auto factory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config)); unique_ptr str(make_unique("asdf")); const size_t size = 2; MessagePtr original(factory->CreateMessage(size)); memcpy(original->GetData(), "AB", size); { MessagePtr copy(factory->CreateMessage()); copy->Copy(*original); EXPECT_EQ(original->GetSize(), copy->GetSize()); EXPECT_EQ(original->GetData(), copy->GetData()); EXPECT_EQ(static_cast(*original).GetRefCount(), 2); EXPECT_EQ(static_cast(*copy).GetRefCount(), 2); // buffer must be still intact ASSERT_EQ(AsStringView(*original)[0], 'A'); ASSERT_EQ(AsStringView(*original)[1], 'B'); ASSERT_EQ(AsStringView(*copy)[0], 'A'); ASSERT_EQ(AsStringView(*copy)[1], 'B'); } EXPECT_EQ(static_cast(*original).GetRefCount(), 1); } // The "zero copy" property of the Copy() method is an implementation detail and is not guaranteed. // Currently it holds true for the shmem (across devices) and for zeromq (within same device) transports. auto ZeroCopyFromUnmanaged(string const& name, bool expandedShmMetadata, uint64_t rcSegmentSize) -> void { ProgOptions config1; ProgOptions config2; string const session(test::Configure(config1, tools::Uuid(), expandedShmMetadata)); test::Configure(config2, session, expandedShmMetadata); // ref counts should be accessible accross different segments config2.SetProperty("shm-segment-id", 2); auto factory1(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config1)); auto factory2(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config2)); const size_t msgSize{100}; const size_t regionSize{1000000}; RegionConfig cfg; cfg.rcSegmentSize = rcSegmentSize; test::Semaphore blocker; auto region = factory1->CreateUnmanagedRegion(regionSize, [&blocker](void*, size_t, void*) { blocker.Signal(); }, cfg); { Channel push("Push", "push", factory1); Channel pull("Pull", "pull", factory2); test::IpcAddress const address(name); ASSERT_TRUE(push.Bind(address)); ASSERT_TRUE(pull.Connect(address)); const size_t offset = 100; auto msg1(push.NewMessage(region, static_cast(region->GetData()), msgSize, nullptr)); auto msg2(push.NewMessage(region, static_cast(region->GetData()) + offset, msgSize, nullptr)); const size_t contentSize = 2; memcpy(msg1->GetData(), "AB", contentSize); memcpy(msg2->GetData(), "CD", contentSize); EXPECT_EQ(static_cast(*msg1).GetRefCount(), 1); { auto copyFromOriginal(push.NewMessage()); copyFromOriginal->Copy(*msg1); EXPECT_EQ(static_cast(*msg1).GetRefCount(), 2); EXPECT_EQ(static_cast(*msg1).GetRefCount(), static_cast(*copyFromOriginal).GetRefCount()); { auto copyFromCopy(push.NewMessage()); copyFromCopy->Copy(*copyFromOriginal); EXPECT_EQ(static_cast(*msg1).GetRefCount(), 3); EXPECT_EQ(static_cast(*msg1).GetRefCount(), static_cast(*copyFromCopy).GetRefCount()); EXPECT_EQ(msg1->GetSize(), copyFromOriginal->GetSize()); EXPECT_EQ(msg1->GetData(), copyFromOriginal->GetData()); EXPECT_EQ(msg1->GetSize(), copyFromCopy->GetSize()); EXPECT_EQ(msg1->GetData(), copyFromCopy->GetData()); EXPECT_EQ(copyFromOriginal->GetSize(), copyFromCopy->GetSize()); EXPECT_EQ(copyFromOriginal->GetData(), copyFromCopy->GetData()); // messing with the ref count should not have affected the user buffer ASSERT_EQ(AsStringView(*msg1)[0], 'A'); ASSERT_EQ(AsStringView(*msg1)[1], 'B'); ASSERT_EQ(push.Send(copyFromCopy), msgSize); ASSERT_EQ(push.Send(msg2), msgSize); auto incomingCopiedMsg(pull.NewMessage()); auto incomingOriginalMsg(pull.NewMessage()); ASSERT_EQ(pull.Receive(incomingCopiedMsg), msgSize); ASSERT_EQ(pull.Receive(incomingOriginalMsg), msgSize); EXPECT_EQ(static_cast(*incomingCopiedMsg).GetRefCount(), 3); EXPECT_EQ(static_cast(*incomingOriginalMsg).GetRefCount(), 1); ASSERT_EQ(AsStringView(*incomingCopiedMsg)[0], 'A'); ASSERT_EQ(AsStringView(*incomingCopiedMsg)[1], 'B'); { // copying on a different segment should work auto copyFromIncoming(pull.NewMessage()); copyFromIncoming->Copy(*incomingOriginalMsg); EXPECT_EQ(static_cast(*copyFromIncoming).GetRefCount(), 2); ASSERT_EQ(AsStringView(*incomingOriginalMsg)[0], 'C'); ASSERT_EQ(AsStringView(*incomingOriginalMsg)[1], 'D'); } EXPECT_EQ(static_cast(*incomingOriginalMsg).GetRefCount(), 1); } EXPECT_EQ(static_cast(*msg1).GetRefCount(), 2); } EXPECT_EQ(static_cast(*msg1).GetRefCount(), 1); } ASSERT_TRUE(blocker.Wait()); ASSERT_TRUE(blocker.Wait()); } using Messages = test::TransportTest; TEST_P(Messages, Resize) // NOLINT { RunPushPullWithMsgResize(GetParam().transport, "message_resize", GetParam().expandedShmMetadata); } TEST_P(Messages, Rebuild) // NOLINT { RunMsgRebuild(GetParam().transport, GetParam().expandedShmMetadata); } TEST_P(Messages, Alignment) // NOLINT { RunPushPullWithAlignment(GetParam().transport, "message_alignment", GetParam().expandedShmMetadata); } TEST_P(Messages, Empty) // NOLINT { EmptyMessage(GetParam().transport, "empty_message", GetParam().expandedShmMetadata); } INSTANTIATE_TEST_SUITE_P(, Messages, test::AllTransports(), test::TransportParamName()); // GetMeta() + GetDataAddressFromHandle() round-trip: simulates a consumer device that // receives message metadata over a side channel and resolves it to a local pointer. // Uses a second factory on the same session to exercise the open_only segment attach path. auto SideChannel(bool expandedShmMetadata = false) -> void { const string session = tools::Uuid(); ProgOptions producerConfig; test::Configure(producerConfig, session, expandedShmMetadata); auto producerFactory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &producerConfig)); ProgOptions consumerConfig; test::Configure(consumerConfig, session); auto consumerFactory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &consumerConfig)); const size_t size = 2; MessagePtr msg(producerFactory->CreateMessage(size)); memcpy(msg->GetData(), "AB", size); // producer side: extract metadata to send over the side channel const auto meta = static_cast(*msg).GetMeta(); EXPECT_EQ(meta.fSize, size); EXPECT_TRUE(meta.fManaged); // consumer side: resolve via a different factory — exercises open_only segment attach. // The virtual address differs between factory mappings; only the content must match. char* ptr = shmem::GetDataAddressFromHandle(*consumerFactory, meta); ASSERT_NE(ptr, nullptr); EXPECT_EQ(ptr[0], 'A'); EXPECT_EQ(ptr[1], 'B'); } // Uses a second factory on the same session to exercise the remote region lookup path. auto SideChannelUnmanaged() -> void { const string session = tools::Uuid(); ProgOptions producerConfig; test::Configure(producerConfig, session); auto producerFactory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &producerConfig)); ProgOptions consumerConfig; test::Configure(consumerConfig, session); auto consumerFactory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &consumerConfig)); const size_t regionSize = 1000000; test::Semaphore blocker; auto region = producerFactory->CreateUnmanagedRegion(regionSize, [&blocker](void*, size_t, void*) { blocker.Signal(); }); const size_t size = 2; auto msg(producerFactory->CreateMessage(region, static_cast(region->GetData()), size, nullptr)); memcpy(msg->GetData(), "AB", size); const auto meta = static_cast(*msg).GetMeta(); EXPECT_EQ(meta.fSize, size); EXPECT_FALSE(meta.fManaged); // consumer resolves via its own factory — exercises remote region lookup. // The virtual address differs between factory mappings; only the content must match. char* ptr = shmem::GetDataAddressFromHandle(*consumerFactory, meta); ASSERT_NE(ptr, nullptr); EXPECT_EQ(ptr[0], 'A'); EXPECT_EQ(ptr[1], 'B'); msg.reset(); ASSERT_TRUE(blocker.Wait()); } auto ReuseReceivedRegionMessage() -> void { ProgOptions config; test::Configure(config); auto factory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config)); const size_t regionSize = 1000000; std::atomic releasedA{0}; std::atomic releasedB{0}; auto regionA = factory->CreateUnmanagedRegion(regionSize, [&](void*, size_t, void*) { ++releasedA; }); auto regionB = factory->CreateUnmanagedRegion(regionSize, [&](void*, size_t, void*) { ++releasedB; }); auto waitFor = [](std::atomic const& counter, int value) { return test::WaitUntil([&] { return counter == value; }); }; Channel push{"Push", "push", factory}; Channel pull{"Pull", "pull", factory}; test::IpcAddress const address("reuse_received_region_msg"); ASSERT_TRUE(push.Bind(address)); ASSERT_TRUE(pull.Connect(address)); // Receive a block of region A and read it. This resolves region A in the message. auto inMsg(pull.NewMessage()); { auto outMsg(push.NewMessage(regionA, regionA->GetData(), 2, nullptr)); ASSERT_EQ(push.Send(outMsg), 2); } ASSERT_EQ(pull.Receive(inMsg), 2); ASSERT_NE(inMsg->GetData(), nullptr); inMsg->Rebuild(); ASSERT_TRUE(waitFor(releasedA, 1)); // Receive a block of region B into the same message, and release it without a read. { auto outMsg(push.NewMessage(regionB, regionB->GetData(), 2, nullptr)); ASSERT_EQ(push.Send(outMsg), 2); } ASSERT_EQ(pull.Receive(inMsg), 2); inMsg->Rebuild(); // The block must go back to region B, not to region A. EXPECT_TRUE(waitFor(releasedB, 1)); EXPECT_EQ(releasedA, 1); } auto SideChannelErrors() -> void { ProgOptions config; test::Configure(config); auto shmFactory(TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config)); auto zmqFactory(TransportFactory::CreateTransportFactory("zeromq", tools::Uuid(), &config)); // non-shmem factory must throw shmem::MetaHeader meta{}; meta.fManaged = true; EXPECT_THROW(shmem::GetDataAddressFromHandle(*zmqFactory, meta), shmem::SharedMemoryError); // bad segment id must throw meta.fSize = 1; meta.fSegmentId = 999; EXPECT_THROW(shmem::GetDataAddressFromHandle(*shmFactory, meta), shmem::SharedMemoryError); // zero-size managed message must return nullptr (mirrors Message::GetData()) meta.fSize = 0; EXPECT_EQ(shmem::GetDataAddressFromHandle(*shmFactory, meta), nullptr); } TEST(Footprint, shmem) // NOLINT { // Guard against size regressions (see issue #477). The values are for LP64 platforms. if constexpr (sizeof(void*) == 8) { // vptr + fTransport (16) + 5 * 8 bytes + 2 * uint16_t + 2 * bool, padded to 8 EXPECT_LE(sizeof(shmem::Message), 72); // MetaHeader is the wire format of the metadata message, keep it stable EXPECT_EQ(sizeof(shmem::MetaHeader), 40); } } TEST(MetaHeaderLayout, shmem) // NOLINT { // Users (e.g. AliceO2) create MetaHeader with positional aggregate initialization, // and the struct is the wire format of the metadata message. Keep the field order. const shmem::MetaHeader meta{1, 2, 3, 4, 5, 6, true}; EXPECT_EQ(meta.fSize, 1); EXPECT_EQ(meta.fHint, 2); EXPECT_EQ(meta.fHandle, 3); EXPECT_EQ(meta.fShared, 4); EXPECT_EQ(meta.fRegionId, 5); EXPECT_EQ(meta.fSegmentId, 6); EXPECT_TRUE(meta.fManaged); if constexpr (sizeof(void*) == 8) { EXPECT_EQ(offsetof(shmem::MetaHeader, fSize), 0); EXPECT_EQ(offsetof(shmem::MetaHeader, fHint), 8); EXPECT_EQ(offsetof(shmem::MetaHeader, fHandle), 16); EXPECT_EQ(offsetof(shmem::MetaHeader, fShared), 24); EXPECT_EQ(offsetof(shmem::MetaHeader, fRegionId), 32); EXPECT_EQ(offsetof(shmem::MetaHeader, fSegmentId), 34); EXPECT_EQ(offsetof(shmem::MetaHeader, fManaged), 36); } } TEST(SideChannel, shmem_unmanaged) // NOLINT { SideChannelUnmanaged(); } TEST(ReuseReceivedRegionMessage, shmem) // NOLINT { ReuseReceivedRegionMessage(); } TEST(SideChannel, errors) // NOLINT { SideChannelErrors(); } using ShmMessages = test::TransportTest; TEST_P(ShmMessages, ZeroCopy) // NOLINT { ZeroCopy(GetParam().expandedShmMetadata); } TEST_P(ShmMessages, SideChannel) // NOLINT { SideChannel(GetParam().expandedShmMetadata); } TEST_P(ShmMessages, ZeroCopyFromUnmanaged) // NOLINT { ZeroCopyFromUnmanaged("zerocopy_unmanaged", GetParam().expandedShmMetadata, 10000000); } TEST_P(ShmMessages, ZeroCopyFromUnmanaged_no_rc_segment) // NOLINT { ZeroCopyFromUnmanaged("zerocopy_unmanaged_no_rc_segment", GetParam().expandedShmMetadata, 0); } INSTANTIATE_TEST_SUITE_P(, ShmMessages, test::ShmTransports(), test::TransportParamName()); } // namespace