1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* In memory space allocator test cases.
* Author: Ramesh Chander, Ramesh.Chander@sandisk.com
*/
#include <iostream>
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
#include "common/Mutex.h"
#include "common/Cond.h"
#include "common/errno.h"
#include "include/stringify.h"
#include "include/Context.h"
#include "os/bluestore/Allocator.h"
#include "os/bluestore/BitAllocator.h"
#if GTEST_HAS_PARAM_TEST
class AllocTest : public ::testing::TestWithParam<const char*> {
public:
boost::scoped_ptr<Allocator> alloc;
AllocTest(): alloc(0) { }
void init_alloc(int64_t size, uint64_t min_alloc_size) {
std::cout << "Creating alloc type " << string(GetParam()) << " \n";
alloc.reset(Allocator::create(g_ceph_context, string(GetParam()), size,
min_alloc_size));
}
void init_close() {
alloc.reset(0);
}
};
TEST_P(AllocTest, test_alloc_init)
{
int64_t blocks = BmapEntry::size();
init_alloc(blocks, 1);
ASSERT_EQ(0U, alloc->get_free());
alloc->shutdown();
blocks = BitMapZone::get_total_blocks() * 2 + 16;
init_alloc(blocks, 1);
ASSERT_EQ(0U, alloc->get_free());
alloc->shutdown();
blocks = BitMapZone::get_total_blocks() * 2;
init_alloc(blocks, 1);
ASSERT_EQ(alloc->get_free(), (uint64_t) 0);
}
TEST_P(AllocTest, test_alloc_min_alloc)
{
int64_t block_size = 1024;
int64_t blocks = BitMapZone::get_total_blocks() * 2 * block_size;
{
init_alloc(blocks, block_size);
alloc->init_add_free(block_size, block_size);
EXPECT_EQ(alloc->reserve(block_size), 0);
AllocExtentVector extents;
EXPECT_EQ(block_size, alloc->allocate(block_size, block_size,
0, (int64_t) 0, &extents));
}
/*
* Allocate extent and make sure all comes in single extent.
*/
{
alloc->init_add_free(0, block_size * 4);
EXPECT_EQ(alloc->reserve(block_size * 4), 0);
AllocExtentVector extents;
EXPECT_EQ(4*block_size,
alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size,
0, (int64_t) 0, &extents));
EXPECT_EQ(1u, extents.size());
EXPECT_EQ(extents[0].length, 4 * block_size);
}
/*
* Allocate extent and make sure we get two different extents.
*/
{
alloc->init_add_free(0, block_size * 2);
alloc->init_add_free(3 * block_size, block_size * 2);
EXPECT_EQ(alloc->reserve(block_size * 4), 0);
AllocExtentVector extents;
EXPECT_EQ(4*block_size,
alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size,
0, (int64_t) 0, &extents));
EXPECT_EQ(2u, extents.size());
EXPECT_EQ(extents[0].length, 2 * block_size);
EXPECT_EQ(extents[1].length, 2 * block_size);
}
alloc->shutdown();
}
TEST_P(AllocTest, test_alloc_min_max_alloc)
{
int64_t block_size = 1024;
int64_t blocks = BitMapZone::get_total_blocks() * 2 * block_size;
init_alloc(blocks, block_size);
/*
* Make sure we get all extents different when
* min_alloc_size == max_alloc_size
*/
{
alloc->init_add_free(0, block_size * 4);
EXPECT_EQ(alloc->reserve(block_size * 4), 0);
AllocExtentVector extents;
EXPECT_EQ(4*block_size,
alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size,
block_size, (int64_t) 0, &extents));
for (auto e : extents) {
EXPECT_EQ(e.length, block_size);
}
EXPECT_EQ(4u, extents.size());
}
/*
* Make sure we get extents of length max_alloc size
* when max alloc size > min_alloc size
*/
{
alloc->init_add_free(0, block_size * 4);
EXPECT_EQ(alloc->reserve(block_size * 4), 0);
AllocExtentVector extents;
EXPECT_EQ(4*block_size,
alloc->allocate(4 * (uint64_t)block_size, (uint64_t) block_size,
2 * block_size, (int64_t) 0, &extents));
EXPECT_EQ(2u, extents.size());
for (auto& e : extents) {
EXPECT_EQ(e.length, block_size * 2);
}
}
/*
* Make sure allocations are of min_alloc_size when min_alloc_size > block_size.
*/
{
alloc->init_add_free(0, block_size * 1024);
EXPECT_EQ(alloc->reserve(block_size * 1024), 0);
AllocExtentVector extents;
EXPECT_EQ(1024 * block_size,
alloc->allocate(1024 * (uint64_t)block_size,
(uint64_t) block_size * 4,
block_size * 4, (int64_t) 0, &extents));
for (auto& e : extents) {
EXPECT_EQ(e.length, block_size * 4);
}
EXPECT_EQ(1024u/4, extents.size());
}
/*
* Allocate and free.
*/
{
alloc->init_add_free(0, block_size * 16);
EXPECT_EQ(alloc->reserve(block_size * 16), 0);
AllocExtentVector extents;
EXPECT_EQ(16 * block_size,
alloc->allocate(16 * (uint64_t)block_size, (uint64_t) block_size,
2 * block_size, (int64_t) 0, &extents));
EXPECT_EQ(extents.size(), 8u);
for (auto& e : extents) {
EXPECT_EQ(e.length, 2 * block_size);
}
}
}
TEST_P(AllocTest, test_alloc_failure)
{
int64_t block_size = 1024;
int64_t blocks = BitMapZone::get_total_blocks() * block_size;
init_alloc(blocks, block_size);
{
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);
AllocExtentVector extents;
EXPECT_EQ(512 * block_size,
alloc->allocate(512 * (uint64_t)block_size,
(uint64_t) block_size * 256,
block_size * 256, (int64_t) 0, &extents));
alloc->init_add_free(0, block_size * 256);
alloc->init_add_free(block_size * 512, block_size * 256);
extents.clear();
EXPECT_EQ(alloc->reserve(block_size * 512), 0);
EXPECT_EQ(-ENOSPC,
alloc->allocate(512 * (uint64_t)block_size,
(uint64_t) block_size * 512,
block_size * 512, (int64_t) 0, &extents));
}
}
TEST_P(AllocTest, test_alloc_hint_bmap)
{
if (GetParam() == std::string("stupid")) {
return;
}
int64_t blocks = BitMapArea::get_level_factor(g_ceph_context, 2) * 4;
int64_t allocated = 0;
int64_t zone_size = 1024;
g_conf->set_val("bluestore_bitmapallocator_blocks_per_zone",
std::to_string(zone_size));
init_alloc(blocks, 1);
alloc->init_add_free(0, blocks);
AllocExtentVector extents;
alloc->reserve(blocks);
allocated = alloc->allocate(1, 1, 1, zone_size, &extents);
ASSERT_EQ(1, allocated);
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(extents[0].offset, (uint64_t) zone_size);
extents.clear();
allocated = alloc->allocate(1, 1, 1, zone_size * 2 - 1, &extents);
EXPECT_EQ(1, allocated);
ASSERT_EQ(1u, extents.size());
EXPECT_EQ((int64_t) extents[0].offset, zone_size * 2 - 1);
/*
* Wrap around with hint
*/
extents.clear();
allocated = alloc->allocate(zone_size * 2, 1, 1, blocks - zone_size * 2,
&extents);
ASSERT_EQ(zone_size * 2, allocated);
EXPECT_EQ(zone_size * 2, (int)extents.size());
EXPECT_EQ((int64_t)extents[0].offset, blocks - zone_size * 2);
extents.clear();
allocated = alloc->allocate(zone_size, 1, 1, blocks - zone_size, &extents);
ASSERT_EQ(zone_size, allocated);
EXPECT_EQ(zone_size, (int)extents.size());
EXPECT_EQ(extents[0].offset, (uint64_t) 0);
}
INSTANTIATE_TEST_CASE_P(
Allocator,
AllocTest,
::testing::Values("stupid", "bitmap"));
#else
TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
#endif
|