Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "connect_benchmark" |
| 18 | |
| 19 | #include <arpa/inet.h> |
| 20 | #include <cutils/sockets.h> |
| 21 | #include <errno.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | #include <map> |
| 26 | #include <functional> |
| 27 | #include <thread> |
| 28 | |
| 29 | #include <android-base/stringprintf.h> |
| 30 | #include <benchmark/benchmark.h> |
| 31 | #include <log/log.h> |
| 32 | #include <utils/StrongPointer.h> |
| 33 | |
| 34 | #include "FwmarkClient.h" |
| 35 | #include "SockDiag.h" |
| 36 | #include "Stopwatch.h" |
| 37 | |
| 38 | using android::base::StringPrintf; |
| 39 | |
| 40 | enum ReportingLevel { |
| 41 | NONE, |
| 42 | METRICS, |
| 43 | FULL |
| 44 | }; |
| 45 | |
| 46 | static int bindAndListen(int s) { |
| 47 | sockaddr_in6 sin6 = { .sin6_family = AF_INET6 }; |
| 48 | if (bind(s, (sockaddr*) &sin6, sizeof(sin6)) == 0) { |
| 49 | if (listen(s, 1)) { |
| 50 | return -1; |
| 51 | } |
| 52 | sockaddr_in sin = {}; |
| 53 | socklen_t len = sizeof(sin); |
| 54 | if (getsockname(s, (sockaddr*) &sin, &len)) { |
| 55 | return -1; |
| 56 | } |
| 57 | return ntohs(sin.sin_port); |
| 58 | } else { |
| 59 | return -1; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void ipv4_loopback(benchmark::State& state, const bool waitBetweenRuns) { |
| 64 | const int listensocket = socket(AF_INET6, SOCK_STREAM, 0); |
| 65 | const int port = bindAndListen(listensocket); |
| 66 | if (port == -1) { |
| 67 | state.SkipWithError("Unable to bind server socket"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // ALOGW("Listening on port = %d", port); |
| 72 | std::vector<uint64_t> latencies(state.max_iterations); |
| 73 | uint64_t iterations = 0; |
| 74 | |
| 75 | while (state.KeepRunning()) { |
| 76 | int sock = socket(AF_INET, SOCK_STREAM, 0); |
| 77 | if (sock < 0) { |
| 78 | state.SkipWithError(StringPrintf("socket() failed with errno=%d", errno).c_str()); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | const Stopwatch stopwatch; |
| 83 | |
| 84 | sockaddr_in server = { .sin_family = AF_INET, .sin_port = htons(port) }; |
| 85 | if (auto ret = connect(sock, (sockaddr*) &server, sizeof(server))) { |
| 86 | state.SkipWithError(StringPrintf("connect() failed with errno=%d", errno).c_str()); |
| 87 | close(sock); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if (waitBetweenRuns) { |
| 92 | latencies[iterations] = stopwatch.timeTaken() * 1e6L; |
| 93 | state.SetIterationTime(latencies[iterations] / 1e9L); |
| 94 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 95 | ++iterations; |
| 96 | } |
| 97 | |
| 98 | sockaddr_in6 client; |
| 99 | socklen_t clientlen = sizeof(client); |
| 100 | int accepted = accept(listensocket, (sockaddr *) &client, &clientlen); |
| 101 | if (accepted < 0) { |
| 102 | state.SkipWithError(StringPrintf("accept() failed with errno=%d", errno).c_str()); |
| 103 | close(sock); |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | close(accepted); |
| 108 | close(sock); |
| 109 | } |
| 110 | close(listensocket); |
| 111 | // ALOGI("Finished test on port = %d", port); |
| 112 | |
| 113 | if (iterations > 0) { |
| 114 | latencies.resize(iterations); |
| 115 | sort(latencies.begin(), latencies.end()); |
| 116 | state.SetLabel(StringPrintf("%lld", (long long) latencies[iterations * 9 / 10])); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void ipv6_loopback(benchmark::State& state, const bool waitBetweenRuns) { |
| 121 | const int listensocket = socket(AF_INET6, SOCK_STREAM, 0); |
| 122 | const int port = bindAndListen(listensocket); |
| 123 | if (port == -1) { |
| 124 | state.SkipWithError("Unable to bind server socket"); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // ALOGW("Listening on port = %d", port); |
| 129 | std::vector<uint64_t> latencies(state.max_iterations); |
| 130 | uint64_t iterations = 0; |
| 131 | |
| 132 | while (state.KeepRunning()) { |
| 133 | int sock = socket(AF_INET6, SOCK_STREAM, 0); |
| 134 | if (sock < 0) { |
| 135 | state.SkipWithError(StringPrintf("socket() failed with errno=%d", errno).c_str()); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | const Stopwatch stopwatch; |
| 140 | |
| 141 | sockaddr_in6 server = { .sin6_family = AF_INET6, .sin6_port = htons(port) }; |
| 142 | if (auto ret = connect(sock, (sockaddr*) &server, sizeof(server))) { |
| 143 | state.SkipWithError(StringPrintf("connect() failed with errno=%d", errno).c_str()); |
| 144 | close(sock); |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (waitBetweenRuns) { |
| 149 | latencies[iterations] = stopwatch.timeTaken() * 1e6L; |
| 150 | state.SetIterationTime(latencies[iterations] / 1e9L); |
| 151 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 152 | ++iterations; |
| 153 | } |
| 154 | |
| 155 | sockaddr_in6 client; |
| 156 | socklen_t clientlen = sizeof(client); |
| 157 | int accepted = accept(listensocket, (sockaddr *) &client, &clientlen); |
| 158 | if (accepted < 0) { |
| 159 | state.SkipWithError(StringPrintf("accept() failed with errno=%d", errno).c_str()); |
| 160 | close(sock); |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | close(accepted); |
| 165 | close(sock); |
| 166 | } |
| 167 | close(listensocket); |
| 168 | // ALOGI("Finished test on port = %d", port); |
| 169 | |
| 170 | if (iterations > 0) { |
| 171 | latencies.resize(iterations); |
| 172 | sort(latencies.begin(), latencies.end()); |
| 173 | state.SetLabel(StringPrintf("%lld", (long long) latencies[iterations * 9 / 10])); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void run_at_reporting_level(decltype(ipv4_loopback) benchmarkFunction, |
| 178 | ::benchmark::State& state, const ReportingLevel reportingLevel, |
| 179 | const bool waitBetweenRuns) { |
| 180 | // Our master thread (thread_index == 0) will control setup and teardown for other threads. |
| 181 | const bool isMaster = (state.thread_index == 0); |
| 182 | |
| 183 | // Previous values of env variables used by fwmarkclient (only read/written by master thread) |
| 184 | const std::string savedSettings[] = { |
| 185 | FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT, |
| 186 | FwmarkClient::ANDROID_FWMARK_METRICS_ONLY |
| 187 | }; |
| 188 | std::map<std::string, std::string> prevSettings; |
| 189 | |
| 190 | // SETUP |
| 191 | if (isMaster) { |
| 192 | for (const auto setting : savedSettings) { |
| 193 | const char* prevEnvStr = getenv(setting.c_str()); |
| 194 | if (prevEnvStr != nullptr) { |
| 195 | prevSettings[setting.c_str()] = prevEnvStr; |
| 196 | } |
| 197 | } |
| 198 | switch (reportingLevel) { |
| 199 | case NONE: |
| 200 | setenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT, "", 1); |
| 201 | break; |
| 202 | case METRICS: |
| 203 | unsetenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT); |
| 204 | setenv(FwmarkClient::ANDROID_FWMARK_METRICS_ONLY, "", 1); |
| 205 | break; |
| 206 | case FULL: |
| 207 | unsetenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT); |
| 208 | unsetenv(FwmarkClient::ANDROID_FWMARK_METRICS_ONLY); |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // TEST |
| 214 | benchmarkFunction(state, waitBetweenRuns); |
| 215 | |
| 216 | // TEARDOWN |
| 217 | if (isMaster) { |
| 218 | for (const auto setting : savedSettings) { |
| 219 | if (prevSettings.count(setting)) { |
| 220 | setenv(setting.c_str(), prevSettings[setting].c_str(), 1); |
| 221 | } else { |
| 222 | unsetenv(setting.c_str()); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | constexpr int MIN_THREADS = 1; |
| 229 | constexpr int MAX_THREADS = 1; |
| 230 | constexpr double MIN_TIME = 0.5 /* seconds */; |
| 231 | |
| 232 | static void ipv4_metrics_reporting_no_fwmark(::benchmark::State& state) { |
| 233 | run_at_reporting_level(ipv4_loopback, state, NONE, true); |
| 234 | } |
| 235 | BENCHMARK(ipv4_metrics_reporting_no_fwmark)->MinTime(MIN_TIME)->UseManualTime(); |
| 236 | |
| 237 | // IPv4 metrics under low load |
| 238 | static void ipv4_metrics_reporting_no_load(::benchmark::State& state) { |
| 239 | run_at_reporting_level(ipv4_loopback, state, METRICS, true); |
| 240 | } |
| 241 | BENCHMARK(ipv4_metrics_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime(); |
| 242 | |
| 243 | /* |
| 244 | // TODO: uncomment once full reporting is available. |
| 245 | static void ipv4_full_reporting_no_load(::benchmark::State& state) { |
| 246 | run_at_reporting_level(ipv4_loopback, state, FULL, true); |
| 247 | } |
| 248 | BENCHMARK(ipv4_full_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime(); |
| 249 | */ |
| 250 | |
| 251 | // IPv4 benchmarks under high load |
| 252 | static void ipv4_metrics_reporting_high_load(::benchmark::State& state) { |
| 253 | run_at_reporting_level(ipv4_loopback, state, METRICS, false); |
| 254 | } |
| 255 | BENCHMARK(ipv4_metrics_reporting_high_load) |
| 256 | ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime(); |
| 257 | |
| 258 | /* |
| 259 | // TODO: uncomment once full reporting is available. |
| 260 | static void ipv4_full_reporting_high_load(::benchmark::State& state) { |
| 261 | run_at_reporting_level(ipv4_loopback, state, FULL, false); |
| 262 | } |
| 263 | BENCHMARK(ipv4_full_reporting_high_load) |
| 264 | ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime(); |
| 265 | */ |
| 266 | |
| 267 | // IPv6 raw connect() without using fwmark |
| 268 | static void ipv6_metrics_reporting_no_fwmark(::benchmark::State& state) { |
| 269 | run_at_reporting_level(ipv6_loopback, state, NONE, true); |
| 270 | } |
| 271 | BENCHMARK(ipv6_metrics_reporting_no_fwmark)->MinTime(MIN_TIME)->UseManualTime(); |
| 272 | |
| 273 | // IPv6 metrics under low load |
| 274 | static void ipv6_metrics_reporting_no_load(::benchmark::State& state) { |
| 275 | run_at_reporting_level(ipv6_loopback, state, METRICS, true); |
| 276 | } |
| 277 | BENCHMARK(ipv6_metrics_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime(); |
| 278 | |
| 279 | /* |
| 280 | // TODO: uncomment once full reporting is available. |
| 281 | static void ipv6_full_reporting_no_load(::benchmark::State& state) { |
| 282 | run_at_reporting_level(ipv6_loopback, state, FULL, true); |
| 283 | } |
| 284 | BENCHMARK(ipv6_full_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime(); |
| 285 | */ |
| 286 | |
| 287 | // IPv6 benchmarks under high load |
| 288 | static void ipv6_metrics_reporting_high_load(::benchmark::State& state) { |
| 289 | run_at_reporting_level(ipv6_loopback, state, METRICS, false); |
| 290 | } |
| 291 | BENCHMARK(ipv6_metrics_reporting_high_load) |
| 292 | ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime(); |
| 293 | |
| 294 | /* |
| 295 | // TODO: uncomment once full reporting is available. |
| 296 | static void ipv6_full_reporting_high_load(::benchmark::State& state) { |
| 297 | run_at_reporting_level(ipv6_loopback, state, FULL, false); |
| 298 | } |
| 299 | BENCHMARK(ipv6_full_reporting_high_load) |
| 300 | ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime(); |
| 301 | */ |