0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd::db::database: Add custom allocator stub; apply per-cache allocators.

This commit is contained in:
Jason Volk 2020-05-04 20:30:47 -07:00
parent 9f2bdd3ca2
commit 36a0394184
3 changed files with 107 additions and 4 deletions

View file

@ -98,6 +98,7 @@ struct ircd::db::database
struct sst;
struct wal;
struct wal_filter;
struct allocator;
std::string name;
uint64_t checkpoint;
@ -110,6 +111,7 @@ struct ircd::db::database
std::shared_ptr<struct events> events;
std::shared_ptr<struct mergeop> mergeop;
std::unique_ptr<struct wal_filter> wal_filter;
std::shared_ptr<struct allocator> allocator;
std::shared_ptr<rocksdb::SstFileManager> ssts;
std::shared_ptr<rocksdb::Cache> row_cache;
std::vector<descriptor> descriptors;

View file

@ -1143,6 +1143,12 @@ try
{
std::make_unique<struct wal_filter>(this)
}
,allocator
{
#ifdef IRCD_DB_HAS_ALLOCATOR
std::make_shared<struct allocator>(this)
#endif
}
,ssts{rocksdb::NewSstFileManager
(
env.get(), // env
@ -1156,7 +1162,10 @@ try
)}
,row_cache
{
std::make_shared<database::cache>(this, this->stats, this->name, 16_MiB)
std::make_shared<database::cache>
(
this, this->stats, this->allocator, this->name, 16_MiB
)
}
,descriptors
{
@ -1879,7 +1888,16 @@ ircd::db::database::column::column(database &d,
,cmp{this->d, this->descriptor->cmp}
,prefix{this->d, this->descriptor->prefix}
,cfilter{this, this->descriptor->compactor}
,stats{std::make_shared<struct database::stats>(this->d)}
,stats
{
std::make_shared<struct database::stats>(this->d)
}
,allocator
{
#ifdef IRCD_DB_HAS_ALLOCATOR
std::make_shared<struct allocator>(this->d, this)
#endif
}
,handle
{
nullptr, [&d](rocksdb::ColumnFamilyHandle *const handle)
@ -2045,7 +2063,7 @@ ircd::db::database::column::column(database &d,
// Setup the cache for assets.
const auto &cache_size(this->descriptor->cache_size);
if(cache_size != 0)
table_opts.block_cache = std::make_shared<database::cache>(this->d, this->stats, this->name, cache_size);
table_opts.block_cache = std::make_shared<database::cache>(this->d, this->stats, this->allocator, this->name, cache_size);
// RocksDB will create an 8_MiB block_cache if we don't create our own.
// To honor the user's desire for a zero-size cache, this must be set.
@ -2058,7 +2076,7 @@ ircd::db::database::column::column(database &d,
// Setup the cache for compressed assets.
const auto &cache_size_comp(this->descriptor->cache_size_comp);
if(cache_size_comp != 0)
table_opts.block_cache_compressed = std::make_shared<database::cache>(this->d, this->stats, this->name, cache_size_comp);
table_opts.block_cache_compressed = std::make_shared<database::cache>(this->d, this->stats, this->allocator, this->name, cache_size_comp);
// Setup the bloom filter.
const auto &bloom_bits(this->descriptor->bloom_bits);
@ -3031,11 +3049,13 @@ ircd::db::database::cache::DEFAULT_HI_PRIO
ircd::db::database::cache::cache(database *const &d,
std::shared_ptr<struct database::stats> stats,
std::shared_ptr<struct database::allocator> allocator,
std::string name,
const ssize_t &initial_capacity)
:d{d}
,name{std::move(name)}
,stats{std::move(stats)}
,allocator{std::move(allocator)}
,c
{
rocksdb::NewLRUCache
@ -3044,6 +3064,9 @@ ircd::db::database::cache::cache(database *const &d,
,DEFAULT_SHARD_BITS
,DEFAULT_STRICT
,DEFAULT_HI_PRIO
#ifdef IRCD_DB_HAS_ALLOCATOR
,d->allocator
#endif
)
}
{
@ -3446,6 +3469,56 @@ const noexcept
return db::name(*d).c_str();
}
///////////////////////////////////////////////////////////////////////////////
//
// database::allocator
//
#ifdef IRCD_DB_HAS_ALLOCATOR
ircd::db::database::allocator::allocator(database *const &d,
database::column *const &c)
:d{d}
,c{c}
{
}
ircd::db::database::allocator::~allocator()
noexcept
{
}
size_t
ircd::db::database::allocator::UsableSize(void *const ptr,
size_t allocation_size)
const noexcept
{
return allocation_size;
}
void
ircd::db::database::allocator::Deallocate(void *const ptr)
noexcept
{
std::free(ptr);
}
void *
ircd::db::database::allocator::Allocate(size_t size)
noexcept
{
return std::malloc(size);
}
const char *
ircd::db::database::allocator::Name()
const noexcept
{
return c? db::name(*c).c_str():
d? db::name(*d).c_str():
"unaffiliated";
}
#endif IRCD_DB_HAS_ALLOCATOR
///////////////////////////////////////////////////////////////////////////////
//

View file

@ -27,6 +27,11 @@ namespace ircd::db
#define IRCD_DB_HAS_CACHE_GETCHARGE
#endif
#if ROCKSDB_MAJOR > 5 \
|| (ROCKSDB_MAJOR == 5 && ROCKSDB_MINOR >= 18)
#define IRCD_DB_HAS_ALLOCATOR
#endif
struct ircd::db::database::cache final
:std::enable_shared_from_this<ircd::db::database::cache>
,rocksdb::Cache
@ -44,6 +49,7 @@ struct ircd::db::database::cache final
database *d;
std::string name;
std::shared_ptr<struct database::stats> stats;
std::shared_ptr<struct database::allocator> allocator;
std::shared_ptr<rocksdb::Cache> c;
const char *Name() const noexcept override;
@ -71,6 +77,7 @@ struct ircd::db::database::cache final
cache(database *const &,
std::shared_ptr<struct database::stats>,
std::shared_ptr<struct database::allocator>,
std::string name,
const ssize_t &initial_capacity = -1);
@ -160,6 +167,7 @@ struct ircd::db::database::column final
prefix_transform prefix;
compaction_filter cfilter;
std::shared_ptr<struct database::stats> stats;
std::shared_ptr<struct database::allocator> allocator;
rocksdb::BlockBasedTableOptions table_opts;
custom_ptr<rocksdb::ColumnFamilyHandle> handle;
@ -282,3 +290,23 @@ struct ircd::db::database::wal_filter
wal_filter(database *const &);
~wal_filter() noexcept;
};
#ifdef IRCD_DB_HAS_ALLOCATOR
/// Dynamic memory
struct ircd::db::database::allocator
:rocksdb::MemoryAllocator
{
database *d {nullptr};
database::column *c{nullptr};
const char *Name() const noexcept override;
void *Allocate(size_t) noexcept override;
void Deallocate(void *) noexcept override;
size_t UsableSize(void *, size_t) const noexcept override;
allocator(database *const &,
database::column *const & = nullptr);
~allocator() noexcept;
};
#endif