| 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> | 
 | 30 |  | 
 | 31 | #include <benchmark/benchmark.h> | 
 | 32 |  | 
 | 33 | #include <backtrace/Backtrace.h> | 
 | 34 | #include <backtrace/BacktraceMap.h> | 
| Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 35 | #include <unwindstack/Memory.h> | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 36 |  | 
 | 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 |  | 
 | 41 | constexpr size_t kNumMaps = 2000; | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 42 |  | 
 | 43 | static 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 |  | 
 | 72 | static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 73 |   // 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 |  | 
| Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 106 |     if (num_maps < kNumMaps) { | 
 | 107 |       fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected at least.\n", num_maps, | 
 | 108 |               kNumMaps); | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 109 |       std::string str; | 
 | 110 |       android::base::ReadFileToString("/proc/self/maps", &str); | 
 | 111 |       fprintf(stderr, "%s\n", str.c_str()); | 
 | 112 |       exit(1); | 
 | 113 |     } | 
 | 114 |  | 
 | 115 |     // Wait for an hour at most. | 
 | 116 |     sleep(3600); | 
 | 117 |     exit(1); | 
 | 118 |   } else if (pid < 0) { | 
 | 119 |     fprintf(stderr, "Fork failed: %s\n", strerror(errno)); | 
 | 120 |     return; | 
 | 121 |   } | 
 | 122 |  | 
 | 123 |   size_t num_maps = 0; | 
 | 124 |   for (size_t i = 0; i < 2000; i++) { | 
| Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 125 |     if (CountMaps(pid, &num_maps) && num_maps >= kNumMaps) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 126 |       break; | 
 | 127 |     } | 
 | 128 |     usleep(1000); | 
 | 129 |   } | 
| Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 130 |   if (num_maps < kNumMaps) { | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 131 |     fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps); | 
 | 132 |     return; | 
 | 133 |   } | 
 | 134 |  | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 135 |   while (state.KeepRunning()) { | 
| Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 136 |     BacktraceMap* map = map_func(pid, false); | 
 | 137 |     if (map == nullptr) { | 
 | 138 |       fprintf(stderr, "Failed to create map\n"); | 
 | 139 |       return; | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 140 |     } | 
| Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 141 |     delete map; | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 142 |   } | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 143 |  | 
 | 144 |   kill(pid, SIGKILL); | 
 | 145 |   waitpid(pid, nullptr, 0); | 
 | 146 | } | 
 | 147 |  | 
 | 148 | static void BM_create_map(benchmark::State& state) { | 
 | 149 |   CreateMap(state, BacktraceMap::Create); | 
 | 150 | } | 
| Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 151 | BENCHMARK(BM_create_map); | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 152 |  | 
| Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 153 | using BacktraceCreateFn = decltype(Backtrace::Create); | 
 | 154 |  | 
 | 155 | static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) { | 
 | 156 |   while (state.KeepRunning()) { | 
 | 157 |     std::unique_ptr<Backtrace> backtrace(fn(getpid(), gettid(), map)); | 
 | 158 |     backtrace->Unwind(0); | 
 | 159 |   } | 
 | 160 | } | 
 | 161 |  | 
 | 162 | static void BM_create_backtrace(benchmark::State& state) { | 
 | 163 |   std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid())); | 
 | 164 |   CreateBacktrace(state, backtrace_map.get(), Backtrace::Create); | 
 | 165 | } | 
 | 166 | BENCHMARK(BM_create_backtrace); | 
 | 167 |  | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 168 | BENCHMARK_MAIN(); |