blob: 5df1491687cd62c0214b5f65e578dd024196d1f0 [file] [log] [blame]
Christopher Ferris175747f2020-05-04 13:46:09 -07001/*
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 <stdint.h>
19
20#include <string>
21
22#include <android-base/file.h>
23#include <android-base/stringprintf.h>
24
25#include <benchmark/benchmark.h>
26
27#include <unwindstack/Maps.h>
28
29class BenchmarkLocalUpdatableMaps : public unwindstack::LocalUpdatableMaps {
30 public:
31 BenchmarkLocalUpdatableMaps() : unwindstack::LocalUpdatableMaps() {}
32 virtual ~BenchmarkLocalUpdatableMaps() = default;
33
34 const std::string GetMapsFile() const override { return maps_file_; }
35
36 void BenchmarkSetMapsFile(const std::string& maps_file) { maps_file_ = maps_file; }
37
38 private:
39 std::string maps_file_;
40};
41
Christopher Ferris5990a9d2020-05-19 10:05:31 -070042static constexpr size_t kNumSmallMaps = 100;
43static constexpr size_t kNumLargeMaps = 10000;
Christopher Ferris175747f2020-05-04 13:46:09 -070044
Christopher Ferris5990a9d2020-05-19 10:05:31 -070045static void CreateMap(const char* filename, size_t num_maps, size_t increment = 1) {
Christopher Ferris175747f2020-05-04 13:46:09 -070046 std::string maps;
Christopher Ferris5990a9d2020-05-19 10:05:31 -070047 for (size_t i = 0; i < num_maps; i += increment) {
Christopher Ferris175747f2020-05-04 13:46:09 -070048 maps += android::base::StringPrintf("%zu-%zu r-xp 0000 00:00 0 name%zu\n", i * 1000,
Christopher Ferris5990a9d2020-05-19 10:05:31 -070049 (i + 1) * 1000 * increment, i * increment);
Christopher Ferris175747f2020-05-04 13:46:09 -070050 }
51 if (!android::base::WriteStringToFile(maps, filename)) {
52 errx(1, "WriteStringToFile failed");
53 }
54}
55
Christopher Ferris5990a9d2020-05-19 10:05:31 -070056static void ReparseBenchmark(benchmark::State& state, const char* maps1, size_t maps1_total,
57 const char* maps2, size_t maps2_total) {
Christopher Ferris175747f2020-05-04 13:46:09 -070058 for (auto _ : state) {
59 BenchmarkLocalUpdatableMaps maps;
Christopher Ferris5990a9d2020-05-19 10:05:31 -070060 maps.BenchmarkSetMapsFile(maps1);
Christopher Ferris175747f2020-05-04 13:46:09 -070061 if (!maps.Reparse()) {
62 errx(1, "Internal Error: reparse of initial maps filed.");
63 }
Christopher Ferris5990a9d2020-05-19 10:05:31 -070064 if (maps.Total() != maps1_total) {
Christopher Ferris175747f2020-05-04 13:46:09 -070065 errx(1, "Internal Error: Incorrect total number of maps %zu, expected %zu.", maps.Total(),
Christopher Ferris5990a9d2020-05-19 10:05:31 -070066 maps1_total);
Christopher Ferris175747f2020-05-04 13:46:09 -070067 }
Christopher Ferris5990a9d2020-05-19 10:05:31 -070068 maps.BenchmarkSetMapsFile(maps2);
Christopher Ferris175747f2020-05-04 13:46:09 -070069 if (!maps.Reparse()) {
70 errx(1, "Internal Error: reparse of second set of maps filed.");
71 }
Christopher Ferris5990a9d2020-05-19 10:05:31 -070072 if (maps.Total() != maps2_total) {
Christopher Ferris175747f2020-05-04 13:46:09 -070073 errx(1, "Internal Error: Incorrect total number of maps %zu, expected %zu.", maps.Total(),
Christopher Ferris5990a9d2020-05-19 10:05:31 -070074 maps2_total);
Christopher Ferris175747f2020-05-04 13:46:09 -070075 }
76 }
77}
Christopher Ferris5990a9d2020-05-19 10:05:31 -070078
79void BM_local_updatable_maps_reparse_double_initial_small(benchmark::State& state) {
80 TemporaryFile initial_maps;
81 CreateMap(initial_maps.path, kNumSmallMaps, 2);
82
83 TemporaryFile reparse_maps;
84 CreateMap(reparse_maps.path, kNumSmallMaps);
85
86 ReparseBenchmark(state, initial_maps.path, kNumSmallMaps / 2, reparse_maps.path, kNumSmallMaps);
87}
88BENCHMARK(BM_local_updatable_maps_reparse_double_initial_small);
89
90void BM_local_updatable_maps_reparse_double_initial_large(benchmark::State& state) {
91 TemporaryFile initial_maps;
92 CreateMap(initial_maps.path, kNumLargeMaps, 2);
93
94 TemporaryFile reparse_maps;
95 CreateMap(reparse_maps.path, kNumLargeMaps);
96
97 ReparseBenchmark(state, initial_maps.path, kNumLargeMaps / 2, reparse_maps.path, kNumLargeMaps);
98}
99BENCHMARK(BM_local_updatable_maps_reparse_double_initial_large);
100
101void BM_local_updatable_maps_reparse_same_maps_small(benchmark::State& state) {
102 static constexpr size_t kNumSmallMaps = 100;
103 TemporaryFile maps;
104 CreateMap(maps.path, kNumSmallMaps);
105
106 ReparseBenchmark(state, maps.path, kNumSmallMaps, maps.path, kNumSmallMaps);
107}
108BENCHMARK(BM_local_updatable_maps_reparse_same_maps_small);
109
110void BM_local_updatable_maps_reparse_same_maps_large(benchmark::State& state) {
111 TemporaryFile maps;
112 CreateMap(maps.path, kNumLargeMaps);
113
114 ReparseBenchmark(state, maps.path, kNumLargeMaps, maps.path, kNumLargeMaps);
115}
116BENCHMARK(BM_local_updatable_maps_reparse_same_maps_large);
117
118void BM_local_updatable_maps_reparse_few_extra_small(benchmark::State& state) {
119 TemporaryFile maps1;
120 CreateMap(maps1.path, kNumSmallMaps - 4);
121
122 TemporaryFile maps2;
123 CreateMap(maps2.path, kNumSmallMaps);
124
125 ReparseBenchmark(state, maps1.path, kNumSmallMaps - 4, maps2.path, kNumSmallMaps);
126}
127BENCHMARK(BM_local_updatable_maps_reparse_few_extra_small);
128
129void BM_local_updatable_maps_reparse_few_extra_large(benchmark::State& state) {
130 TemporaryFile maps1;
131 CreateMap(maps1.path, kNumLargeMaps - 4);
132
133 TemporaryFile maps2;
134 CreateMap(maps2.path, kNumLargeMaps);
135
136 ReparseBenchmark(state, maps1.path, kNumLargeMaps - 4, maps2.path, kNumLargeMaps);
137}
138BENCHMARK(BM_local_updatable_maps_reparse_few_extra_large);
139
140void BM_local_updatable_maps_reparse_few_less_small(benchmark::State& state) {
141 TemporaryFile maps1;
142 CreateMap(maps1.path, kNumSmallMaps);
143
144 TemporaryFile maps2;
145 CreateMap(maps2.path, kNumSmallMaps - 4);
146
147 ReparseBenchmark(state, maps1.path, kNumSmallMaps, maps2.path, kNumSmallMaps - 4);
148}
149BENCHMARK(BM_local_updatable_maps_reparse_few_less_small);
150
151void BM_local_updatable_maps_reparse_few_less_large(benchmark::State& state) {
152 TemporaryFile maps1;
153 CreateMap(maps1.path, kNumLargeMaps);
154
155 TemporaryFile maps2;
156 CreateMap(maps2.path, kNumLargeMaps - 4);
157
158 ReparseBenchmark(state, maps1.path, kNumLargeMaps, maps2.path, kNumLargeMaps - 4);
159}
160BENCHMARK(BM_local_updatable_maps_reparse_few_less_large);