Refactor shared memory storage to allow for multiple named datasets
This commit is contained in:
committed by
Patrick Niklaus
parent
bc120776f0
commit
666ce46d36
@@ -32,20 +32,28 @@ template <typename AlgorithmT, typename FacadeT> class DataWatchdogImpl;
|
||||
template <typename AlgorithmT>
|
||||
class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacade<AlgorithmT>> final
|
||||
{
|
||||
using mutex_type = typename storage::SharedMonitor<storage::SharedDataTimestamp>::mutex_type;
|
||||
using mutex_type = typename storage::SharedMonitor<storage::SharedRegionRegister>::mutex_type;
|
||||
using Facade = datafacade::ContiguousInternalMemoryDataFacade<AlgorithmT>;
|
||||
|
||||
public:
|
||||
DataWatchdogImpl() : active(true), timestamp(0)
|
||||
DataWatchdogImpl() : active(true)
|
||||
{
|
||||
// create the initial facade before launching the watchdog thread
|
||||
{
|
||||
boost::interprocess::scoped_lock<mutex_type> current_region_lock(barrier.get_mutex());
|
||||
|
||||
auto& shared_register = barrier.data();
|
||||
auto region_id = shared_register.Find("data");
|
||||
if (region_id == storage::SharedRegionRegister::INVALID_REGION_ID)
|
||||
{
|
||||
throw util::exception("Could not find shared memory region. Did you run osrm-datastore?");
|
||||
}
|
||||
shared_region = &shared_register.GetRegion(region_id);
|
||||
region = *shared_region;
|
||||
|
||||
facade_factory =
|
||||
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
|
||||
std::make_shared<datafacade::SharedMemoryAllocator>(barrier.data().region));
|
||||
timestamp = barrier.data().timestamp;
|
||||
std::make_shared<datafacade::SharedMemoryAllocator>(region.shm_key));
|
||||
}
|
||||
|
||||
watcher = std::thread(&DataWatchdogImpl::Run, this);
|
||||
@@ -74,30 +82,30 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
|
||||
{
|
||||
boost::interprocess::scoped_lock<mutex_type> current_region_lock(barrier.get_mutex());
|
||||
|
||||
while (active && timestamp == barrier.data().timestamp)
|
||||
while (active && region.timestamp == shared_region->timestamp)
|
||||
{
|
||||
barrier.wait(current_region_lock);
|
||||
}
|
||||
|
||||
if (timestamp != barrier.data().timestamp)
|
||||
if (region.timestamp != shared_region->timestamp)
|
||||
{
|
||||
auto region = barrier.data().region;
|
||||
region = *shared_region;
|
||||
facade_factory =
|
||||
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
|
||||
std::make_shared<datafacade::SharedMemoryAllocator>(region));
|
||||
timestamp = barrier.data().timestamp;
|
||||
util::Log() << "updated facade to region " << region << " with timestamp "
|
||||
<< timestamp;
|
||||
std::make_shared<datafacade::SharedMemoryAllocator>(region.shm_key));
|
||||
util::Log() << "updated facade to region " << region.shm_key << " with timestamp "
|
||||
<< region.timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
util::Log() << "DataWatchdog thread stopped";
|
||||
}
|
||||
|
||||
storage::SharedMonitor<storage::SharedDataTimestamp> barrier;
|
||||
storage::SharedMonitor<storage::SharedRegionRegister> barrier;
|
||||
std::thread watcher;
|
||||
bool active;
|
||||
unsigned timestamp;
|
||||
storage::SharedRegion region;
|
||||
storage::SharedRegion *shared_region;
|
||||
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT> facade_factory;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace datafacade
|
||||
class SharedMemoryAllocator : public ContiguousBlockAllocator
|
||||
{
|
||||
public:
|
||||
explicit SharedMemoryAllocator(storage::SharedDataType data_region);
|
||||
explicit SharedMemoryAllocator(storage::SharedRegionRegister::ShmKey data_shm_key);
|
||||
~SharedMemoryAllocator() override final;
|
||||
|
||||
// interface to give access to the datafacades
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
#include "util/exception.hpp"
|
||||
#include "util/exception_utils.hpp"
|
||||
#include "util/log.hpp"
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace osrm
|
||||
@@ -160,40 +160,100 @@ class DataLayout
|
||||
std::map<std::string, Block> blocks;
|
||||
};
|
||||
|
||||
enum SharedDataType
|
||||
struct SharedRegion
|
||||
{
|
||||
REGION_NONE,
|
||||
REGION_1,
|
||||
REGION_2
|
||||
};
|
||||
static constexpr const int MAX_NAME_LENGTH = 254;
|
||||
|
||||
struct SharedDataTimestamp
|
||||
{
|
||||
explicit SharedDataTimestamp(SharedDataType region, unsigned timestamp)
|
||||
: region(region), timestamp(timestamp)
|
||||
SharedRegion() : name{0}, timestamp{0} {}
|
||||
SharedRegion(const std::string &name_, std::uint64_t timestamp, std::uint8_t shm_key)
|
||||
: name{0}, timestamp{timestamp}, shm_key{shm_key}
|
||||
{
|
||||
std::copy_n(name_.begin(), std::min<std::size_t>(MAX_NAME_LENGTH, name_.size()), name);
|
||||
}
|
||||
|
||||
SharedDataType region;
|
||||
unsigned timestamp;
|
||||
bool IsEmpty() const { return timestamp == 0; }
|
||||
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
std::uint64_t timestamp;
|
||||
std::uint8_t shm_key;
|
||||
};
|
||||
|
||||
// Keeps a list of all shared regions in a fixed-sized struct
|
||||
// for fast access and deserialization.
|
||||
struct SharedRegionRegister
|
||||
{
|
||||
using RegionID = std::uint8_t;
|
||||
static constexpr const RegionID INVALID_REGION_ID = std::numeric_limits<RegionID>::max();
|
||||
using ShmKey = decltype(SharedRegion::shm_key);
|
||||
|
||||
// Returns the key of the region with the given name
|
||||
RegionID Find(const std::string &name) const
|
||||
{
|
||||
auto iter = std::find_if(regions.begin(), regions.end(), [&](const auto ®ion) {
|
||||
return std::strncmp(region.name, name.c_str(), SharedRegion::MAX_NAME_LENGTH) == 0;
|
||||
});
|
||||
|
||||
if (iter == regions.end())
|
||||
{
|
||||
return INVALID_REGION_ID;
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::distance(regions.begin(), iter);
|
||||
}
|
||||
}
|
||||
|
||||
RegionID Register(const std::string &name, ShmKey key)
|
||||
{
|
||||
auto iter = std::find_if(
|
||||
regions.begin(), regions.end(), [&](const auto ®ion) { return region.IsEmpty(); });
|
||||
if (iter == regions.end())
|
||||
{
|
||||
throw util::exception("No shared memory regions left. Could not register " + name +
|
||||
".");
|
||||
}
|
||||
else
|
||||
{
|
||||
constexpr std::uint32_t INITIAL_TIMESTAMP = 1;
|
||||
*iter = SharedRegion{name, INITIAL_TIMESTAMP, key};
|
||||
RegionID key = std::distance(regions.begin(), iter);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
void Deregister(const RegionID key) { regions[key] = SharedRegion{}; }
|
||||
|
||||
const auto &GetRegion(const RegionID key) const { return regions[key]; }
|
||||
|
||||
auto &GetRegion(const RegionID key) { return regions[key]; }
|
||||
|
||||
ShmKey ReserveKey()
|
||||
{
|
||||
auto free_key_iter = std::find(shm_key_in_use.begin(), shm_key_in_use.end(), false);
|
||||
if (free_key_iter == shm_key_in_use.end())
|
||||
{
|
||||
throw util::exception("Could not reserve a new SHM key. All keys are in use");
|
||||
}
|
||||
|
||||
*free_key_iter = true;
|
||||
return std::distance(shm_key_in_use.begin(), free_key_iter);
|
||||
}
|
||||
|
||||
void ReleaseKey(ShmKey key) { shm_key_in_use[key] = false; }
|
||||
|
||||
static constexpr const std::uint8_t MAX_SHARED_REGIONS =
|
||||
std::numeric_limits<RegionID>::max() - 1;
|
||||
static_assert(MAX_SHARED_REGIONS < std::numeric_limits<RegionID>::max(),
|
||||
"Number of shared memory regions needs to be less than the region id size.");
|
||||
|
||||
static constexpr const std::uint8_t MAX_SHM_KEYS = std::numeric_limits<std::uint8_t>::max() - 1;
|
||||
|
||||
static constexpr const char *name = "osrm-region";
|
||||
};
|
||||
|
||||
inline std::string regionToString(const SharedDataType region)
|
||||
{
|
||||
switch (region)
|
||||
{
|
||||
case REGION_1:
|
||||
return "REGION_1";
|
||||
case REGION_2:
|
||||
return "REGION_2";
|
||||
case REGION_NONE:
|
||||
return "REGION_NONE";
|
||||
default:
|
||||
return "INVALID_REGION";
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::array<SharedRegion, MAX_SHARED_REGIONS> regions;
|
||||
std::array<bool, MAX_SHM_KEYS> shm_key_in_use;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ template <typename Data> struct SharedMonitor
|
||||
bi::offset_t size = 0;
|
||||
if (shmem.get_size(size) && size == 0)
|
||||
{
|
||||
shmem.truncate(internal_size + sizeof(Data));
|
||||
shmem.truncate(rounded_internal_size + sizeof(Data));
|
||||
region = bi::mapped_region(shmem, bi::read_write);
|
||||
new (&internal()) InternalData;
|
||||
new (&data()) Data(initial_data);
|
||||
@@ -65,11 +65,11 @@ template <typename Data> struct SharedMonitor
|
||||
shmem = bi::shared_memory_object(bi::open_only, Data::name, bi::read_write);
|
||||
|
||||
bi::offset_t size = 0;
|
||||
if (!shmem.get_size(size) || size != internal_size + sizeof(Data))
|
||||
if (!shmem.get_size(size) || size != rounded_internal_size + sizeof(Data))
|
||||
{
|
||||
auto message =
|
||||
boost::format("Wrong shared memory block '%1%' size %2%, expected %3% bytes") %
|
||||
(const char *)Data::name % size % (internal_size + sizeof(Data));
|
||||
(const char *)Data::name % size % (rounded_internal_size + sizeof(Data));
|
||||
throw util::exception(message.str() + SOURCE_REF);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ template <typename Data> struct SharedMonitor
|
||||
Data &data() const
|
||||
{
|
||||
auto region_pointer = reinterpret_cast<char *>(region.get_address());
|
||||
return *reinterpret_cast<Data *>(region_pointer + internal_size);
|
||||
return *reinterpret_cast<Data *>(region_pointer + rounded_internal_size);
|
||||
}
|
||||
|
||||
mutex_type &get_mutex() const { return internal().mutex; }
|
||||
@@ -120,15 +120,13 @@ template <typename Data> struct SharedMonitor
|
||||
|
||||
private:
|
||||
#if USE_BOOST_INTERPROCESS_CONDITION
|
||||
|
||||
static constexpr int internal_size = 128;
|
||||
|
||||
struct InternalData
|
||||
{
|
||||
mutex_type mutex;
|
||||
bi::interprocess_condition condition;
|
||||
};
|
||||
|
||||
|
||||
#else
|
||||
// Implement a conditional variable using a queue of semaphores.
|
||||
// OSX checks the virtual address of a mutex in pthread_cond_wait and fails with EINVAL
|
||||
@@ -137,7 +135,6 @@ template <typename Data> struct SharedMonitor
|
||||
// fail if a waiter is killed.
|
||||
|
||||
static constexpr int buffer_size = 256;
|
||||
static constexpr int internal_size = 4 * 4096;
|
||||
|
||||
struct InternalData
|
||||
{
|
||||
@@ -210,9 +207,8 @@ template <typename Data> struct SharedMonitor
|
||||
static_assert(buffer_size >= 2, "buffer size is too small");
|
||||
|
||||
#endif
|
||||
|
||||
static_assert(sizeof(InternalData) + sizeof(Data) <= internal_size, "not enough space");
|
||||
static_assert(sizeof(InternalData) % alignof(Data) == 0, "incorrect data alignment");
|
||||
static constexpr int rounded_internal_size = ((sizeof(InternalData) + alignof(Data) - 1) / alignof(Data)) * alignof(Data);
|
||||
static_assert(rounded_internal_size < sizeof(InternalData) + sizeof(Data), "Data and internal data need to fit into shared memory");
|
||||
|
||||
InternalData &internal() const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user