blob: 2593e0530392559b8cd85fa73b44b099d2bf28c5 [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>
30
31#include <benchmark/benchmark.h>
32
33#include <backtrace/Backtrace.h>
34#include <backtrace/BacktraceMap.h>
Josh Gaob9670bb2017-10-25 15:12:24 -070035#include <unwindstack/Memory.h>
Christopher Ferris60521c72017-08-18 15:10:53 -070036
37// Definitions of prctl arguments to set a vma name in Android kernels.
38#define ANDROID_PR_SET_VMA 0x53564d41
39#define ANDROID_PR_SET_VMA_ANON_NAME 0
40
41constexpr size_t kNumMaps = 2000;
Christopher Ferris60521c72017-08-18 15:10:53 -070042
43static bool CountMaps(pid_t pid, size_t* num_maps) {
44 // Minimize the calls that might allocate memory. If too much memory
45 // gets allocated, then this routine will add extra maps and the next
46 // call will fail to get the same number of maps as before.
47 int fd =
48 open((std::string("/proc/") + std::to_string(pid) + "/maps").c_str(), O_RDONLY | O_CLOEXEC);
49 if (fd == -1) {
50 fprintf(stderr, "Cannot open map file for pid %d: %s\n", pid, strerror(errno));
51 return false;
52 }
53 *num_maps = 0;
54 while (true) {
55 char buffer[2048];
56 ssize_t bytes = read(fd, buffer, sizeof(buffer));
57 if (bytes <= 0) {
58 break;
59 }
60 // Count the '\n'.
61 for (size_t i = 0; i < static_cast<size_t>(bytes); i++) {
62 if (buffer[i] == '\n') {
63 ++*num_maps;
64 }
65 }
66 }
67
68 close(fd);
69 return true;
70}
71
72static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) {
Christopher Ferris60521c72017-08-18 15:10:53 -070073 // Create a remote process so that the map data is exactly the same.
74 // Also, so that we can create a set number of maps.
75 pid_t pid;
76 if ((pid = fork()) == 0) {
77 size_t num_maps;
78 if (!CountMaps(getpid(), &num_maps)) {
79 exit(1);
80 }
81 // Create uniquely named maps.
82 std::vector<void*> maps;
83 for (size_t i = num_maps; i < kNumMaps; i++) {
84 int flags = PROT_READ | PROT_WRITE;
85 // Alternate page type to make sure a map entry is added for each call.
86 if ((i % 2) == 0) {
87 flags |= PROT_EXEC;
88 }
89 void* memory = mmap(nullptr, PAGE_SIZE, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
90 if (memory == MAP_FAILED) {
91 fprintf(stderr, "Failed to create map: %s\n", strerror(errno));
92 exit(1);
93 }
94 memset(memory, 0x1, PAGE_SIZE);
95 if (prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") ==
96 -1) {
97 fprintf(stderr, "Failed: %s\n", strerror(errno));
98 }
99 maps.push_back(memory);
100 }
101
102 if (!CountMaps(getpid(), &num_maps)) {
103 exit(1);
104 }
105
106 if (num_maps != kNumMaps) {
107 fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected.\n", num_maps, kNumMaps);
108 std::string str;
109 android::base::ReadFileToString("/proc/self/maps", &str);
110 fprintf(stderr, "%s\n", str.c_str());
111 exit(1);
112 }
113
114 // Wait for an hour at most.
115 sleep(3600);
116 exit(1);
117 } else if (pid < 0) {
118 fprintf(stderr, "Fork failed: %s\n", strerror(errno));
119 return;
120 }
121
122 size_t num_maps = 0;
123 for (size_t i = 0; i < 2000; i++) {
124 if (CountMaps(pid, &num_maps) && num_maps == kNumMaps) {
125 break;
126 }
127 usleep(1000);
128 }
129 if (num_maps != kNumMaps) {
130 fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps);
131 return;
132 }
133
Christopher Ferris60521c72017-08-18 15:10:53 -0700134 while (state.KeepRunning()) {
Josh Gao3b094c12017-10-26 13:57:40 -0700135 BacktraceMap* map = map_func(pid, false);
136 if (map == nullptr) {
137 fprintf(stderr, "Failed to create map\n");
138 return;
Christopher Ferris60521c72017-08-18 15:10:53 -0700139 }
Josh Gao3b094c12017-10-26 13:57:40 -0700140 delete map;
Christopher Ferris60521c72017-08-18 15:10:53 -0700141 }
Christopher Ferris60521c72017-08-18 15:10:53 -0700142
143 kill(pid, SIGKILL);
144 waitpid(pid, nullptr, 0);
145}
146
147static void BM_create_map(benchmark::State& state) {
148 CreateMap(state, BacktraceMap::Create);
149}
Josh Gao3b094c12017-10-26 13:57:40 -0700150BENCHMARK(BM_create_map);
Christopher Ferris60521c72017-08-18 15:10:53 -0700151
152static void BM_create_map_new(benchmark::State& state) {
153 CreateMap(state, BacktraceMap::CreateNew);
154}
Josh Gao3b094c12017-10-26 13:57:40 -0700155BENCHMARK(BM_create_map_new);
Christopher Ferris60521c72017-08-18 15:10:53 -0700156
Josh Gaob9670bb2017-10-25 15:12:24 -0700157
158using BacktraceCreateFn = decltype(Backtrace::Create);
159
160static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) {
161 while (state.KeepRunning()) {
162 std::unique_ptr<Backtrace> backtrace(fn(getpid(), gettid(), map));
163 backtrace->Unwind(0);
164 }
165}
166
167static void BM_create_backtrace(benchmark::State& state) {
168 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid()));
169 CreateBacktrace(state, backtrace_map.get(), Backtrace::Create);
170}
171BENCHMARK(BM_create_backtrace);
172
173static void BM_create_backtrace_new(benchmark::State& state) {
174 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::CreateNew(getpid()));
175 CreateBacktrace(state, backtrace_map.get(), Backtrace::CreateNew);
176}
177BENCHMARK(BM_create_backtrace_new);
178
Christopher Ferris60521c72017-08-18 15:10:53 -0700179BENCHMARK_MAIN();