From 516d7e8ad77c48fe70788ba9413c269b3f3ad9c4 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 7 May 2020 22:49:06 -0700 Subject: [PATCH] ircd::db::database::allocator: Support aligned allocations. ircd::db::database::allocator: Implement callback for true allocated size hint. --- ircd/db.cc | 50 ++++++++++++++++++++++++++++++++++++++++++---- ircd/db_database.h | 8 ++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/ircd/db.cc b/ircd/db.cc index 0cdb84e54..935d683b3 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -3490,10 +3490,24 @@ const noexcept // #ifdef IRCD_DB_HAS_ALLOCATOR +decltype(ircd::db::database::allocator::ALIGN_DEFAULT) +ircd::db::database::allocator::ALIGN_DEFAULT +{ + #if defined(__AVX__) + 32 + #elif defined(__SSE__) + 16 + #else + sizeof(void *) + #endif +}; + ircd::db::database::allocator::allocator(database *const &d, - database::column *const &c) + database::column *const &c, + const size_t &alignment) :d{d} ,c{c} +,alignment{alignment} { } @@ -3504,10 +3518,20 @@ noexcept size_t ircd::db::database::allocator::UsableSize(void *const ptr, - size_t allocation_size) + size_t size) const noexcept { - return allocation_size; + const size_t ret + { + size % alignment != 0? + size + (alignment - (size % alignment)): + size + }; + + assert(ret % alignment == 0); + assert(ret < size + alignment); + assert(alignment % sizeof(void *) == 0); + return ret; } void @@ -3521,7 +3545,25 @@ void * ircd::db::database::allocator::Allocate(size_t size) noexcept { - return std::malloc(size); + auto ptr + { + ircd::allocator::aligned_alloc(alignment, size) + }; + + #ifdef RB_DEBUG_DB_ENV + assert(d); + log::debug + { + log, "[%s]'%s' allocate:%zu alignment:%zu %p", + db::name(*d), + c? string_view(db::name(*c)): string_view{}, + size, + alignment, + ptr.get(), + }; + #endif + + return ptr.release(); } const char * diff --git a/ircd/db_database.h b/ircd/db_database.h index dea742f54..f6e05b831 100644 --- a/ircd/db_database.h +++ b/ircd/db_database.h @@ -295,8 +295,11 @@ struct ircd::db::database::wal_filter struct ircd::db::database::allocator final :rocksdb::MemoryAllocator { + static const size_t ALIGN_DEFAULT; + database *d {nullptr}; - database::column *c{nullptr}; + database::column *c {nullptr}; + size_t alignment {ALIGN_DEFAULT}; const char *Name() const noexcept override; void *Allocate(size_t) noexcept override; @@ -304,7 +307,8 @@ struct ircd::db::database::allocator final size_t UsableSize(void *, size_t) const noexcept override; allocator(database *const &, - database::column *const & = nullptr); + database::column *const & = nullptr, + const size_t &alignment = ALIGN_DEFAULT); ~allocator() noexcept; };