Merge "Add mmap syscall benchmarks" into main

This commit is contained in:
Suren Baghdasaryan 2023-09-30 20:51:48 +00:00 committed by Gerrit Code Review
commit 706e228154
4 changed files with 277 additions and 0 deletions

View file

@ -57,6 +57,7 @@ cc_defaults {
"stdio_benchmark.cpp", "stdio_benchmark.cpp",
"stdlib_benchmark.cpp", "stdlib_benchmark.cpp",
"string_benchmark.cpp", "string_benchmark.cpp",
"syscall_mmap_benchmark.cpp",
"time_benchmark.cpp", "time_benchmark.cpp",
"unistd_benchmark.cpp", "unistd_benchmark.cpp",
"wctype_benchmark.cpp", "wctype_benchmark.cpp",

View file

@ -521,12 +521,22 @@ std::map<std::string, args_vector_t> GetShorthand() {
all_sizes.insert(all_sizes.end(), kMediumSizes.begin(), kMediumSizes.end()); all_sizes.insert(all_sizes.end(), kMediumSizes.begin(), kMediumSizes.end());
all_sizes.insert(all_sizes.end(), kLargeSizes.begin(), kLargeSizes.end()); all_sizes.insert(all_sizes.end(), kLargeSizes.begin(), kLargeSizes.end());
int page_sz = getpagesize();
std::vector<int> sub_page_sizes = {page_sz / 2, page_sz / 4, page_sz / 8};
std::vector<int> multi_page_sizes = {page_sz, page_sz * 2, page_sz * 3, page_sz * 10,
page_sz * 100};
std::vector<int> all_page_sizes(sub_page_sizes);
all_page_sizes.insert(all_page_sizes.end(), multi_page_sizes.begin(), multi_page_sizes.end());
std::map<std::string, args_vector_t> args_shorthand { std::map<std::string, args_vector_t> args_shorthand {
{"AT_COMMON_SIZES", GetArgs(kCommonSizes)}, {"AT_COMMON_SIZES", GetArgs(kCommonSizes)},
{"AT_SMALL_SIZES", GetArgs(kSmallSizes)}, {"AT_SMALL_SIZES", GetArgs(kSmallSizes)},
{"AT_MEDIUM_SIZES", GetArgs(kMediumSizes)}, {"AT_MEDIUM_SIZES", GetArgs(kMediumSizes)},
{"AT_LARGE_SIZES", GetArgs(kLargeSizes)}, {"AT_LARGE_SIZES", GetArgs(kLargeSizes)},
{"AT_ALL_SIZES", GetArgs(all_sizes)}, {"AT_ALL_SIZES", GetArgs(all_sizes)},
{"AT_SUB_PAGE_SIZES", GetArgs(sub_page_sizes)},
{"AT_MULTI_PAGE_SIZES", GetArgs(multi_page_sizes)},
{"AT_All_PAGE_SIZES", GetArgs(all_page_sizes)},
{"AT_ALIGNED_ONEBUF", GetArgs(kCommonSizes, 0)}, {"AT_ALIGNED_ONEBUF", GetArgs(kCommonSizes, 0)},
{"AT_ALIGNED_ONEBUF_SMALL", GetArgs(kSmallSizes, 0)}, {"AT_ALIGNED_ONEBUF_SMALL", GetArgs(kSmallSizes, 0)},

View file

@ -0,0 +1,52 @@
<!-- mmap tests -->
<fn>
<name>BM_syscall_mmap_anon_rw</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_anon_noreserve</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_anon_none</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_anon_rw_fixed</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_anon_none_fixed</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_file_rd_priv</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_file_rw_shared</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_file_rw_priv_fixed_start</name>
<iterations>10</iterations>
<args>AT_All_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_file_rw_priv_fixed_mid</name>
<iterations>10</iterations>
<args>AT_MULTI_PAGE_SIZES</args>
</fn>
<fn>
<name>BM_syscall_mmap_file_rw_priv_fixed_end</name>
<iterations>10</iterations>
<args>AT_MULTI_PAGE_SIZES</args>
</fn>

View file

@ -0,0 +1,214 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <benchmark/benchmark.h>
#include "util.h"
static size_t page_sz = getpagesize();
struct MmapParams {
int prot;
int flags;
int64_t size;
};
// mmap syscall benchmarks
static void MmapBenchmark(benchmark::State& state, const struct MmapParams& params, int fd,
void* area = nullptr) {
for (auto _ : state) {
void* addr = mmap(area, params.size, params.prot, params.flags, fd, 0);
if (addr == MAP_FAILED) {
state.SkipWithError(android::base::StringPrintf("mmap failed: %s", strerror(errno)).c_str());
break;
}
if (params.prot & PROT_WRITE) {
MakeAllocationResident(addr, params.size, page_sz);
}
if (munmap(addr, params.size) != 0) {
state.SkipWithError(
android::base::StringPrintf("munmap failed: %s", strerror(errno)).c_str());
break;
}
}
}
static void MmapFixedBenchmark(benchmark::State& state, const struct MmapParams& params, int fd,
size_t area_size, size_t offs) {
uint8_t* area = reinterpret_cast<uint8_t*>(mmap(0, area_size, params.prot, params.flags, fd, 0));
if (area == MAP_FAILED) {
state.SkipWithError(android::base::StringPrintf("mmap failed: %s", strerror(errno)).c_str());
return;
}
MmapBenchmark(state, params, fd, area + offs);
if (munmap(area, area_size) != 0) {
state.SkipWithError(android::base::StringPrintf("munmap failed: %s", strerror(errno)).c_str());
return;
}
}
static void MmapFileBenchmark(benchmark::State& state, const struct MmapParams& params,
size_t area_size, size_t offs) {
TemporaryFile tf;
if (tf.fd < 0) {
state.SkipWithError(
android::base::StringPrintf("failed to create a temporary file: %s", strerror(errno))
.c_str());
return;
}
if (area_size > 0 && ftruncate(tf.fd, area_size)) {
state.SkipWithError(
android::base::StringPrintf("ftruncate failed: %s", strerror(errno)).c_str());
return;
}
if (params.flags & MAP_FIXED) {
MmapFixedBenchmark(state, params, tf.fd, area_size, offs);
} else {
MmapBenchmark(state, params, tf.fd);
}
}
// anon mmap
static void BM_syscall_mmap_anon_rw(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ | PROT_WRITE,
.flags = MAP_PRIVATE | MAP_ANONYMOUS,
.size = state.range(0),
};
MmapBenchmark(state, params, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_anon_rw, "AT_All_PAGE_SIZES");
static void BM_syscall_mmap_anon_noreserve(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_NONE,
.flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
.size = state.range(0),
};
MmapBenchmark(state, params, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_anon_noreserve, "AT_All_PAGE_SIZES");
static void BM_syscall_mmap_anon_none(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_NONE,
.flags = MAP_PRIVATE | MAP_ANONYMOUS,
.size = state.range(0),
};
MmapBenchmark(state, params, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_anon_none, "AT_All_PAGE_SIZES");
// anon fixed mmap
static void BM_syscall_mmap_anon_rw_fixed(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ | PROT_WRITE,
.flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
.size = state.range(0),
};
MmapFixedBenchmark(state, params, 0, params.size, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_anon_rw_fixed, "AT_All_PAGE_SIZES");
static void BM_syscall_mmap_anon_none_fixed(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_NONE,
.flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
.size = state.range(0),
};
MmapFixedBenchmark(state, params, 0, params.size, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_anon_none_fixed, "AT_All_PAGE_SIZES");
// file mmap
static void BM_syscall_mmap_file_rd_priv(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ,
.flags = MAP_PRIVATE,
.size = state.range(0),
};
MmapFileBenchmark(state, params, params.size, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_file_rd_priv, "AT_All_PAGE_SIZES");
static void BM_syscall_mmap_file_rw_shared(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ | PROT_WRITE,
.flags = MAP_SHARED,
.size = state.range(0),
};
MmapFileBenchmark(state, params, params.size, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_file_rw_shared, "AT_All_PAGE_SIZES");
// file fixed mmap
static void BM_syscall_mmap_file_rw_priv_fixed_start(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ | PROT_WRITE,
.flags = MAP_PRIVATE | MAP_FIXED,
.size = state.range(0),
};
// allocate 3x area and map at the start
MmapFileBenchmark(state, params, params.size * 3, 0);
}
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_file_rw_priv_fixed_start, "AT_All_PAGE_SIZES");
static void BM_syscall_mmap_file_rw_priv_fixed_mid(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ | PROT_WRITE,
.flags = MAP_PRIVATE | MAP_FIXED,
.size = state.range(0),
};
// allocate 3x area and map at the middle
MmapFileBenchmark(state, params, params.size * 3, params.size);
}
// mapping at sub-page size offset is not supported, so run only for AT_MULTI_PAGE_SIZES
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_file_rw_priv_fixed_mid, "AT_MULTI_PAGE_SIZES");
static void BM_syscall_mmap_file_rw_priv_fixed_end(benchmark::State& state) {
struct MmapParams params = {
.prot = PROT_READ | PROT_WRITE,
.flags = MAP_PRIVATE | MAP_FIXED,
.size = state.range(0),
};
// allocate 3x area and map at the end
MmapFileBenchmark(state, params, params.size * 3, params.size * 2);
}
// mapping at sub-page size offset is not supported, so run only for AT_MULTI_PAGE_SIZES
BIONIC_BENCHMARK_WITH_ARG(BM_syscall_mmap_file_rw_priv_fixed_end, "AT_MULTI_PAGE_SIZES");