ft-serialize-benchmark.c 10.4 KB
Newer Older
1
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
// vim: expandtab:ts=8:sw=4:softtabstop=4:
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ident "$Id$"
#ident "Copyright (c) 2007, 2008 Tokutek Inc.  All rights reserved."

#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include "test.h"

#include "includes.h"

#define MIN(x, y) (((x) < (y)) ? (x) : (y))
const double USECS_PER_SEC = 1000000.0;

static int omt_cmp(OMTVALUE p, void *q)
{
18 19
    LEAFENTRY a = cast_to_typeof(a) p;
    LEAFENTRY b = cast_to_typeof(b) q;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    void *ak, *bk;
    u_int32_t al, bl;
    ak = le_key_and_len(a, &al);
    bk = le_key_and_len(b, &bl);
    int l = MIN(al, bl);
    int c = memcmp(ak, bk, l);
    if (c < 0) { return -1; }
    if (c > 0) { return +1; }
    int d = al - bl;
    if (d < 0) { return -1; }
    if (d > 0) { return +1; }
    else       { return  0; }
}

static LEAFENTRY
le_fastmalloc(char *key, int keylen, char *val, int vallen)
{
37
    LEAFENTRY r = cast_to_typeof(r) toku_malloc(sizeof(r->type) + sizeof(r->keylen) + sizeof(r->u.clean.vallen) +
38 39 40 41 42 43 44 45 46 47 48 49 50
                              keylen + vallen);
    resource_assert(r);
    r->type = LE_CLEAN;
    r->keylen = keylen;
    r->u.clean.vallen = vallen;
    memcpy(&r->u.clean.key_val[0], key, keylen);
    memcpy(&r->u.clean.key_val[keylen], val, vallen);
    return r;
}

static int
long_key_cmp(DB *UU(e), const DBT *a, const DBT *b)
{
51 52
    const long *x = cast_to_typeof(x) a->data;
    const long *y = cast_to_typeof(y) b->data;
53 54 55 56 57
    return (*x > *y) - (*x < *y);
}

static void
test_serialize_leaf(int valsize, int nelts, double entropy) {
58
    //    struct ft_handle source_ft;
59
    const int nodesize = (1<<22);
60
    struct ftnode *sn, *dn;
61

62
    int fd = open(__SRCFILE__ ".ft", O_RDWR|O_CREAT|O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO); assert(fd >= 0);
63 64 65

    int r;

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    XCALLOC(sn);

    sn->max_msn_applied_to_node_on_disk.msn = 0;
    sn->nodesize = nodesize;
    sn->flags = 0x11223344;
    sn->thisnodename.b = 20;
    sn->layout_version = FT_LAYOUT_VERSION;
    sn->layout_version_original = FT_LAYOUT_VERSION;
    sn->height = 0;
    sn->n_children = 8;
    sn->dirty = 1;
    MALLOC_N(sn->n_children, sn->bp);
    MALLOC_N(sn->n_children-1, sn->childkeys);
    sn->totalchildkeylens = 0;
    for (int i = 0; i < sn->n_children; ++i) {
        BP_STATE(sn,i) = PT_AVAIL;
        set_BLB(sn, i, toku_create_empty_bn());
83
    }
84
    int nperbn = nelts / sn->n_children;
85 86
    LEAFENTRY les[nelts];
    memset(les, 0, sizeof les);
87
    for (int ck = 0; ck < sn->n_children; ++ck) {
88 89 90 91 92 93
        long k;
        for (long i = 0; i < nperbn; ++i) {
            k = ck * nperbn + i;
            char buf[valsize];
            int c;
            for (c = 0; c < valsize * entropy; ) {
94 95 96
                int *p = (int *) &buf[c];
                *p = rand();
                c += sizeof(*p);
97 98 99
            }
            memset(&buf[c], 0, valsize - c);
            les[k] = le_fastmalloc((char *)&k, sizeof k, buf, sizeof buf);
100
            r = toku_omt_insert(BLB_BUFFER(sn, ck), les[k], omt_cmp, les[k], NULL); assert(r==0);
101
        }
102
        BLB_NBYTESINBUF(sn, ck) = nperbn*(KEY_VALUE_OVERHEAD+(sizeof(long)+valsize)) + toku_omt_size(BLB_BUFFER(sn, ck));
103
        if (ck < 7) {
104 105
            toku_fill_dbt(&sn->childkeys[ck], toku_xmemdup(&k, sizeof k), sizeof k);
            sn->totalchildkeylens += sizeof k;
106 107 108
        }
    }

109 110
    FT_HANDLE XMALLOC(brt);
    FT XCALLOC(brt_h);
111 112 113 114 115 116 117
    toku_ft_init(brt_h,
                 make_blocknum(0),
                 ZERO_LSN,
                 TXNID_NONE,
                 4*1024*1024,
                 128*1024,
                 TOKU_DEFAULT_COMPRESSION_METHOD);
118
    brt->ft = brt_h;
119
    brt_h->panic = 0; brt_h->panic_string = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
120
    brt_h->compare_fun = long_key_cmp;
121
    toku_ft_init_treelock(brt_h);
122
    toku_blocktable_create_new(&brt_h->blocktable);
123
    { int r_truncate = ftruncate(fd, 0); CKERR(r_truncate); }
124 125 126 127 128 129 130 131 132 133
    //Want to use block #20
    BLOCKNUM b = make_blocknum(0);
    while (b.b < 20) {
        toku_allocate_blocknum(brt_h->blocktable, &b, brt_h);
    }
    assert(b.b == 20);

    {
        DISKOFF offset;
        DISKOFF size;
134
        toku_blocknum_realloc_on_disk(brt_h->blocktable, b, 100, &offset, brt_h, fd, FALSE);
135 136 137 138 139 140 141 142 143
        assert(offset==BLOCK_ALLOCATOR_TOTAL_HEADER_RESERVE);

        toku_translate_blocknum_to_offset_size(brt_h->blocktable, b, &offset, &size);
        assert(offset == BLOCK_ALLOCATOR_TOTAL_HEADER_RESERVE);
        assert(size   == 100);
    }

    struct timeval t[2];
    gettimeofday(&t[0], NULL);
144
    FTNODE_DISK_DATA ndd = NULL;
145
    r = toku_serialize_ftnode_to(fd, make_blocknum(20), sn, &ndd, TRUE, brt->ft, FALSE);
146 147 148 149 150 151
    assert(r==0);
    gettimeofday(&t[1], NULL);
    double dt;
    dt = (t[1].tv_sec - t[0].tv_sec) + ((t[1].tv_usec - t[0].tv_usec) / USECS_PER_SEC);
    printf("serialize leaf:   %0.05lf\n", dt);

152
    struct ftnode_fetch_extra bfe;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
153
    fill_bfe_for_full_read(&bfe, brt_h);
154
    gettimeofday(&t[0], NULL);
155 156
    FTNODE_DISK_DATA ndd2 = NULL;
    r = toku_deserialize_ftnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, &dn, &ndd2, &bfe);
