Skip to content

Commit

Permalink
shm: reimplement alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
rbx authored and dennisklein committed Sep 7, 2021
1 parent 4e8f247 commit 815b2f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
49 changes: 38 additions & 11 deletions fairmq/shmem/Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/variant.hpp>

#include <algorithm> // max
#include <condition_variable>
#include <cstddef> // max_align_t
#include <cstdlib> // getenv
#include <cstring> // memcpy
#include <memory> // make_unique
#include <mutex>
#include <set>
Expand All @@ -55,6 +58,25 @@
namespace fair::mq::shmem
{

struct ShmPtr
{
explicit ShmPtr(char* rPtr)
: realPtr(rPtr)
{}

char* RealPtr()
{
return realPtr;
}

char* UserPtr()
{
return realPtr + sizeof(uint16_t) + *(reinterpret_cast<uint16_t*>(realPtr));
}

char* realPtr;
};

class Manager
{
public:
Expand Down Expand Up @@ -618,9 +640,13 @@ class Manager
return boost::apply_visitor(SegmentAddressFromHandle(handle), fSegments.at(segmentId));
}

char* Allocate(const size_t size, size_t alignment = 0)
ShmPtr Allocate(size_t size, size_t alignment = 0)
{
alignment = std::max(alignment, alignof(std::max_align_t));

char* ptr = nullptr;
// [offset(uint16_t)][alignment][buffer]
size_t fullSize = sizeof(uint16_t) + alignment + size;
// tools::RateLimiter rateLimiter(20);

while (ptr == nullptr) {
Expand All @@ -629,14 +655,15 @@ class Manager
// char* hint = 0; // unused for boost::interprocess::allocate_new
// ptr = fSegments.at(fSegmentId).allocation_command<char>(boost::interprocess::allocate_new, size, actualSize, hint);
size_t segmentSize = boost::apply_visitor(SegmentSize(), fSegments.at(fSegmentId));
if (size > segmentSize) {
throw MessageBadAlloc(tools::ToString("Requested message size (", size, ") exceeds segment size (", segmentSize, ")"));
}
if (alignment == 0) {
ptr = reinterpret_cast<char*>(boost::apply_visitor(SegmentAllocate{size}, fSegments.at(fSegmentId)));
} else {
ptr = reinterpret_cast<char*>(boost::apply_visitor(SegmentAllocateAligned{size, alignment}, fSegments.at(fSegmentId)));
if (fullSize > segmentSize) {
throw MessageBadAlloc(tools::ToString("Requested message size (", fullSize, ") exceeds segment size (", segmentSize, ")"));
}

ptr = reinterpret_cast<char*>(boost::apply_visitor(SegmentAllocate{fullSize}, fSegments.at(fSegmentId)));
assert(reinterpret_cast<uintptr_t>(ptr) % 2 == 0);
uint16_t offset = 0;
offset = alignment - ((reinterpret_cast<uintptr_t>(ptr) + sizeof(uint16_t)) % alignment);
std::memcpy(ptr, &offset, sizeof(offset));
} catch (boost::interprocess::bad_alloc& ba) {
// LOG(warn) << "Shared memory full...";
if (ThrowingOnBadAlloc()) {
Expand All @@ -645,7 +672,7 @@ class Manager
// rateLimiter.maybe_sleep();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (Interrupted()) {
return ptr;
return ShmPtr(ptr);
} else {
continue;
}
Expand All @@ -657,13 +684,13 @@ class Manager
(*fMsgDebug).emplace(fSegmentId, fShmVoidAlloc);
}
(*fMsgDebug).at(fSegmentId).emplace(
static_cast<size_t>(GetHandleFromAddress(ptr, fSegmentId)),
static_cast<size_t>(GetHandleFromAddress(ShmPtr(ptr).UserPtr(), fSegmentId)),
MsgDebug(getpid(), size, std::chrono::system_clock::now().time_since_epoch().count())
);
#endif
}

return ptr;
return ShmPtr(ptr);
}

void Deallocate(boost::interprocess::managed_shared_memory::handle_t handle, uint16_t segmentId)
Expand Down
26 changes: 15 additions & 11 deletions fairmq/shmem/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ class Message final : public fair::mq::Message
if (fMeta.fRegionId == 0) {
if (fMeta.fSize > 0) {
fManager.GetSegment(fMeta.fSegmentId);
fLocalPtr = reinterpret_cast<char*>(fManager.GetAddressFromHandle(fMeta.fHandle, fMeta.fSegmentId));
ShmPtr shmPtr(reinterpret_cast<char*>(fManager.GetAddressFromHandle(fMeta.fHandle, fMeta.fSegmentId)));
fLocalPtr = shmPtr.UserPtr();
} else {
fLocalPtr = nullptr;
}
Expand All @@ -202,7 +203,7 @@ class Message final : public fair::mq::Message
}
}

return fLocalPtr;
return static_cast<void*>(fLocalPtr);
}

size_t GetSize() const override { return fMeta.fSize; }
Expand All @@ -217,20 +218,22 @@ class Message final : public fair::mq::Message
} else if (newSize <= fMeta.fSize) {
try {
try {
fLocalPtr = fManager.ShrinkInPlace(newSize, fLocalPtr, fMeta.fSegmentId);
ShmPtr shmPtr(fManager.ShrinkInPlace(newSize, static_cast<char*>(fManager.GetAddressFromHandle(fMeta.fHandle, fMeta.fSegmentId)), fMeta.fSegmentId));
fLocalPtr = shmPtr.UserPtr();
fMeta.fSize = newSize;
return true;
} catch (boost::interprocess::bad_alloc& e) {
// if shrinking fails (can happen due to boost alignment requirements):
// unused size >= 1000000 bytes: reallocate fully
// unused size < 1000000 bytes: simply reset the size and keep the rest of the buffer until message destruction
if (fMeta.fSize - newSize >= 1000000) {
char* newPtr = fManager.Allocate(newSize, fAlignment);
if (newPtr) {
std::memcpy(newPtr, fLocalPtr, newSize);
ShmPtr shmPtr = fManager.Allocate(newSize, fAlignment);
if (shmPtr.RealPtr()) {
char* userPtr = shmPtr.UserPtr();
std::memcpy(userPtr, fLocalPtr, newSize);
fManager.Deallocate(fMeta.fHandle, fMeta.fSegmentId);
fLocalPtr = newPtr;
fMeta.fHandle = fManager.GetHandleFromAddress(fLocalPtr, fMeta.fSegmentId);
fLocalPtr = userPtr;
fMeta.fHandle = fManager.GetHandleFromAddress(shmPtr.RealPtr(), fMeta.fSegmentId);
} else {
LOG(debug) << "could not set used size: " << e.what();
return false;
Expand Down Expand Up @@ -288,10 +291,11 @@ class Message final : public fair::mq::Message

char* InitializeChunk(const size_t size, size_t alignment = 0)
{
fLocalPtr = fManager.Allocate(size, alignment);
if (fLocalPtr) {
fMeta.fHandle = fManager.GetHandleFromAddress(fLocalPtr, fMeta.fSegmentId);
ShmPtr shmPtr = fManager.Allocate(size, alignment);
if (shmPtr.RealPtr()) {
fMeta.fHandle = fManager.GetHandleFromAddress(shmPtr.RealPtr(), fMeta.fSegmentId);
fMeta.fSize = size;
fLocalPtr = shmPtr.UserPtr();
}
return fLocalPtr;
}
Expand Down

0 comments on commit 815b2f1

Please sign in to comment.