2013-01-11 02:42:37 +05:30
|
|
|
// Copyright (C) 2011-2012 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools 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.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-01-11 03:06:38 +05:30
|
|
|
#include "persistent-data/space-maps/disk.h"
|
|
|
|
#include "persistent-data/space-maps/core.h"
|
|
|
|
#include "persistent-data/space-maps/careful_alloc.h"
|
|
|
|
#include "persistent-data/space-maps/recursive.h"
|
2011-07-22 20:39:56 +05:30
|
|
|
|
|
|
|
#define BOOST_TEST_MODULE SpaceMapDiskTests
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2011-11-03 20:14:00 +05:30
|
|
|
block_address const NR_BLOCKS = 1000; // FIXME: bump up
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address const SUPERBLOCK = 0;
|
2011-11-03 20:14:00 +05:30
|
|
|
block_address const MAX_LOCKS = 8;
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr
|
2011-07-22 20:39:56 +05:30
|
|
|
create_tm() {
|
2011-08-31 17:18:41 +05:30
|
|
|
block_manager<>::ptr bm(
|
2013-01-15 11:01:48 +05:30
|
|
|
new block_manager<>("./test.data", NR_BLOCKS, MAX_LOCKS, block_io<>::READ_WRITE));
|
2013-01-10 17:45:50 +05:30
|
|
|
space_map::ptr sm(new core_map(NR_BLOCKS));
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr tm(
|
|
|
|
new transaction_manager(bm, sm));
|
2011-07-22 20:39:56 +05:30
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:42:37 +05:30
|
|
|
struct sm_core_creator {
|
2013-01-10 16:51:07 +05:30
|
|
|
static space_map::ptr
|
|
|
|
create() {
|
|
|
|
return space_map::ptr(new persistent_data::core_map(NR_BLOCKS));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-11 02:42:37 +05:30
|
|
|
struct sm_careful_alloc_creator {
|
2013-01-10 17:45:50 +05:30
|
|
|
static space_map::ptr
|
|
|
|
create() {
|
2013-01-11 02:35:10 +05:30
|
|
|
return create_careful_alloc_sm(
|
2013-01-10 17:45:50 +05:30
|
|
|
checked_space_map::ptr(
|
|
|
|
new core_map(NR_BLOCKS)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-11 02:42:37 +05:30
|
|
|
struct sm_recursive_creator {
|
|
|
|
static checked_space_map::ptr
|
|
|
|
create() {
|
|
|
|
return create_recursive_sm(
|
|
|
|
checked_space_map::ptr(
|
|
|
|
new core_map(NR_BLOCKS)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sm_disk_creator {
|
2013-01-10 01:54:11 +05:30
|
|
|
static persistent_space_map::ptr
|
|
|
|
create() {
|
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
return persistent_data::create_disk_sm(tm, NR_BLOCKS);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
static persistent_space_map::ptr
|
|
|
|
open(void *root) {
|
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
return persistent_data::open_disk_sm(tm, root);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-11 02:42:37 +05:30
|
|
|
struct sm_metadata_creator {
|
2013-01-10 01:54:11 +05:30
|
|
|
static persistent_space_map::ptr
|
|
|
|
create() {
|
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
return persistent_data::create_metadata_sm(tm, NR_BLOCKS);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
static persistent_space_map::ptr
|
|
|
|
open(void *root) {
|
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
return persistent_data::open_metadata_sm(tm, root);
|
|
|
|
}
|
|
|
|
};
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
//--------------------------------
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_get_nr_blocks(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_blocks(), NR_BLOCKS);
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_get_nr_free(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_nr_free(), NR_BLOCKS);
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
optional<block_address> mb = sm->new_block();
|
|
|
|
BOOST_REQUIRE(mb);
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_nr_free(), NR_BLOCKS - i - 1);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->dec(i);
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_nr_free(), i + 1);
|
|
|
|
}
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_runs_out_of_space(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
optional<block_address> mb;
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
mb = sm->new_block();
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
mb = sm->new_block();
|
|
|
|
BOOST_REQUIRE(!mb);
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_inc_and_dec(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
block_address b = 63;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 50; i++) {
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_count(b), i);
|
|
|
|
sm->inc(b);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 50; i > 0; i--) {
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_count(b), i);
|
|
|
|
sm->dec(b);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_not_allocated_twice(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
optional<block_address> mb = sm->new_block();
|
|
|
|
BOOST_REQUIRE(mb);
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (;;) {
|
|
|
|
boost::optional<block_address> b = sm->new_block();
|
|
|
|
if (!b)
|
|
|
|
break;
|
2011-07-22 21:02:47 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
if (b)
|
|
|
|
BOOST_REQUIRE(*b != *mb);
|
|
|
|
}
|
2011-07-22 21:02:47 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_set_count(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
sm->set_count(43, 5);
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_count(43), 5);
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
void test_set_affects_nr_allocated(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->set_count(i, 1);
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_nr_free(), NR_BLOCKS - i - 1);
|
|
|
|
}
|
2011-11-09 15:51:25 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->set_count(i, 0);
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_nr_free(), i + 1);
|
|
|
|
}
|
|
|
|
}
|
2011-11-09 15:51:25 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
// Ref counts below 3 gets stored as bitmaps, above 3 they go into
|
|
|
|
// a btree with uint32_t values. Worth checking this thoroughly,
|
|
|
|
// especially for the metadata format which may have complications
|
|
|
|
// due to recursion.
|
|
|
|
void test_high_ref_counts(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
srand(1234);
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
sm->set_count(i, rand() % 6789);
|
|
|
|
sm->commit();
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->inc(i);
|
|
|
|
sm->inc(i);
|
|
|
|
if (i % 1000)
|
|
|
|
sm->commit();
|
|
|
|
}
|
|
|
|
sm->commit();
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
srand(1234);
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_count(i), (rand() % 6789) + 2);
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
sm->dec(i);
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
srand(1234);
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_count(i), (rand() % 6789) + 1);
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
template <typename SMCreator>
|
|
|
|
void test_sm_reopen()
|
|
|
|
{
|
|
|
|
unsigned char buffer[128];
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
{
|
|
|
|
persistent_space_map::ptr sm = SMCreator::create();
|
|
|
|
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
|
|
|
|
sm->inc(i);
|
|
|
|
sm->commit();
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
BOOST_REQUIRE(sm->root_size() <= sizeof(buffer));
|
|
|
|
sm->copy_root(buffer, sizeof(buffer));
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
{
|
|
|
|
persistent_space_map::ptr sm = SMCreator::open(buffer);
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
|
|
|
|
BOOST_REQUIRE_EQUAL(sm->get_count(i), 1);
|
|
|
|
}
|
|
|
|
}
|
2011-11-09 15:51:25 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
typedef void (*sm_test)(space_map::ptr);
|
2011-07-22 21:13:15 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
template <typename SMCreator, unsigned NTests>
|
|
|
|
void do_tests(sm_test (&tests)[NTests])
|
2011-07-22 21:13:15 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
for (unsigned t = 0; t < NTests; t++) {
|
|
|
|
space_map::ptr sm = SMCreator::create();
|
|
|
|
tests[t](sm);
|
2011-07-22 21:13:15 +05:30
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
sm_test space_map_tests[] = {
|
|
|
|
test_get_nr_blocks,
|
|
|
|
test_get_nr_free,
|
|
|
|
test_runs_out_of_space,
|
|
|
|
test_inc_and_dec,
|
|
|
|
test_not_allocated_twice,
|
|
|
|
test_set_count,
|
|
|
|
test_set_affects_nr_allocated,
|
|
|
|
test_high_ref_counts
|
|
|
|
};
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-01-10 16:51:07 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_sm_core)
|
|
|
|
{
|
|
|
|
do_tests<sm_core_creator>(space_map_tests);
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:35:10 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_sm_careful_alloc)
|
2013-01-10 17:45:50 +05:30
|
|
|
{
|
2013-01-11 02:35:10 +05:30
|
|
|
do_tests<sm_careful_alloc_creator>(space_map_tests);
|
2013-01-10 17:45:50 +05:30
|
|
|
}
|
|
|
|
|
2013-01-11 02:42:37 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_sm_recursive)
|
|
|
|
{
|
|
|
|
do_tests<sm_recursive_creator>(space_map_tests);
|
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_sm_disk)
|
2011-11-03 20:14:00 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
do_tests<sm_disk_creator>(space_map_tests);
|
|
|
|
test_sm_reopen<sm_disk_creator>();
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_sm_metadata)
|
2011-11-09 15:51:25 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
do_tests<sm_metadata_creator>(space_map_tests);
|
|
|
|
test_sm_reopen<sm_metadata_creator>();
|
2011-07-22 21:13:15 +05:30
|
|
|
}
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
//----------------------------------------------------------------
|