Skip to content

Commit fe3f158

Browse files
authored
Merge pull request #1236 from lplewa/falsesharing
fix false sharing issue in benchmark
2 parents 18fb231 + b481509 commit fe3f158

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

benchmark/benchmark.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
*
77
*/
88

9-
#include <benchmark/benchmark.h>
10-
119
#include "benchmark.hpp"
1210

11+
#include <iostream>
12+
13+
#include <benchmark/benchmark.h>
14+
1315
#define UMF_BENCHMARK_TEMPLATE_DEFINE(BaseClass, Method, ...) \
1416
BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, __VA_ARGS__) \
1517
(benchmark::State & state) { \
@@ -160,8 +162,10 @@ UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, fixed_provider)
160162
//BENCHMARK_MAIN();
161163
int main(int argc, char **argv) {
162164
if (initAffinityMask()) {
165+
std::cerr << "initAffinityMask failed" << std::endl;
163166
return -1;
164167
}
168+
165169
benchmark::Initialize(&argc, argv);
166170
benchmark::RunSpecifiedBenchmarks();
167171
benchmark::Shutdown();

benchmark/benchmark.hpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
297297
size_t max_allocs = 0;
298298

299299
vector2d<alloc_data> allocations;
300-
std::vector<unsigned> iters;
301-
std::vector<size_t> memused;
302300
vector2d<next_alloc_data> next;
303-
std::vector<std::vector<next_alloc_data>::const_iterator> next_iter;
301+
using next_alloc_data_iterator =
302+
std::vector<next_alloc_data>::const_iterator;
303+
std::vector<std::unique_ptr<next_alloc_data_iterator>> next_iter;
304304
int64_t iterations;
305305

306306
public:
@@ -318,7 +318,6 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
318318
allocations.resize(state.threads());
319319
next.resize(state.threads());
320320
next_iter.resize(state.threads());
321-
memused.assign(state.threads(), 0);
322321

323322
#ifndef WIN32
324323
// Ensure that system malloc does not have memory pooled on the heap
@@ -352,8 +351,10 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
352351
auto tid = state.thread_index();
353352
if (tid == 0) {
354353
size_t current_memory_allocated = 0;
355-
for (const auto &used : memused) {
356-
current_memory_allocated += used;
354+
for (const auto &allocationsPerThread : allocations) {
355+
for (const auto &allocation : allocationsPerThread) {
356+
current_memory_allocated += allocation.size;
357+
}
357358
}
358359

359360
auto memory_used = state.counters["provider_memory_allocated"];
@@ -377,27 +378,24 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
377378
next.clear();
378379
next_iter.clear();
379380
allocations.clear();
380-
iters.clear();
381381
}
382382
base::TearDown(state);
383383
}
384384

385385
void bench(benchmark::State &state) {
386386
auto tid = state.thread_index();
387387
auto &allocation = allocations[tid];
388-
auto &memuse = memused[tid];
388+
auto &iter = next_iter[tid];
389389
for (int i = 0; i < allocsPerIterations; i++) {
390-
auto &n = *next_iter[tid]++;
390+
auto &n = *(*iter)++;
391391
auto &alloc = allocation[n.offset];
392392
base::allocator.benchFree(alloc.ptr, alloc.size);
393-
memuse -= alloc.size;
394393
alloc.size = n.size;
395394
alloc.ptr = base::allocator.benchAlloc(alloc.size);
396395

397396
if (alloc.ptr == NULL) {
398397
state.SkipWithError("allocation failed");
399398
}
400-
memuse += alloc.size;
401399
}
402400
}
403401

@@ -418,7 +416,6 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
418416
auto tid = state.thread_index();
419417
auto &i = allocations[tid];
420418
i.resize(max_allocs);
421-
auto &memuse = memused[tid];
422419
auto sizeGenerator = base::alloc_sizes[tid];
423420

424421
for (size_t j = 0; j < max_allocs; j++) {
@@ -429,7 +426,6 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
429426
return;
430427
}
431428
i[j].size = size;
432-
memuse += size;
433429
}
434430
}
435431

@@ -439,7 +435,6 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
439435
for (auto &j : i) {
440436
if (j.ptr != NULL) {
441437
base::allocator.benchFree(j.ptr, j.size);
442-
memused[tid] -= j.size;
443438
j.ptr = NULL;
444439
j.size = 0;
445440
}
@@ -460,6 +455,6 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
460455
j++) {
461456
n.push_back({dist(generator), sizeGenerator.nextSize()});
462457
}
463-
next_iter[tid] = n.cbegin();
458+
next_iter[tid] = std::make_unique<next_alloc_data_iterator>(n.cbegin());
464459
}
465460
};

0 commit comments

Comments
 (0)