1 /*
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
Gather(uint64_t * rss_bytes)47 static 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");
56 }
57 *rss_bytes += update_vma.usage.rss;
58 }
59 }
60 }
61 #endif
62
63 template <typename MapType>
MapBenchmark(benchmark::State & state,size_t num_elements)64 static 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();
72 mallopt(M_PURGE_ALL, 0);
73 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();
83 mallopt(M_PURGE_ALL, 0);
84 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
BM_std_map_8(benchmark::State & state)97 static void BM_std_map_8(benchmark::State& state) {
98 MapBenchmark<std::map<uint64_t, char[8]>>(state, 1000000);
99 }
100 BIONIC_BENCHMARK(BM_std_map_8);
101
BM_std_map_16(benchmark::State & state)102 static void BM_std_map_16(benchmark::State& state) {
103 MapBenchmark<std::map<uint64_t, char[16]>>(state, 1000000);
104 }
105 BIONIC_BENCHMARK(BM_std_map_16);
106
BM_std_map_32(benchmark::State & state)107 static void BM_std_map_32(benchmark::State& state) {
108 MapBenchmark<std::map<uint64_t, char[32]>>(state, 1000000);
109 }
110 BIONIC_BENCHMARK(BM_std_map_32);
111
BM_std_map_64(benchmark::State & state)112 static void BM_std_map_64(benchmark::State& state) {
113 MapBenchmark<std::map<uint64_t, char[64]>>(state, 1000000);
114 }
115 BIONIC_BENCHMARK(BM_std_map_64);
116
BM_std_map_96(benchmark::State & state)117 static void BM_std_map_96(benchmark::State& state) {
118 MapBenchmark<std::map<uint64_t, char[96]>>(state, 1000000);
119 }
120 BIONIC_BENCHMARK(BM_std_map_96);
121
BM_std_map_128(benchmark::State & state)122 static void BM_std_map_128(benchmark::State& state) {
123 MapBenchmark<std::map<uint64_t, char[128]>>(state, 500000);
124 }
125 BIONIC_BENCHMARK(BM_std_map_128);
126
BM_std_map_256(benchmark::State & state)127 static void BM_std_map_256(benchmark::State& state) {
128 MapBenchmark<std::map<uint64_t, char[256]>>(state, 500000);
129 }
130 BIONIC_BENCHMARK(BM_std_map_256);
131
BM_std_map_512(benchmark::State & state)132 static void BM_std_map_512(benchmark::State& state) {
133 MapBenchmark<std::map<uint64_t, char[512]>>(state, 500000);
134 }
135 BIONIC_BENCHMARK(BM_std_map_512);
136
BM_std_unordered_map_8(benchmark::State & state)137 static void BM_std_unordered_map_8(benchmark::State& state) {
138 MapBenchmark<std::unordered_map<uint64_t, char[8]>>(state, 1000000);
139 }
140 BIONIC_BENCHMARK(BM_std_unordered_map_8);
141
BM_std_unordered_map_16(benchmark::State & state)142 static void BM_std_unordered_map_16(benchmark::State& state) {
143 MapBenchmark<std::unordered_map<uint64_t, char[16]>>(state, 1000000);
144 }
145 BIONIC_BENCHMARK(BM_std_unordered_map_16);
146
BM_std_unordered_map_32(benchmark::State & state)147 static void BM_std_unordered_map_32(benchmark::State& state) {
148 MapBenchmark<std::unordered_map<uint64_t, char[32]>>(state, 1000000);
149 }
150 BIONIC_BENCHMARK(BM_std_unordered_map_32);
151
BM_std_unordered_map_64(benchmark::State & state)152 static void BM_std_unordered_map_64(benchmark::State& state) {
153 MapBenchmark<std::unordered_map<uint64_t, char[64]>>(state, 1000000);
154 }
155 BIONIC_BENCHMARK(BM_std_unordered_map_64);
156
BM_std_unordered_map_96(benchmark::State & state)157 static void BM_std_unordered_map_96(benchmark::State& state) {
158 MapBenchmark<std::unordered_map<uint64_t, char[96]>>(state, 1000000);
159 }
160 BIONIC_BENCHMARK(BM_std_unordered_map_96);
161
BM_std_unordered_map_128(benchmark::State & state)162 static void BM_std_unordered_map_128(benchmark::State& state) {
163 MapBenchmark<std::unordered_map<uint64_t, char[128]>>(state, 500000);
164 }
165 BIONIC_BENCHMARK(BM_std_unordered_map_128);
166
BM_std_unordered_map_256(benchmark::State & state)167 static void BM_std_unordered_map_256(benchmark::State& state) {
168 MapBenchmark<std::unordered_map<uint64_t, char[256]>>(state, 500000);
169 }
170 BIONIC_BENCHMARK(BM_std_unordered_map_256);
171
BM_std_unordered_map_512(benchmark::State & state)172 static void BM_std_unordered_map_512(benchmark::State& state) {
173 MapBenchmark<std::unordered_map<uint64_t, char[512]>>(state, 500000);
174 }
175 BIONIC_BENCHMARK(BM_std_unordered_map_512);
176