blob: be106a3a39cf5706fcba20fc51268eddf2d67e68 [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
42constexpr size_t kNumMaps = 10000;
43
44static void CreateInitialMap(const char* filename) {
45 std::string maps;
46 for (size_t i = 0; i < kNumMaps; i += 2) {
47 maps += android::base::StringPrintf("%zu-%zu r-xp 0000 00:00 0 name%zu\n", i * 1000,
48 (i + 1) * 1000, i);
49 }
50 if (!android::base::WriteStringToFile(maps, filename)) {
51 errx(1, "WriteStringToFile failed");
52 }
53}
54
55static void CreateReparseMap(const char* filename) {
56 std::string maps;
57 for (size_t i = 0; i < kNumMaps; i++) {
58 maps += android::base::StringPrintf("%zu-%zu r-xp 0000 00:00 0 name%zu\n", i * 2000,
59 (i + 1) * 2000, 2 * i);
60 }
61 if (!android::base::WriteStringToFile(maps, filename)) {
62 errx(1, "WriteStringToFile failed");
63 }
64}
65
66void BM_local_updatable_maps_reparse(benchmark::State& state) {
67 TemporaryFile initial_map;
68 CreateInitialMap(initial_map.path);
69
70 TemporaryFile reparse_map;
71 CreateReparseMap(reparse_map.path);
72
73 for (auto _ : state) {
74 BenchmarkLocalUpdatableMaps maps;
75 maps.BenchmarkSetMapsFile(initial_map.path);
76 if (!maps.Reparse()) {
77 errx(1, "Internal Error: reparse of initial maps filed.");
78 }
79 if (maps.Total() != (kNumMaps / 2)) {
80 errx(1, "Internal Error: Incorrect total number of maps %zu, expected %zu.", maps.Total(),
81 kNumMaps / 2);
82 }
83 maps.BenchmarkSetMapsFile(reparse_map.path);
84 if (!maps.Reparse()) {
85 errx(1, "Internal Error: reparse of second set of maps filed.");
86 }
87 if (maps.Total() != kNumMaps) {
88 errx(1, "Internal Error: Incorrect total number of maps %zu, expected %zu.", maps.Total(),
89 kNumMaps);
90 }
91 }
92}
93BENCHMARK(BM_local_updatable_maps_reparse);