1 /*
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 #include <android-base/threads.h>
31 
32 #include <benchmark/benchmark.h>
33 
34 #include <backtrace/Backtrace.h>
35 #include <backtrace/BacktraceMap.h>
36 #include <unwindstack/Memory.h>
37 
38 constexpr size_t kNumMaps = 2000;
39 
CountMaps(pid_t pid,size_t * num_maps)40 static 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 
CreateMap(benchmark::State & state,BacktraceMap * (* map_func)(pid_t,bool))69 static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) {
70   // 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);
92 #if defined(PR_SET_VMA)
93       if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") == -1) {
94         fprintf(stderr, "Failed: %s\n", strerror(errno));
95       }
96 #endif
97       maps.push_back(memory);
98     }
99 
100     if (!CountMaps(getpid(), &num_maps)) {
101       exit(1);
102     }
103 
104     if (num_maps < kNumMaps) {
105       fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected at least.\n", num_maps,
106               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 
133   while (state.KeepRunning()) {
134     BacktraceMap* map = map_func(pid, false);
135     if (map == nullptr) {
136       fprintf(stderr, "Failed to create map\n");
137       return;
138     }
139     delete map;
140   }
141 
142   kill(pid, SIGKILL);
143   waitpid(pid, nullptr, 0);
144 }
145 
BM_create_map(benchmark::State & state)146 static void BM_create_map(benchmark::State& state) {
147   CreateMap(state, BacktraceMap::Create);
148 }
149 BENCHMARK(BM_create_map);
150 
151 using BacktraceCreateFn = decltype(Backtrace::Create);
152 
CreateBacktrace(benchmark::State & state,BacktraceMap * map,BacktraceCreateFn fn)153 static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) {
154   while (state.KeepRunning()) {
155     std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map));
156     backtrace->Unwind(0);
157   }
158 }
159 
BM_create_backtrace(benchmark::State & state)160 static 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 }
164 BENCHMARK(BM_create_backtrace);
165 
166 BENCHMARK_MAIN();
167