2023-12-07 02:17:51 +03:00
|
|
|
|
#include "btree.h"
|
|
|
|
|
#include "btree.c"
|
|
|
|
|
|
2023-12-14 05:32:45 +03:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
2023-12-07 02:17:51 +03:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_test_nodeinteg(BT_state *state, BT_findpath *path,
|
|
|
|
|
vaof_t lo, vaof_t hi, pgno_t pg)
|
|
|
|
|
{
|
|
|
|
|
size_t childidx = 0;
|
|
|
|
|
BT_page *parent = 0;
|
|
|
|
|
|
|
|
|
|
assert(SUCC(_bt_find(state, path, lo, hi)));
|
|
|
|
|
parent = path->path[path->depth];
|
|
|
|
|
childidx = path->idx[path->depth];
|
|
|
|
|
assert(parent->datk[childidx].fo == pg);
|
|
|
|
|
assert(parent->datk[childidx].va == lo);
|
|
|
|
|
assert(parent->datk[childidx+1].va == hi);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 14:36:53 +03:00
|
|
|
|
static size_t
|
|
|
|
|
_mlist_sizep(BT_mlistnode *head)
|
|
|
|
|
/* calculate the size of the mlist in pages */
|
|
|
|
|
{
|
|
|
|
|
size_t sz = 0;
|
|
|
|
|
while (head) {
|
|
|
|
|
sz += head->sz;
|
|
|
|
|
head = head->next;
|
|
|
|
|
}
|
|
|
|
|
return sz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
_flist_sizep(BT_flistnode *head)
|
|
|
|
|
/* calculate the size of the flist in pages */
|
|
|
|
|
{
|
|
|
|
|
size_t sz = 0;
|
|
|
|
|
while (head) {
|
|
|
|
|
sz += head->sz;
|
|
|
|
|
head = head->next;
|
|
|
|
|
}
|
|
|
|
|
return sz;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 02:17:51 +03:00
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
DPUTS("PMA Tests");
|
|
|
|
|
|
2023-12-09 02:15:31 +03:00
|
|
|
|
BT_state *state1;
|
2023-12-07 02:17:51 +03:00
|
|
|
|
BT_findpath path = {0};
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
2023-12-09 02:15:31 +03:00
|
|
|
|
|
2023-12-07 02:17:51 +03:00
|
|
|
|
DPUTS("== test 1: insert");
|
|
|
|
|
|
2023-12-09 02:15:31 +03:00
|
|
|
|
bt_state_new(&state1);
|
2023-12-14 02:25:35 +03:00
|
|
|
|
if (mkdir("./pmatest1", 0774) == -1)
|
|
|
|
|
return errno;
|
2023-12-09 02:15:31 +03:00
|
|
|
|
assert(SUCC(bt_state_open(state1, "./pmatest1", 0, 0644)));
|
|
|
|
|
|
|
|
|
|
#define LOWEST_ADDR 0x200000;
|
|
|
|
|
vaof_t lo = LOWEST_ADDR;
|
2023-12-07 02:17:51 +03:00
|
|
|
|
vaof_t hi = 0xDEADBEEF;
|
|
|
|
|
pgno_t pg = 1; /* dummy value */
|
|
|
|
|
for (size_t i = 0; i < BT_DAT_MAXKEYS * 4; ++i) {
|
2023-12-09 02:15:31 +03:00
|
|
|
|
_bt_insert(state1, lo, hi, pg);
|
|
|
|
|
_test_nodeinteg(state1, &path, lo, hi, pg);
|
2023-12-07 02:17:51 +03:00
|
|
|
|
lo++; pg++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 02:15:31 +03:00
|
|
|
|
bt_state_close(state1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DPUTS("== test 2: malloc");
|
|
|
|
|
BT_state *state2;
|
|
|
|
|
|
|
|
|
|
bt_state_new(&state2);
|
2023-12-14 02:25:35 +03:00
|
|
|
|
if (mkdir("./pmatest2", 0774) == -1)
|
|
|
|
|
return errno;
|
2023-12-09 02:15:31 +03:00
|
|
|
|
assert(SUCC(bt_state_open(state2, "./pmatest2", 0, 0644)));
|
|
|
|
|
|
|
|
|
|
void *t2a = bt_malloc(state2, 10);
|
|
|
|
|
bt_free(state2, t2a, (BT_page*)t2a + 10);
|
2023-12-09 02:40:15 +03:00
|
|
|
|
void *t2b = bt_malloc(state2, 10);
|
|
|
|
|
/* should have pulled the same pointer due to eager mlist coalescing */
|
2023-12-09 02:58:24 +03:00
|
|
|
|
assert(t2a == t2b);
|
2023-12-09 02:40:15 +03:00
|
|
|
|
ZERO(&path, sizeof path);
|
|
|
|
|
_bt_find(state2, &path, addr2off(t2b), addr2off((BT_page *)t2b + 10));
|
2023-12-09 03:40:37 +03:00
|
|
|
|
#define T2P1_PRNT0 (path.path[path.depth])
|
|
|
|
|
#define T2P1_CIDX0 (path.idx[path.depth])
|
|
|
|
|
#define T2P1_CIDX1 (path.idx[path.depth] + 1)
|
|
|
|
|
/* check length as represented in btree */
|
|
|
|
|
assert(T2P1_PRNT0->datk[T2P1_CIDX1].va
|
|
|
|
|
- T2P1_PRNT0->datk[T2P1_CIDX0].va
|
|
|
|
|
== 10);
|
2023-12-09 02:40:15 +03:00
|
|
|
|
bt_free(state2, t2b, (BT_page*)t2b + 10);
|
|
|
|
|
ZERO(&path, sizeof path);
|
|
|
|
|
_bt_find(state2, &path, addr2off(t2b), addr2off((BT_page *)t2b + 10));
|
2023-12-09 03:40:37 +03:00
|
|
|
|
/* fo should be zero (free) */
|
|
|
|
|
assert(path.path[path.depth]->datk[path.idx[path.depth]].fo == 0);
|
2023-12-09 02:40:15 +03:00
|
|
|
|
/* should invoke deletion coalescing - 10 page free range in btree */
|
|
|
|
|
void *t2c = bt_malloc(state2, 20);
|
2023-12-09 02:15:31 +03:00
|
|
|
|
|
2023-12-14 05:32:45 +03:00
|
|
|
|
bt_state_close(state2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DPUTS("== test 3: ephemeral structure restoration");
|
|
|
|
|
BT_state *state3;
|
|
|
|
|
|
|
|
|
|
bt_state_new(&state3);
|
|
|
|
|
if (mkdir("./pmatest3", 0774) == -1)
|
|
|
|
|
return errno;
|
|
|
|
|
assert(SUCC(bt_state_open(state3, "./pmatest3", 0, 0644)));
|
|
|
|
|
|
|
|
|
|
typedef struct lohi_pair lohi_pair;
|
|
|
|
|
struct lohi_pair
|
|
|
|
|
{
|
|
|
|
|
BT_page *lo;
|
|
|
|
|
BT_page *hi;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define ITERATIONS 1000
|
|
|
|
|
#define MAXALLOCPG 0xFF
|
|
|
|
|
lohi_pair allocs[ITERATIONS] = {0};
|
2023-12-15 14:36:53 +03:00
|
|
|
|
size_t alloc_sizp = 0;
|
|
|
|
|
size_t flist_sizp = _flist_sizep(state3->flist);
|
|
|
|
|
size_t mlist_sizp = _mlist_sizep(state3->mlist);
|
2023-12-14 05:32:45 +03:00
|
|
|
|
for (size_t i = 0; i < ITERATIONS; i++) {
|
2023-12-14 05:36:34 +03:00
|
|
|
|
/* malloc a random number of pages <= 256 and store in the allocs array */
|
2023-12-15 08:50:26 +03:00
|
|
|
|
int pages = random();
|
2023-12-14 05:32:45 +03:00
|
|
|
|
pages &= MAXALLOCPG;
|
2023-12-14 05:36:34 +03:00
|
|
|
|
pages += 1;
|
2023-12-14 05:32:45 +03:00
|
|
|
|
allocs[i].lo = bt_malloc(state3, pages);
|
|
|
|
|
allocs[i].hi = allocs[i].lo + pages;
|
2023-12-15 14:36:53 +03:00
|
|
|
|
alloc_sizp += pages;
|
|
|
|
|
/* validate size changes to mlist and flist */
|
|
|
|
|
assert(_flist_sizep(state3->flist)
|
|
|
|
|
== (flist_sizp - alloc_sizp));
|
|
|
|
|
assert(_mlist_sizep(state3->mlist)
|
|
|
|
|
== (mlist_sizp - alloc_sizp));
|
2023-12-14 05:32:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* sync the state */
|
2023-12-15 08:50:26 +03:00
|
|
|
|
/* bt_sync(state3); */
|
|
|
|
|
|
|
|
|
|
/* TODO: close and reopen state. validate ephemeral structures */
|
|
|
|
|
|
2023-12-15 14:36:53 +03:00
|
|
|
|
flist_sizp = _flist_sizep(state3->flist);
|
|
|
|
|
mlist_sizp = _mlist_sizep(state3->mlist);
|
|
|
|
|
alloc_sizp = 0;
|
2023-12-15 08:50:26 +03:00
|
|
|
|
for (size_t i = 0; i < ITERATIONS / 2; i++) {
|
|
|
|
|
/* free half of the allocations */
|
|
|
|
|
bt_free(state3, allocs[i].lo, allocs[i].hi);
|
2023-12-15 14:36:53 +03:00
|
|
|
|
alloc_sizp += allocs[i].hi - allocs[i].lo;
|
|
|
|
|
/* validate size changes to mlist */
|
|
|
|
|
assert(_mlist_sizep(state3->mlist)
|
|
|
|
|
== (mlist_sizp + alloc_sizp));
|
2023-12-15 08:50:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* resync the state */
|
2023-12-14 05:32:45 +03:00
|
|
|
|
bt_sync(state3);
|
|
|
|
|
|
2023-12-15 08:50:26 +03:00
|
|
|
|
/* TODO: close and reopen state. validate ephemeral structures */
|
|
|
|
|
|
2023-12-07 02:17:51 +03:00
|
|
|
|
return 0;
|
|
|
|
|
}
|