blob: a93a25e90dfb1b6e003701677e8fd13189ef8486 [file] [log] [blame]
Christopher Ferris60521c72017-08-18 15:10:53 -07001/*
2 * Copyright (C) 2017 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 <errno.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <string.h>
21#include <sys/prctl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <unistd.h>
26
27#include <string>
28
29#include <android-base/file.h>
Elliott Hughes38488902018-07-11 11:13:16 -070030#include <android-base/threads.h>
Christopher Ferris60521c72017-08-18 15:10:53 -070031
32#include <benchmark/benchmark.h>
33
34#include <backtrace/Backtrace.h>
35#include <backtrace/BacktraceMap.h>
Josh Gaob9670bb2017-10-25 15:12:24 -070036#include <unwindstack/Memory.h>
Christopher Ferris60521c72017-08-18 15:10:53 -070037
Christopher Ferris60521c72017-08-18 15:10:53 -070038constexpr size_t kNumMaps = 2000;
Christopher Ferris60521c72017-08-18 15:10:53 -070039
40static bool CountMaps(pid_t pid, size_t* num_maps) {
41 // Minimize the calls that might allocate memory. If too much memory
42 // gets allocated, then this routine will add extra maps and the next
43 // call will fail to get the same number of maps as before.
44 int fd =
45 open((std::string("/proc/") + std::to_string(pid) + "/maps").c_str(), O_RDONLY | O_CLOEXEC);
46 if (fd == -1) {
47 fprintf(stderr, "Cannot open map file for pid %d: %s\n", pid, strerror(errno));
48 return false;
49 }
50 *num_maps = 0;
51 while (true) {
52 char buffer[2048];
53 ssize_t bytes = read(fd, buffer, sizeof(buffer));
54 if (bytes <= 0) {
55 break;
56 }
57 // Count the '\n'.
58 for (size_t i = 0; i < static_cast<size_t>(bytes); i++) {
59 if (buffer[i] == '\n') {
60 ++*num_maps;
61 }
62 }
63 }
64
65 close(fd);
66 return true;
67}
68
69static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) {
Christopher Ferris60521c72017-08-18 15:10:53 -070070 // Create a remote process so that the map data is exactly the same.
71 // Also, so that we can create a set number of maps.
72 pid_t pid;
73 if ((pid = fork()) == 0) {
74 size_t num_maps;
75 if (!CountMaps(getpid(), &num_maps)) {
76 exit(1);
77 }
78 // Create uniquely named maps.
79 std::vector<void*> maps;
80 for (size_t i = num_maps; i < kNumMaps; i++) {
81 int flags = PROT_READ | PROT_WRITE;
82 // Alternate page type to make sure a map entry is added for each call.
83 if ((i % 2) == 0) {
84 flags |= PROT_EXEC;
85 }
86 void* memory = mmap(nullptr, PAGE_SIZE, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
87 if (memory == MAP_FAILED) {
88 fprintf(stderr, "Failed to create map: %s\n", strerror(errno));
89 exit(1);
90 }
91 memset(memory, 0x1, PAGE_SIZE);
Elliott Hughes95c34a72018-08-22 12:06:20 -070092#if defined(PR_SET_VMA)
93 if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") == -1) {
Christopher Ferris60521c72017-08-18 15:10:53 -070094 fprintf(stderr, "Failed: %s\n", strerror(errno));
95 }
Elliott Hughes95c34a72018-08-22 12:06:20 -070096#endif
Christopher Ferris60521c72017-08-18 15:10:53 -070097 maps.push_back(memory);
98 }
99
100 if (!CountMaps(getpid(), &num_maps)) {
101 exit(1);
102 }
103
Christopher Ferris88b48f72017-12-08 12:19:44 -0800104 if (num_maps < kNumMaps) {
105 fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected at least.\n", num_maps,
106 kNumMaps);
Christopher Ferris60521c72017-08-18 15:10:53 -0700107 std::string str;
108 android::base::ReadFileToString("/proc/self/maps", &str);
109 fprintf(stderr, "%s\n", str.c_str());
110 exit(1);
111 }
112
113 // Wait for an hour at most.
114 sleep(3600);
115 exit(1);
116 } else if (pid < 0) {
117 fprintf(stderr, "Fork failed: %s\n", strerror(errno));
118 return;
119 }
120
121 size_t num_maps = 0;
122 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris88b48f72017-12-08 12:19:44 -0800123 if (CountMaps(pid, &num_maps) && num_maps >= kNumMaps) {
Christopher Ferris60521c72017-08-18 15:10:53 -0700124 break;
125 }
126 usleep(1000);
127 }
Christopher Ferris88b48f72017-12-08 12:19:44 -0800128 if (num_maps < kNumMaps) {
Christopher Ferris60521c72017-08-18 15:10:53 -0700129 fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps);
130 return;
131 }
132
Christopher Ferris60521c72017-08-18 15:10:53 -0700133 while (state.KeepRunning()) {
Josh Gao3b094c12017-10-26 13:57:40 -0700134 BacktraceMap* map = map_func(pid, false);
135 if (map == nullptr) {
136 fprintf(stderr, "Failed to create map\n");
137 return;
Christopher Ferris60521c72017-08-18 15:10:53 -0700138 }
Josh Gao3b094c12017-10-26 13:57:40 -0700139 delete map;
Christopher Ferris60521c72017-08-18 15:10:53 -0700140 }
Christopher Ferris60521c72017-08-18 15:10:53 -0700141
142 kill(pid, SIGKILL);
143 waitpid(pid, nullptr, 0);
144}
145
146static void BM_create_map(benchmark::State& state) {
147 CreateMap(state, BacktraceMap::Create);
148}
Josh Gao3b094c12017-10-26 13:57:40 -0700149BENCHMARK(BM_create_map);
Christopher Ferris60521c72017-08-18 15:10:53 -0700150
Josh Gaob9670bb2017-10-25 15:12:24 -0700151using BacktraceCreateFn = decltype(Backtrace::Create);
152
153static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) {
154 while (state.KeepRunning()) {
Elliott Hughes38488902018-07-11 11:13:16 -0700155 std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map));
Josh Gaob9670bb2017-10-25 15:12:24 -0700156 backtrace->Unwind(0);
157 }
158}
159
160static void BM_create_backtrace(benchmark::State& state) {
161 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid()));
162 CreateBacktrace(state, backtrace_map.get(), Backtrace::Create);
163}
164BENCHMARK(BM_create_backtrace);
165
Christopher Ferris60521c72017-08-18 15:10:53 -0700166BENCHMARK_MAIN();