Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 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 <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 | |
| 29 | class 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 Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 42 | static constexpr size_t kNumSmallMaps = 100; |
| 43 | static constexpr size_t kNumLargeMaps = 10000; |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 44 | |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 45 | static void CreateMap(const char* filename, size_t num_maps, size_t increment = 1) { |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 46 | std::string maps; |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 47 | for (size_t i = 0; i < num_maps; i += increment) { |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 48 | maps += android::base::StringPrintf("%zu-%zu r-xp 0000 00:00 0 name%zu\n", i * 1000, |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 49 | (i + 1) * 1000 * increment, i * increment); |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 50 | } |
| 51 | if (!android::base::WriteStringToFile(maps, filename)) { |
| 52 | errx(1, "WriteStringToFile failed"); |
| 53 | } |
| 54 | } |
| 55 | |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 56 | static void ReparseBenchmark(benchmark::State& state, const char* maps1, size_t maps1_total, |
| 57 | const char* maps2, size_t maps2_total) { |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 58 | for (auto _ : state) { |
| 59 | BenchmarkLocalUpdatableMaps maps; |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 60 | maps.BenchmarkSetMapsFile(maps1); |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 61 | if (!maps.Reparse()) { |
| 62 | errx(1, "Internal Error: reparse of initial maps filed."); |
| 63 | } |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 64 | if (maps.Total() != maps1_total) { |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 65 | errx(1, "Internal Error: Incorrect total number of maps %zu, expected %zu.", maps.Total(), |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 66 | maps1_total); |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 67 | } |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 68 | maps.BenchmarkSetMapsFile(maps2); |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 69 | if (!maps.Reparse()) { |
| 70 | errx(1, "Internal Error: reparse of second set of maps filed."); |
| 71 | } |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 72 | if (maps.Total() != maps2_total) { |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 73 | errx(1, "Internal Error: Incorrect total number of maps %zu, expected %zu.", maps.Total(), |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 74 | maps2_total); |
Christopher Ferris | 175747f | 2020-05-04 13:46:09 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
Christopher Ferris | 5990a9d | 2020-05-19 10:05:31 -0700 | [diff] [blame] | 78 | |
| 79 | void 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 | } |
| 88 | BENCHMARK(BM_local_updatable_maps_reparse_double_initial_small); |
| 89 | |
| 90 | void 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 | } |
| 99 | BENCHMARK(BM_local_updatable_maps_reparse_double_initial_large); |
| 100 | |
| 101 | void 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 | } |
| 108 | BENCHMARK(BM_local_updatable_maps_reparse_same_maps_small); |
| 109 | |
| 110 | void 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 | } |
| 116 | BENCHMARK(BM_local_updatable_maps_reparse_same_maps_large); |
| 117 | |
| 118 | void 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 | } |
| 127 | BENCHMARK(BM_local_updatable_maps_reparse_few_extra_small); |
| 128 | |
| 129 | void 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 | } |
| 138 | BENCHMARK(BM_local_updatable_maps_reparse_few_extra_large); |
| 139 | |
| 140 | void 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 | } |
| 149 | BENCHMARK(BM_local_updatable_maps_reparse_few_less_small); |
| 150 | |
| 151 | void 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 | } |
| 160 | BENCHMARK(BM_local_updatable_maps_reparse_few_less_large); |