Skip to content

Commit

Permalink
Identify Memtable v/s Branch page types via BTree-print routines.
Browse files Browse the repository at this point in the history
This commit extends BTree-print routines to also report the page
type, whether it's a branch or a memtable BTree. As the structures and
print methods are shared between two objects, this extra information
will help in diagnostics. Trunk nodes are likewise identified.
Minor fix in trunk_print_pivots() to align outputs for pivot key's string.
  • Loading branch information
gapisback committed Jan 13, 2023
1 parent 7e85a29 commit a668628
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 45 deletions.
95 changes: 69 additions & 26 deletions src/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2552,7 +2552,8 @@ btree_iterator_print(iterator *itor)
btree_print_node(Platform_default_log_handle,
btree_itor->cc,
btree_itor->cfg,
&btree_itor->curr);
&btree_itor->curr,
PAGE_TYPE_BRANCH);
}

const static iterator_ops btree_iterator_ops = {
Expand Down Expand Up @@ -3139,9 +3140,11 @@ static void
btree_print_index_node(platform_log_handle *log_handle,
btree_config *cfg,
uint64 addr,
btree_hdr *hdr)
btree_hdr *hdr,
page_type type)
{
platform_log(log_handle, "** INDEX NODE \n");
platform_log(
log_handle, "** Page type: %s, INDEX NODE \n", page_type_str[type]);
platform_log(log_handle, "** Header ptr: %p\n", hdr);
platform_log(log_handle, "** addr: %lu \n", addr);
platform_log(log_handle, "** next_addr: %lu \n", hdr->next_addr);
Expand Down Expand Up @@ -3182,9 +3185,11 @@ static void
btree_print_leaf_node(platform_log_handle *log_handle,
btree_config *cfg,
uint64 addr,
btree_hdr *hdr)
btree_hdr *hdr,
page_type type)
{
platform_log(log_handle, "** LEAF NODE \n");
platform_log(
log_handle, "** Page type: %s, LEAF NODE \n", page_type_str[type]);
platform_log(log_handle, "** hdrptr: %p\n", hdr);
platform_log(log_handle, "** addr: %lu \n", addr);
platform_log(log_handle, "** next_addr: %lu \n", hdr->next_addr);
Expand Down Expand Up @@ -3220,14 +3225,15 @@ void
btree_print_locked_node(platform_log_handle *log_handle,
btree_config *cfg,
uint64 addr,
btree_hdr *hdr)
btree_hdr *hdr,
page_type type)
{
platform_log(log_handle, "*******************\n");
platform_log(log_handle, "BTree node at addr=%lu\n{\n", addr);
if (btree_height(hdr) > 0) {
btree_print_index_node(log_handle, cfg, addr, hdr);
btree_print_index_node(log_handle, cfg, addr, hdr, type);
} else {
btree_print_leaf_node(log_handle, cfg, addr, hdr);
btree_print_leaf_node(log_handle, cfg, addr, hdr, type);
}
platform_log(log_handle, "} -- End BTree node at addr=%lu\n", addr);
}
Expand All @@ -3237,7 +3243,8 @@ void
btree_print_node(platform_log_handle *log_handle,
cache *cc,
btree_config *cfg,
btree_node *node)
btree_node *node,
page_type type)
{
if (!allocator_page_valid(cache_get_allocator(cc), node->addr)) {
platform_log(log_handle, "*******************\n");
Expand All @@ -3246,42 +3253,52 @@ btree_print_node(platform_log_handle *log_handle,
platform_log(log_handle, "-------------------\n");
return;
}
btree_node_get(cc, cfg, node, PAGE_TYPE_BRANCH);
btree_print_locked_node(log_handle, cfg, node->addr, node->hdr);
btree_node_get(cc, cfg, node, type);
btree_print_locked_node(log_handle, cfg, node->addr, node->hdr, type);
btree_node_unget(cc, cfg, node);
}

void
btree_print_subtree(platform_log_handle *log_handle,
cache *cc,
btree_config *cfg,
uint64 addr)
uint64 addr,
page_type type)
{
btree_node node;
node.addr = addr;
btree_print_node(log_handle, cc, cfg, &node);
if (!allocator_page_valid(cache_get_allocator(cc), node.addr)) {
platform_log(log_handle, "Unallocated BTree node addr=%lu\n", addr);
platform_log(log_handle,
"Unallocated %s BTree node addr=%lu\n",
page_type_str[type],
addr);
return;
}
btree_node_get(cc, cfg, &node, PAGE_TYPE_BRANCH);
// Print node's contents only if it's a validly allocated node.
btree_print_node(log_handle, cc, cfg, &node, type);

btree_node_get(cc, cfg, &node, type);
table_index idx;

if (node.hdr->height > 0) {
int nentries = node.hdr->num_entries;
platform_log(log_handle,
"\n---- BTree sub-trees under addr=%lu"
"\n---- Page type: %s, BTree sub-trees under addr=%lu"
" num_entries=%d"
", height=%d {\n",
page_type_str[type],
addr,
nentries,
node.hdr->height);

for (idx = 0; idx < nentries; idx++) {
platform_log(
log_handle, "\n-- Sub-tree index=%d of %d\n", idx, nentries);
btree_print_subtree(
log_handle, cc, cfg, btree_get_child_addr(cfg, node.hdr, idx));
btree_print_subtree(log_handle,
cc,
cfg,
btree_get_child_addr(cfg, node.hdr, idx),
type);
}
platform_log(log_handle,
"\n} -- End BTree sub-trees under"
Expand All @@ -3291,6 +3308,18 @@ btree_print_subtree(platform_log_handle *log_handle,
btree_node_unget(cc, cfg, &node);
}

/*
* Driver routine to print a Memtable BTree starting from root_addr.
*/
void
btree_print_memtable_tree(platform_log_handle *log_handle,
cache *cc,
btree_config *cfg,
uint64 root_addr)
{
btree_print_subtree(log_handle, cc, cfg, root_addr, PAGE_TYPE_MEMTABLE);
}

/*
* btree_print_tree()
*
Expand All @@ -3302,7 +3331,20 @@ btree_print_tree(platform_log_handle *log_handle,
btree_config *cfg,
uint64 root_addr)
{
btree_print_subtree(log_handle, cc, cfg, root_addr);
btree_print_subtree(log_handle, cc, cfg, root_addr, PAGE_TYPE_BRANCH);
}

/*
* Routine to print a BTree of page-type 'type' starting from root_addr
*/
static void
btree_print_tree_as(platform_log_handle *log_handle,
cache *cc,
btree_config *cfg,
uint64 root_addr,
page_type type)
{
btree_print_subtree(log_handle, cc, cfg, root_addr, type);
}

void
Expand Down Expand Up @@ -3388,7 +3430,8 @@ btree_verify_node(cache *cc,
{
btree_node_unget(cc, cfg, &child);
btree_node_unget(cc, cfg, &node);
btree_print_tree(Platform_error_log_handle, cc, cfg, addr);
btree_print_tree_as(
Platform_error_log_handle, cc, cfg, addr, type);
platform_error_log("out of order pivots\n");
platform_error_log("addr: %lu idx %u\n", node.addr, idx);
goto out;
Expand Down Expand Up @@ -3425,9 +3468,9 @@ btree_verify_node(cache *cc,
platform_error_log("addr: %lu idx %u\n", node.addr, idx);
platform_error_log("child addr: %lu idx %u\n", child.addr, idx);
btree_print_locked_node(
Platform_error_log_handle, cfg, node.addr, node.hdr);
Platform_error_log_handle, cfg, node.addr, node.hdr, type);
btree_print_locked_node(
Platform_error_log_handle, cfg, child.addr, child.hdr);
Platform_error_log_handle, cfg, child.addr, child.hdr, type);
platform_assert(0);
btree_node_unget(cc, cfg, &child);
btree_node_unget(cc, cfg, &node);
Expand All @@ -3447,9 +3490,9 @@ btree_verify_node(cache *cc,
platform_error_log("addr: %lu idx %u\n", node.addr, idx);
platform_error_log("child addr: %lu idx %u\n", child.addr, idx);
btree_print_locked_node(
Platform_error_log_handle, cfg, node.addr, node.hdr);
Platform_error_log_handle, cfg, node.addr, node.hdr, type);
btree_print_locked_node(
Platform_error_log_handle, cfg, child.addr, child.hdr);
Platform_error_log_handle, cfg, child.addr, child.hdr, type);
platform_assert(0);
btree_node_unget(cc, cfg, &child);
btree_node_unget(cc, cfg, &node);
Expand Down Expand Up @@ -3490,7 +3533,7 @@ btree_print_lookup(cache *cc, // IN
int64 child_idx;

node.addr = root_addr;
btree_print_node(Platform_default_log_handle, cc, cfg, &node);
btree_print_node(Platform_default_log_handle, cc, cfg, &node, type);
btree_node_get(cc, cfg, &node, type);

for (h = node.hdr->height; h > 0; h--) {
Expand All @@ -3500,7 +3543,7 @@ btree_print_lookup(cache *cc, // IN
child_idx = 0;
}
child_node.addr = btree_get_child_addr(cfg, node.hdr, child_idx);
btree_print_node(Platform_default_log_handle, cc, cfg, &child_node);
btree_print_node(Platform_default_log_handle, cc, cfg, &child_node, type);
btree_node_get(cc, cfg, &child_node, type);
btree_node_unget(cc, cfg, &node);
node = child_node;
Expand Down
12 changes: 10 additions & 2 deletions src/btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ btree_rough_count(cache *cc,
key min_key,
key max_key);

void
btree_print_memtable_tree(platform_log_handle *log_handle,
cache *cc,
btree_config *cfg,
uint64 addr);

void
btree_print_tree(platform_log_handle *log_handle,
cache *cc,
Expand All @@ -390,13 +396,15 @@ void
btree_print_locked_node(platform_log_handle *log_handle,
btree_config *cfg,
uint64 addr,
btree_hdr *hdr);
btree_hdr *hdr,
page_type type);

void
btree_print_node(platform_log_handle *log_handle,
cache *cc,
btree_config *cfg,
btree_node *node);
btree_node *node,
page_type type);

void
btree_print_tree_stats(platform_log_handle *log_handle,
Expand Down
2 changes: 1 addition & 1 deletion src/data_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ static inline void
data_key_to_string(const data_config *cfg, key k, char *str, size_t size)
{
if (key_is_negative_infinity(k)) {
snprintf(str, size, "(negaitive_infinity)");
snprintf(str, size, "(negative_infinity)");
} else if (key_is_negative_infinity(k)) {
snprintf(str, size, "(positive_infinity)");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/memtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ memtable_verify(cache *cc, memtable *mt)
static inline void
memtable_print(platform_log_handle *log_handle, cache *cc, memtable *mt)
{
btree_print_tree(log_handle, cc, mt->cfg, mt->root_addr);
btree_print_memtable_tree(log_handle, cc, mt->cfg, mt->root_addr);
}

static inline void
memtable_print_stats(platform_log_handle *log_handle, cache *cc, memtable *mt)
{
btree_print_tree_stats(log_handle, cc, mt->cfg, mt->root_addr);
};
}
39 changes: 27 additions & 12 deletions src/trunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7920,6 +7920,12 @@ trunk_print_locked_node(platform_log_handle *log_handle,
trunk_node *node)
{
uint16 height = trunk_height(node);

platform_log(log_handle,
"\nPage type: %s, Node addr=%lu\n{\n",
page_type_str[PAGE_TYPE_TRUNK],
node->addr);

// clang-format off
platform_log(log_handle, "----------------------------------------------------------------------------------------------------\n");
platform_log(log_handle, "| | addr | next addr | height | gen | pvt gen | |\n");
Expand All @@ -7939,6 +7945,8 @@ trunk_print_locked_node(platform_log_handle *log_handle,
platform_log(log_handle, "}\n");
}

#define PIVOT_KEY_OUTBUF_LEN 24 // Used just for formatting output below.

/*
* trunk_print_pivots() -- Print pivot array information.
*/
Expand All @@ -7958,13 +7966,16 @@ trunk_print_pivots(platform_log_handle *log_handle,
for (uint16 pivot_no = 0; pivot_no < trunk_num_pivot_keys(spl, node);
pivot_no++)
{
key pivot = trunk_get_pivot(spl, node, pivot_no);
key pivot = trunk_get_pivot(spl, node, pivot_no);
// Used to format generated string for negative / positive infinity keys.
char buf[PIVOT_KEY_OUTBUF_LEN + 1];
snprintf(buf, sizeof(buf), "%-24s", key_string(spl->cfg.data_cfg, pivot));

trunk_pivot_data *pdata = trunk_get_pivot_data(spl, node, pivot_no);
if (pivot_no == trunk_num_pivot_keys(spl, node) - 1) {
platform_log(log_handle,
"| %.*s | %12s | %12s | %11s | %9s | %5s | %5s |\n",
24,
key_string(spl->cfg.data_cfg, pivot),
"| %s | %12s | %12s | %11s | %9s | %5s | %5s |\n",
buf,
"",
"",
"",
Expand All @@ -7973,9 +7984,8 @@ trunk_print_pivots(platform_log_handle *log_handle,
"");
} else {
platform_log(log_handle,
"| %.*s | %12lu | %12lu | %11lu | %9lu | %5ld | %5lu |\n",
24,
key_string(spl->cfg.data_cfg, pivot),
"| %s | %12lu | %12lu | %11lu | %9lu | %5ld | %5lu |\n",
buf,
pdata->addr,
pdata->filter.addr,
pdata->num_tuples_whole + pdata->num_tuples_bundle,
Expand Down Expand Up @@ -8152,22 +8162,22 @@ trunk_print_memtable(platform_log_handle *log_handle, trunk_handle *spl)
platform_log(log_handle, "&&&&&&&&&&&&&&&&&&&\n");
platform_log(log_handle, "&& MEMTABLES \n");
platform_log(log_handle, "&& curr: %lu\n", curr_memtable);
platform_log(log_handle, "-------------------\n");
platform_log(log_handle, "-------------------\n{\n");

uint64 mt_gen_start = memtable_generation(spl->mt_ctxt);
uint64 mt_gen_end = memtable_generation_retired(spl->mt_ctxt);
for (uint64 mt_gen = mt_gen_start; mt_gen != mt_gen_end; mt_gen--) {
memtable *mt = trunk_get_memtable(spl, mt_gen);
platform_log(log_handle,
"Memtable root_addr=%lu: gen %lu ref_count %u state %d\n",
mt_gen,
mt->root_addr,
mt_gen,
allocator_get_refcount(spl->al, mt->root_addr),
mt->state);

memtable_print(log_handle, spl->cc, mt);
}
platform_log(log_handle, "\n");
platform_log(log_handle, "\n}\n");
}

/*
Expand Down Expand Up @@ -8821,11 +8831,16 @@ trunk_node_print_branches(trunk_handle *spl, uint64 addr, void *arg)
platform_log(
log_handle,
"------------------------------------------------------------------\n");
platform_log(
log_handle, "| node %12lu height %u\n", addr, trunk_height(&node));
platform_log(log_handle,
"| Page type: %s, Node addr=%lu height=%u next_addr=%lu\n",
page_type_str[PAGE_TYPE_TRUNK],
addr,
trunk_height(&node),
trunk_next_addr(&node));
platform_log(
log_handle,
"------------------------------------------------------------------\n");

uint16 num_pivot_keys = trunk_num_pivot_keys(spl, &node);
platform_log(log_handle, "| pivots:\n");
for (uint16 pivot_no = 0; pivot_no < num_pivot_keys; pivot_no++) {
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/btree_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,16 @@ leaf_split_tests(btree_config *cfg,
bool success = btree_leaf_incorporate_tuple(
cfg, hid, hdr, tuple_key, bigger_msg, &spec, &generation);
if (success) {
btree_print_locked_node(Platform_error_log_handle, cfg, 0, hdr);
btree_print_locked_node(
Platform_error_log_handle, cfg, 0, hdr, PAGE_TYPE_MEMTABLE);
ASSERT_FALSE(success,
"Weird. An incorporate that was supposed to fail "
"actually succeeded (nkvs=%d, realnkvs=%d, i=%d).\n",
nkvs,
realnkvs,
i);
btree_print_locked_node(Platform_error_log_handle, cfg, 0, hdr);
btree_print_locked_node(
Platform_error_log_handle, cfg, 0, hdr, PAGE_TYPE_MEMTABLE);
ASSERT_FALSE(success);
}
leaf_splitting_plan plan =
Expand Down

0 comments on commit a668628

Please sign in to comment.