-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsmmalloc_test01.cpp
349 lines (294 loc) · 10.6 KB
/
smmalloc_test01.cpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <array>
#include <gtest/gtest.h>
#include <inttypes.h>
#include <smmalloc.h>
#include <vector>
bool IsAligned(void* p, size_t alignment)
{
uintptr_t v = uintptr_t(p);
size_t lowBits = v & (alignment - 1);
return (lowBits == 0);
}
bool _IsAligned(size_t v, size_t alignment)
{
size_t lowBits = v & (alignment - 1);
return (lowBits == 0);
}
TEST(SimpleTests, AlignmentTest01)
{
sm_allocator heap = _sm_allocator_create(5, (48 * 1024 * 1024));
for (size_t iter = 0; iter < 10000; iter++)
{
void* p1 = _sm_malloc(heap, 20, 1);
ASSERT_TRUE(IsAligned(p1, 1));
void* p2 = _sm_malloc(heap, 20, 2);
ASSERT_TRUE(IsAligned(p2, 2));
void* p4 = _sm_malloc(heap, 20, 4);
ASSERT_TRUE(IsAligned(p4, 4));
void* p8 = _sm_malloc(heap, 20, 8);
ASSERT_TRUE(IsAligned(p8, 8));
void* p16 = _sm_malloc(heap, 20, 16);
ASSERT_TRUE(IsAligned(p16, 16));
void* p32 = _sm_malloc(heap, 20, 32);
ASSERT_TRUE(IsAligned(p32, 32));
void* p64 = _sm_malloc(heap, 20, 64);
ASSERT_TRUE(IsAligned(p64, 64));
void* p128 = _sm_malloc(heap, 20, 128);
ASSERT_TRUE(IsAligned(p128, 128));
}
_sm_allocator_destroy(heap);
}
TEST(SimpleTests, AlignmentTest02)
{
sm_allocator heap = _sm_allocator_create(32, (2 * 1024 * 1024));
void* p128 = _sm_malloc(heap, 20, 128);
ASSERT_TRUE(IsAligned(p128, 128));
int32_t bucketIndex = _sm_mbucket(heap, p128);
ASSERT_TRUE(bucketIndex >= 0);
uint32_t elementsCount = heap->GetBucketElementsCount(bucketIndex);
for (uint32_t iter = 0; iter < (elementsCount * 4); iter++)
{
void* _p128 = _sm_malloc(heap, 20, 128);
ASSERT_TRUE(IsAligned(_p128, 128));
}
_sm_allocator_destroy(heap);
}
TEST(SimpleTests, AlignmentTest03)
{
std::array<size_t, 8> alignments = {1, 2, 4, 8, 16, 32, 64, 128};
const size_t kNumBuckets = 10;
size_t maxBlockSize = sm::GetBucketSizeInBytesByIndex(kNumBuckets);
for (const size_t alignment : alignments)
{
printf("Alignment: %zu\n", alignment);
for (size_t blockSize = 1; blockSize < maxBlockSize; blockSize++)
{
// 128kb per bucket
sm_allocator heap = _sm_allocator_create(kNumBuckets, (128 * 1024));
_sm_allocator_thread_cache_create(heap, sm::CACHE_HOT, {32, 32, 32, 32, 32, 32, 32, 32, 32, 32});
size_t numAllocations = 0;
for (size_t bucketIndex = 0; bucketIndex < kNumBuckets; bucketIndex++)
{
uint32_t elementsCountInBucket = heap->GetBucketElementsCount(bucketIndex);
numAllocations += elementsCountInBucket;
}
for (size_t i = 0; i < numAllocations; i++)
{
void* p = _sm_malloc(heap, blockSize, alignment);
ASSERT_TRUE(IsAligned(p, alignment));
if (_sm_mbucket(heap, p) == -1)
{
// we've saturated smmalloc all other allocations will be served with default alloc
break;
}
}
_sm_allocator_thread_cache_destroy(heap);
_sm_allocator_destroy(heap);
}
}
}
TEST(SimpleTests, BucketSizeTest)
{
for (size_t elemSize = 1; elemSize < (1024 * 1024); elemSize++)
{
size_t bucketIndex = sm::GetBucketIndexBySize(elemSize);
size_t bucketSizeInBytes = sm::GetBucketSizeInBytesByIndex(bucketIndex);
ASSERT_TRUE(elemSize <= bucketSizeInBytes);
}
}
TEST(SimpleTests, BucketSizeAlignment)
{
// make sure bucket sizes are at least aligned to kMinValidAlignment
for (size_t bucketIndex = 0; bucketIndex < SMM_MAX_BUCKET_COUNT; bucketIndex++)
{
size_t bucketSizeInBytes = sm::GetBucketSizeInBytesByIndex(bucketIndex);
ASSERT_TRUE(_IsAligned(bucketSizeInBytes, sm::Allocator::kMinValidAlignment));
}
}
TEST(SimpleTests, Basic)
{
sm_allocator heap = _sm_allocator_create(5, (48 * 1024 * 1024));
void* a = _sm_malloc(heap, 34, 16);
void* b = _sm_malloc(heap, 34, 16);
void* c = _sm_malloc(heap, 34, 16);
void* d = _sm_malloc(heap, 34, 16);
EXPECT_NE(a, b);
EXPECT_NE(b, c);
EXPECT_NE(c, d);
// ptrdiff_t d1 = (uintptr_t)b - (uintptr_t)a;
// ptrdiff_t d2 = (uintptr_t)c - (uintptr_t)b;
// ptrdiff_t d3 = (uintptr_t)d - (uintptr_t)c;
_sm_free(heap, d);
_sm_free(heap, c);
_sm_free(heap, b);
_sm_free(heap, a);
a = _sm_malloc(heap, 34, 16);
b = _sm_malloc(heap, 34, 16);
c = _sm_malloc(heap, 34, 16);
d = _sm_malloc(heap, 34, 16);
EXPECT_NE(a, b);
EXPECT_NE(b, c);
EXPECT_NE(c, d);
// d1 = (uintptr_t)b - (uintptr_t)a;
// d2 = (uintptr_t)c - (uintptr_t)b;
// d3 = (uintptr_t)d - (uintptr_t)c;
_sm_free(heap, d);
_sm_free(heap, c);
_sm_free(heap, b);
_sm_free(heap, a);
_sm_allocator_destroy(heap);
}
TEST(SimpleTests, MegaAlloc)
{
uint32_t poolSizeInBytes = 16777216;
uint32_t poolSize = poolSizeInBytes / 16;
sm_allocator heap = _sm_allocator_create(2, poolSizeInBytes);
uint32_t cacheSize = 256 * 1024;
_sm_allocator_thread_cache_create(heap, sm::CACHE_HOT, {cacheSize});
std::vector<void*> ptrs;
ptrs.reserve(poolSize + 2);
int64_t opsCount = 0;
clock_t start = clock();
#ifdef _DEBUG
const int iterationsCount = 4;
#else
const int iterationsCount = 512;
#endif
for (int pass = 0; pass < iterationsCount; pass++)
{
size_t count = 0;
for (;; count++)
{
opsCount++;
void* p = _sm_malloc(heap, 16, 16);
ptrs.push_back(p);
if (_sm_mbucket(heap, p) != 0)
{
break;
}
}
int64_t diff = poolSize - count;
EXPECT_EQ(diff, 0);
for (size_t i = 0; i < ptrs.size(); i++)
{
_sm_free(heap, ptrs[i]);
opsCount++;
}
ptrs.clear();
}
clock_t end = clock();
float sec = (end - start) / (float)CLOCKS_PER_SEC;
printf("%" PRId64 " allocs took %3.2f sec, %3.2f operations per second\n", opsCount, sec, opsCount / sec);
#ifdef SMMALLOC_STATS_SUPPORT
const sm::GlobalStats& gstats = heap->GetGlobalStats();
size_t numAllocationAttempts = gstats.totalNumAllocationAttempts.load();
size_t numAllocationsServed = gstats.totalAllocationsServed.load();
size_t numAllocationsRouted = gstats.totalAllocationsRoutedToDefaultAllocator.load();
double servedPercentage = (numAllocationAttempts == 0) ? 0.0 : (double(numAllocationsServed) / double(numAllocationAttempts) * 100.0);
double routedPercentage = (numAllocationAttempts == 0) ? 0.0 : (double(numAllocationsRouted) / double(numAllocationAttempts) * 100.0);
printf("Allocation attempts: %zu\n", numAllocationAttempts);
printf("Allocations served: %zu (%3.2f%%)\n", numAllocationsServed, servedPercentage);
printf("Allocated using default malloc: %zu (%3.2f%%)\n", numAllocationsRouted, routedPercentage);
printf(" - Because of size: %zu\n", gstats.routingReasonBySize.load());
printf(" - Because of saturation: %zu\n", gstats.routingReasonSaturation.load());
size_t bucketsCount = heap->GetBucketsCount();
for (size_t bucketIndex = 0; bucketIndex < bucketsCount; bucketIndex++)
{
uint32_t elementsCount = heap->GetBucketElementsCount(bucketIndex);
size_t elementsSize = sm::GetBucketSizeInBytesByIndex(bucketIndex);
printf("Bucket[%zu], Elements[%d], SizeOf[%zu] -----\n", bucketIndex, elementsCount, elementsSize);
const sm::BucketStats* stats = heap->GetBucketStats(bucketIndex);
if (!stats)
{
continue;
}
printf(" Cache Hit : %zu\n", stats->cacheHitCount.load());
printf(" Hits : %zu\n", stats->hitCount.load());
printf(" Misses : %zu\n", stats->missCount.load());
printf(" Operations : %zu\n", stats->cacheHitCount.load() + stats->hitCount.load() + stats->missCount.load());
}
#endif
_sm_allocator_thread_cache_destroy(heap);
_sm_allocator_destroy(heap);
}
TEST(SimpleTests, ReAlloc)
{
sm_allocator heap = _sm_allocator_create(5, (48 * 1024 * 1024));
void* p = _sm_malloc(heap, 17, 16);
size_t s = _sm_msize(heap, p);
EXPECT_NE(p, nullptr);
EXPECT_TRUE(IsAligned(p, 16));
EXPECT_GE(s, 17);
p = _sm_realloc(heap, p, 20, 16);
s = _sm_msize(heap, p);
EXPECT_NE(p, nullptr);
EXPECT_TRUE(IsAligned(p, 16));
EXPECT_GE(s, 20);
p = _sm_realloc(heap, p, 50, 16);
s = _sm_msize(heap, p);
EXPECT_NE(p, nullptr);
EXPECT_TRUE(IsAligned(p, 16));
EXPECT_GE(s, 50);
p = _sm_realloc(heap, p, 900, 16);
s = _sm_msize(heap, p);
EXPECT_NE(p, nullptr);
EXPECT_TRUE(IsAligned(p, 16));
EXPECT_GE(s, 900);
_sm_free(heap, p);
p = nullptr;
s = _sm_msize(heap, p);
EXPECT_EQ(p, nullptr);
EXPECT_EQ(s, 0);
void* p2 = _sm_realloc(heap, nullptr, 15, 16);
size_t s2 = _sm_msize(heap, p2);
EXPECT_NE(p2, nullptr);
EXPECT_TRUE(IsAligned(p2, 16));
EXPECT_GE(s2, 15);
p2 = _sm_realloc(heap, p2, 0, 16);
s2 = _sm_msize(heap, p2);
EXPECT_EQ(p2, nullptr);
EXPECT_TRUE(IsAligned(p2, 16));
EXPECT_EQ(s2, 0);
_sm_free(heap, p2);
s2 = _sm_msize(heap, p2);
EXPECT_EQ(s2, 0);
void* p3 = _sm_realloc(heap, nullptr, 1024 * 1024, 1);
size_t s3 = _sm_msize(heap, p3);
EXPECT_NE(p3, nullptr);
EXPECT_TRUE(IsAligned(p3, sm::Allocator::kMinValidAlignment));
EXPECT_GE(s3, 1024 * 1024);
p3 = _sm_realloc(heap, p3, 4, 8);
s3 = _sm_msize(heap, p3);
EXPECT_NE(p3, nullptr);
EXPECT_TRUE(IsAligned(p3, 8));
EXPECT_GE(s3, 4);
p3 = _sm_realloc(heap, p3, 0, 8);
s3 = _sm_msize(heap, p3);
EXPECT_EQ(p3, nullptr);
EXPECT_EQ(s3, 0);
_sm_free(heap, p3);
s3 = _sm_msize(heap, p3);
EXPECT_EQ(s3, 0);
void* p4 = _sm_malloc(heap, 2 * 1024 * 1024, 32);
size_t s4 = _sm_msize(heap, p4);
EXPECT_NE(p4, nullptr);
EXPECT_TRUE(IsAligned(p4, 32));
EXPECT_GE(s4, 2 * 1024 * 1024);
p4 = _sm_realloc(heap, p4, 1024 * 1024, 16);
s4 = _sm_msize(heap, p4);
EXPECT_NE(p4, nullptr);
EXPECT_TRUE(IsAligned(p4, 16));
EXPECT_GE(s4, 1024 * 1024);
p4 = _sm_realloc(heap, p4, 4 * 1024 * 1024, 64);
s4 = _sm_msize(heap, p4);
EXPECT_NE(p4, nullptr);
EXPECT_TRUE(IsAligned(p4, 64));
EXPECT_GE(s4, 4 * 1024 * 1024);
p4 = _sm_realloc(heap, p4, 0, 32);
p4 = nullptr;
s4 = _sm_msize(heap, p4);
EXPECT_EQ(s4, 0);
size_t s5 = sm::GenericAllocator::GetUsableSpace(sm::GenericAllocator::Invalid(), nullptr);
EXPECT_EQ(s5, 0);
_sm_allocator_destroy(heap);
}