Skip to content

bench: Add benchmark for mprotect and fix mmap #1082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 31 additions & 2 deletions test/internal_benchmarks/memory_allocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
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();

Check warning on line 33 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L31-L33

Added lines #L31 - L33 were not covered by tests
munmap(m, size);
#else
(void)size;
Expand All @@ -52,4 +53,32 @@
BENCHMARK_TEMPLATE(allocate, calloc_) ARGS;
BENCHMARK_TEMPLATE(allocate, os_specific) ARGS;


#if defined(__unix__) || defined(__APPLE__)
void bench_mprotect(benchmark::State& state)

Check warning on line 58 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L58

Added line #L58 was not covered by tests
{
const auto page_size = static_cast<size_t>(getpagesize());
const auto size = static_cast<size_t>(state.range(0)) * page_size;
const auto idx = static_cast<size_t>(state.range(1));

Check warning on line 62 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L60-L62

Added lines #L60 - L62 were not covered by tests

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");

Check warning on line 67 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L64-L67

Added lines #L64 - L67 were not covered by tests

const auto p = &static_cast<char*>(m)[idx * page_size];

Check warning on line 69 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L69

Added line #L69 was not covered by tests

for (auto _ : state)

Check warning on line 71 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L71

Added line #L71 was not covered by tests
{
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");

Check warning on line 76 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L73-L76

Added lines #L73 - L76 were not covered by tests
}

munmap(m, size);
}

Check warning on line 80 in test/internal_benchmarks/memory_allocation.cpp

View check run for this annotation

Codecov / codecov/patch

test/internal_benchmarks/memory_allocation.cpp#L79-L80

Added lines #L79 - L80 were not covered by tests
BENCHMARK(bench_mprotect)->Args({1, 0})->Args({8 * 1024, 13 * 9});
#endif

} // namespace