Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,14 @@ PLATFORM_IO_SYS = $(OBJDIR)/$(SRCDIR)/$(PLATFORM_DIR)/laio.o

UTIL_SYS = $(OBJDIR)/$(SRCDIR)/util.o $(PLATFORM_SYS)

CLOCKCACHE_SYS = $(OBJDIR)/$(SRCDIR)/clockcache.o \
$(OBJDIR)/$(SRCDIR)/allocator.o \
$(OBJDIR)/$(SRCDIR)/rc_allocator.o \
ALLOCATOR_SYS = $(OBJDIR)/$(SRCDIR)/allocator.o \
$(OBJDIR)/$(SRCDIR)/rc_allocator.o \
$(PLATFORM_IO_SYS)

CLOCKCACHE_SYS = $(OBJDIR)/$(SRCDIR)/clockcache.o \
$(OBJDIR)/$(SRCDIR)/task.o \
$(ALLOCATOR_SYS) \
$(UTIL_SYS) \
$(PLATFORM_IO_SYS)

BTREE_SYS = $(OBJDIR)/$(SRCDIR)/btree.o \
$(OBJDIR)/$(SRCDIR)/data_internal.o \
Expand Down Expand Up @@ -462,6 +464,14 @@ $(BINDIR)/$(UNITDIR)/platform_apis_test: $(UTIL_SYS) \
$(COMMON_UNIT_TESTOBJ) \
$(PLATFORM_SYS)

$(BINDIR)/$(UNITDIR)/allocator_test: $(ALLOCATOR_SYS) \
$(OBJDIR)/$(TESTS_DIR)/config.o \
$(UTIL_SYS)

$(BINDIR)/$(UNITDIR)/mini_allocator_test: $(COMMON_TESTOBJ) \
$(OBJDIR)/$(FUNCTIONAL_TESTSDIR)/test_async.o \
$(LIBDIR)/libsplinterdb.so

########################################
# Convenience mini unit-test targets
unit/util_test: $(BINDIR)/$(UNITDIR)/util_test
Expand Down
42 changes: 42 additions & 0 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,45 @@ allocator_config_init(allocator_config *allocator_cfg,
uint64 log_extent_size = 63 - __builtin_clzll(io_cfg->extent_size);
allocator_cfg->extent_mask = ~((1ULL << log_extent_size) - 1);
}

/*
* Return page number for the page at 'addr', in terms of page-size.
* This routine assume that input 'addr' is a valid page address.
*/
uint64
allocator_page_number(allocator *al, uint64 page_addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
debug_assert(allocator_valid_page_addr(al, page_addr));
return ((page_addr / allocator_cfg->io_cfg->page_size));
}

/*
* Return page offset for the page at 'addr', in terms of page-size,
* as an index into the extent holding the page.
* This routine assume that input 'addr' is a valid page address.
*
* Returns index from [0 .. ( <#-of-pages-in-an-extent> - 1) ]
*/
uint64
allocator_page_offset(allocator *al, uint64 page_addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
debug_assert(allocator_valid_page_addr(al, page_addr));
uint64 npages_in_extent =
(allocator_cfg->io_cfg->extent_size / allocator_cfg->io_cfg->page_size);
return (allocator_page_number(al, page_addr) % npages_in_extent);
}

/*
* Return extent number of the extent holding the page at 'addr'.
* This routine assume that input 'addr' is a valid page address.
*/
uint64
allocator_extent_number(allocator *al, uint64 page_addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
debug_assert(allocator_valid_page_addr(al, page_addr));
return ((allocator_extent_base_addr(al, page_addr)
/ allocator_cfg->io_cfg->extent_size));
}
58 changes: 53 additions & 5 deletions src/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ allocator_config_init(allocator_config *allocator_cfg,
io_config *io_cfg,
uint64 capacity);

// Return the address of the extent holding page at address 'addr'
static inline uint64
allocator_config_extent_base_addr(allocator_config *allocator_cfg, uint64 addr)
{
Expand Down Expand Up @@ -253,12 +254,50 @@ allocator_print_allocated(allocator *al)
return al->ops->print_allocated(al);
}

// Return the address of the extent holding page at address 'addr'
static inline uint64
allocator_extent_base_addr(allocator *al, uint64 addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
return allocator_config_extent_base_addr(allocator_cfg, addr);
}

// Is the 'addr' a valid page address?
static inline bool
allocator_page_valid(allocator *al, uint64 addr)
allocator_valid_page_addr(allocator *al, uint64 addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
return ((addr % allocator_cfg->io_cfg->page_size) == 0);
}

// Returns the address of the page next to input 'page_addr'
static inline uint64
allocator_next_page_addr(allocator *al, uint64 page_addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
return (page_addr + allocator_cfg->io_cfg->page_size);
}

