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 | |
| 38 | // Definitions of prctl arguments to set a vma name in Android kernels. |
| 39 | #define ANDROID_PR_SET_VMA 0x53564d41 |
| 40 | #define ANDROID_PR_SET_VMA_ANON_NAME 0 |
| 41 | |
| 42 | constexpr size_t kNumMaps = 2000; |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 43 | |
| 44 | static bool CountMaps(pid_t pid, size_t* num_maps) { |
| 45 | // Minimize the calls that might allocate memory. If too much memory |
| 46 | // gets allocated, then this routine will add extra maps and the next |
| 47 | // call will fail to get the same number of maps as before. |
| 48 | int fd = |
| 49 | open((std::string("/proc/") + std::to_string(pid) + "/maps").c_str(), O_RDONLY | O_CLOEXEC); |
| 50 | if (fd == -1) { |
| 51 | fprintf(stderr, "Cannot open map file for pid %d: %s\n", pid, strerror(errno)); |
| 52 | return false; |
| 53 | } |
| 54 | *num_maps = 0; |
| 55 | while (true) { |
| 56 | char buffer[2048]; |
| 57 | ssize_t bytes = read(fd, buffer, sizeof(buffer)); |
| 58 | if (bytes <= 0) { |
| 59 | break; |
| 60 | } |
| 61 | // Count the '\n'. |
| 62 | for (size_t i = 0; i < static_cast<size_t>(bytes); i++) { |
| 63 | if (buffer[i] == '\n') { |
| 64 | ++*num_maps; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | close(fd); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) { |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 74 | // Create a remote process so that the map data is exactly the same. |
| 75 | // Also, so that we can create a set number of maps. |
| 76 | pid_t pid; |
| 77 | if ((pid = fork()) == 0) { |
| 78 | size_t num_maps; |
| 79 | if (!CountMaps(getpid(), &num_maps)) { |
| 80 | exit(1); |
| 81 | } |
| 82 | // Create uniquely named maps. |
| 83 | std::vector<void*> maps; |
| 84 | for (size_t i = num_maps; i < kNumMaps; i++) { |
| 85 | int flags = PROT_READ | PROT_WRITE; |
| 86 | // Alternate page type to make sure a map entry is added for each call. |
| 87 | if ((i % 2) == 0) { |
| 88 | flags |= PROT_EXEC; |
| 89 | } |
| 90 | void* memory = mmap(nullptr, PAGE_SIZE, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 91 | if (memory == MAP_FAILED) { |
| 92 | fprintf(stderr, "Failed to create map: %s\n", strerror(errno)); |
| 93 | exit(1); |
| 94 | } |
| 95 | memset(memory, 0x1, PAGE_SIZE); |
| 96 | if (prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") == |
| 97 | -1) { |
| 98 | fprintf(stderr, "Failed: %s\n", strerror(errno)); |
| 99 | } |
| 100 | maps.push_back(memory); |
| 101 | } |
| 102 | |
| 103 | if (!CountMaps(getpid(), &num_maps)) { |
| 104 | exit(1); |
| 105 | } |
| 106 | |
Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 107 | if (num_maps < kNumMaps) { |
| 108 | fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected at least.\n", num_maps, |
| 109 | kNumMaps); |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 110 | std::string str; |
| 111 | android::base::ReadFileToString("/proc/self/maps", &str); |
| 112 | fprintf(stderr, "%s\n", str.c_str()); |
| 113 | exit(1); |
| 114 | } |
| 115 | |
| 116 | // Wait for an hour at most. |
| 117 | sleep(3600); |
| 118 | exit(1); |
| 119 | } else if (pid < 0) { |
| 120 | fprintf(stderr, "Fork failed: %s\n", strerror(errno)); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | size_t num_maps = 0; |
| 125 | for (size_t i = 0; i < 2000; i++) { |
Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 126 | if (CountMaps(pid, &num_maps) && num_maps >= kNumMaps) { |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | usleep(1000); |
| 130 | } |
Christopher Ferris | 88b48f7 | 2017-12-08 12:19:44 -0800 | [diff] [blame] | 131 | if (num_maps < kNumMaps) { |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 132 | fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps); |
| 133 | return; |
| 134 | } |
| 135 | |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 136 | while (state.KeepRunning()) { |
Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 137 | BacktraceMap* map = map_func(pid, false); |
| 138 | if (map == nullptr) { |
| 139 | fprintf(stderr, "Failed to create map\n"); |
| 140 | return; |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 141 | } |
Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 142 | delete map; |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 143 | } |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 144 | |
| 145 | kill(pid, SIGKILL); |
| 146 | waitpid(pid, nullptr, 0); |
| 147 | } |
| 148 | |
| 149 | static void BM_create_map(benchmark::State& state) { |
| 150 | CreateMap(state, BacktraceMap::Create); |
| 151 | } |
Josh Gao | 3b094c1 | 2017-10-26 13:57:40 -0700 | [diff] [blame] | 152 | BENCHMARK(BM_create_map); |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 153 | |
Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 154 | using BacktraceCreateFn = decltype(Backtrace::Create); |
| 155 | |
| 156 | static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) { |
| 157 | while (state.KeepRunning()) { |
Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame^] | 158 | std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map)); |
Josh Gao | b9670bb | 2017-10-25 15:12:24 -0700 | [diff] [blame] | 159 | backtrace->Unwind(0); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void BM_create_backtrace(benchmark::State& state) { |
| 164 | std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid())); |
| 165 | CreateBacktrace(state, backtrace_map.get(), Backtrace::Create); |
| 166 | } |
| 167 | BENCHMARK(BM_create_backtrace); |
| 168 | |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 169 | BENCHMARK_MAIN(); |