-
Notifications
You must be signed in to change notification settings - Fork 26
/
smmalloc_perf02.cpp
260 lines (216 loc) · 7.62 KB
/
smmalloc_perf02.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
#include <cstddef>
#include <dlmalloc.h>
#include <rpmalloc.h>
#include <smmalloc.h>
#include <ubench.h>
#include <vector>
#if defined(_WIN32)
#include <hoard.h>
#include <mimalloc.h>
#endif
struct UBenchGlobals
{
static const int kNumAllocations = 10000000;
static const int kWorkingsetSize = 10000;
std::vector<size_t> randomSequence;
std::vector<void*> workingSet;
UBenchGlobals()
{
srand(1306);
// num allocations
randomSequence.resize(kNumAllocations);
for (size_t i = 0; i < randomSequence.size(); i++)
{
// 16 - 256 bytes
size_t sz = 16 + (rand() % 240);
randomSequence[i] = sz;
}
// working set
workingSet.resize(kWorkingsetSize);
memset(workingSet.data(), 0, sizeof(void*) * workingSet.size());
}
static UBenchGlobals& get()
{
static UBenchGlobals g;
return g;
}
};
// smmalloc ubench test
UBENCH_EX(PerfTest, smmalloc_10m)
{
UBenchGlobals& g = UBenchGlobals::get();
size_t wsSize = g.workingSet.size();
//
sm_allocator space = _sm_allocator_create(18, (48 * 1024 * 1024));
_sm_allocator_thread_cache_create(space, sm::CACHE_COLD,
{512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512});
UBENCH_DO_BENCHMARK()
{
size_t freeIndex = 0;
size_t allocIndex = wsSize - 1;
for (size_t i = 0; i < g.randomSequence.size(); i++)
{
size_t numBytesToAllocate = g.randomSequence[i];
void* ptr = _sm_malloc(space, numBytesToAllocate, 1);
memset(ptr, 33, numBytesToAllocate);
g.workingSet[allocIndex % wsSize] = ptr;
void* ptrToFree = g.workingSet[freeIndex % wsSize];
_sm_free(space, ptrToFree);
g.workingSet[freeIndex % wsSize] = nullptr;
allocIndex++;
freeIndex++;
}
for (size_t i = 0; i < wsSize; i++)
{
_sm_free(space, g.workingSet[i]);
g.workingSet[i] = nullptr;
}
}
#ifdef SMMALLOC_STATS_SUPPORT
const sm::GlobalStats& gstats = space->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 = space->GetBucketsCount();
for (size_t bucketIndex = 0; bucketIndex < bucketsCount; bucketIndex++)
{
uint32_t elementsCount = space->GetBucketElementsCount(bucketIndex);
size_t elementsSize = sm::GetBucketSizeInBytesByIndex(bucketIndex);
printf("Bucket[%zu], Elements[%d], SizeOf[%zu] -----\n", bucketIndex, elementsCount, elementsSize);
const sm::BucketStats* stats = space->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(space);
_sm_allocator_destroy(space);
}
// crt ubench test
UBENCH_EX(PerfTest, crt_10m)
{
UBenchGlobals& g = UBenchGlobals::get();
size_t wsSize = g.workingSet.size();
UBENCH_DO_BENCHMARK()
{
size_t freeIndex = 0;
size_t allocIndex = wsSize - 1;
for (size_t i = 0; i < g.randomSequence.size(); i++)
{
size_t numBytesToAllocate = g.randomSequence[i];
void* ptr = malloc(numBytesToAllocate);
memset(ptr, 33, numBytesToAllocate);
g.workingSet[allocIndex % wsSize] = ptr;
void* ptrToFree = g.workingSet[freeIndex % wsSize];
free(ptrToFree);
g.workingSet[freeIndex % wsSize] = nullptr;
allocIndex++;
freeIndex++;
}
for (size_t i = 0; i < wsSize; i++)
{
free(g.workingSet[i]);
g.workingSet[i] = nullptr;
}
}
}
// dlmalloc ubench test
UBENCH_EX(PerfTest, dlmalloc_10m)
{
UBenchGlobals& g = UBenchGlobals::get();
size_t wsSize = g.workingSet.size();
UBENCH_DO_BENCHMARK()
{
size_t freeIndex = 0;
size_t allocIndex = wsSize - 1;
for (size_t i = 0; i < g.randomSequence.size(); i++)
{
size_t numBytesToAllocate = g.randomSequence[i];
void* ptr = dlmemalign(16, numBytesToAllocate);
memset(ptr, 33, numBytesToAllocate);
g.workingSet[allocIndex % wsSize] = ptr;
void* ptrToFree = g.workingSet[freeIndex % wsSize];
dlfree(ptrToFree);
g.workingSet[freeIndex % wsSize] = nullptr;
allocIndex++;
freeIndex++;
}
for (size_t i = 0; i < wsSize; i++)
{
dlfree(g.workingSet[i]);
g.workingSet[i] = nullptr;
}
}
}
#if defined(_WIN32)
// hoard ubench test
UBENCH_EX(PerfTest, hoard_malloc_10m)
{
UBenchGlobals& g = UBenchGlobals::get();
size_t wsSize = g.workingSet.size();
hoardInitialize();
UBENCH_DO_BENCHMARK()
{
size_t freeIndex = 0;
size_t allocIndex = wsSize - 1;
for (size_t i = 0; i < g.randomSequence.size(); i++)
{
size_t numBytesToAllocate = g.randomSequence[i];
void* ptr = xxmalloc(numBytesToAllocate);
memset(ptr, 33, numBytesToAllocate);
g.workingSet[allocIndex % wsSize] = ptr;
void* ptrToFree = g.workingSet[freeIndex % wsSize];
xxfree(ptrToFree);
g.workingSet[freeIndex % wsSize] = nullptr;
allocIndex++;
freeIndex++;
}
for (size_t i = 0; i < wsSize; i++)
{
xxfree(g.workingSet[i]);
g.workingSet[i] = nullptr;
}
}
hoardFinalize();
}
// mamalloc ubench test
UBENCH_EX(PerfTest, mi_malloc_10m)
{
UBenchGlobals& g = UBenchGlobals::get();
size_t wsSize = g.workingSet.size();
UBENCH_DO_BENCHMARK()
{
size_t freeIndex = 0;
size_t allocIndex = wsSize - 1;
for (size_t i = 0; i < g.randomSequence.size(); i++)
{
size_t numBytesToAllocate = g.randomSequence[i];
void* ptr = mi_malloc(numBytesToAllocate);
memset(ptr, 33, numBytesToAllocate);
g.workingSet[allocIndex % wsSize] = ptr;
void* ptrToFree = g.workingSet[freeIndex % wsSize];
mi_free(ptrToFree);
g.workingSet[freeIndex % wsSize] = nullptr;
allocIndex++;
freeIndex++;
}
for (size_t i = 0; i < wsSize; i++)
{
mi_free(g.workingSet[i]);
g.workingSet[i] = nullptr;
}
}
}
#endif