| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 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> | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 30 | #include <android-base/threads.h> | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 31 |  | 
 | 32 | #include <benchmark/benchmark.h> | 
 | 33 |  | 
 | 34 | #include <backtrace/Backtrace.h> | 
 | 35 | #include <backtrace/BacktraceMap.h> | 
| Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 36 | #include <unwindstack/Memory.h> | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 37 |  | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 38 | constexpr size_t kNumMaps = 2000; | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 39 |  | 
 | 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 |  | 
 | 69 | static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 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); | 
| Elliott Hughes | 95c34a7 | 2018-08-22 12:06:20 -0700 | [diff] [blame] | 92 | #if defined(PR_SET_VMA) | 
 | 93 |       if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") == -1) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 94 |         fprintf(stderr, "Failed: %s\n", strerror(errno)); | 
 | 95 |       } | 
| Elliott Hughes | 95c34a7 | 2018-08-22 12:06:20 -0700 | [diff] [blame] | 96 | #endif | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 97 |       maps.push_back(memory); | 
 | 98 |     } | 
 | 99 |  | 
 | 100 |     if (!CountMaps(getpid(), &num_maps)) { | 
 | 101 |       exit(1); | 
 | 102 |     } | 
 | 103 |  | 
| Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 104 |     if (num_maps < kNumMaps) { | 
 | 105 |       fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected at least.\n", num_maps, | 
 | 106 |               kNumMaps); | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 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++) { | 
| Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 123 |     if (CountMaps(pid, &num_maps) && num_maps >= kNumMaps) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 124 |       break; | 
 | 125 |     } | 
 | 126 |     usleep(1000); | 
 | 127 |   } | 
| Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 128 |   if (num_maps < kNumMaps) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 129 |     fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps); | 
 | 130 |     return; | 
 | 131 |   } | 
 | 132 |  | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 133 |   while (state.KeepRunning()) { | 
| Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 134 |     BacktraceMap* map = map_func(pid, false); | 
 | 135 |     if (map == nullptr) { | 
 | 136 |       fprintf(stderr, "Failed to create map\n"); | 
 | 137 |       return; | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 138 |     } | 
| Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 139 |     delete map; | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 140 |   } | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 141 |  | 
 | 142 |   kill(pid, SIGKILL); | 
 | 143 |   waitpid(pid, nullptr, 0); | 
 | 144 | } | 
 | 145 |  | 
 | 146 | static void BM_create_map(benchmark::State& state) { | 
 | 147 |   CreateMap(state, BacktraceMap::Create); | 
 | 148 | } | 
| Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 149 | BENCHMARK(BM_create_map); | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 150 |  | 
| Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 151 | using BacktraceCreateFn = decltype(Backtrace::Create); | 
 | 152 |  | 
 | 153 | static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) { | 
 | 154 |   while (state.KeepRunning()) { | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 155 |     std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map)); | 
| Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 156 |     backtrace->Unwind(0); | 
 | 157 |   } | 
 | 158 | } | 
 | 159 |  | 
 | 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 |  | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 166 | BENCHMARK_MAIN(); |