Commit bf494843 authored by John Esmet's avatar John Esmet

FT-272 Simple block allocator strategy unit test, improved base unit

test
parent 55ccdcbf
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
/*
COPYING CONDITIONS NOTICE:
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation, and provided that the
following conditions are met:
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below).
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below) in the documentation and/or other materials
provided with the distribution.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
COPYRIGHT NOTICE:
TokuDB, Tokutek Fractal Tree Indexing Library.
Copyright (C) 2007-2013 Tokutek, Inc.
DISCLAIMER:
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
UNIVERSITY PATENT NOTICE:
The technology is licensed by the Massachusetts Institute of
Technology, Rutgers State University of New Jersey, and the Research
Foundation of State University of New York at Stony Brook under
United States of America Serial No. 11/760379 and to the patents
and/or patent applications resulting from it.
PATENT MARKING NOTICE:
This software is covered by US Patent No. 8,185,551.
This software is covered by US Patent No. 8,489,638.
PATENT RIGHTS GRANT:
"THIS IMPLEMENTATION" means the copyrightable works distributed by
Tokutek as part of the Fractal Tree project.
"PATENT CLAIMS" means the claims of patents that are owned or
licensable by Tokutek, both currently or in the future; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
"PATENT CHALLENGE" shall mean a challenge to the validity,
patentability, enforceability and/or non-infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
Tokutek hereby grants to you, for the term and geographical scope of
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and
otherwise run, modify, and propagate the contents of THIS
IMPLEMENTATION, where such license applies only to the PATENT
CLAIMS. This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION. If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any rights
granted to you under this License shall terminate as of the date
such litigation is filed. If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE, then Tokutek may terminate any rights granted to you
under this License.
*/
#include "ft/tests/test.h"
#include "ft/serialize/block_allocator_strategy.h"
static const uint64_t alignment = 4096;
static void test_first_vs_best_fit(void) {
struct block_allocator::blockpair pairs[] = {
block_allocator::blockpair(1 * alignment, 6 * alignment),
// hole between 7x align -> 8x align
block_allocator::blockpair(8 * alignment, 4 * alignment),
// hole between 12x align -> 16x align
block_allocator::blockpair(16 * alignment, 1 * alignment),
block_allocator::blockpair(17 * alignment, 2 * alignment),
// hole between 19 align -> 21x align
block_allocator::blockpair(21 * alignment, 2 * alignment),
};
const uint64_t n_blocks = sizeof(pairs) / sizeof(pairs[0]);
block_allocator::blockpair *bp;
// first fit
bp = block_allocator_strategy::first_fit(pairs, n_blocks, 100, alignment);
assert(bp == &pairs[0]);
bp = block_allocator_strategy::first_fit(pairs, n_blocks, 4096, alignment);
assert(bp == &pairs[0]);
bp = block_allocator_strategy::first_fit(pairs, n_blocks, 3 * 4096, alignment);
assert(bp == &pairs[1]);
bp = block_allocator_strategy::first_fit(pairs, n_blocks, 5 * 4096, alignment);
assert(bp == nullptr);
// best fit
bp = block_allocator_strategy::best_fit(pairs, n_blocks, 100, alignment);
assert(bp == &pairs[0]);
bp = block_allocator_strategy::best_fit(pairs, n_blocks, 4100, alignment);
assert(bp == &pairs[3]);
bp = block_allocator_strategy::best_fit(pairs, n_blocks, 3 * 4096, alignment);
assert(bp == &pairs[1]);
bp = block_allocator_strategy::best_fit(pairs, n_blocks, 5 * 4096, alignment);
assert(bp == nullptr);
}
static void test_padded_fit(void) {
struct block_allocator::blockpair pairs[] = {
block_allocator::blockpair(1 * alignment, 1 * alignment),
// 4096 byte hole after bp[0]
block_allocator::blockpair(3 * alignment, 1 * alignment),
// 8192 byte hole after bp[1]
block_allocator::blockpair(6 * alignment, 1 * alignment),
// 16384 byte hole after bp[2]
block_allocator::blockpair(11 * alignment, 1 * alignment),
// 32768 byte hole after bp[3]
block_allocator::blockpair(17 * alignment, 1 * alignment),
// 116kb hole after bp[4]
block_allocator::blockpair(113 * alignment, 1 * alignment),
// 256kb hole after bp[5]
block_allocator::blockpair(371 * alignment, 1 * alignment),
};
const uint64_t n_blocks = sizeof(pairs) / sizeof(pairs[0]);
block_allocator::blockpair *bp;
// padding for a 100 byte allocation will be < than standard alignment,
// so it should fit in the first 4096 byte hole.
bp = block_allocator_strategy::padded_fit(pairs, n_blocks, 4000, alignment);
assert(bp == &pairs[0]);
// Even padded, a 12kb alloc will fit in a 16kb hole
bp = block_allocator_strategy::padded_fit(pairs, n_blocks, 3 * alignment, alignment);
assert(bp == &pairs[2]);
// would normally fit in the 116kb hole but the padding will bring it over
bp = block_allocator_strategy::padded_fit(pairs, n_blocks, 116 * alignment, alignment);
assert(bp == &pairs[5]);
bp = block_allocator_strategy::padded_fit(pairs, n_blocks, 127 * alignment, alignment);
assert(bp == &pairs[5]);
}
int test_main(int argc, const char *argv[]) {
(void) argc;
(void) argv;
test_first_vs_best_fit();
test_padded_fit();
return 0;
}
......@@ -93,7 +93,8 @@ PATENT RIGHTS GRANT:
static void ba_alloc(block_allocator *ba, uint64_t size, uint64_t *answer) {
ba->validate();
uint64_t actual_answer;
ba->alloc_block(512 * size, 0, &actual_answer);
const uint64_t heat = random() % 2;
ba->alloc_block(512 * size, heat, &actual_answer);
ba->validate();
assert(actual_answer%512==0);
......@@ -123,11 +124,11 @@ static void ba_check_none(block_allocator *ba, uint64_t blocknum_in_layout_order
// Simple block allocator test
static void
test_ba0 (void) {
static void test_ba0(block_allocator::allocation_strategy strategy) {
block_allocator allocator;
block_allocator *ba = &allocator;
ba->create(100*512, 1*512);
ba->set_strategy(strategy);
assert(ba->allocated_limit()==100*512);
uint64_t b2, b3, b4, b5, b6, b7;
......@@ -160,22 +161,23 @@ test_ba0 (void) {
// Manually to get coverage of all the code in the block allocator.
static void
test_ba1 (int n_initial) {
test_ba1(block_allocator::allocation_strategy strategy, int n_initial) {
block_allocator allocator;
block_allocator *ba = &allocator;
ba->create(0*512, 1*512);
int i;
ba->set_strategy(strategy);
int n_blocks=0;
uint64_t blocks[1000];
for (i=0; i<1000; i++) {
if (i<n_initial || random()%2 == 0) {
if (n_blocks<1000) {
for (int i = 0; i < 1000; i++) {
if (i < n_initial || random() % 2 == 0) {
if (n_blocks < 1000) {
ba_alloc(ba, 1, &blocks[n_blocks]);
//printf("A[%d]=%ld\n", n_blocks, blocks[n_blocks]);
n_blocks++;
}
} else {
if (n_blocks>0) {
if (n_blocks > 0) {
int blocknum = random()%n_blocks;
//printf("F[%d]%ld\n", blocknum, blocks[blocknum]);
ba_free(ba, blocks[blocknum]);
......@@ -197,7 +199,9 @@ test_ba2 (void)
uint64_t b[6];
enum { BSIZE = 1024 };
ba->create(100*512, BSIZE*512);
ba->set_strategy(block_allocator::BA_STRATEGY_FIRST_FIT);
assert(ba->allocated_limit()==100*512);
ba_check_l (ba, 0, 0, 100);
ba_check_none (ba, 1);
......@@ -206,16 +210,16 @@ test_ba2 (void)
ba_check_l (ba, 1, BSIZE, 100);
ba_check_none (ba, 2);
ba_alloc (ba, BSIZE+100, &b[1]);
ba_alloc (ba, BSIZE + 100, &b[1]);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_none (ba, 3);
ba_alloc (ba, 100, &b[2]);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 3, 4*BSIZE, 100);
ba_check_none (ba, 4);
......@@ -224,7 +228,7 @@ test_ba2 (void)
ba_alloc (ba, 100, &b[5]);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 3, 4*BSIZE, 100);
ba_check_l (ba, 4, 5*BSIZE, 100);
ba_check_l (ba, 5, 6*BSIZE, 100);
......@@ -234,7 +238,7 @@ test_ba2 (void)
ba_free (ba, 4*BSIZE);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 3, 5*BSIZE, 100);
ba_check_l (ba, 4, 6*BSIZE, 100);
ba_check_l (ba, 5, 7*BSIZE, 100);
......@@ -245,7 +249,7 @@ test_ba2 (void)
assert(b2==4*BSIZE);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 3, 4*BSIZE, 100);
ba_check_l (ba, 4, 5*BSIZE, 100);
ba_check_l (ba, 5, 6*BSIZE, 100);
......@@ -255,7 +259,7 @@ test_ba2 (void)
ba_free (ba, BSIZE);
ba_free (ba, 5*BSIZE);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 1, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 2, 4*BSIZE, 100);
ba_check_l (ba, 3, 6*BSIZE, 100);
ba_check_l (ba, 4, 7*BSIZE, 100);
......@@ -273,7 +277,7 @@ test_ba2 (void)
assert(b5==5*BSIZE);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 3, 4*BSIZE, 100);
ba_check_l (ba, 4, 5*BSIZE, 100);
ba_check_l (ba, 5, 6*BSIZE, 100);
......@@ -290,7 +294,7 @@ test_ba2 (void)
assert(b8==10*BSIZE);
ba_check_l (ba, 0, 0, 100);
ba_check_l (ba, 1, BSIZE, 100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE+100);
ba_check_l (ba, 2, 2*BSIZE, BSIZE + 100);
ba_check_l (ba, 3, 4*BSIZE, 100);
ba_check_l (ba, 4, 5*BSIZE, 100);
ba_check_l (ba, 5, 6*BSIZE, 100);
......@@ -321,10 +325,18 @@ test_ba2 (void)
int
test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) {
test_ba0();
test_ba1(0);
test_ba1(10);
test_ba1(20);
enum block_allocator::allocation_strategy strategies[] = {
block_allocator::BA_STRATEGY_FIRST_FIT,
block_allocator::BA_STRATEGY_BEST_FIT,
block_allocator::BA_STRATEGY_PADDED_FIT,
block_allocator::BA_STRATEGY_HEAT_ZONE,
};
for (size_t i = 0; i < sizeof(strategies) / sizeof(strategies[0]); i++) {
test_ba0(strategies[i]);
test_ba1(strategies[i], 0);
test_ba1(strategies[i], 10);
test_ba1(strategies[i], 20);
}
test_ba2();
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment