Files
FairMQ/test/transport/_shmem.cxx
Claude 50387fe5cc fix(shmem): fill the result of Monitor::GetDebugInfo()
- The function reserved each result vector and then wrote to
  result[segment][handle], past the end of the vector: it returned
  empty vectors and corrupted the heap
- Append one entry per buffer, in the order of the buffer offset
- Check the debug map for null before its first use
- Name the map entries (segment id, offset, buffer) instead of
  .first and .second
- Enable Monitor.GetDebugInfo (needs FAIRMQ_DEBUG_MODE)
- Update the documentation page that described the defect

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:42:53 +01:00

124 lines
4.0 KiB
C++

/********************************************************************************
* Copyright (C) 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 <fairmq/ProgOptions.h>
#include <fairmq/shmem/Monitor.h>
#include <fairmq/tools/Unique.h>
#include <fairmq/TransportFactory.h>
#include <helper/Transport.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <string>
#include <vector>
#include <unistd.h> // getpid
namespace
{
using namespace std;
using namespace fair::mq;
void GetFreeMemory()
{
ProgOptions config;
string const sessionId(test::Configure(config));
ASSERT_THROW(shmem::Monitor::GetFreeMemory(shmem::SessionId{sessionId}, 0), shmem::Monitor::MonitorError);
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
ASSERT_NO_THROW(shmem::Monitor::GetFreeMemory(shmem::SessionId{sessionId}, 0));
ASSERT_THROW(shmem::Monitor::GetFreeMemory(shmem::SessionId{sessionId}, 1), shmem::Monitor::MonitorError);
}
TEST(Monitor, GetFreeMemory)
{
GetFreeMemory();
}
auto ShmConfig(ProgOptions& config, bool noCleanup) -> shmem::SessionId
{
shmem::SessionId const session{test::Configure(config)};
// Without the monitor: it would clean up the segment of a closed factory.
config.SetProperty<bool>("shm-monitor", false);
config.SetProperty<bool>("shm-no-cleanup", noCleanup);
return session;
}
TEST(Monitor, SegmentIsPresent)
{
ProgOptions config;
auto const session = ShmConfig(config, false);
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 0));
{
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
EXPECT_TRUE(shmem::Monitor::SegmentIsPresent(session, 0));
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 1));
}
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 0));
}
TEST(Monitor, RegionIsPresent)
{
ProgOptions config;
auto const session = ShmConfig(config, false);
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
uint16_t id = 0;
{
auto region = factory->CreateUnmanagedRegion(10000, [](void*, size_t, void*) {});
id = region->GetId();
EXPECT_TRUE(shmem::Monitor::RegionIsPresent(session, id));
}
EXPECT_FALSE(shmem::Monitor::RegionIsPresent(session, id));
}
TEST(Monitor, CleanupAfterNoCleanup)
{
ProgOptions config;
auto const session = ShmConfig(config, true);
{
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
auto msg = factory->CreateMessage(1000);
}
// shm-no-cleanup: the segment stays after the factory is gone.
ASSERT_TRUE(shmem::Monitor::SegmentIsPresent(session, 0));
auto const removed = shmem::Monitor::Cleanup(session, false);
EXPECT_FALSE(removed.empty());
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 0));
}
#ifdef FAIRMQ_DEBUG_MODE
TEST(Monitor, GetDebugInfo)
{
ProgOptions config;
auto const session = ShmConfig(config, false);
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
auto msg1 = factory->CreateMessage(100);
auto msg2 = factory->CreateMessage(200);
auto const info = shmem::Monitor::GetDebugInfo(session);
ASSERT_EQ(info.count(0), 1U);
vector<size_t> sizes;
for (auto const& buffer : info.at(0)) {
EXPECT_EQ(buffer.fPid, getpid());
sizes.push_back(buffer.fSize);
}
sort(sizes.begin(), sizes.end());
EXPECT_EQ(sizes, (vector<size_t>{100, 200}));
}
#endif
} // namespace