From d613f89bf00d447145241eb90089ba44e29e4043 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 20 Feb 2020 17:12:48 -0800 Subject: [PATCH] Add a std::map, std::unordered_map benchmark. This benchmark also includes the measuring of RSS. Bug: 137795072 Test: Ran different iterations and verified the RSS_MB value is nearly Test: identical no matter the number of iterations. Change-Id: I465a0eae9dcff2e1fb739c35623a26291680951f --- benchmarks/Android.bp | 10 ++ benchmarks/malloc_map_benchmark.cpp | 175 ++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 benchmarks/malloc_map_benchmark.cpp diff --git a/benchmarks/Android.bp b/benchmarks/Android.bp index b4176de2e..5f8c11306 100644 --- a/benchmarks/Android.bp +++ b/benchmarks/Android.bp @@ -34,6 +34,7 @@ cc_defaults { "inttypes_benchmark.cpp", "malloc_benchmark.cpp", "malloc_sql_benchmark.cpp", + "malloc_map_benchmark.cpp", "math_benchmark.cpp", "property_benchmark.cpp", "pthread_benchmark.cpp", @@ -52,6 +53,15 @@ cc_defaults { "libtinyxml2", ], stl: "libc++_static", + + target: { + android: { + static_libs: [ + "libmeminfo", + "libprocinfo", + ], + }, + }, } cc_defaults { diff --git a/benchmarks/malloc_map_benchmark.cpp b/benchmarks/malloc_map_benchmark.cpp new file mode 100644 index 000000000..ba4d62c0b --- /dev/null +++ b/benchmarks/malloc_map_benchmark.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include "util.h" + +#include + +#if defined(__BIONIC__) + +#include +#include + +static void Gather(uint64_t* rss_bytes) { + android::meminfo::ProcMemInfo proc_mem(getpid()); + const std::vector& maps = proc_mem.MapsWithoutUsageStats(); + for (auto& vma : maps) { + if (vma.name == "[anon:libc_malloc]" || android::base::StartsWith(vma.name, "[anon:scudo:") || + android::base::StartsWith(vma.name, "[anon:GWP-ASan")) { + android::meminfo::Vma update_vma(vma); + if (!proc_mem.FillInVmaStats(update_vma)) { + err(1, "FillInVmaStats failed\n"); + } + *rss_bytes += update_vma.usage.rss; + } + } +} +#endif + +template +static void MapBenchmark(benchmark::State& state, size_t num_elements) { +#if defined(__BIONIC__) + uint64_t rss_bytes = 0; +#endif + + for (auto _ : state) { +#if defined(__BIONIC__) + state.PauseTiming(); + mallopt(M_PURGE, 0); + uint64_t rss_bytes_before = 0; + Gather(&rss_bytes_before); + state.ResumeTiming(); +#endif + MapType map; + for (size_t i = 0; i < num_elements; i++) { + map[i][0] = 0; + } +#if defined(__BIONIC__) + state.PauseTiming(); + mallopt(M_PURGE, 0); + Gather(&rss_bytes); + // Try and record only the memory used in the map. + rss_bytes -= rss_bytes_before; + state.ResumeTiming(); +#endif + } + +#if defined(__BIONIC__) + double rss_mb = (rss_bytes / static_cast(state.iterations())) / 1024.0 / 1024.0; + state.counters["RSS_MB"] = rss_mb; +#endif +} + +static void BM_std_map_8(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_map_8); + +static void BM_std_map_16(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_map_16); + +static void BM_std_map_32(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_map_32); + +static void BM_std_map_64(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_map_64); + +static void BM_std_map_96(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_map_96); + +static void BM_std_map_128(benchmark::State& state) { + MapBenchmark>(state, 500000); +} +BIONIC_BENCHMARK(BM_std_map_128); + +static void BM_std_map_256(benchmark::State& state) { + MapBenchmark>(state, 500000); +} +BIONIC_BENCHMARK(BM_std_map_256); + +static void BM_std_map_512(benchmark::State& state) { + MapBenchmark>(state, 500000); +} +BIONIC_BENCHMARK(BM_std_map_512); + +static void BM_std_unordered_map_8(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_8); + +static void BM_std_unordered_map_16(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_16); + +static void BM_std_unordered_map_32(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_32); + +static void BM_std_unordered_map_64(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_64); + +static void BM_std_unordered_map_96(benchmark::State& state) { + MapBenchmark>(state, 1000000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_96); + +static void BM_std_unordered_map_128(benchmark::State& state) { + MapBenchmark>(state, 500000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_128); + +static void BM_std_unordered_map_256(benchmark::State& state) { + MapBenchmark>(state, 500000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_256); + +static void BM_std_unordered_map_512(benchmark::State& state) { + MapBenchmark>(state, 500000); +} +BIONIC_BENCHMARK(BM_std_unordered_map_512);