1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <err.h>
18 #include <inttypes.h>
19 #include <malloc.h>
20 #include <stdint.h>
21
22 #include <string>
23 #include <vector>
24
25 #include <benchmark/benchmark.h>
26
27 #include <unwindstack/Elf.h>
28 #include <unwindstack/Memory.h>
29
30 #include "Utils.h"
31
BenchmarkSymbolLookup(benchmark::State & state,std::vector<uint64_t> offsets,std::string elf_file,bool expect_found,uint32_t runs=1)32 static void BenchmarkSymbolLookup(benchmark::State& state, std::vector<uint64_t> offsets,
33 std::string elf_file, bool expect_found, uint32_t runs = 1) {
34 #if defined(__BIONIC__)
35 uint64_t rss_bytes = 0;
36 #endif
37 uint64_t alloc_bytes = 0;
38 for (auto _ : state) {
39 state.PauseTiming();
40 #if defined(__BIONIC__)
41 mallopt(M_PURGE, 0);
42 uint64_t rss_bytes_before = 0;
43 GatherRss(&rss_bytes_before);
44 #endif
45 uint64_t alloc_bytes_before = mallinfo().uordblks;
46 state.ResumeTiming();
47
48 unwindstack::Elf elf(unwindstack::Memory::CreateFileMemory(elf_file, 0).release());
49 if (!elf.Init() || !elf.valid()) {
50 errx(1, "Internal Error: Cannot open elf.");
51 }
52
53 unwindstack::SharedString name;
54 uint64_t offset;
55 for (size_t i = 0; i < runs; i++) {
56 for (auto pc : offsets) {
57 bool found = elf.GetFunctionName(pc, &name, &offset);
58 if (expect_found && !found) {
59 errx(1, "expected pc 0x%" PRIx64 " present, but not found.", pc);
60 } else if (!expect_found && found) {
61 errx(1, "expected pc 0x%" PRIx64 " not present, but found.", pc);
62 }
63 }
64 }
65
66 state.PauseTiming();
67 #if defined(__BIONIC__)
68 mallopt(M_PURGE, 0);
69 #endif
70 alloc_bytes += mallinfo().uordblks - alloc_bytes_before;
71 #if defined(__BIONIC__)
72 GatherRss(&rss_bytes);
73 rss_bytes -= rss_bytes_before;
74 #endif
75 state.ResumeTiming();
76 }
77
78 #if defined(__BIONIC__)
79 state.counters["RSS_BYTES"] = rss_bytes / static_cast<double>(state.iterations());
80 #endif
81 state.counters["ALLOCATED_BYTES"] = alloc_bytes / static_cast<double>(state.iterations());
82 }
83
BenchmarkSymbolLookup(benchmark::State & state,uint64_t pc,std::string elf_file,bool expect_found,uint32_t runs=1)84 static void BenchmarkSymbolLookup(benchmark::State& state, uint64_t pc, std::string elf_file,
85 bool expect_found, uint32_t runs = 1) {
86 BenchmarkSymbolLookup(state, std::vector<uint64_t>{pc}, elf_file, expect_found, runs);
87 }
88
BM_elf_and_symbol_not_present(benchmark::State & state)89 void BM_elf_and_symbol_not_present(benchmark::State& state) {
90 BenchmarkSymbolLookup(state, 0, GetElfFile(), false);
91 }
92 BENCHMARK(BM_elf_and_symbol_not_present);
93
BM_elf_and_symbol_find_single(benchmark::State & state)94 void BM_elf_and_symbol_find_single(benchmark::State& state) {
95 BenchmarkSymbolLookup(state, 0x22b2bc, GetElfFile(), true);
96 }
97 BENCHMARK(BM_elf_and_symbol_find_single);
98
BM_elf_and_symbol_find_single_many_times(benchmark::State & state)99 void BM_elf_and_symbol_find_single_many_times(benchmark::State& state) {
100 BenchmarkSymbolLookup(state, 0x22b2bc, GetElfFile(), true, 4096);
101 }
102 BENCHMARK(BM_elf_and_symbol_find_single_many_times);
103
BM_elf_and_symbol_find_multiple(benchmark::State & state)104 void BM_elf_and_symbol_find_multiple(benchmark::State& state) {
105 BenchmarkSymbolLookup(state,
106 std::vector<uint64_t>{0x22b2bc, 0xd5d30, 0x1312e8, 0x13582e, 0x1389c8},
107 GetElfFile(), true);
108 }
109 BENCHMARK(BM_elf_and_symbol_find_multiple);
110
BM_elf_and_symbol_not_present_from_sorted(benchmark::State & state)111 void BM_elf_and_symbol_not_present_from_sorted(benchmark::State& state) {
112 BenchmarkSymbolLookup(state, 0, GetSymbolSortedElfFile(), false);
113 }
114 BENCHMARK(BM_elf_and_symbol_not_present_from_sorted);
115
BM_elf_and_symbol_find_single_from_sorted(benchmark::State & state)116 void BM_elf_and_symbol_find_single_from_sorted(benchmark::State& state) {
117 BenchmarkSymbolLookup(state, 0x138638, GetSymbolSortedElfFile(), true);
118 }
119 BENCHMARK(BM_elf_and_symbol_find_single_from_sorted);
120
BM_elf_and_symbol_find_single_many_times_from_sorted(benchmark::State & state)121 void BM_elf_and_symbol_find_single_many_times_from_sorted(benchmark::State& state) {
122 BenchmarkSymbolLookup(state, 0x138638, GetSymbolSortedElfFile(), true, 4096);
123 }
124 BENCHMARK(BM_elf_and_symbol_find_single_many_times_from_sorted);
125
BM_elf_and_symbol_find_multiple_from_sorted(benchmark::State & state)126 void BM_elf_and_symbol_find_multiple_from_sorted(benchmark::State& state) {
127 BenchmarkSymbolLookup(state,
128 std::vector<uint64_t>{0x138638, 0x84350, 0x14df18, 0x1f3a38, 0x1f3ca8},
129 GetSymbolSortedElfFile(), true);
130 }
131 BENCHMARK(BM_elf_and_symbol_find_multiple_from_sorted);
132