From 330ffd3791bce93d0bdb941a3f1700f04ded6d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Plewa?= Date: Mon, 13 Jan 2025 16:23:32 +0100 Subject: [PATCH] reduce code duplication in Benchmark by using Apply() to set arguments --- benchmark/benchmark.cpp | 131 +++++++++++++++++----------------------- benchmark/benchmark.hpp | 25 +++++--- 2 files changed, 72 insertions(+), 84 deletions(-) diff --git a/benchmark/benchmark.cpp b/benchmark/benchmark.cpp index df4fe6e5d..6c8175e1d 100644 --- a/benchmark/benchmark.cpp +++ b/benchmark/benchmark.cpp @@ -1,11 +1,13 @@ /* - * Copyright (C) 2024 Intel Corporation + * Copyright (C) 2024-2025 Intel Corporation * * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * */ +#include + #include "benchmark.hpp" #define UMF_BENCHMARK_TEMPLATE_DEFINE(BaseClass, Method, ...) \ @@ -18,14 +20,8 @@ #define UMF_BENCHMARK_REGISTER_F(BaseClass, Method) \ BENCHMARK_REGISTER_F(BaseClass, Method) \ - ->ArgNames( \ - BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::argsName()) \ - ->Name(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::name()) \ - ->Iterations( \ - BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::iterations()) - -UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_fix, fixed_alloc_size, - glibc_malloc); + ->Apply( \ + &BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::defaultArgs) // Benchmarks scenarios: @@ -33,54 +29,56 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_fix, fixed_alloc_size, // benchmark arguments, allocator arguments, size generator arguments. // The exact meaning of each argument depends on the benchmark, allocator, and size components used. // Refer to the 'argsName()' function in each component to find detailed descriptions of these arguments. + +static void default_alloc_fix_size(benchmark::internal::Benchmark *benchmark) { + benchmark->Args({10000, 0, 4096}); + benchmark->Args({10000, 100000, 4096}); + benchmark->Threads(4); + benchmark->Threads(1); +} + +static void +default_alloc_uniform_size(benchmark::internal::Benchmark *benchmark) { + benchmark->Args({10000, 0, 8, 64 * 1024, 8}); + benchmark->Threads(4); + benchmark->Threads(1); +} + +UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_fix, fixed_alloc_size, + glibc_malloc); + UMF_BENCHMARK_REGISTER_F(alloc_benchmark, glibc_fix) - ->Args({10000, 0, 4096}) - ->Args({10000, 100000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_uniform, uniform_alloc_size, glibc_malloc); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, glibc_uniform) - ->Args({10000, 0, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_uniform_size); UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, os_provider, fixed_alloc_size, provider_allocator); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, os_provider) - ->Args({10000, 0, 4096}) - ->Args({10000, 100000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, proxy_pool, fixed_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, proxy_pool) - ->Args({1000, 0, 4096}) - ->Args({1000, 100000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_fix_size); #ifdef UMF_POOL_DISJOINT_ENABLED UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, disjoint_pool_fix, fixed_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, disjoint_pool_fix) - ->Args({10000, 0, 4096}) - ->Args({10000, 100000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_fix_size); // TODO: debug why this crashes /*UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, disjoint_pool_uniform, uniform_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, disjoint_pool_uniform) - ->Args({10000, 0, 8, 64 * 1024, 8}) - // ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_uniform_size); */ #endif @@ -89,18 +87,13 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, jemalloc_pool_fix, fixed_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, jemalloc_pool_fix) - ->Args({10000, 0, 4096}) - ->Args({10000, 100000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, jemalloc_pool_uniform, uniform_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, jemalloc_pool_uniform) - ->Args({10000, 0, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_uniform_size); #endif #ifdef UMF_POOL_SCALABLE_ENABLED @@ -109,71 +102,67 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, scalable_pool_fix, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, scalable_pool_fix) - ->Args({10000, 0, 4096}) - ->Args({10000, 100000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, scalable_pool_uniform, uniform_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(alloc_benchmark, scalable_pool_uniform) - ->Args({10000, 0, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_alloc_uniform_size); #endif // Multiple allocs/free +static void +default_multiple_alloc_fix_size(benchmark::internal::Benchmark *benchmark) { + benchmark->Args({10000, 4096}); + benchmark->Threads(4); + benchmark->Threads(1); +} + +static void +default_multiple_alloc_uniform_size(benchmark::internal::Benchmark *benchmark) { + benchmark->Args({10000, 8, 64 * 1024, 8}); + benchmark->Threads(4); + benchmark->Threads(1); +} UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, glibc_fix, fixed_alloc_size, glibc_malloc); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, glibc_fix) - ->Args({10000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, glibc_uniform, uniform_alloc_size, glibc_malloc); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, glibc_uniform) - ->Args({10000, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_uniform_size); UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, proxy_pool, fixed_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, proxy_pool) - ->Args({10000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, os_provider, fixed_alloc_size, provider_allocator); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, os_provider) - ->Args({10000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_fix_size); #ifdef UMF_POOL_DISJOINT_ENABLED UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, disjoint_pool_fix, fixed_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, disjoint_pool_fix) - ->Args({10000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_fix_size); // TODO: debug why this crashes /*UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, disjoint_pool_uniform, uniform_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, disjoint_pool_uniform) - ->Args({10000, 0, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_uniform_size); */ #endif @@ -182,17 +171,13 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, jemalloc_pool_fix, fixed_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, jemalloc_pool_fix) - ->Args({10000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, jemalloc_pool_uniform, uniform_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, jemalloc_pool_uniform) - ->Args({1000, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_uniform_size); #endif @@ -202,18 +187,14 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, scalable_pool_fix, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, scalable_pool_fix) - ->Args({10000, 4096}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_fix_size); UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, scalable_pool_uniform, uniform_alloc_size, pool_allocator>); UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, scalable_pool_uniform) - ->Args({10000, 8, 64 * 1024, 8}) - ->Threads(4) - ->Threads(1); + ->Apply(&default_multiple_alloc_uniform_size); #endif BENCHMARK_MAIN(); diff --git a/benchmark/benchmark.hpp b/benchmark/benchmark.hpp index df5d6a592..50e75f8fb 100644 --- a/benchmark/benchmark.hpp +++ b/benchmark/benchmark.hpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Intel Corporation + * Copyright (C) 2024-2025 Intel Corporation * * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -162,8 +162,15 @@ struct benchmark_interface : public benchmark::Fixture { return res; } - static std::string name() { return Allocator::name(); } - static int64_t iterations() { return 10000; } + virtual std::string name() { return Allocator::name(); } + virtual int64_t iterations() { return 10000; } + static void defaultArgs(Benchmark *benchmark) { + auto *bench = + static_cast *>(benchmark); + benchmark->ArgNames(bench->argsName()) + ->Name(bench->name()) + ->Iterations(bench->iterations()); + } Size alloc_size; Allocator allocator; }; @@ -260,15 +267,15 @@ class alloc_benchmark : public benchmark_interface { } } - static std::vector argsName() { + virtual std::vector argsName() { auto n = benchmark_interface::argsName(); std::vector res = {"max_allocs", "pre_allocs"}; res.insert(res.end(), n.begin(), n.end()); return res; } - static std::string name() { return base::name() + "/alloc"; } - static int64_t iterations() { return 200000; } + virtual std::string name() { return base::name() + "/alloc"; } + virtual int64_t iterations() { return 200000; } protected: using base = benchmark_interface; @@ -346,18 +353,18 @@ class multiple_malloc_free_benchmark : public alloc_benchmark { } } - static std::string name() { + virtual std::string name() { return base::base::name() + "/multiple_malloc_free"; } - static std::vector argsName() { + virtual std::vector argsName() { auto n = benchmark_interface::argsName(); std::vector res = {"max_allocs"}; res.insert(res.end(), n.begin(), n.end()); return res; } - static int64_t iterations() { return 2000; } + virtual int64_t iterations() { return 2000; } std::default_random_engine generator; distribution dist;