963 lines
24 KiB
C
963 lines
24 KiB
C
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "../include/common.h"
|
|
#include "../include/roaring.h"
|
|
#include "../include/sparsemap.h"
|
|
#include "../include/tdigest.h"
|
|
|
|
#include "midl.c"
|
|
|
|
typedef size_t pgno_t;
|
|
|
|
char *
|
|
bytes_as(double bytes, char *s, size_t size)
|
|
{
|
|
const char *units[] = { "b", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
|
|
size_t i = 0;
|
|
|
|
while (bytes >= 1024 && i < sizeof(units) / sizeof(units[0]) - 1) {
|
|
bytes /= 1024;
|
|
i++;
|
|
}
|
|
|
|
snprintf(s, size, "%.2f %s", bytes, units[i]);
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* A "coin toss" function that is critical to the proper operation of the
|
|
* Skiplist. For example, when `max = 6` this function returns 0 with
|
|
* probability 0.5, 1 with 0.25, 2 with 0.125, etc. until 6 with 0.5^7.
|
|
*/
|
|
static int
|
|
toss(size_t max)
|
|
{
|
|
size_t level = 0;
|
|
double probability = 0.5;
|
|
|
|
double random_value = (double)xorshift32() / RAND_MAX;
|
|
while (random_value < probability && level < max) {
|
|
level++;
|
|
probability *= 0.5;
|
|
}
|
|
return level;
|
|
}
|
|
|
|
bool recording = true;
|
|
|
|
static void
|
|
record_set_mutation(FILE *out, pgno_t pg)
|
|
{
|
|
if (recording) {
|
|
fprintf(out, "set %lu\n", pg);
|
|
}
|
|
}
|
|
|
|
static void
|
|
record_clear_mutation(FILE *out, pgno_t pg)
|
|
{
|
|
if (recording) {
|
|
fprintf(out, "clear %lu\n", pg);
|
|
}
|
|
}
|
|
|
|
static void
|
|
record_take_span_mutation(FILE *out, pgno_t pg, unsigned len)
|
|
{
|
|
if (recording) {
|
|
fprintf(out, "take %lu %u\n", pg, len);
|
|
}
|
|
}
|
|
|
|
static void
|
|
record_release_span_mutation(FILE *out, pgno_t pg, unsigned len)
|
|
{
|
|
if (recording) {
|
|
fprintf(out, "release %lu %u\n", pg, len);
|
|
}
|
|
}
|
|
|
|
static void
|
|
__scan_record_offsets(sm_idx_t v[], size_t n, void *aux)
|
|
{
|
|
FILE *out = (FILE *)aux;
|
|
for (size_t i = 0; i < n; i++) {
|
|
fprintf(out, "%u ", v[i]);
|
|
}
|
|
}
|
|
|
|
static void
|
|
record_merge_mutation(FILE *out, void *handle)
|
|
{
|
|
if (recording) {
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
fprintf(out, "merge %zu ", sparsemap_get_ending_offset(map));
|
|
sparsemap_scan(map, __scan_record_offsets, 0, (void *)out);
|
|
fprintf(out, "\n");
|
|
}
|
|
}
|
|
|
|
/* sparsemap ------------------------------------------------------------- */
|
|
|
|
static sparsemap_idx_t
|
|
_sparsemap_set(sparsemap_t **map, sparsemap_idx_t idx, bool value)
|
|
{
|
|
do {
|
|
sparsemap_idx_t l = sparsemap_set(*map, idx, value);
|
|
if (l != idx) {
|
|
if (errno == ENOSPC) {
|
|
*map = sparsemap_set_data_size(*map, NULL, sparsemap_get_capacity(*map) + 64);
|
|
assert(*map != NULL);
|
|
errno = 0;
|
|
} else {
|
|
assert(false);
|
|
}
|
|
} else {
|
|
return l;
|
|
}
|
|
} while (true);
|
|
}
|
|
|
|
static void *
|
|
__sm_alloc(size_t capacity)
|
|
{
|
|
sparsemap_t *map = sparsemap(capacity);
|
|
assert(map != NULL);
|
|
return map;
|
|
}
|
|
|
|
static void
|
|
__sm_free(void *handle)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
free(map);
|
|
}
|
|
|
|
static pgno_t
|
|
__sm_set(void **handle, pgno_t pg)
|
|
{
|
|
sparsemap_t **map = (sparsemap_t **)handle;
|
|
return (pgno_t)_sparsemap_set(map, pg, true);
|
|
}
|
|
|
|
static bool
|
|
__sm_is_set(void *handle, pgno_t pg)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
return sparsemap_is_set(map, pg);
|
|
}
|
|
|
|
static pgno_t
|
|
__sm_clear(void **handle, pgno_t pg)
|
|
{
|
|
sparsemap_t **map = (sparsemap_t **)handle;
|
|
return (pgno_t)_sparsemap_set(map, pg, false);
|
|
}
|
|
|
|
static pgno_t
|
|
__sm_find_span(void *handle, unsigned len)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
pgno_t pgno = (pgno_t)sparsemap_span(map, 0, len, true);
|
|
assert(SPARSEMAP_NOT_FOUND(pgno) == false);
|
|
return pgno;
|
|
}
|
|
|
|
static bool
|
|
__sm_take_span(void **handle, pgno_t pg, unsigned len)
|
|
{
|
|
sparsemap_t **map = (sparsemap_t **)handle;
|
|
for (pgno_t i = pg; i < pg + len; i++) {
|
|
assert(_sparsemap_set(map, i, false) == i);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__sm_release_span(void **handle, pgno_t pg, unsigned len)
|
|
{
|
|
sparsemap_t **map = (sparsemap_t **)handle;
|
|
for (pgno_t i = pg; i < pg + len; i++) {
|
|
assert(_sparsemap_set(map, i, true) == i);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__sm_is_span(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
for (pgno_t i = pg; i < pg + len; i++) {
|
|
if (sparsemap_is_set(map, i) != true) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__sm_is_empty(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
for (pgno_t i = 0; i < len; i++) {
|
|
if (sparsemap_is_set(map, pg + i) != false) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__sm_is_first(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
for (sparsemap_idx_t i = 0; i < pg + len; i++) {
|
|
sparsemap_idx_t j = 0;
|
|
while (sparsemap_is_set(map, i + j) == true && j < len) {
|
|
j++;
|
|
}
|
|
if (j == len) {
|
|
return i == pg;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
__sm_merge(void **handle, void *other_handle)
|
|
{
|
|
sparsemap_t **map = (sparsemap_t **)handle;
|
|
sparsemap_t *other = (sparsemap_t *)other_handle;
|
|
do {
|
|
int retval = sparsemap_merge(*map, other);
|
|
if (retval != 0) {
|
|
if (errno == ENOSPC) {
|
|
size_t new_size = retval + (64 - (retval % 64)) + 64;
|
|
*map = sparsemap_set_data_size(*map, NULL, sparsemap_get_capacity(*map) + new_size);
|
|
assert(*map != NULL);
|
|
errno = 0;
|
|
} else {
|
|
assert(false);
|
|
}
|
|
} else {
|
|
break;
|
|
}
|
|
} while (true);
|
|
return true;
|
|
}
|
|
|
|
static size_t
|
|
__sm_size(void *handle)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
return sparsemap_get_size(map);
|
|
}
|
|
|
|
static size_t
|
|
__sm_count(void *handle)
|
|
{
|
|
sparsemap_t *map = (sparsemap_t *)handle;
|
|
return sparsemap_rank(map, 0, SPARSEMAP_IDX_MAX, true);
|
|
}
|
|
|
|
/* midl ------------------------------------------------------------------ */
|
|
|
|
static void *
|
|
__midl_alloc(size_t capacity)
|
|
{
|
|
MDB_IDL list = mdb_midl_alloc(capacity);
|
|
assert(list != NULL);
|
|
return (void *)list;
|
|
}
|
|
|
|
static void
|
|
__midl_free(void *handle)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
mdb_midl_free(list);
|
|
}
|
|
|
|
static pgno_t
|
|
__midl_set(void **handle, pgno_t pg)
|
|
{
|
|
MDB_IDL *_list = (MDB_IDL *)handle, list = *_list;
|
|
if (list[0] + 1 == list[-1]) {
|
|
mdb_midl_need(_list, list[-1] + 1);
|
|
list = *_list;
|
|
}
|
|
mdb_midl_insert(list, pg);
|
|
return pg;
|
|
}
|
|
|
|
static bool
|
|
__midl_is_set(void *handle, pgno_t pg)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
pgno_t i = mdb_midl_search(list, pg);
|
|
return i <= list[0] && list[i] == pg;
|
|
}
|
|
|
|
static pgno_t
|
|
__midl_clear(void **handle, pgno_t pg)
|
|
{
|
|
MDB_IDL list = *(MDB_IDL *)handle;
|
|
unsigned len = list[0];
|
|
list[0] = len -= 1;
|
|
for (unsigned j = pg - 1; j < len;)
|
|
list[++j] = list[++pg];
|
|
for (unsigned j = len + 1; j <= list[-1]; j++)
|
|
list[j] = 0;
|
|
return pg;
|
|
}
|
|
|
|
static pgno_t
|
|
__midl_find_span(void *handle, unsigned len)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
|
|
/* Seek a big enough contiguous page range. Prefer
|
|
* pages at the tail, just truncating the list.
|
|
*/
|
|
int retry = 1;
|
|
unsigned i = 0;
|
|
pgno_t pgno = 0, *mop = list;
|
|
unsigned n2 = len, mop_len = mop[0];
|
|
do {
|
|
if (mop_len > n2) {
|
|
i = mop_len;
|
|
do {
|
|
pgno = mop[i];
|
|
if (mop[i - n2] == pgno + n2)
|
|
goto search_done;
|
|
} while (--i > n2);
|
|
if (--retry < 0)
|
|
break;
|
|
}
|
|
} while (1);
|
|
search_done:;
|
|
return pgno;
|
|
}
|
|
|
|
static bool
|
|
__midl_take_span(void **handle, pgno_t pg, unsigned len)
|
|
{
|
|
MDB_IDL list = *(MDB_IDL *)handle;
|
|
int i = list[list[0]] == pg ? list[0] : mdb_midl_search(list, pg);
|
|
unsigned j, num = len;
|
|
pgno_t *mop = list;
|
|
unsigned mop_len = mop[0];
|
|
|
|
mop[0] = mop_len -= num;
|
|
/* Move any stragglers down */
|
|
for (j = i - num; j < mop_len;)
|
|
mop[++j] = mop[++i];
|
|
/* Set all unused values in the array to 0
|
|
for (j = mop_len + 1; j <= mop[-1]; j++)
|
|
mop[j] = 0; */
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__midl_release_span(void **handle, pgno_t pg, unsigned len)
|
|
{
|
|
MDB_IDL *_list = (MDB_IDL *)handle, list = *_list;
|
|
if (list[0] + len >= list[-1]) {
|
|
mdb_midl_need(_list, list[-1] + len);
|
|
list = *_list;
|
|
}
|
|
for (size_t i = pg; i < pg + len; i++) {
|
|
mdb_midl_insert(list, i);
|
|
}
|
|
mdb_midl_sort(list);
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__midl_is_span(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
pgno_t idx = mdb_midl_search(list, pg);
|
|
bool found = idx <= list[0] && list[idx] == pg;
|
|
if (!found)
|
|
return false;
|
|
if (len == 1)
|
|
return true;
|
|
if (list[len] + 1 != list[len - 1])
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__midl_is_empty(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
for (pgno_t i = pg; i < pg + len; i++) {
|
|
pgno_t idx = mdb_midl_search(list, pg);
|
|
bool found = idx <= list[0] && list[idx] == pg;
|
|
if (found)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__midl_merge(void **handle, void *other_handle)
|
|
{
|
|
MDB_IDL *_list = (MDB_IDL *)handle, list = *_list;
|
|
MDB_IDL other = (MDB_IDL)other_handle;
|
|
if (list[0] + other[0] >= list[-1]) {
|
|
mdb_midl_need(_list, list[-1] + other[0]);
|
|
list = *_list;
|
|
}
|
|
mdb_midl_append_list(_list, other);
|
|
list = *_list;
|
|
mdb_midl_sort(list);
|
|
return true;
|
|
}
|
|
|
|
static size_t
|
|
__midl_size(void *handle)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
return list[0] * sizeof(pgno_t);
|
|
}
|
|
|
|
static size_t
|
|
__midl_count(void *handle)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
return list[0];
|
|
}
|
|
|
|
static bool
|
|
__midl_validate(void *handle)
|
|
{
|
|
MDB_IDL list = (MDB_IDL)handle;
|
|
pgno_t id = 1;
|
|
while (id < list[0]) {
|
|
if (list[id] >= list[id + 1])
|
|
return false;
|
|
id++;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* roaring --------------------------------------------------------------- */
|
|
|
|
static void *
|
|
__roar_alloc(size_t capacity)
|
|
{
|
|
roaring_bitmap_t *map = roaring_bitmap_create();
|
|
assert(map != NULL);
|
|
return map;
|
|
}
|
|
|
|
static void
|
|
__roar_free(void *handle)
|
|
{
|
|
roaring_bitmap_t *rbm = (roaring_bitmap_t *)handle;
|
|
roaring_free(rbm);
|
|
}
|
|
|
|
static pgno_t
|
|
__roar_set(void **handle, pgno_t pg)
|
|
{
|
|
roaring_bitmap_t **_rbm = (roaring_bitmap_t **)handle, *rbm = *_rbm;
|
|
assert(roaring_bitmap_add_checked(rbm, pg) == true);
|
|
return pg;
|
|
}
|
|
|
|
static bool
|
|
__roar_is_set(void *handle, pgno_t pg)
|
|
{
|
|
roaring_bitmap_t *rbm = (roaring_bitmap_t *)handle;
|
|
return roaring_bitmap_contains(rbm, pg);
|
|
}
|
|
|
|
static pgno_t
|
|
__roar_clear(void **handle, pgno_t pg)
|
|
{
|
|
roaring_bitmap_t **_rbm = (roaring_bitmap_t **)handle, *rbm = *_rbm;
|
|
roaring_bitmap_remove(rbm, pg);
|
|
return pg;
|
|
}
|
|
|
|
static pgno_t
|
|
__roar_find_span(void *handle, unsigned len)
|
|
{
|
|
roaring_bitmap_t *rbm = (roaring_bitmap_t *)handle;
|
|
uint64_t max = roaring_bitmap_maximum(rbm);
|
|
uint64_t offset = roaring_bitmap_minimum(rbm);
|
|
do {
|
|
if (len == 1 || roaring_bitmap_range_cardinality(rbm, offset, offset + len) == len) {
|
|
break;
|
|
}
|
|
offset++;
|
|
} while (offset <= max);
|
|
return offset;
|
|
}
|
|
|
|
static bool
|
|
__roar_take_span(void **handle, pgno_t pg, unsigned len)
|
|
{
|
|
roaring_bitmap_t **_rbm = (roaring_bitmap_t **)handle, *rbm = *_rbm;
|
|
roaring_bitmap_remove_range(rbm, pg, pg + len);
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__roar_release_span(void **handle, pgno_t pg, unsigned len)
|
|
{
|
|
roaring_bitmap_t **_rbm = (roaring_bitmap_t **)handle, *rbm = *_rbm;
|
|
for (size_t i = pg; i < pg + len; i++) {
|
|
assert(roaring_bitmap_add_checked(rbm, i) == true);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__roar_is_span(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
roaring_bitmap_t *rbm = (roaring_bitmap_t *)handle;
|
|
for (pgno_t i = pg; i < pg + len; i++) {
|
|
if (roaring_bitmap_contains(rbm, i) != true) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__roar_is_empty(void *handle, pgno_t pg, unsigned len)
|
|
{
|
|
roaring_bitmap_t *rbm = (roaring_bitmap_t *)handle;
|
|
for (pgno_t i = 0; i < len; i++) {
|
|
if (roaring_bitmap_contains(rbm, pg + i) != false) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
__roar_merge(void **handle, void *other_handle)
|
|
{
|
|
roaring_bitmap_t **_rbm = (roaring_bitmap_t **)handle, *rbm = *_rbm;
|
|
roaring_bitmap_t *other = (roaring_bitmap_t *)other_handle;
|
|
roaring_bitmap_or_inplace(rbm, other);
|
|
return true;
|
|
}
|
|
|
|
static size_t
|
|
__roar_size(void *handle)
|
|
{
|
|
// TODO
|
|
return 0;
|
|
}
|
|
|
|
static size_t
|
|
__roar_count(void *handle)
|
|
{
|
|
// TODO
|
|
return 0;
|
|
}
|
|
|
|
static bool
|
|
__roar_validate(void *handle)
|
|
{
|
|
roaring_bitmap_t *rbm = (roaring_bitmap_t *)handle;
|
|
roaring_bitmap_run_optimize(rbm);
|
|
return true;
|
|
}
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
typedef enum { SM, ML, RB } container_impl_t;
|
|
|
|
typedef struct container {
|
|
const char *name;
|
|
void *(*alloc)(size_t capacity);
|
|
void (*free)(void *handle);
|
|
pgno_t (*set)(void **handle, pgno_t pg);
|
|
bool (*is_set)(void *handle, pgno_t pg);
|
|
pgno_t (*clear)(void **handle, pgno_t pg);
|
|
pgno_t (*find_span)(void *handle, unsigned len);
|
|
bool (*take_span)(void **handle, pgno_t pg, unsigned len);
|
|
bool (*release_span)(void **handle, pgno_t pg, unsigned len);
|
|
bool (*is_span)(void *handle, pgno_t pg, unsigned len);
|
|
bool (*is_empty)(void *handle, pgno_t pg, unsigned len);
|
|
bool (*is_first)(void *handle, pgno_t pg, unsigned len);
|
|
bool (*merge)(void **handle, void *other_handle);
|
|
size_t (*size)(void *handle);
|
|
size_t (*count)(void *handle);
|
|
bool (*validate)(void *handle);
|
|
} container_t;
|
|
|
|
// clang-format off
|
|
container_t containers[] = {
|
|
{ "sparsemap",
|
|
.alloc = __sm_alloc,
|
|
.free = __sm_free,
|
|
.set = __sm_set,
|
|
.is_set = __sm_is_set,
|
|
.clear = __sm_clear,
|
|
.find_span = __sm_find_span,
|
|
.take_span = __sm_take_span,
|
|
.release_span = __sm_release_span,
|
|
.is_span = __sm_is_span,
|
|
.is_empty = __sm_is_empty,
|
|
.is_first = __sm_is_first,
|
|
.merge = __sm_merge,
|
|
.size = __sm_size,
|
|
.count = __sm_count,
|
|
.validate = NULL
|
|
},
|
|
{ "midl",
|
|
.alloc = __midl_alloc,
|
|
.free = __midl_free,
|
|
.set = __midl_set,
|
|
.is_set = __midl_is_set,
|
|
.clear = __midl_clear,
|
|
.find_span = __midl_find_span,
|
|
.take_span = __midl_take_span,
|
|
.release_span = __midl_release_span,
|
|
.is_span = __midl_is_span,
|
|
.is_empty = __midl_is_empty,
|
|
.is_first = NULL,
|
|
.merge = __midl_merge,
|
|
.size = __midl_size,
|
|
.count = __midl_count,
|
|
.validate = __midl_validate
|
|
},
|
|
{ "roaring",
|
|
.alloc = __roar_alloc,
|
|
.free = __roar_free,
|
|
.set = __roar_set,
|
|
.is_set = __roar_is_set,
|
|
.clear = __roar_clear,
|
|
.find_span = __roar_find_span,
|
|
.take_span = __roar_take_span,
|
|
.release_span = __roar_release_span,
|
|
.is_span = __roar_is_span,
|
|
.is_empty = __roar_is_empty,
|
|
.is_first = NULL,
|
|
.merge = __roar_merge,
|
|
.size = __roar_size,
|
|
.count = __roar_count,
|
|
.validate = __roar_validate,
|
|
},
|
|
};
|
|
// clang-format on
|
|
|
|
void *handles[(sizeof((containers)) / sizeof((containers)[0]))];
|
|
void *new_handles[(sizeof((containers)) / sizeof((containers)[0]))];
|
|
FILE *fp;
|
|
|
|
#define alloc(type, size) containers[type].alloc(size);
|
|
#define cast(type, fn, ...) \
|
|
if (containers[type].fn) \
|
|
containers[type].fn(handles[type], ##__VA_ARGS__)
|
|
#define invoke(type, fn, ...) containers[type].fn(handles[type], __VA_ARGS__)
|
|
#define mutate(type, fn, ...) (type == 0) ? record_##fn##_mutation(fp, __VA_ARGS__) : (void)0, containers[type].fn(&handles[type], __VA_ARGS__)
|
|
#define foreach(set) for (unsigned type = 0; type < (sizeof((set)) / sizeof((set)[0])); type++)
|
|
#define compare(set) \
|
|
for (unsigned type = 1; type < (sizeof((set)) / sizeof((set)[0])); type++) { \
|
|
verify_eq(0, handles[0], type, handles[type]); \
|
|
}
|
|
|
|
bool
|
|
verify_sm_eq_rb(sparsemap_t *map, roaring_bitmap_t *rbm)
|
|
{
|
|
uint64_t max = roaring_bitmap_maximum(rbm);
|
|
roaring_uint32_iterator_t iter;
|
|
roaring_iterator_init(rbm, &iter);
|
|
for (uint64_t i = 0; i <= max; i++) {
|
|
if (i == iter.current_value) {
|
|
assert(sparsemap_is_set(map, i) == true);
|
|
roaring_uint32_iterator_advance(&iter);
|
|
} else {
|
|
assert(sparsemap_is_set(map, i) == false);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
verify_sm_eq_ml(sparsemap_t *map, MDB_IDL list)
|
|
{
|
|
for (MDB_ID i = 1; i <= list[0]; i++) {
|
|
pgno_t pg = list[i];
|
|
unsigned skipped = i == 1 ? 0 : list[i - 1] - list[i] - 1;
|
|
if (skipped) {
|
|
for (MDB_ID j = list[i - 1]; j > list[i]; j--) {
|
|
if (sparsemap_is_set(map, pg - j) != false) {
|
|
__diag("%zu\n", pg - j);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
if (sparsemap_is_set(map, pg) != true) {
|
|
__diag("%zu\n", pg);
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
verify_eq(unsigned a, void *ad, unsigned b, void *bd)
|
|
{
|
|
bool ret = true;
|
|
|
|
// 'a' should always be a Sparsemap
|
|
switch (b) {
|
|
case ML:
|
|
assert((ret = verify_sm_eq_ml((sparsemap_t *)ad, (MDB_IDL)bd)) == true);
|
|
break;
|
|
case RB:
|
|
assert((ret = verify_sm_eq_rb((sparsemap_t *)ad, (roaring_bitmap_t *)bd)) == true);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
td_histogram_t *l_span_loc;
|
|
td_histogram_t *b_span_loc;
|
|
td_histogram_t *l_span_take;
|
|
td_histogram_t *b_span_take;
|
|
td_histogram_t *l_span_merge;
|
|
td_histogram_t *b_span_merge;
|
|
|
|
void
|
|
stats_header(void)
|
|
{
|
|
printf(
|
|
"timestamp,iterations,idl_cap,idl_used,idl_bytes,sm_cap,sm_used,idl_loc_p50,idl_loc_p75,idl_loc_p90,idl_loc_p99,idl_loc_p999,sm_loc_p50,sm_loc_p75,sm_loc_p90,sm_loc_p99,sm_loc_p999,idl_take_p50,idl_take_p75,idl_take_p90,idl_take_p99,idl_take_p999,sm_take_p50,sm_take_p75,sm_take_p90,sm_take_p99,sm_take_p999,idl_merge_p50,idl_merge_p75,idl_merge_p90,idl_merge_p99,idl_merge_p999,sm_merge_p50,sm_merge_p75,sm_merge_p90,sm_merge_p99,sm_merge_p999\n");
|
|
}
|
|
|
|
void
|
|
stats(size_t iterations, sparsemap_t *map, MDB_IDL list)
|
|
{
|
|
if (iterations < 10)
|
|
return;
|
|
|
|
td_compress(l_span_loc);
|
|
td_compress(b_span_loc);
|
|
td_compress(l_span_take);
|
|
td_compress(b_span_take);
|
|
td_compress(l_span_merge);
|
|
td_compress(b_span_merge);
|
|
|
|
printf(
|
|
"%f,%zu,%zu,%zu,%zu,%zu,%zu,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f\n",
|
|
nsts(), iterations, list[-1], list[0], MDB_IDL_SIZEOF(list), sparsemap_get_capacity(map), sparsemap_get_size(map), td_quantile(l_span_loc, .5),
|
|
td_quantile(l_span_loc, .75), td_quantile(l_span_loc, .90), td_quantile(l_span_loc, .99), td_quantile(l_span_loc, .999), td_quantile(b_span_loc, .5),
|
|
td_quantile(b_span_loc, .75), td_quantile(b_span_loc, .90), td_quantile(b_span_loc, .99), td_quantile(b_span_loc, .999), td_quantile(l_span_take, .5),
|
|
td_quantile(l_span_take, .75), td_quantile(l_span_take, .90), td_quantile(l_span_take, .99), td_quantile(l_span_take, .999), td_quantile(b_span_take, .5),
|
|
td_quantile(b_span_take, .75), td_quantile(b_span_take, .90), td_quantile(b_span_take, .99), td_quantile(b_span_take, .999), td_quantile(l_span_merge, .5),
|
|
td_quantile(l_span_merge, .75), td_quantile(l_span_merge, .90), td_quantile(l_span_merge, .99), td_quantile(l_span_merge, .999),
|
|
td_quantile(b_span_merge, .5), td_quantile(b_span_merge, .75), td_quantile(b_span_merge, .90), td_quantile(b_span_merge, .99),
|
|
td_quantile(b_span_merge, .999));
|
|
}
|
|
|
|
#define INITIAL_AMOUNT 1024 * 2
|
|
|
|
#define SHORT_OPT "r:fa:bh"
|
|
#define LONG_OPT "record:,force,amount:,buffer,help"
|
|
|
|
void
|
|
print_usage(const char *program_name)
|
|
{
|
|
printf("Usage: %s [OPTIONS]\n", program_name);
|
|
printf(" -r, --record <file> Path to the file for recording (optional)\n");
|
|
printf(" -f, --force Force overwrite of existing file (optional)\n");
|
|
printf(" -b, --buffer Disable buffering writes to stdout/err (optional)\n");
|
|
printf(" -a, --amount <number> Specify the number of entries to record (must be positive, optional)\n");
|
|
printf(" -h, --help Print this help message\n");
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
int opt;
|
|
const char *record_file = NULL;
|
|
int force_flag = 0;
|
|
size_t left, amt = INITIAL_AMOUNT;
|
|
bool buffer = true;
|
|
|
|
fp = stdout;
|
|
|
|
while ((opt = getopt(argc, argv, SHORT_OPT LONG_OPT)) != -1) {
|
|
switch (opt) {
|
|
case 'r':
|
|
record_file = optarg;
|
|
break;
|
|
case 'f':
|
|
force_flag = 1;
|
|
break;
|
|
case 'b':
|
|
buffer = false;
|
|
break;
|
|
case 'a':
|
|
amt = atoi(optarg);
|
|
if (amt <= 0) {
|
|
fprintf(stderr, "Error: Invalid amount. Amount must be a positive number.\n");
|
|
return 1;
|
|
}
|
|
break;
|
|
case 'h':
|
|
print_usage(argv[0]);
|
|
return 0;
|
|
case '?':
|
|
fprintf(stderr, "Unknown option: %c\n", optopt);
|
|
return 1;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Check if record file is specified
|
|
if (record_file == NULL) {
|
|
recording = false;
|
|
} else {
|
|
// Check for existing file without force flag
|
|
if (access(record_file, F_OK) == 0 && !force_flag) {
|
|
fprintf(stderr, "Warning: File '%s' already exists. Use -f or --force to overwrite.\n", record_file);
|
|
return 1;
|
|
}
|
|
|
|
// Open the file for writing (truncate if force flag is set)
|
|
fp = fopen(record_file, force_flag ? "w" : "a");
|
|
if (fp == NULL) {
|
|
perror("Error opening file");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// disable buffering
|
|
if (!buffer) {
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
setvbuf(fp, NULL, _IONBF, 0);
|
|
}
|
|
unsigned types[] = { SM, ML, RB };
|
|
unsigned num_types = (sizeof((types)) / sizeof((types)[0]));
|
|
|
|
/* Setup: add an amt of bits to each container. */
|
|
foreach(types)
|
|
{
|
|
handles[type] = alloc(type, amt);
|
|
for (size_t i = 0; i < amt; i++) {
|
|
assert(invoke(type, is_set, i) == false);
|
|
assert(mutate(type, set, i) == i);
|
|
assert(invoke(type, is_set, i) == true);
|
|
}
|
|
cast(type, validate);
|
|
}
|
|
compare(types);
|
|
left = amt;
|
|
|
|
while (true) {
|
|
// the an amount [1, 16] of pages to find preferring smaller sizes
|
|
unsigned len = toss(15) + 1;
|
|
pgno_t loc[num_types];
|
|
foreach(types)
|
|
{
|
|
loc[type] = invoke(type, find_span, len);
|
|
}
|
|
for (unsigned n = 0; n < num_types; n++) {
|
|
foreach(types)
|
|
{
|
|
assert(invoke(type, is_span, loc[n], len));
|
|
}
|
|
}
|
|
foreach(types)
|
|
{
|
|
cast(type, validate);
|
|
}
|
|
|
|
unsigned which_loc = (unsigned)xorshift32() % num_types;
|
|
foreach(types)
|
|
{
|
|
assert(mutate(type, take_span, loc[which_loc], len));
|
|
cast(type, validate);
|
|
}
|
|
compare(types);
|
|
left -= len;
|
|
|
|
// Once we've used 1/10th of the free list, let's replenish it a bit.
|
|
if (amt - left > amt / 10) {
|
|
do {
|
|
pgno_t pgno;
|
|
size_t len, retries = amt;
|
|
// Find a hole in the map to replenish.
|
|
do {
|
|
len = toss(15) + 1;
|
|
pgno = sparsemap_span(handles[SM], 0, len, false);
|
|
} while (SPARSEMAP_NOT_FOUND(pgno) && --retries);
|
|
if (retries == 0) {
|
|
goto larger_please;
|
|
}
|
|
if (SPARSEMAP_FOUND(pgno)) {
|
|
foreach(types)
|
|
{
|
|
assert(invoke(type, is_empty, pgno, len));
|
|
}
|
|
compare(types);
|
|
foreach(types)
|
|
{
|
|
assert(invoke(type, is_span, pgno, len) == false);
|
|
assert(mutate(type, release_span, pgno, len));
|
|
assert(invoke(type, is_span, pgno, len) == true);
|
|
cast(type, validate);
|
|
}
|
|
compare(types);
|
|
left += len;
|
|
}
|
|
} while (amt - left > amt / 100);
|
|
}
|
|
|
|
if (toss(10) > 8) {
|
|
size_t new_offset, new_amt;
|
|
pgno_t max;
|
|
larger_please:
|
|
new_amt = 1024 + (xorshift32() % 2048) + toss(1024);
|
|
new_offset = sparsemap_get_ending_offset(handles[SM]);
|
|
|
|
// Build a new container to merge with the existing one.
|
|
foreach(types)
|
|
{
|
|
new_handles[type] = alloc(type, new_amt);
|
|
for (size_t i = 0; i < new_amt; i++) {
|
|
// We don't want to record and we're using new_handles not
|
|
// handles, so call fn directly.
|
|
assert(containers[type].is_set(new_handles[type], i + new_offset) == false);
|
|
containers[type].set(&new_handles[type], i + new_offset);
|
|
assert(containers[type].is_set(new_handles[type], i + new_offset) == true);
|
|
}
|
|
}
|
|
foreach(types)
|
|
{
|
|
assert(mutate(type, merge, new_handles[type]));
|
|
cast(type, validate);
|
|
}
|
|
compare(types);
|
|
left += new_amt;
|
|
amt += new_amt;
|
|
foreach(types)
|
|
{
|
|
containers[type].free(new_handles[type]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|