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