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 <procinfo/process_map.h>
18
19 #include <string.h>
20 #include <sys/types.h>
21
22 #include <string>
23
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <backtrace/BacktraceMap.h>
27 #include <unwindstack/Maps.h>
28
29 #include <benchmark/benchmark.h>
30
BM_ReadMapFile(benchmark::State & state)31 static void BM_ReadMapFile(benchmark::State& state) {
32 std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
33 for (auto _ : state) {
34 std::vector<android::procinfo::MapInfo> maps;
35 android::procinfo::ReadMapFile(map_file, [&](uint64_t start, uint64_t end, uint16_t flags,
36 uint64_t pgoff, ino_t inode, const char* name) {
37 maps.emplace_back(start, end, flags, pgoff, inode, name);
38 });
39 CHECK_EQ(maps.size(), 2043u);
40 }
41 }
42 BENCHMARK(BM_ReadMapFile);
43
BM_unwindstack_FileMaps(benchmark::State & state)44 static void BM_unwindstack_FileMaps(benchmark::State& state) {
45 std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
46 for (auto _ : state) {
47 unwindstack::FileMaps maps(map_file);
48 maps.Parse();
49 CHECK_EQ(maps.Total(), 2043u);
50 }
51 }
52 BENCHMARK(BM_unwindstack_FileMaps);
53
BM_unwindstack_BufferMaps(benchmark::State & state)54 static void BM_unwindstack_BufferMaps(benchmark::State& state) {
55 std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
56 std::string content;
57 CHECK(android::base::ReadFileToString(map_file, &content));
58 for (auto _ : state) {
59 unwindstack::BufferMaps maps(content.c_str());
60 maps.Parse();
61 CHECK_EQ(maps.Total(), 2043u);
62 }
63 }
64 BENCHMARK(BM_unwindstack_BufferMaps);
65
BM_backtrace_BacktraceMap(benchmark::State & state)66 static void BM_backtrace_BacktraceMap(benchmark::State& state) {
67 pid_t pid = getpid();
68 for (auto _ : state) {
69 BacktraceMap* map = BacktraceMap::Create(pid, true);
70 CHECK(map != nullptr);
71 delete map;
72 }
73 }
74 BENCHMARK(BM_backtrace_BacktraceMap);
75
76 BENCHMARK_MAIN();
77