Files
spark/src/lib/utils/SharedList.h
T

379 lines
11 KiB
C++

#pragma once
#include "lib/logging/logging.h"
#include "lib/utils/SharedMemory.h"
#include "lib/utils/bts_array_size.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_no_copy.h"
#include "lib/utils/defer.h"
#include "lib/config/config.h"
#include "lib/config/protos/system_config.pb.h"
#include "lib/os/os.h"
#include <atomic>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <utility>
#include <boost/filesystem.hpp>
#include <tbb/queuing_mutex.h>
namespace bts {
namespace utils {
/*
* SharedList<THeader, TEntry> - Write only, single thread owner, list in shared
* memory SharedListReader<THeader, TEntry> - Multi thread / multi reader safe
* contains the same items as SharedList of the same name
*
* SharedListEntryBase, SharedListHeaderBase
* - example structures for use in the SharedList showing what fields are
* required in the template
*
* Notes:
* initial_size and block_size should be multiples of the page size (4k)
* Not doing this won't break but will waste space.
*/
struct ALIGNED_(CACHE_LINE) SegmentHeader {
SegmentHeader() : count(0) {}
size_t count;
};
inline bool should_share(bool desire_shared) {
using namespace bts::config;
return desire_shared && !get<SystemConfig>().unittest_environment();
}
// Single writer, single thread!
template <class THeader, class TEntry> class SharedList {
public:
SharedList(
const std::string &name,
size_t initial_size_kb = 8192,
size_t block_size_kb = 1024 /*1mb*/,
bool shared = true)
: _name(name), _use_count(0), _block_size(block_size_kb * 1024),
_current_offset(0), _current_region_size(initial_size_kb * 1024),
_segments_mapped(0), _current_region(nullptr),
_current_segment_header(nullptr), _header(nullptr),
_shm(create_low_level_shared_memory(should_share(shared))) {
LOG(md, Info,
"SharedList::cons name: {} initial_size_kb: {} block_size_kb: {} "
"shared: {}",
name, initial_size_kb, block_size_kb, shared);
if (shared)
delete_segments(_name);
// Start with 1 segment
expand(_current_region_size);
// Create the header in the first segment
_header = new (allocate(sizeof(THeader))) THeader();
_header->segment_count = 1;
bts_assert_d(_current_offset > 0);
}
~SharedList() {
LOG(md, Info, "~SharedList {}", _name);
delete_segments(_name);
}
THeader &header() {
bts_assert_d(_header != nullptr);
return *_header;
}
TEntry *add(TEntry &entry) {
// We pass in an entry to copy isntead of forwarding parameters and using
// an in place construction to allow entry to vary in size
enter();
DEFER(leave());
void *buffer = allocate(entry.size());
memcpy(buffer, &entry, entry.size());
// After the entry is copied increment the segment counter and total
// counter So anyone walking will see the values
_current_segment_header->count++;
_header->count++;
return (TEntry *)buffer;
}
// Allow the client to pass in a size instead of a fully constructed object
// Assumption is that the result will be cast to the right type.
// Returned memory will be zeroed.
byte *add(size_t entry_size) {
enter();
DEFER(leave());
byte *buffer = allocate(entry_size);
memset(buffer, 0, entry_size);
_current_segment_header->count++;
_header->count++;
return buffer;
}
static void get_segment_name(
char *buffer,
size_t buffer_len,
const std::string &basename,
int index) {
bts_assert(
(size_t)sprintf(buffer, "%s_%i", basename.c_str(), index) <
buffer_len);
}
static int delete_segments(const std::string &name) {
using namespace bts::config;
if (!should_share(true))
return 0;
std::unique_ptr<LowLevelSharedMemory> pshm =
create_low_level_shared_memory(true);
int x;
int count = 0;
for (x = 0; x < 1024; x++) {
char segment_name[512];
get_segment_name(segment_name, BTS_ARRAYSIZE(segment_name), name, x);
bool removed = pshm->release(segment_name);
if (removed)
++count;
if (x - count > 10) {
break;
}
}
return count;
}
private:
void expand(size_t size) {
bts_assert(size > 0);
// using namespace boost::interprocess;
char segment_name[512];
get_segment_name(
segment_name, BTS_ARRAYSIZE(segment_name), _name, _segments_mapped);
LOG(md, Info, "Creating shared segment '{}'. size: {}",
std::string(segment_name), size);
_current_region = _shm->create(segment_name, size).address;
bts_assert_msg(
_current_region,
"expand failed for segment: " + std::string(segment_name));
_current_region_size = size;
_current_offset = 0;
// Start each segment with a SegmentHeader
_current_segment_header =
new (allocate(sizeof(SegmentHeader))) SegmentHeader();
if (_header) {
_header->segment_count++;
}
bts_assert(_current_segment_header->count == 0);
_segments_mapped++;
}
byte *allocate(uint32_t size) {
bts_assert(_current_region);
bts_assert(
size < _block_size ||
size < (_current_region_size - _current_offset));
// Always leave 1 blank TEntry at the end of the buffer
if (_current_offset + size + sizeof(TEntry) >= _current_region_size) {
expand(_block_size);
}
bts_assert(_current_offset + size < _current_region_size);
byte *result = _current_region + _current_offset;
_current_offset += size;
return result;
}
void enter() { bts_assert(++_use_count == 1); }
void leave() { --_use_count; }
private:
BTS_NO_COPY(SharedList);
const std::string _name;
std::atomic<uint32_t> _use_count;
size_t _block_size;
size_t _current_offset;
size_t _current_region_size;
size_t _segments_mapped;
byte *_current_region;
SegmentHeader *_current_segment_header;
THeader *_header;
std::unique_ptr<LowLevelSharedMemory> _shm;
};
// Thread safe to read
template <class THeader, class TEntry> class SharedListReader {
public:
SharedListReader(const std::string &name, bool shared = true)
: _name(name), _header(nullptr), _mutex(),
_shm(create_low_level_shared_memory(should_share(shared))),
_segments() {
init();
}
bool is_initialized() const { return _header != nullptr; }
const THeader &header() {
init();
if (!is_initialized())
return header_static_holder();
return *_header;
}
template <class CB> void all_entries(CB callback) {
init();
if (!is_initialized())
return;
// Copy the region list to avoid holding the lock
// Once a region is allocated it is never freed or moved so this is safe
tbb::queuing_mutex::scoped_lock lock(_mutex);
std::vector<Segment> segments(_segments.begin(), _segments.end());
lock.release();
// For each segment
// Start at 0 and walk until the end or until size=0
// Also stop when we reach the header count
size_t offset = sizeof(THeader) +
sizeof(SegmentHeader); // The first segment has both a
// SegmentHeader and a THeader
size_t count = 0;
for (const auto &segment : segments) {
size_t segment_count = 0;
const byte *region_base = segment.address;
size_t region_size = segment.size;
SegmentHeader *seg_header = (SegmentHeader *)region_base;
while (offset < region_size && segment_count < seg_header->count) {
const TEntry *entry = (TEntry *)(region_base + offset);
++count;
++segment_count;
callback(*entry);
offset += entry->size();
if (count == _header->count)
return;
}
// Each segment starts with a SegmentHeader
offset = sizeof(SegmentHeader);
}
}
void dump_to(const char *directory) const {
using namespace std;
boost::system::error_code _;
if (!boost::filesystem::exists(directory, _)) {
boost::filesystem::create_directories(directory, _);
}
if (!boost::filesystem::is_directory(directory)) {
LOG(md, Error, "Error dumping memory to {}", std::string(directory));
return;
}
boost::filesystem::path root(directory);
// OK to hold the lock for a while as we're typically crashing when this
// is called
tbb::queuing_mutex::scoped_lock lock(_mutex);
for (size_t x = 0; x < _segments.size(); ++x) {
char segment_name[512];
SharedList<THeader, TEntry>::get_segment_name(
segment_name, BTS_ARRAYSIZE(segment_name), _name, x);
// create/overwrite output file
std::ofstream writer(
(root / segment_name).string(), ios::out | ios::app | ios::binary);
if (writer.is_open()) {
// writer just loves signed types
writer.write(
(const char *)_segments[x].address,
(std::streamsize)_segments[x].size);
}
}
}
private:
BTS_NO_COPY(SharedListReader);
void init() {
tbb::queuing_mutex::scoped_lock lock(_mutex);
if (is_initialized()) {
check_segments();
return;
}
// Find the first segment
char segment_name[512];
SharedList<THeader, TEntry>::get_segment_name(
segment_name, BTS_ARRAYSIZE(segment_name), _name, 0);
auto segment = _shm->open(segment_name, !os::is_windows());
if (segment) {
LOG(md, Info, "Opening shared segment '{}' - SUCCEEDED",
std::string(segment_name));
_header = (THeader *)(segment.address + sizeof(SegmentHeader));
_segments.push_back(segment);
// Map all segments
check_segments();
} else {
LOG_ONCE_MAYBE(
md, Info, "Opening shared segment '{}' - FAILED",
std::string(segment_name));
return;
}
}
void check_segments() {
if (_segments.size() != _header->segment_count) {
for (uint32_t x = _segments.size(); x < _header->segment_count; ++x) {
char segment_name[512];
SharedList<THeader, TEntry>::get_segment_name(
segment_name, BTS_ARRAYSIZE(segment_name), _name, x);
auto segment = _shm->open(segment_name, !os::is_windows());
bts_assert(segment);
_segments.push_back(segment);
}
}
}
const THeader &header_static_holder() const {
static THeader header;
return header;
}
const std::string _name;
THeader *_header; // null value indicates no memory segment
mutable tbb::queuing_mutex _mutex;
std::unique_ptr<LowLevelSharedMemory> _shm;
std::vector<Segment> _segments;
};
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#endif
struct SharedListHeaderBase {
size_t count;
size_t segment_count;
};
struct SharedListEntryBase {
// Using a method allows us to compute a fixed size with no space cost
uint32_t size() const { return sizeof(SharedListEntryBase); }
bool is_public;
};
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
} // namespace utils
} // namespace bts