From 86b6ca3dc217d45bd7d7bc913659d0bf04e0dbe9 Mon Sep 17 00:00:00 2001 From: Ramesh Chander Date: Fri, 16 Dec 2016 06:58:53 -0800 Subject: [PATCH] allocat_extent api rename and tests for new api Signed-off-by: Ramesh Chander --- src/os/bluestore/Allocator.h | 12 +++--- src/os/bluestore/BitMapAllocator.cc | 8 ++-- src/os/bluestore/BitMapAllocator.h | 6 +-- src/os/bluestore/BlueFS.cc | 14 +++---- src/os/bluestore/BlueStore.cc | 6 +-- src/os/bluestore/StupidAllocator.cc | 6 +-- src/os/bluestore/StupidAllocator.h | 4 +- src/test/objectstore/Allocator_test.cc | 26 ++++++------- src/test/objectstore/BitAllocator_test.cc | 46 ++++++++++++++--------- 9 files changed, 69 insertions(+), 59 deletions(-) diff --git a/src/os/bluestore/Allocator.h b/src/os/bluestore/Allocator.h index d74822a0877..cbcbc3693be 100644 --- a/src/os/bluestore/Allocator.h +++ b/src/os/bluestore/Allocator.h @@ -35,13 +35,13 @@ public: * Apart from that extents can vary between these lower and higher limits according * to free block search algorithm and availability of contiguous space. */ - virtual int alloc_extents(uint64_t want_size, uint64_t alloc_unit, - uint64_t max_alloc_size, int64_t hint, - AllocExtentVector *extents, int *count, uint64_t *ret_len) = 0; + virtual int allocate(uint64_t want_size, uint64_t alloc_unit, + uint64_t max_alloc_size, int64_t hint, + AllocExtentVector *extents, int *count, uint64_t *ret_len) = 0; - int alloc_extents(uint64_t want_size, uint64_t alloc_unit, - int64_t hint, AllocExtentVector *extents, int *count, uint64_t *ret_len) { - return alloc_extents(want_size, alloc_unit, want_size, hint, extents, count, ret_len); + int allocate(uint64_t want_size, uint64_t alloc_unit, + int64_t hint, AllocExtentVector *extents, int *count, uint64_t *ret_len) { + return allocate(want_size, alloc_unit, want_size, hint, extents, count, ret_len); } virtual int release( diff --git a/src/os/bluestore/BitMapAllocator.cc b/src/os/bluestore/BitMapAllocator.cc index 169208b017c..326442f5edf 100644 --- a/src/os/bluestore/BitMapAllocator.cc +++ b/src/os/bluestore/BitMapAllocator.cc @@ -105,7 +105,7 @@ void BitMapAllocator::unreserve(uint64_t unused) m_bit_alloc->unreserve_blocks(nblks); } -int BitMapAllocator::alloc_extents( +int BitMapAllocator::allocate( uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, int64_t hint, mempool::bluestore_alloc::vector *extents, int *count, uint64_t *ret_len) { @@ -121,11 +121,11 @@ int BitMapAllocator::alloc_extents( << " hint " << hint << dendl; - return alloc_extents_dis(want_size, alloc_unit / m_block_size, - max_alloc_size, hint / m_block_size, extents, count, ret_len); + return allocate_dis(want_size, alloc_unit / m_block_size, + max_alloc_size, hint / m_block_size, extents, count, ret_len); } -int BitMapAllocator::alloc_extents_dis( +int BitMapAllocator::allocate_dis( uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, int64_t hint, mempool::bluestore_alloc::vector *extents, int *count, uint64_t *ret_len) { diff --git a/src/os/bluestore/BitMapAllocator.h b/src/os/bluestore/BitMapAllocator.h index 3214b2fa735..a721e23ca05 100644 --- a/src/os/bluestore/BitMapAllocator.h +++ b/src/os/bluestore/BitMapAllocator.h @@ -19,10 +19,8 @@ class BitMapAllocator : public Allocator { BitAllocator *m_bit_alloc; // Bit allocator instance void insert_free(uint64_t offset, uint64_t len); - int alloc_extents_cont(uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, - int64_t hint, mempool::bluestore_alloc::vector *extents, int *count); - int alloc_extents_dis(uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, + int allocate_dis(uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, int64_t hint, mempool::bluestore_alloc::vector *extents, int *count, uint64_t *ret_len); public: @@ -33,7 +31,7 @@ public: int reserve(uint64_t need); void unreserve(uint64_t unused); - int alloc_extents( + int allocate( uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, int64_t hint, mempool::bluestore_alloc::vector *extents, int *count, uint64_t *ret_len); diff --git a/src/os/bluestore/BlueFS.cc b/src/os/bluestore/BlueFS.cc index 6a8120b498a..97079d0aa70 100644 --- a/src/os/bluestore/BlueFS.cc +++ b/src/os/bluestore/BlueFS.cc @@ -181,8 +181,8 @@ int BlueFS::reclaim_blocks(unsigned id, uint64_t want, uint64_t alloc_len = 0;; AllocExtentVector extents = AllocExtentVector(want / g_conf->bluefs_alloc_size); - r = alloc[id]->alloc_extents(want, g_conf->bluefs_alloc_size, 0, - &extents, &count, &alloc_len); + r = alloc[id]->allocate(want, g_conf->bluefs_alloc_size, 0, + &extents, &count, &alloc_len); *length = alloc_len; assert(r >= 0); @@ -190,9 +190,9 @@ int BlueFS::reclaim_blocks(unsigned id, uint64_t want, alloc[id]->unreserve(want - *length); for (int i = 0; i < count; i++) { - block_all[id].erase(extents[i].offset, extents[i].length); - block_total[id] -= extents[i].length; - log_t.op_alloc_rm(id, extents[i].offset, extents[i].length); + block_all[id].erase(extents[i].offset, extents[i].length); + block_total[id] -= extents[i].length; + log_t.op_alloc_rm(id, extents[i].offset, extents[i].length); } r = _flush_and_sync_log(l); @@ -1772,8 +1772,8 @@ int BlueFS::_allocate(uint8_t id, uint64_t len, uint64_t alloc_len = 0; AllocExtentVector extents = AllocExtentVector(left / min_alloc_size); - r = alloc[id]->alloc_extents(left, min_alloc_size, hint, - &extents, &count, &alloc_len); + r = alloc[id]->allocate(left, min_alloc_size, hint, + &extents, &count, &alloc_len); if (r < 0 || alloc_len < left) { derr << __func__ << " allocate failed on 0x" << std::hex << left << " min_alloc_size 0x" << min_alloc_size << std::dec << dendl; diff --git a/src/os/bluestore/BlueStore.cc b/src/os/bluestore/BlueStore.cc index f8b0761869e..4cf1d858721 100644 --- a/src/os/bluestore/BlueStore.cc +++ b/src/os/bluestore/BlueStore.cc @@ -3595,7 +3595,7 @@ int BlueStore::_balance_bluefs_freespace(vector *extents) int count = 0; uint64_t alloc_len = 0; AllocExtentVector exts = AllocExtentVector(gift / min_alloc_size); - r = alloc->alloc_extents(gift, g_conf->bluefs_alloc_size, 0, 0, &exts, &count, &alloc_len); + r = alloc->allocate(gift, g_conf->bluefs_alloc_size, 0, 0, &exts, &count, &alloc_len); if (r < 0 || alloc_len < gift) { derr << __func__ << " allocate failed on 0x" << std::hex << gift @@ -7836,8 +7836,8 @@ int BlueStore::_do_alloc_write( uint64_t alloc_len = 0; AllocExtentVector extents = AllocExtentVector(final_length / min_alloc_size); - int r = alloc->alloc_extents(final_length, min_alloc_size, max_alloc_size, - hint, &extents, &count, &alloc_len); + int r = alloc->allocate(final_length, min_alloc_size, max_alloc_size, + hint, &extents, &count, &alloc_len); assert(r == 0 && alloc_len == final_length); need -= final_length; txc->statfs_delta.allocated() += final_length; diff --git a/src/os/bluestore/StupidAllocator.cc b/src/os/bluestore/StupidAllocator.cc index 33bcd062667..c84ae7bb726 100644 --- a/src/os/bluestore/StupidAllocator.cc +++ b/src/os/bluestore/StupidAllocator.cc @@ -82,7 +82,7 @@ static uint64_t aligned_len(btree_interval_set::iterator p, return p.get_len() - skew; } -int StupidAllocator::allocate( +int StupidAllocator::allocate_int( uint64_t want_size, uint64_t alloc_unit, int64_t hint, uint64_t *offset, uint32_t *length) { @@ -200,7 +200,7 @@ int StupidAllocator::allocate( return 0; } -int StupidAllocator::alloc_extents( +int StupidAllocator::allocate( uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, @@ -223,7 +223,7 @@ int StupidAllocator::alloc_extents( ExtentList block_list = ExtentList(extents, 1, max_alloc_size); while (allocated_size < want_size) { - res = allocate(MIN(max_alloc_size, (want_size - allocated_size)), + res = allocate_int(MIN(max_alloc_size, (want_size - allocated_size)), alloc_unit, hint, &offset, &length); if (res != 0) { /* diff --git a/src/os/bluestore/StupidAllocator.h b/src/os/bluestore/StupidAllocator.h index 9aeab7b5111..004b6d60457 100644 --- a/src/os/bluestore/StupidAllocator.h +++ b/src/os/bluestore/StupidAllocator.h @@ -30,11 +30,11 @@ public: int reserve(uint64_t need); void unreserve(uint64_t unused); - int alloc_extents( + int allocate( uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size, int64_t hint, mempool::bluestore_alloc::vector *extents, int *count, uint64_t *ret_len); - int allocate( + int allocate_int( uint64_t want_size, uint64_t alloc_unit, int64_t hint, uint64_t *offset, uint32_t *length); diff --git a/src/test/objectstore/Allocator_test.cc b/src/test/objectstore/Allocator_test.cc index 27638d4c47f..0b75f9c4f46 100644 --- a/src/test/objectstore/Allocator_test.cc +++ b/src/test/objectstore/Allocator_test.cc @@ -60,7 +60,7 @@ TEST_P(AllocTest, test_alloc_min_alloc) EXPECT_EQ(alloc->reserve(block_size), 0); AllocExtentVector extents = AllocExtentVector (1, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(block_size, block_size, + EXPECT_EQ(alloc->allocate(block_size, block_size, 0, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(alloc_len, (uint64_t) block_size); } @@ -74,7 +74,7 @@ TEST_P(AllocTest, test_alloc_min_alloc) AllocExtentVector extents = AllocExtentVector (4, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(4 * (uint64_t)block_size, (uint64_t) block_size, + EXPECT_EQ(alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size, 0, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(alloc_len, 4 * (uint64_t) block_size); EXPECT_EQ(extents[0].length, 4 * block_size); @@ -92,7 +92,7 @@ TEST_P(AllocTest, test_alloc_min_alloc) AllocExtentVector extents = AllocExtentVector (4, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(4 * (uint64_t)block_size, (uint64_t) block_size, + EXPECT_EQ(alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size, 0, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(alloc_len, 4 * (uint64_t) block_size); EXPECT_EQ(extents[0].length, 2 * block_size); @@ -122,7 +122,7 @@ TEST_P(AllocTest, test_alloc_min_max_alloc) AllocExtentVector extents = AllocExtentVector (4, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(4 * (uint64_t)block_size, (uint64_t) block_size, + EXPECT_EQ(alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size, block_size, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(alloc_len, 4 * (uint64_t) block_size); for (int i = 0; i < 4; i++) { @@ -142,7 +142,7 @@ TEST_P(AllocTest, test_alloc_min_max_alloc) AllocExtentVector extents = AllocExtentVector (2, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(4 * (uint64_t)block_size, (uint64_t) block_size, + EXPECT_EQ(alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size, 2 * block_size, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(alloc_len, 4 * (uint64_t) block_size); for (int i = 0; i < 2; i++) { @@ -160,7 +160,7 @@ TEST_P(AllocTest, test_alloc_min_max_alloc) AllocExtentVector extents = AllocExtentVector (1024, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(1024 * (uint64_t)block_size, (uint64_t) block_size * 4, + EXPECT_EQ(alloc->allocate(1024 * (uint64_t)block_size, (uint64_t) block_size * 4, block_size * 4, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(alloc_len, 1024 * (uint64_t) block_size); @@ -179,7 +179,7 @@ TEST_P(AllocTest, test_alloc_min_max_alloc) AllocExtentVector extents = AllocExtentVector (8, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(16 * (uint64_t)block_size, (uint64_t) block_size, + EXPECT_EQ(alloc->allocate(16 * (uint64_t)block_size, (uint64_t) block_size, 2 * block_size, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(count, 8); @@ -207,13 +207,13 @@ TEST_P(AllocTest, test_alloc_failure) AllocExtentVector extents = AllocExtentVector (4, AllocExtent(0, 0)); - EXPECT_EQ(alloc->alloc_extents(512 * (uint64_t)block_size, (uint64_t) block_size * 256, + EXPECT_EQ(alloc->allocate(512 * (uint64_t)block_size, (uint64_t) block_size * 256, block_size * 256, (int64_t) 0, &extents, &count, &alloc_len), 0); EXPECT_EQ(512 * (uint64_t)block_size, alloc_len); alloc->init_add_free(0, block_size * 256); alloc->init_add_free(block_size * 512, block_size * 256); EXPECT_EQ(alloc->reserve(block_size * 512), 0); - EXPECT_EQ(alloc->alloc_extents(512 * (uint64_t)block_size, (uint64_t) block_size * 512, + EXPECT_EQ(alloc->allocate(512 * (uint64_t)block_size, (uint64_t) block_size * 512, block_size * 512, (int64_t) 0, &extents, &count, &alloc_len), -ENOSPC); EXPECT_EQ(alloc_len, (uint64_t) 0); } @@ -238,13 +238,13 @@ TEST_P(AllocTest, test_alloc_hint_bmap) (zone_size * 4, AllocExtent(-1, -1)); alloc->reserve(blocks); - allocated = alloc->alloc_extents(1, 1, 1, zone_size, &extents, &count, &alloc_len); + allocated = alloc->allocate(1, 1, 1, zone_size, &extents, &count, &alloc_len); ASSERT_EQ(0, allocated); ASSERT_EQ((uint64_t) 1, alloc_len); ASSERT_EQ(1, count); ASSERT_EQ(extents[0].offset, (uint64_t) zone_size); - allocated = alloc->alloc_extents(1, 1, 1, zone_size * 2 - 1, &extents, &count, &alloc_len); + allocated = alloc->allocate(1, 1, 1, zone_size * 2 - 1, &extents, &count, &alloc_len); ASSERT_EQ((uint64_t) 1, alloc_len); EXPECT_EQ(0, allocated); ASSERT_EQ(1, count); @@ -253,13 +253,13 @@ TEST_P(AllocTest, test_alloc_hint_bmap) /* * Wrap around with hint */ - allocated = alloc->alloc_extents(zone_size * 2, 1, 1, blocks - zone_size * 2, &extents, &count, &alloc_len); + allocated = alloc->allocate(zone_size * 2, 1, 1, blocks - zone_size * 2, &extents, &count, &alloc_len); EXPECT_EQ(0, allocated); ASSERT_EQ((uint64_t) zone_size * 2, alloc_len); EXPECT_EQ(zone_size * 2, count); EXPECT_EQ((int64_t)extents[0].offset, blocks - zone_size * 2); - allocated = alloc->alloc_extents(zone_size, 1, 1, blocks - zone_size, &extents, &count, &alloc_len); + allocated = alloc->allocate(zone_size, 1, 1, blocks - zone_size, &extents, &count, &alloc_len); EXPECT_EQ(0, allocated); ASSERT_EQ((uint64_t) zone_size, alloc_len); EXPECT_EQ(zone_size, count); diff --git a/src/test/objectstore/BitAllocator_test.cc b/src/test/objectstore/BitAllocator_test.cc index 28ea36c7a7a..d5ca99d6e8d 100644 --- a/src/test/objectstore/BitAllocator_test.cc +++ b/src/test/objectstore/BitAllocator_test.cc @@ -367,7 +367,7 @@ TEST(BitAllocator, test_zone_alloc) TEST(BitAllocator, test_bmap_alloc) { -// const int max_iter = 2; + const int max_iter = 3; for (int round = 0; round < 3; round++) { // Test zone of different sizes: 512, 1024, 2048 @@ -385,9 +385,34 @@ TEST(BitAllocator, test_bmap_alloc) int64_t total_blocks = zone_size * 4; int64_t allocated = 0; - //int64_t start_block = 0; BitAllocator *alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT); + + int64_t alloc_size = 2; + for (int64_t iter = 0; iter < max_iter; iter++) { + for (int64_t j = 0; alloc_size <= total_blocks; j++) { + int64_t blk_size = 1024; + auto extents = AllocExtentVector + (alloc->size(), AllocExtent(-1, -1)); + ExtentList *block_list = new ExtentList(&extents, blk_size, alloc_size); + for (int64_t i = 0; i < total_blocks; i += alloc_size) { + bmap_test_assert(alloc->reserve_blocks(alloc_size) == true); + allocated = alloc->alloc_blocks_dis_res(alloc_size, MIN(alloc_size, zone_size), + 0, block_list); + bmap_test_assert(alloc_size == allocated); + bmap_test_assert(block_list->get_extent_count() == + (alloc_size > zone_size? alloc_size / zone_size: 1)); + bmap_test_assert(extents[0].offset == (uint64_t) i * blk_size); + bmap_test_assert((int64_t) extents[0].length == + ((alloc_size > zone_size? zone_size: alloc_size) * blk_size)); + block_list->reset(); + } + for (int64_t i = 0; i < total_blocks; i += alloc_size) { + alloc->free_blocks(i, alloc_size); + } + alloc_size = 2 << j; + } + } int64_t blk_size = 1024; auto extents = AllocExtentVector @@ -437,7 +462,7 @@ bool alloc_extents_max_block(BitAllocator *alloc, ExtentList *block_list = new ExtentList(&extents, blk_size, max_alloc); - EXPECT_EQ(alloc->reserve_blocks(total_alloc), true); + EXPECT_EQ(alloc->reserve_blocks(total_alloc), true); allocated = alloc->alloc_blocks_dis_res(total_alloc, blk_size, 0, block_list); EXPECT_EQ(allocated, total_alloc); @@ -468,19 +493,6 @@ TEST(BitAllocator, test_bmap_alloc2) alloc_extents_max_block(alloc, 32, 16); } -void -verify_blocks(int64_t num_blocks, int64_t *blocks) -{ - int64_t i = 0; - int wraps = 0; - for (i = 0; i < num_blocks - 1; i++) { - if (blocks[i] > blocks[i + 1]) { - wraps++; - bmap_test_assert(wraps <= 1); - } - } -} - __thread int my_tid; void @@ -514,7 +526,7 @@ worker(void *args) my_tid = __sync_fetch_and_add(&tid, 1); BitAllocator *alloc = (BitAllocator *) args; printf("Starting thread %d", my_tid); - do_work_dis(alloc); + do_work_dis(alloc); return NULL; } -- 2.39.5