blob: aa1662fb13060d63a2c89118a846c5274ded8c78 [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>
35
36// Definitions of prctl arguments to set a vma name in Android kernels.
37#define ANDROID_PR_SET_VMA 0x53564d41
38#define ANDROID_PR_SET_VMA_ANON_NAME 0
39
40constexpr size_t kNumMaps = 2000;
41constexpr size_t kNumIterations = 1000;
42
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()) {
135 for (size_t i = 0; i < static_cast<size_t>(state.range(0)); i++) {
136 BacktraceMap* map = map_func(pid, false);
137 if (map == nullptr) {
138 fprintf(stderr, "Failed to create map\n");
139 return;
140 }
141 delete map;
142 }
143 }
Christopher Ferris60521c72017-08-18 15:10:53 -0700144
145 kill(pid, SIGKILL);
146 waitpid(pid, nullptr, 0);
147}
148
149static void BM_create_map(benchmark::State& state) {
150 CreateMap(state, BacktraceMap::Create);
151}
152BENCHMARK(BM_create_map)->Arg(kNumIterations);
153
154static void BM_create_map_new(benchmark::State& state) {
155 CreateMap(state, BacktraceMap::CreateNew);
156}
157BENCHMARK(BM_create_map_new)->Arg(kNumIterations);
158
159BENCHMARK_MAIN();