blob: 9fdf60c66064b72905195485d72f6b61068ea06b [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;
Christopher Ferris60521c72017-08-18 15:10:53 -070041
42static bool CountMaps(pid_t pid, size_t* num_maps) {
43 // Minimize the calls that might allocate memory. If too much memory
44 // gets allocated, then this routine will add extra maps and the next
45 // call will fail to get the same number of maps as before.
46 int fd =
47 open((std::string("/proc/") + std::to_string(pid) + "/maps").c_str(), O_RDONLY | O_CLOEXEC);
48 if (fd == -1) {
49 fprintf(stderr, "Cannot open map file for pid %d: %s\n", pid, strerror(errno));
50 return false;
51 }
52 *num_maps = 0;
53 while (true) {
54 char buffer[2048];
55 ssize_t bytes = read(fd, buffer, sizeof(buffer));
56 if (bytes <= 0) {
57 break;
58 }
59 // Count the '\n'.
60 for (size_t i = 0; i < static_cast<size_t>(bytes); i++) {
61 if (buffer[i] == '\n') {
62 ++*num_maps;
63 }
64 }
65 }
66
67 close(fd);
68 return true;
69}
70
71static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) {
Christopher Ferris60521c72017-08-18 15:10:53 -070072 // Create a remote process so that the map data is exactly the same.
73 // Also, so that we can create a set number of maps.
74 pid_t pid;
75 if ((pid = fork()) == 0) {
76 size_t num_maps;
77 if (!CountMaps(getpid(), &num_maps)) {
78 exit(1);
79 }
80 // Create uniquely named maps.
81 std::vector<void*> maps;
82 for (size_t i = num_maps; i < kNumMaps; i++) {
83 int flags = PROT_READ | PROT_WRITE;
84 // Alternate page type to make sure a map entry is added for each call.
85 if ((i % 2) == 0) {
86 flags |= PROT_EXEC;
87 }
88 void* memory = mmap(nullptr, PAGE_SIZE, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
89 if (memory == MAP_FAILED) {
90 fprintf(stderr, "Failed to create map: %s\n", strerror(errno));
91 exit(1);
92 }
93 memset(memory, 0x1, PAGE_SIZE);
94 if (prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") ==
95 -1) {
96 fprintf(stderr, "Failed: %s\n", strerror(errno));
97 }
98 maps.push_back(memory);
99 }
100
101 if (!CountMaps(getpid(), &num_maps)) {
102 exit(1);
103 }
104
105 if (num_maps != kNumMaps) {
106 fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected.\n", num_maps, kNumMaps);
107 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++) {
123 if (CountMaps(pid, &num_maps) && num_maps == kNumMaps) {
124 break;
125 }
126 usleep(1000);
127 }
128 if (num_maps != kNumMaps) {
129 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
151static void BM_create_map_new(benchmark::State& state) {
152 CreateMap(state, BacktraceMap::CreateNew);
153}
Josh Gao3b094c12017-10-26 13:57:40 -0700154BENCHMARK(BM_create_map_new);
Christopher Ferris60521c72017-08-18 15:10:53 -0700155
156BENCHMARK_MAIN();