| 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> | 
|  | 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 |  | 
|  | 40 | constexpr size_t kNumMaps = 2000; | 
|  | 41 | constexpr size_t kNumIterations = 1000; | 
|  | 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)) { | 
|  | 73 | state.PauseTiming(); | 
|  | 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 |  | 
|  | 107 | if (num_maps != kNumMaps) { | 
|  | 108 | fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected.\n", num_maps, kNumMaps); | 
|  | 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++) { | 
|  | 125 | if (CountMaps(pid, &num_maps) && num_maps == kNumMaps) { | 
|  | 126 | break; | 
|  | 127 | } | 
|  | 128 | usleep(1000); | 
|  | 129 | } | 
|  | 130 | if (num_maps != kNumMaps) { | 
|  | 131 | fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps); | 
|  | 132 | return; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | state.ResumeTiming(); | 
|  | 136 | while (state.KeepRunning()) { | 
|  | 137 | for (size_t i = 0; i < static_cast<size_t>(state.range(0)); i++) { | 
|  | 138 | BacktraceMap* map = map_func(pid, false); | 
|  | 139 | if (map == nullptr) { | 
|  | 140 | fprintf(stderr, "Failed to create map\n"); | 
|  | 141 | return; | 
|  | 142 | } | 
|  | 143 | delete map; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | state.PauseTiming(); | 
|  | 147 |  | 
|  | 148 | kill(pid, SIGKILL); | 
|  | 149 | waitpid(pid, nullptr, 0); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | static void BM_create_map(benchmark::State& state) { | 
|  | 153 | CreateMap(state, BacktraceMap::Create); | 
|  | 154 | } | 
|  | 155 | BENCHMARK(BM_create_map)->Arg(kNumIterations); | 
|  | 156 |  | 
|  | 157 | static void BM_create_map_new(benchmark::State& state) { | 
|  | 158 | CreateMap(state, BacktraceMap::CreateNew); | 
|  | 159 | } | 
|  | 160 | BENCHMARK(BM_create_map_new)->Arg(kNumIterations); | 
|  | 161 |  | 
|  | 162 | BENCHMARK_MAIN(); |