-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patharena_benchmark.cpp
More file actions
428 lines (356 loc) · 11.2 KB
/
arena_benchmark.cpp
File metadata and controls
428 lines (356 loc) · 11.2 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <cstdlib>
#include <iostream>
#include <vector>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include <boost/bind.hpp>
#include "obstack.hpp"
#include "max_alignment_type.hpp"
using namespace boost::posix_time;
typedef std::vector<size_t> alloc_order_vec;
static alloc_order_vec make_alloc_sequence(const size_t total_memory, const size_t min_alloc_size, const size_t max_alloc_size) {
boost::mt19937 gen;
gen.seed(42);
alloc_order_vec out;
boost::uniform_int<size_t> dist(min_alloc_size, max_alloc_size);
size_t mem_sum = 0;
while(mem_sum < total_memory) {
const size_t s = dist(gen);
out.push_back(s);
mem_sum += s;
}
//fix last chunk to exactly fill total_memory
if(mem_sum > total_memory) {
const size_t last_size = out[out.size()-1];
out[out.size()-1] = last_size - (mem_sum - total_memory);
const size_t new_last_size = out[out.size()-1];
if( new_last_size == 0) {
out[out.size()-1] = 1;
}
}
return out;
}
static alloc_order_vec make_free_sequence(const alloc_order_vec &alloc_seq) {
alloc_order_vec out;
out.resize(alloc_seq.size());
boost::mt19937 gen;
gen.seed(42);
alloc_order_vec positions;
positions.resize(alloc_seq.size());
for(size_t i=0; i<positions.size(); i++) {
positions[i] = i;
}
for(size_t i=0; i<out.size(); i++) {
boost::uniform_int<size_t> dist(0, positions.size()-1);
const size_t k = dist(gen);
out[i] = positions[k];
positions.erase(positions.begin()+k);
}
return out;
}
size_t sum_vec(const alloc_order_vec &seq) {
size_t sum=0;
for(size_t i=0; i<seq.size(); i++) {
sum += seq[i];
}
return sum;
}
class timing_registry {
private:
typedef boost::mutex mutex_type;
typedef boost::lock_guard<mutex_type> lock_type;
public:
enum benchmark {
BENCHMARK_OBSTACK = 0,
BENCHMARK_MALLOC_FREE = 1,
BENCHMARK_NEW_DELETE = 2
};
void account(benchmark which, const time_duration &how_long) {
lock_type lock(mutexes[to_index(which)]);
durations[to_index(which)] += how_long;
}
time_duration get(benchmark which) {
lock_type lock(mutexes[to_index(which)]);
return durations[to_index(which)];
}
private:
size_t to_index(benchmark e) { return static_cast<size_t>(e); }
time_duration durations[3];
mutex_type mutexes[3];
};
//this function keeps the optimizer from optimizing away the allocation benchmarking code
//and does error checking for failed allocations
static inline void check_alloc(volatile char *p, const char *file, int line, const char *func) {
if(p==NULL) {
std::cerr << "out of memory in: " << file << ":" << line << " " << func << std::endl;
exit(1);
}
}
#define CHECK_ALLOC(p) check_alloc(p, __FILE__, __LINE__, __FUNCTION__);
static void benchmark_malloc(
boost::barrier &start_allocs,
const alloc_order_vec &alloc_seq,
const alloc_order_vec &free_seq,
const size_t iterations,
timing_registry &timings
) {
std::vector<char*> chunks;
chunks.resize(alloc_seq.size());
start_allocs.wait();
ptime start(microsec_clock::universal_time());
for(size_t i=0; i<iterations; i++) {
//1. in order alloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = (char*)malloc(s);
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<chunks.size(); i++) {
char* const p = chunks[i];
free(p);
}
//2. reverse order alloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = (char*)malloc(s);
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<chunks.size(); i++) {
char* const p = chunks[chunks.size()-1-i];
free(p);
}
//3. random order alloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = (char*)malloc(s);
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<free_seq.size(); i++) {
char* const p = chunks[free_seq[i]];
free(p);
}
}
ptime end(microsec_clock::universal_time());
timings.account(timing_registry::BENCHMARK_MALLOC_FREE, end-start);
}
static void benchmark_obstack(
boost::barrier &start_allocs,
const alloc_order_vec &alloc_seq,
const alloc_order_vec &free_seq,
const size_t iterations,
timing_registry &timings
) {
std::vector<char*> chunks;
chunks.resize(alloc_seq.size());
const size_t alloc_sum = sum_vec(alloc_seq);
const size_t required_size = alloc_sum + boost::arena::obstack::max_overhead(alloc_seq.size());
boost::arena::obstack obs(required_size);
start_allocs.wait();
ptime start(microsec_clock::universal_time());
for(size_t i=0; i<iterations; i++) {
//1. in order alloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = obs.alloc_array<char>(s);
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<chunks.size(); i++) {
char* const p = chunks[i];
obs.dealloc(p);
}
//2. reverse order malloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = obs.alloc_array<char>(s);
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<chunks.size(); i++) {
char* const p = chunks[chunks.size()-1-i];
obs.dealloc(p);
}
//3. random order malloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = obs.alloc_array<char>(s);
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<free_seq.size(); i++) {
char* const p = chunks[free_seq[i]];
obs.dealloc(p);
}
}
ptime end(microsec_clock::universal_time());
timings.account(timing_registry::BENCHMARK_OBSTACK, end-start);
}
static void benchmark_new_delete(
boost::barrier &start_allocs,
const alloc_order_vec &alloc_seq,
const alloc_order_vec &free_seq,
const size_t iterations,
timing_registry &timings
) {
std::vector<char*> chunks;
chunks.resize(alloc_seq.size());
try {
start_allocs.wait();
ptime start(microsec_clock::universal_time());
for(size_t i=0; i<iterations; i++) {
//1. in order alloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = new char[s];
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<chunks.size(); i++) {
char* const p = chunks[i];
delete[] p;
}
//2. reverse order malloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = new char[s];
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<chunks.size(); i++) {
char* const p = chunks[chunks.size()-1-i];
delete[] p;
}
//3. random order malloc/free
for(size_t i=0; i<alloc_seq.size(); i++) {
const size_t s = alloc_seq[i];
chunks[i] = new char[s];
CHECK_ALLOC(chunks[i]);
}
for(size_t i=0; i<free_seq.size(); i++) {
char* const p = chunks[free_seq[i]];
delete[] p;
}
}
ptime end(microsec_clock::universal_time());
timings.account(timing_registry::BENCHMARK_NEW_DELETE, end-start);
} catch(std::bad_alloc&) {
std::cerr << "bad_alloc in new/delete benchmark" << std::endl;
exit(1);
}
}
static void benchmark_threaded(
const size_t num_threads,
const size_t total_memory,
const size_t min_alloc_size,
const size_t max_alloc_size,
const size_t iterations
) {
timing_registry timings;
//create splitted alloc orders
std::vector<alloc_order_vec> alloc_orders;
std::vector<alloc_order_vec> free_orders;
const size_t per_thread_memory = total_memory / num_threads;
for(size_t i=0; i<num_threads; i++) {
const alloc_order_vec &alloc_seq = make_alloc_sequence(per_thread_memory, min_alloc_size, max_alloc_size);
const alloc_order_vec &free_seq = make_free_sequence(alloc_seq);
alloc_orders.push_back(alloc_seq);
free_orders.push_back(free_seq);
}
const size_t per_thread_iterations = iterations / num_threads;
size_t total_allocs=0;
for(size_t i=0; i<num_threads; i++) {
total_allocs += alloc_orders[i].size();
}
std::cout << "running memory management benchmarks with " << num_threads << " threads" << std::endl;
std::cout << " memory per thread: " << per_thread_memory / 1024 << "kB" << std::endl;
std::cout << " alloc/dealloc ops per thread: " << (total_allocs * iterations) / num_threads << std::endl;
std::cout << " total alloc/dealloc ops: " << total_allocs * iterations << std::endl;
//start benchmarking
//malloc
{
boost::thread_group threads;
boost::barrier start_allocs(num_threads);
for(size_t i=0; i<num_threads; i++) {
threads.create_thread(
boost::bind(
benchmark_malloc,
boost::ref(start_allocs),
boost::ref(alloc_orders[i]),
boost::ref(free_orders[i]),
per_thread_iterations,
boost::ref(timings)
)
);
}
threads.join_all();
}
//new_delete
{
boost::thread_group threads;
boost::barrier start_allocs(num_threads);
for(size_t i=0; i<num_threads; i++) {
threads.create_thread(
boost::bind(
benchmark_new_delete,
boost::ref(start_allocs),
boost::ref(alloc_orders[i]),
boost::ref(free_orders[i]),
per_thread_iterations,
boost::ref(timings)
)
);
}
threads.join_all();
}
//obstack
{
boost::thread_group threads;
boost::barrier start_allocs(num_threads);
for(size_t i=0; i<num_threads; i++) {
threads.create_thread(
boost::bind(
benchmark_obstack,
boost::ref(start_allocs),
boost::ref(alloc_orders[i]),
boost::ref(free_orders[i]),
per_thread_iterations,
boost::ref(timings)
)
);
}
threads.join_all();
}
//print statistics
std::cout << " done!" << std::endl;
std::cout << " timings:" << std::endl;
std::cout << " malloc/free heap: " <<
timings.get(timing_registry::BENCHMARK_MALLOC_FREE).total_milliseconds() << "ms" << std::endl;
std::cout << " new/delete heap: " <<
timings.get(timing_registry::BENCHMARK_NEW_DELETE).total_milliseconds() << "ms" << std::endl;
std::cout << " obstack arena: " <<
timings.get(timing_registry::BENCHMARK_OBSTACK).total_milliseconds() << "ms" << std::endl;
std::cout << std::endl;
}
int main(int argc, char **argv) {
const size_t total_memory = 1024*1024* 512;
const size_t min_alloc_size = 1;
const size_t max_alloc_size = 1024*1024* 4;
const size_t iterations = 1000;
const size_t num_cores = boost::thread::hardware_concurrency();
std::cout << "global parameters:" << std::endl;
std::cout << " cpu cores: " << num_cores << std::endl;
std::cout << " total memory: " << total_memory / 1024 << "kB" << std::endl;
std::cout << " min/max block size: " << min_alloc_size << "B/" << max_alloc_size/1024 << "kB" << std::endl;
std::cout << std::endl;
//allways benchmark with 1 and 2 threads
benchmark_threaded(1, total_memory, min_alloc_size, max_alloc_size, iterations);
benchmark_threaded(2, total_memory, min_alloc_size, max_alloc_size, iterations);
//if the machine has more than 1 core, some more runs may be interesting
const bool has_equal_num_cores_run = (num_cores == 1 || num_cores == 2);
const bool has_double_num_cores_run = (num_cores*2 == 2);
if(!has_equal_num_cores_run) {
benchmark_threaded(num_cores, total_memory, min_alloc_size, max_alloc_size, iterations);
}
if(!has_double_num_cores_run) {
benchmark_threaded(num_cores*2, total_memory, min_alloc_size, max_alloc_size, iterations);
}
return 0;
}