Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions benchmark/benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ class provider_allocator : public allocator_interface {
return argPos;
}

void preBench(::benchmark::State &state) override {
provider.preBench(state);
}

void postBench(::benchmark::State &state) override {
provider.postBench(state);
}

void TearDown(::benchmark::State &state) override {
provider.TearDown(state);
}
Expand Down Expand Up @@ -204,13 +212,18 @@ template <typename Pool> class pool_allocator : public allocator_interface {
return argPos;
}

void preBench(::benchmark::State &state) override { pool.preBench(state); }
void postBench(::benchmark::State &state) override {
pool.postBench(state);
}

void TearDown(::benchmark::State &state) override { pool.TearDown(state); }

virtual void *benchAlloc(size_t size) override {
void *benchAlloc(size_t size) override {
return umfPoolMalloc(pool.pool, size);
}

virtual void benchFree(void *ptr, [[maybe_unused]] size_t size) override {
void benchFree(void *ptr, [[maybe_unused]] size_t size) override {
umfPoolFree(pool.pool, ptr);
}

Expand Down Expand Up @@ -241,7 +254,7 @@ struct benchmark_interface : public benchmark::Fixture {
allocator.TearDown(state);
}

virtual void bench(::benchmark::State &state) = 0;
void bench([[maybe_unused]] ::benchmark::State &state){};

virtual std::vector<std::string> argsName() {
auto s = Size::argsName();
Expand All @@ -260,6 +273,9 @@ struct benchmark_interface : public benchmark::Fixture {
benchmark->ArgNames(bench->argsName())->Name(bench->name());
}

void custom_counters(::benchmark::State &state) {
allocator.custom_counters(state);
}
std::vector<Size> alloc_sizes;
Allocator allocator;
};
Expand All @@ -282,7 +298,7 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {

vector2d<alloc_data> allocations;
std::vector<unsigned> iters;

std::vector<size_t> memused;
vector2d<next_alloc_data> next;
std::vector<std::vector<next_alloc_data>::const_iterator> next_iter;
int64_t iterations;
Expand All @@ -302,6 +318,7 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
allocations.resize(state.threads());
next.resize(state.threads());
next_iter.resize(state.threads());
memused.assign(state.threads(), 0);

#ifndef WIN32
// Ensure that system malloc does not have memory pooled on the heap
Expand All @@ -323,13 +340,36 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
waitForAllThreads(state);
// prepare workload for actual benchmark.
freeAllocs(state);

prealloc(state);
prepareWorkload(state);
waitForAllThreads(state);
base::allocator.preBench(state);
}

void TearDown(::benchmark::State &state) override {
base::allocator.postBench(state);
auto tid = state.thread_index();
if (tid == 0) {
size_t current_memory_allocated = 0;
for (const auto &used : memused) {
current_memory_allocated += used;
}

auto memory_used = state.counters["provider_memory_allocated"];

if (memory_used != 0) {
state.counters["benchmark_memory_allocated"] =
static_cast<double>(current_memory_allocated);
state.counters["memory_overhead"] =
100.0 * (memory_used - current_memory_allocated) /
memory_used;
} else {
state.counters.erase("provider_memory_allocated");
}
}

waitForAllThreads(state);
freeAllocs(state);
waitForAllThreads(state);
if (tid == 0) {
Expand All @@ -342,20 +382,22 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
base::TearDown(state);
}

void bench(benchmark::State &state) override {
void bench(benchmark::State &state) {
auto tid = state.thread_index();
auto &allocation = allocations[tid];
auto &memuse = memused[tid];
for (int i = 0; i < allocsPerIterations; i++) {
auto &n = *next_iter[tid]++;
auto &alloc = allocation[n.offset];
base::allocator.benchFree(alloc.ptr, alloc.size);

memuse -= alloc.size;
alloc.size = n.size;
alloc.ptr = base::allocator.benchAlloc(alloc.size);

if (alloc.ptr == NULL) {
state.SkipWithError("allocation failed");
}
memuse += alloc.size;
}
}

Expand All @@ -376,7 +418,9 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
auto tid = state.thread_index();
auto &i = allocations[tid];
i.resize(max_allocs);
auto &memuse = memused[tid];
auto sizeGenerator = base::alloc_sizes[tid];

for (size_t j = 0; j < max_allocs; j++) {
auto size = sizeGenerator.nextSize();
i[j].ptr = base::allocator.benchAlloc(size);
Expand All @@ -385,6 +429,7 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
return;
}
i[j].size = size;
memuse += size;
}
}

Expand All @@ -394,6 +439,7 @@ class multiple_malloc_free_benchmark : public benchmark_interface<Size, Alloc> {
for (auto &j : i) {
if (j.ptr != NULL) {
base::allocator.benchFree(j.ptr, j.size);
memused[tid] -= j.size;
j.ptr = NULL;
j.size = 0;
}
Expand Down
69 changes: 53 additions & 16 deletions benchmark/benchmark_umf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <benchmark/benchmark.h>
#include <umf/memory_pool.h>
#include <umf/memory_provider.h>

#include <benchmark/benchmark.h>
#include <umf/pools/pool_disjoint.h>
#include <umf/pools/pool_proxy.h>

Expand All @@ -30,7 +28,7 @@ struct provider_interface {
using params_ptr = std::unique_ptr<void, void (*)(void *)>;

umf_memory_provider_handle_t provider = NULL;
virtual void SetUp(::benchmark::State &state) {
void SetUp(::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
Expand All @@ -42,7 +40,27 @@ struct provider_interface {
}
}

virtual void TearDown([[maybe_unused]] ::benchmark::State &state) {
void preBench([[maybe_unused]] ::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
umfCtlExec("umf.provider.by_handle.stats.reset", provider, NULL);
}

void postBench([[maybe_unused]] ::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
size_t arg;
umf_result_t ret = umfCtlGet(
"umf.provider.by_handle.stats.allocated_memory", provider, &arg);
if (ret == UMF_RESULT_SUCCESS) {
state.counters["provider_memory_allocated"] =
static_cast<double>(arg);
}
}

void TearDown([[maybe_unused]] ::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
Expand All @@ -53,9 +71,7 @@ struct provider_interface {
}

virtual umf_memory_provider_ops_t *
getOps([[maybe_unused]] ::benchmark::State &state) {
return nullptr;
}
getOps([[maybe_unused]] ::benchmark::State &state) = 0;

virtual params_ptr getParams([[maybe_unused]] ::benchmark::State &state) {
return {nullptr, [](void *) {}};
Expand All @@ -68,7 +84,7 @@ template <typename T,
struct pool_interface {
using params_ptr = std::unique_ptr<void, void (*)(void *)>;

virtual void SetUp(::benchmark::State &state) {
void SetUp(::benchmark::State &state) {
provider.SetUp(state);
if (state.thread_index() != 0) {
return;
Expand All @@ -80,7 +96,22 @@ struct pool_interface {
state.SkipWithError("umfPoolCreate() failed");
}
}
virtual void TearDown([[maybe_unused]] ::benchmark::State &state) {

void preBench([[maybe_unused]] ::benchmark::State &state) {
provider.preBench(state);
if (state.thread_index() != 0) {
return;
}
}

void postBench([[maybe_unused]] ::benchmark::State &state) {
provider.postBench(state);
if (state.thread_index() != 0) {
return;
}
}

void TearDown([[maybe_unused]] ::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
Expand All @@ -93,15 +124,17 @@ struct pool_interface {
if (pool) {
umfPoolDestroy(pool);
}

provider.TearDown(state);
};

virtual umf_memory_pool_ops_t *
getOps([[maybe_unused]] ::benchmark::State &state) {
return nullptr;
}
getOps([[maybe_unused]] ::benchmark::State &state) = 0;

virtual params_ptr getParams([[maybe_unused]] ::benchmark::State &state) {
return {nullptr, [](void *) {}};
}

T provider;
umf_memory_pool_handle_t pool;
};
Expand All @@ -110,6 +143,8 @@ class allocator_interface {
public:
virtual unsigned SetUp([[maybe_unused]] ::benchmark::State &state,
[[maybe_unused]] unsigned argPos) = 0;
virtual void preBench([[maybe_unused]] ::benchmark::State &state) = 0;
virtual void postBench([[maybe_unused]] ::benchmark::State &state) = 0;
virtual void TearDown([[maybe_unused]] ::benchmark::State &state) = 0;
virtual void *benchAlloc(size_t size) = 0;
virtual void benchFree(void *ptr, [[maybe_unused]] size_t size) = 0;
Expand All @@ -121,7 +156,9 @@ struct glibc_malloc : public allocator_interface {
unsigned argPos) override {
return argPos;
}
void TearDown([[maybe_unused]] ::benchmark::State &state) override{};
void preBench([[maybe_unused]] ::benchmark::State &state) override {}
void postBench([[maybe_unused]] ::benchmark::State &state) override {}
void TearDown([[maybe_unused]] ::benchmark::State &state) override {}
void *benchAlloc(size_t size) override { return malloc(size); }
void benchFree(void *ptr, [[maybe_unused]] size_t size) override {
free(ptr);
Expand Down Expand Up @@ -163,7 +200,7 @@ struct fixed_provider : public provider_interface {
char *mem = NULL;
const size_t size = 1024 * 1024 * 1024; // 1GB
public:
virtual void SetUp(::benchmark::State &state) override {
void SetUp(::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
Expand All @@ -175,7 +212,7 @@ struct fixed_provider : public provider_interface {
provider_interface::SetUp(state);
}

virtual void TearDown(::benchmark::State &state) override {
void TearDown(::benchmark::State &state) {
if (state.thread_index() != 0) {
return;
}
Expand Down Expand Up @@ -295,7 +332,7 @@ struct jemalloc_pool : public pool_interface<Provider> {
#ifdef UMF_POOL_SCALABLE_ENABLED
template <typename Provider>
struct scalable_pool : public pool_interface<Provider> {
virtual umf_memory_pool_ops_t *
umf_memory_pool_ops_t *
getOps([[maybe_unused]] ::benchmark::State &state) override {
return umfScalablePoolOps();
}
Expand Down
Loading
Loading