if ((addr % allocator_cfg->io_cfg->page_size) != 0) {
/*
* Is the 'addr' a valid address of the start of an extent;
* i.e. an extent address?
*/
static inline bool
allocator_valid_extent_addr(allocator *al, uint64 addr)
{
return (allocator_extent_base_addr(al, addr) == addr);
}

/*
* Check if the page given by address 'addr' is a valid page-address within the
* database capacity and that the holding extent is also allocated (i.e., has a
* non-zero ref-count).
*/
static inline bool
allocator_page_valid(allocator *al, uint64 addr)
{
allocator_config *allocator_cfg = allocator_get_config(al);
if (!allocator_valid_page_addr(al, addr)) {
platform_error_log("%s():%d: Specified addr=%lu is not divisible by"
" configured page size=%lu\n",
__FUNCTION__,
Expand All @@ -268,8 +307,8 @@ allocator_page_valid(allocator *al, uint64 addr)
return FALSE;
}

uint64 base_addr = allocator_config_extent_base_addr(allocator_cfg, addr);
if ((base_addr != 0) && (addr < allocator_cfg->capacity)) {
uint64 base_addr = allocator_extent_base_addr(al, addr);
if ((base_addr != 0) && (addr < allocator_get_capacity(al))) {
uint8 refcount = allocator_get_refcount(al, base_addr);
if (refcount == 0) {
platform_error_log(
Expand All @@ -285,7 +324,7 @@ allocator_page_valid(allocator *al, uint64 addr)
} else {
platform_error_log("%s():%d: Extent out of allocator capacity range."
" base_addr=%lu, addr=%lu"
", allocator_get_capacity()=%lu\n",
", allocator_get_capacity()=%lu pages.\n",
__FUNCTION__,
__LINE__,
base_addr,
Expand All @@ -294,3 +333,12 @@ allocator_page_valid(allocator *al, uint64 addr)
return FALSE;
}
}

uint64
allocator_page_number(allocator *al, uint64 addr);

uint64
allocator_page_offset(allocator *al, uint64 page_addr);

uint64
allocator_extent_number(allocator *al, uint64 addr);
25 changes: 12 additions & 13 deletions src/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,11 @@ btree_truncate_index(const btree_config *cfg, // IN
*-----------------------------------------------------------------------------
* btree_alloc --
*
* Allocates a node from the preallocator. Will refill it if there are no
* more nodes available for the given height.
* Allocates a new page from the mini-allocator for a new BTree node.
* from the (previously setup) mini-allocator. Will refill the mini-
* allocator's cache of pre-allocated extents if there are no more nodes
* (pages) available from the already-allocated extent for the given
* height.
*-----------------------------------------------------------------------------
*/
bool
Expand Down Expand Up @@ -1132,15 +1135,6 @@ btree_addrs_share_extent(cache *cc, uint64 left_addr, uint64 right_addr)
allocator_get_config(al), right_addr, left_addr);
}

static inline uint64
btree_root_to_meta_addr(const btree_config *cfg,
uint64 root_addr,
uint64 meta_page_no)
{
return root_addr + (meta_page_no + 1) * btree_page_size(cfg);
}


/*----------------------------------------------------------
* Creating and destroying B-trees.
*----------------------------------------------------------
Expand All @@ -1158,11 +1152,15 @@ btree_create(cache *cc,
uint64 base_addr;
platform_status rc = allocator_alloc(al, &base_addr, type);
platform_assert_status_ok(rc);
platform_assert(allocator_valid_extent_addr(al, base_addr),
"base_addr=%lu is not a valid start of extent addr.\n",
base_addr);
page_handle *root_page = cache_alloc(cc, base_addr, type);
bool pinned = (type == PAGE_TYPE_MEMTABLE);

// set up the root
btree_node root;
ZERO_STRUCT(root);
root.page = root_page;
root.addr = base_addr;
root.hdr = (btree_hdr *)root_page->data;
Expand All @@ -1181,15 +1179,16 @@ btree_create(cache *cc,
cache_unclaim(cc, root_page);
cache_unget(cc, root_page);

// set up the mini allocator
// set up the mini allocator, using the page adjacent to BTree root page as
// the meta-head page for the mini-allocator.
mini_init(mini,
cc,
cfg->data_cfg,
root.addr + btree_page_size(cfg),
0,
BTREE_MAX_HEIGHT,
type,
type == PAGE_TYPE_BRANCH);
type == PAGE_TYPE_BRANCH); // Unkeyed mini-allocator for Memtable

return root.addr;
}
Expand Down
8 changes: 8 additions & 0 deletions src/btree_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,11 @@ btree_get_child_addr(const btree_config *cfg,
{
return index_entry_child_addr(btree_get_index_entry(cfg, hdr, k));
}

static inline uint64
btree_root_to_meta_addr(const btree_config *cfg,
uint64 root_addr,
uint64 meta_page_no)
{
return root_addr + (meta_page_no + 1) * btree_page_size(cfg);
}
Loading