From bbae0529bec8e0b0ab6aa55b6ae2bf685b51b63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 9 Dec 2024 11:22:03 +0100 Subject: [PATCH] bench: Add benchmark for mprotect and fix mmap --- .../internal_benchmarks/memory_allocation.cpp | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/test/internal_benchmarks/memory_allocation.cpp b/test/internal_benchmarks/memory_allocation.cpp index a4a3894bab..fce37b371a 100644 --- a/test/internal_benchmarks/memory_allocation.cpp +++ b/test/internal_benchmarks/memory_allocation.cpp @@ -28,8 +28,9 @@ void calloc_(size_t size) noexcept void os_specific(size_t size) noexcept { #if defined(__unix__) || defined(__APPLE__) - auto m = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0); - benchmark::DoNotOptimize(m); + auto m = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (m == MAP_FAILED) + __builtin_trap(); munmap(m, size); #else (void)size; @@ -52,4 +53,32 @@ BENCHMARK_TEMPLATE(allocate, malloc_) ARGS; BENCHMARK_TEMPLATE(allocate, calloc_) ARGS; BENCHMARK_TEMPLATE(allocate, os_specific) ARGS; + +#if defined(__unix__) || defined(__APPLE__) +void bench_mprotect(benchmark::State& state) +{ + const auto page_size = static_cast(getpagesize()); + const auto size = static_cast(state.range(0)) * page_size; + const auto idx = static_cast(state.range(1)); + + auto prot = PROT_READ | PROT_WRITE; + const auto m = mmap(nullptr, size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (m == MAP_FAILED) + state.SkipWithError("mmap failed"); + + const auto p = &static_cast(m)[idx * page_size]; + + for (auto _ : state) + { + prot = (prot == PROT_NONE) ? (PROT_READ | PROT_WRITE) : PROT_NONE; + const auto res = mprotect(p, page_size, prot); + if (res != 0) [[unlikely]] + state.SkipWithError("mprotect failed"); + } + + munmap(m, size); +} +BENCHMARK(bench_mprotect)->Args({1, 0})->Args({8 * 1024, 13 * 9}); +#endif + } // namespace