157 158 159 160 161
    assert(r==0);
    gettimeofday(&t[1], NULL);
    dt = (t[1].tv_sec - t[0].tv_sec) + ((t[1].tv_usec - t[0].tv_usec) / USECS_PER_SEC);
    printf("deserialize leaf: %0.05lf\n", dt);

162
    toku_ftnode_free(&dn);
163 164

    for (int i = 0; i < nelts; ++i) {
165 166 167
        if (les[i]) {
            toku_free(les[i]);
        }
168
    }
169
    toku_ftnode_free(&sn);
170 171 172

    toku_block_free(brt_h->blocktable, BLOCK_ALLOCATOR_TOTAL_HEADER_RESERVE);
    toku_blocktable_destroy(&brt_h->blocktable);
173
    toku_ft_destroy_treelock(brt_h);
174
    toku_free(brt_h->h);
175 176
    toku_free(brt_h);
    toku_free(brt);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
177 178
    toku_free(ndd);
    toku_free(ndd2);
179 180 181 182 183 184

    r = close(fd); assert(r != -1);
}

static void
test_serialize_nonleaf(int valsize, int nelts, double entropy) {
185
    //    struct ft_handle source_ft;
186
    const int nodesize = (1<<22);
187
    struct ftnode sn, *dn;
188

189
    int fd = open(__SRCFILE__ ".ft", O_RDWR|O_CREAT|O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO); assert(fd >= 0);
190 191 192

    int r;

193
    //    source_ft.fd=fd;
194 195 196 197
    sn.max_msn_applied_to_node_on_disk.msn = 0;
    sn.nodesize = nodesize;
    sn.flags = 0x11223344;
    sn.thisnodename.b = 20;
198 199
    sn.layout_version = FT_LAYOUT_VERSION;
    sn.layout_version_original = FT_LAYOUT_VERSION;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    sn.height = 1;
    sn.n_children = 8;
    sn.dirty = 1;
    MALLOC_N(sn.n_children, sn.bp);
    MALLOC_N(sn.n_children-1, sn.childkeys);
    sn.totalchildkeylens = 0;
    for (int i = 0; i < sn.n_children; ++i) {
        BP_BLOCKNUM(&sn, i).b = 30 + (i*5);
        BP_STATE(&sn,i) = PT_AVAIL;
        set_BNC(&sn, i, toku_create_empty_nl());
    }
    //Create XIDS
    XIDS xids_0 = xids_get_root_xids();
    XIDS xids_123;
    r = xids_create_child(xids_0, &xids_123, (TXNID)123);
    CKERR(r);
    int nperchild = nelts / 8;
    for (int ck = 0; ck < sn.n_children; ++ck) {
        long k;
219
        NONLEAF_CHILDINFO bnc = BNC(&sn, ck);
220 221 222 223 224
        for (long i = 0; i < nperchild; ++i) {
            k = ck * nperchild + i;
            char buf[valsize];
            int c;
            for (c = 0; c < valsize * entropy; ) {
225 226 227
                int *p = (int *) &buf[c];
                *p = rand();
                c += sizeof(*p);
228 229
            }
            memset(&buf[c], 0, valsize - c);
230

231
            r = toku_bnc_insert_msg(bnc, &k, sizeof k, buf, valsize, FT_NONE, next_dummymsn(), xids_123, true, NULL, long_key_cmp); assert_zero(r);
232 233
        }
        if (ck < 7) {
234
            toku_fill_dbt(&sn.childkeys[ck], toku_xmemdup(&k, sizeof k), sizeof k);
235 236 237 238 239 240 241 242
            sn.totalchildkeylens += sizeof k;
        }
    }

    //Cleanup:
    xids_destroy(&xids_0);
    xids_destroy(&xids_123);

243 244
    FT_HANDLE XMALLOC(brt);
    FT XCALLOC(brt_h);
245 246 247 248 249 250 251
    toku_ft_init(brt_h,
                 make_blocknum(0),
                 ZERO_LSN,
                 TXNID_NONE,
                 4*1024*1024,
                 128*1024,
                 TOKU_DEFAULT_COMPRESSION_METHOD);
252
    brt->ft = brt_h;
253
    brt_h->panic = 0; brt_h->panic_string = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
254
    brt_h->compare_fun = long_key_cmp;
255
    toku_ft_init_treelock(brt_h);
256
    toku_blocktable_create_new(&brt_h->blocktable);
257
    { int r_truncate = ftruncate(fd, 0); CKERR(r_truncate); }
258 259 260 261 262 263 264 265 266 267
    //Want to use block #20
    BLOCKNUM b = make_blocknum(0);
    while (b.b < 20) {
        toku_allocate_blocknum(brt_h->blocktable, &b, brt_h);
    }
    assert(b.b == 20);

    {
        DISKOFF offset;
        DISKOFF size;
268
        toku_blocknum_realloc_on_disk(brt_h->blocktable, b, 100, &offset, brt_h, fd, FALSE);
269 270 271 272 273 274 275 276 277
        assert(offset==BLOCK_ALLOCATOR_TOTAL_HEADER_RESERVE);

        toku_translate_blocknum_to_offset_size(brt_h->blocktable, b, &offset, &size);
        assert(offset == BLOCK_ALLOCATOR_TOTAL_HEADER_RESERVE);
        assert(size   == 100);
    }

    struct timeval t[2];
    gettimeofday(&t[0], NULL);
278
    FTNODE_DISK_DATA ndd = NULL;
279
    r = toku_serialize_ftnode_to(fd, make_blocknum(20), &sn, &ndd, TRUE, brt->ft, FALSE);
280 281 282 283 284 285
    assert(r==0);
    gettimeofday(&t[1], NULL);
    double dt;
    dt = (t[1].tv_sec - t[0].tv_sec) + ((t[1].tv_usec - t[0].tv_usec) / USECS_PER_SEC);
    printf("serialize nonleaf:   %0.05lf\n", dt);

286
    struct ftnode_fetch_extra bfe;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
287
    fill_bfe_for_full_read(&bfe, brt_h);
288
    gettimeofday(&t[0], NULL);
289 290
    FTNODE_DISK_DATA ndd2 = NULL;
    r = toku_deserialize_ftnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, &dn, &ndd2, &bfe);
