blob: 57573253923b3cee0f55e40895be202bf6c8c61b [file] [log] [blame]
Christopher Ferrisd613f892020-02-20 17:12:48 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <err.h>
30#include <malloc.h>
31#include <stdint.h>
32
33#include <map>
34#include <unordered_map>
35#include <vector>
36
37#include <benchmark/benchmark.h>
38#include "util.h"
39
40#include <android-base/strings.h>
41
42#if defined(__BIONIC__)
43
44#include <meminfo/procmeminfo.h>
45#include <procinfo/process_map.h>
46
47static void Gather(uint64_t* rss_bytes) {
48 android::meminfo::ProcMemInfo proc_mem(getpid());
49 const std::vector<android::meminfo::Vma>& maps = proc_mem.MapsWithoutUsageStats();
50 for (auto& vma : maps) {
51 if (vma.name == "[anon:libc_malloc]" || android::base::StartsWith(vma.name, "[anon:scudo:") ||
52 android::base::StartsWith(vma.name, "[anon:GWP-ASan")) {
53 android::meminfo::Vma update_vma(vma);
54 if (!proc_mem.FillInVmaStats(update_vma)) {
55 err(1, "FillInVmaStats failed\n");
56 }
57 *rss_bytes += update_vma.usage.rss;
58 }
59 }
60}
61#endif
62
63template <typename MapType>
64static void MapBenchmark(benchmark::State& state, size_t num_elements) {
65#if defined(__BIONIC__)
66 uint64_t rss_bytes = 0;
67#endif
68
69 for (auto _ : state) {
70#if defined(__BIONIC__)
71 state.PauseTiming();
Christopher Ferrisd86eb862023-02-28 12:45:54 -080072 mallopt(M_PURGE_ALL, 0);
Christopher Ferrisd613f892020-02-20 17:12:48 -080073 uint64_t rss_bytes_before = 0;
74 Gather(&rss_bytes_before);
75 state.ResumeTiming();
76#endif
77 MapType map;
78 for (size_t i = 0; i < num_elements; i++) {
79 map[i][0] = 0;
80 }
81#if defined(__BIONIC__)
82 state.PauseTiming();
Christopher Ferrisd86eb862023-02-28 12:45:54 -080083 mallopt(M_PURGE_ALL, 0);
Christopher Ferrisd613f892020-02-20 17:12:48 -080084 Gather(&rss_bytes);
85 // Try and record only the memory used in the map.
86 rss_bytes -= rss_bytes_before;
87 state.ResumeTiming();
88#endif
89 }
90
91#if defined(__BIONIC__)
92 double rss_mb = (rss_bytes / static_cast<double>(state.iterations())) / 1024.0 / 1024.0;
93 state.counters["RSS_MB"] = rss_mb;
94#endif
95}
96
97static void BM_std_map_8(benchmark::State& state) {
98 MapBenchmark<std::map<uint64_t, char[8]>>(state, 1000000);
99}
100BIONIC_BENCHMARK(BM_std_map_8);
101
102static void BM_std_map_16(benchmark::State& state) {
103 MapBenchmark<std::map<uint64_t, char[16]>>(state, 1000000);
104}
105BIONIC_BENCHMARK(BM_std_map_16);
106
107static void BM_std_map_32(benchmark::State& state) {
108 MapBenchmark<std::map<uint64_t, char[32]>>(state, 1000000);
109}
110BIONIC_BENCHMARK(BM_std_map_32);
111
112static void BM_std_map_64(benchmark::State& state) {
113 MapBenchmark<std::map<uint64_t, char[64]>>(state, 1000000);
114}
115BIONIC_BENCHMARK(BM_std_map_64);
116
117static void BM_std_map_96(benchmark::State& state) {
118 MapBenchmark<std::map<uint64_t, char[96]>>(state, 1000000);
119}
120BIONIC_BENCHMARK(BM_std_map_96);
121
122static void BM_std_map_128(benchmark::State& state) {
123 MapBenchmark<std::map<uint64_t, char[128]>>(state, 500000);
124}
125BIONIC_BENCHMARK(BM_std_map_128);
126
127static void BM_std_map_256(benchmark::State& state) {
128 MapBenchmark<std::map<uint64_t, char[256]>>(state, 500000);
129}
130BIONIC_BENCHMARK(BM_std_map_256);
131
132static void BM_std_map_512(benchmark::State& state) {
133 MapBenchmark<std::map<uint64_t, char[512]>>(state, 500000);
134}
135BIONIC_BENCHMARK(BM_std_map_512);
136
137static void BM_std_unordered_map_8(benchmark::State& state) {
138 MapBenchmark<std::unordered_map<uint64_t, char[8]>>(state, 1000000);
139}
140BIONIC_BENCHMARK(BM_std_unordered_map_8);
141
142static void BM_std_unordered_map_16(benchmark::State& state) {
143 MapBenchmark<std::unordered_map<uint64_t, char[16]>>(state, 1000000);
144}
145BIONIC_BENCHMARK(BM_std_unordered_map_16);
146
147static void BM_std_unordered_map_32(benchmark::State& state) {
148 MapBenchmark<std::unordered_map<uint64_t, char[32]>>(state, 1000000);
149}
150BIONIC_BENCHMARK(BM_std_unordered_map_32);
151
152static void BM_std_unordered_map_64(benchmark::State& state) {
153 MapBenchmark<std::unordered_map<uint64_t, char[64]>>(state, 1000000);
154}
155BIONIC_BENCHMARK(BM_std_unordered_map_64);
156
157static void BM_std_unordered_map_96(benchmark::State& state) {
158 MapBenchmark<std::unordered_map<uint64_t, char[96]>>(state, 1000000);
159}
160BIONIC_BENCHMARK(BM_std_unordered_map_96);
161
162static void BM_std_unordered_map_128(benchmark::State& state) {
163 MapBenchmark<std::unordered_map<uint64_t, char[128]>>(state, 500000);
164}
165BIONIC_BENCHMARK(BM_std_unordered_map_128);
166
167static void BM_std_unordered_map_256(benchmark::State& state) {
168 MapBenchmark<std::unordered_map<uint64_t, char[256]>>(state, 500000);
169}
170BIONIC_BENCHMARK(BM_std_unordered_map_256);
171
172static void BM_std_unordered_map_512(benchmark::State& state) {
173 MapBenchmark<std::unordered_map<uint64_t, char[512]>>(state, 500000);
174}
175BIONIC_BENCHMARK(BM_std_unordered_map_512);