1 /*
2 * Copyright (C) 2018 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 <stdint.h>
18
19 #include <memory>
20
21 #include <benchmark/benchmark.h>
22
23 #include <android-base/strings.h>
24
25 #include <unwindstack/Elf.h>
26 #include <unwindstack/Maps.h>
27 #include <unwindstack/Memory.h>
28 #include <unwindstack/Regs.h>
29 #include <unwindstack/RegsGetLocal.h>
30 #include <unwindstack/Unwinder.h>
31
Call6(std::shared_ptr<unwindstack::Memory> & process_memory,unwindstack::Maps * maps)32 size_t Call6(std::shared_ptr<unwindstack::Memory>& process_memory, unwindstack::Maps* maps) {
33 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
34 unwindstack::RegsGetLocal(regs.get());
35 unwindstack::Unwinder unwinder(32, maps, regs.get(), process_memory);
36 unwinder.Unwind();
37 return unwinder.NumFrames();
38 }
39
Call5(std::shared_ptr<unwindstack::Memory> & process_memory,unwindstack::Maps * maps)40 size_t Call5(std::shared_ptr<unwindstack::Memory>& process_memory, unwindstack::Maps* maps) {
41 return Call6(process_memory, maps);
42 }
43
Call4(std::shared_ptr<unwindstack::Memory> & process_memory,unwindstack::Maps * maps)44 size_t Call4(std::shared_ptr<unwindstack::Memory>& process_memory, unwindstack::Maps* maps) {
45 return Call5(process_memory, maps);
46 }
47
Call3(std::shared_ptr<unwindstack::Memory> & process_memory,unwindstack::Maps * maps)48 size_t Call3(std::shared_ptr<unwindstack::Memory>& process_memory, unwindstack::Maps* maps) {
49 return Call4(process_memory, maps);
50 }
51
Call2(std::shared_ptr<unwindstack::Memory> & process_memory,unwindstack::Maps * maps)52 size_t Call2(std::shared_ptr<unwindstack::Memory>& process_memory, unwindstack::Maps* maps) {
53 return Call3(process_memory, maps);
54 }
55
Call1(std::shared_ptr<unwindstack::Memory> & process_memory,unwindstack::Maps * maps)56 size_t Call1(std::shared_ptr<unwindstack::Memory>& process_memory, unwindstack::Maps* maps) {
57 return Call2(process_memory, maps);
58 }
59
BM_uncached_unwind(benchmark::State & state)60 static void BM_uncached_unwind(benchmark::State& state) {
61 auto process_memory = unwindstack::Memory::CreateProcessMemory(getpid());
62 unwindstack::LocalMaps maps;
63 if (!maps.Parse()) {
64 state.SkipWithError("Failed to parse local maps.");
65 }
66
67 for (auto _ : state) {
68 benchmark::DoNotOptimize(Call1(process_memory, &maps));
69 }
70 }
71 BENCHMARK(BM_uncached_unwind);
72
BM_cached_unwind(benchmark::State & state)73 static void BM_cached_unwind(benchmark::State& state) {
74 auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(getpid());
75 unwindstack::LocalMaps maps;
76 if (!maps.Parse()) {
77 state.SkipWithError("Failed to parse local maps.");
78 }
79
80 for (auto _ : state) {
81 benchmark::DoNotOptimize(Call1(process_memory, &maps));
82 }
83 }
84 BENCHMARK(BM_cached_unwind);
85
Initialize(benchmark::State & state,unwindstack::Maps & maps,unwindstack::MapInfo ** build_id_map_info)86 static void Initialize(benchmark::State& state, unwindstack::Maps& maps,
87 unwindstack::MapInfo** build_id_map_info) {
88 if (!maps.Parse()) {
89 state.SkipWithError("Failed to parse local maps.");
90 return;
91 }
92
93 // Find the libc.so share library and use that for benchmark purposes.
94 *build_id_map_info = nullptr;
95 for (auto& map_info : maps) {
96 if (map_info->offset == 0 && map_info->GetBuildID() != "") {
97 *build_id_map_info = map_info.get();
98 break;
99 }
100 }
101
102 if (*build_id_map_info == nullptr) {
103 state.SkipWithError("Failed to find a map with a BuildID.");
104 }
105 }
106
BM_get_build_id_from_elf(benchmark::State & state)107 static void BM_get_build_id_from_elf(benchmark::State& state) {
108 unwindstack::LocalMaps maps;
109 unwindstack::MapInfo* build_id_map_info;
110 Initialize(state, maps, &build_id_map_info);
111
112 unwindstack::Elf* elf = build_id_map_info->GetElf(std::shared_ptr<unwindstack::Memory>(),
113 unwindstack::Regs::CurrentArch());
114 if (!elf->valid()) {
115 state.SkipWithError("Cannot get valid elf from map.");
116 }
117
118 for (auto _ : state) {
119 uintptr_t id = build_id_map_info->build_id;
120 if (id != 0) {
121 delete reinterpret_cast<std::string*>(id);
122 build_id_map_info->build_id = 0;
123 }
124 benchmark::DoNotOptimize(build_id_map_info->GetBuildID());
125 }
126 }
127 BENCHMARK(BM_get_build_id_from_elf);
128
BM_get_build_id_from_file(benchmark::State & state)129 static void BM_get_build_id_from_file(benchmark::State& state) {
130 unwindstack::LocalMaps maps;
131 unwindstack::MapInfo* build_id_map_info;
132 Initialize(state, maps, &build_id_map_info);
133
134 for (auto _ : state) {
135 uintptr_t id = build_id_map_info->build_id;
136 if (id != 0) {
137 delete reinterpret_cast<std::string*>(id);
138 build_id_map_info->build_id = 0;
139 }
140 benchmark::DoNotOptimize(build_id_map_info->GetBuildID());
141 }
142 }
143 BENCHMARK(BM_get_build_id_from_file);
144
145 BENCHMARK_MAIN();
146