291 292 293 294 295
    assert(r==0);
    gettimeofday(&t[1], NULL);
    dt = (t[1].tv_sec - t[0].tv_sec) + ((t[1].tv_usec - t[0].tv_usec) / USECS_PER_SEC);
    printf("deserialize nonleaf: %0.05lf\n", dt);

296
    toku_ftnode_free(&dn);
297 298

    for (int i = 0; i < sn.n_children-1; ++i) {
299
        toku_free(sn.childkeys[i].data);
300 301 302 303 304 305 306 307 308
    }
    for (int i = 0; i < sn.n_children; ++i) {
        destroy_nonleaf_childinfo(BNC(&sn, i));
    }
    toku_free(sn.bp);
    toku_free(sn.childkeys);

    toku_block_free(brt_h->blocktable, BLOCK_ALLOCATOR_TOTAL_HEADER_RESERVE);
    toku_blocktable_destroy(&brt_h->blocktable);
309
    toku_ft_destroy_treelock(brt_h);
310
    toku_free(brt_h->h);
311 312
    toku_free(brt_h);
    toku_free(brt);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
313 314
    toku_free(ndd);
    toku_free(ndd2);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

    r = close(fd); assert(r != -1);
}

int
test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) {
    long valsize, nelts;
    double entropy = 0.3;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <valsize> <nelts>\n", argv[0]);
        return 2;
    }
    valsize = strtol(argv[1], NULL, 0);
    nelts = strtol(argv[2], NULL, 0);

331
    initialize_dummymsn();
332 333 334 335 336
    test_serialize_leaf(valsize, nelts, entropy);
    test_serialize_nonleaf(valsize, nelts, entropy);

    return 0;
}