mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-16 01:51:45 +00:00
Shm: configurable allocation strategy
This commit is contained in:
@@ -19,8 +19,11 @@
|
||||
#include <boost/interprocess/containers/map.hpp>
|
||||
#include <boost/interprocess/containers/string.hpp>
|
||||
#include <boost/interprocess/containers/vector.hpp>
|
||||
#include <boost/interprocess/indexes/null_index.hpp>
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
#include <boost/interprocess/mem_algo/simple_seq_fit.hpp>
|
||||
#include <boost/unordered_map.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
@@ -32,6 +35,17 @@ namespace mq
|
||||
namespace shmem
|
||||
{
|
||||
|
||||
struct SharedMemoryError : std::runtime_error { using std::runtime_error::runtime_error; };
|
||||
|
||||
using SimpleSeqFitSegment = boost::interprocess::basic_managed_shared_memory<char,
|
||||
boost::interprocess::simple_seq_fit<boost::interprocess::mutex_family>,
|
||||
boost::interprocess::null_index>;
|
||||
// boost::interprocess::iset_index>;
|
||||
using RBTreeBestFitSegment = boost::interprocess::basic_managed_shared_memory<char,
|
||||
boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,
|
||||
boost::interprocess::null_index>;
|
||||
// boost::interprocess::iset_index>;
|
||||
|
||||
using SegmentManager = boost::interprocess::managed_shared_memory::segment_manager;
|
||||
using VoidAlloc = boost::interprocess::allocator<void, SegmentManager>;
|
||||
using CharAlloc = boost::interprocess::allocator<char, SegmentManager>;
|
||||
@@ -39,6 +53,12 @@ using Str = boost::interprocess::basic_string<char, std::char_traits<
|
||||
using StrAlloc = boost::interprocess::allocator<Str, SegmentManager>;
|
||||
using StrVector = boost::interprocess::vector<Str, StrAlloc>;
|
||||
|
||||
enum class AllocationAlgorithm : int
|
||||
{
|
||||
rbtree_best_fit,
|
||||
simple_seq_fit
|
||||
};
|
||||
|
||||
struct RegionInfo
|
||||
{
|
||||
RegionInfo(const VoidAlloc& alloc)
|
||||
@@ -65,6 +85,19 @@ using Uint64RegionInfoPairAlloc = boost::interprocess::allocator<std::pair<const
|
||||
using Uint64RegionInfoMap = boost::interprocess::map<uint64_t, RegionInfo, std::less<uint64_t>, Uint64RegionInfoPairAlloc>;
|
||||
using Uint64RegionInfoHashMap = boost::unordered_map<uint64_t, RegionInfo, boost::hash<uint64_t>, std::equal_to<uint64_t>, Uint64RegionInfoPairAlloc>;
|
||||
|
||||
struct SegmentInfo
|
||||
{
|
||||
SegmentInfo(AllocationAlgorithm aa)
|
||||
: fAllocationAlgorithm(aa)
|
||||
{}
|
||||
|
||||
AllocationAlgorithm fAllocationAlgorithm;
|
||||
};
|
||||
|
||||
using Uint64SegmentInfoPairAlloc = boost::interprocess::allocator<std::pair<const uint64_t, SegmentInfo>, SegmentManager>;
|
||||
using Uint64SegmentInfoMap = boost::interprocess::map<uint64_t, SegmentInfo, std::less<uint64_t>, Uint64SegmentInfoPairAlloc>;
|
||||
using Uint64SegmentInfoHashMap = boost::unordered_map<uint64_t, SegmentInfo, boost::hash<uint64_t>, std::equal_to<uint64_t>, Uint64SegmentInfoPairAlloc>;
|
||||
|
||||
struct DeviceCounter
|
||||
{
|
||||
DeviceCounter(unsigned int c)
|
||||
@@ -153,6 +186,101 @@ inline std::string buildShmIdFromSessionIdAndUserId(const std::string& sessionId
|
||||
return shmId;
|
||||
}
|
||||
|
||||
struct SegmentSize : public boost::static_visitor<size_t>
|
||||
{
|
||||
template<typename S>
|
||||
size_t operator()(S& s) const { return s.get_size(); }
|
||||
};
|
||||
|
||||
struct SegmentAddress : public boost::static_visitor<void*>
|
||||
{
|
||||
template<typename S>
|
||||
void* operator()(S& s) const { return s.get_address(); }
|
||||
};
|
||||
|
||||
struct SegmentMemoryZeroer : public boost::static_visitor<>
|
||||
{
|
||||
template<typename S>
|
||||
void operator()(S& s) const { s.zero_free_memory(); }
|
||||
};
|
||||
|
||||
struct SegmentFreeMemory : public boost::static_visitor<size_t>
|
||||
{
|
||||
template<typename S>
|
||||
size_t operator()(S& s) const { return s.get_free_memory(); }
|
||||
};
|
||||
|
||||
struct SegmentHandleFromAddress : public boost::static_visitor<boost::interprocess::managed_shared_memory::handle_t>
|
||||
{
|
||||
SegmentHandleFromAddress(const void* _ptr) : ptr(_ptr) {}
|
||||
|
||||
template<typename S>
|
||||
boost::interprocess::managed_shared_memory::handle_t operator()(S& s) const { return s.get_handle_from_address(ptr); }
|
||||
|
||||
const void* ptr;
|
||||
};
|
||||
|
||||
struct SegmentAddressFromHandle : public boost::static_visitor<void*>
|
||||
{
|
||||
SegmentAddressFromHandle(const boost::interprocess::managed_shared_memory::handle_t _handle) : handle(_handle) {}
|
||||
|
||||
template<typename S>
|
||||
void* operator()(S& s) const { return s.get_address_from_handle(handle); }
|
||||
|
||||
const boost::interprocess::managed_shared_memory::handle_t handle;
|
||||
};
|
||||
|
||||
struct SegmentAllocate : public boost::static_visitor<void*>
|
||||
{
|
||||
SegmentAllocate(const size_t _size) : size(_size) {}
|
||||
|
||||
template<typename S>
|
||||
void* operator()(S& s) const { return s.allocate(size); }
|
||||
|
||||
const size_t size;
|
||||
};
|
||||
|
||||
struct SegmentAllocateAligned : public boost::static_visitor<void*>
|
||||
{
|
||||
SegmentAllocateAligned(const size_t _size, const size_t _alignment) : size(_size), alignment(_alignment) {}
|
||||
|
||||
template<typename S>
|
||||
void* operator()(S& s) const { return s.allocate_aligned(size, alignment); }
|
||||
|
||||
const size_t size;
|
||||
const size_t alignment;
|
||||
};
|
||||
|
||||
struct SegmentBufferShrink : public boost::static_visitor<char*>
|
||||
{
|
||||
SegmentBufferShrink(const size_t _old_size, const size_t _new_size, char* _local_ptr)
|
||||
: old_size(_old_size)
|
||||
, new_size(_new_size)
|
||||
, local_ptr(_local_ptr)
|
||||
{}
|
||||
|
||||
template<typename S>
|
||||
char* operator()(S& s) const
|
||||
{
|
||||
boost::interprocess::managed_shared_memory::size_type shrunk_size = new_size;
|
||||
return s.template allocation_command<char>(boost::interprocess::shrink_in_place, old_size + 128, shrunk_size, local_ptr);
|
||||
}
|
||||
|
||||
const size_t old_size;
|
||||
const size_t new_size;
|
||||
mutable char* local_ptr;
|
||||
};
|
||||
|
||||
struct SegmentDeallocate : public boost::static_visitor<>
|
||||
{
|
||||
SegmentDeallocate(void* _ptr) : ptr(_ptr) {}
|
||||
|
||||
template<typename S>
|
||||
void operator()(S& s) const { return s.deallocate(ptr); }
|
||||
|
||||
void* ptr;
|
||||
};
|
||||
|
||||
} // namespace shmem
|
||||
} // namespace mq
|
||||
} // namespace fair
|
||||
|
Reference in New Issue
Block a user