blob: ec6eeec554adae86b28429d2fdf94a9d1c165c64 [file] [log] [blame]
Robin Lee2cf56172016-09-13 18:55:42 +09001/*
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"
Michal Karpinskid46aa712016-10-13 10:04:36 +010037#include "android/net/metrics/INetdEventListener.h"
Robin Lee2cf56172016-09-13 18:55:42 +090038
39using android::base::StringPrintf;
Michal Karpinskid46aa712016-10-13 10:04:36 +010040using android::net::metrics::INetdEventListener;
Robin Lee2cf56172016-09-13 18:55:42 +090041
42static int bindAndListen(int s) {
43 sockaddr_in6 sin6 = { .sin6_family = AF_INET6 };
44 if (bind(s, (sockaddr*) &sin6, sizeof(sin6)) == 0) {
45 if (listen(s, 1)) {
46 return -1;
47 }
48 sockaddr_in sin = {};
49 socklen_t len = sizeof(sin);
50 if (getsockname(s, (sockaddr*) &sin, &len)) {
51 return -1;
52 }
53 return ntohs(sin.sin_port);
54 } else {
55 return -1;
56 }
57}
58
59static void ipv4_loopback(benchmark::State& state, const bool waitBetweenRuns) {
60 const int listensocket = socket(AF_INET6, SOCK_STREAM, 0);
61 const int port = bindAndListen(listensocket);
62 if (port == -1) {
63 state.SkipWithError("Unable to bind server socket");
64 return;
65 }
66
67 // ALOGW("Listening on port = %d", port);
68 std::vector<uint64_t> latencies(state.max_iterations);
69 uint64_t iterations = 0;
70
71 while (state.KeepRunning()) {
72 int sock = socket(AF_INET, SOCK_STREAM, 0);
73 if (sock < 0) {
74 state.SkipWithError(StringPrintf("socket() failed with errno=%d", errno).c_str());
75 break;
76 }
77
78 const Stopwatch stopwatch;
79
80 sockaddr_in server = { .sin_family = AF_INET, .sin_port = htons(port) };
81 if (auto ret = connect(sock, (sockaddr*) &server, sizeof(server))) {
82 state.SkipWithError(StringPrintf("connect() failed with errno=%d", errno).c_str());
83 close(sock);
84 break;
85 }
86
87 if (waitBetweenRuns) {
88 latencies[iterations] = stopwatch.timeTaken() * 1e6L;
89 state.SetIterationTime(latencies[iterations] / 1e9L);
90 std::this_thread::sleep_for(std::chrono::milliseconds(10));
91 ++iterations;
92 }
93
94 sockaddr_in6 client;
95 socklen_t clientlen = sizeof(client);
96 int accepted = accept(listensocket, (sockaddr *) &client, &clientlen);
97 if (accepted < 0) {
98 state.SkipWithError(StringPrintf("accept() failed with errno=%d", errno).c_str());
99 close(sock);
100 break;
101 }
102
103 close(accepted);
104 close(sock);
105 }
106 close(listensocket);
107 // ALOGI("Finished test on port = %d", port);
108
109 if (iterations > 0) {
110 latencies.resize(iterations);
111 sort(latencies.begin(), latencies.end());
112 state.SetLabel(StringPrintf("%lld", (long long) latencies[iterations * 9 / 10]));
113 }
114}
115
116static void ipv6_loopback(benchmark::State& state, const bool waitBetweenRuns) {
117 const int listensocket = socket(AF_INET6, SOCK_STREAM, 0);
118 const int port = bindAndListen(listensocket);
119 if (port == -1) {
120 state.SkipWithError("Unable to bind server socket");
121 return;
122 }
123
124 // ALOGW("Listening on port = %d", port);
125 std::vector<uint64_t> latencies(state.max_iterations);
126 uint64_t iterations = 0;
127
128 while (state.KeepRunning()) {
129 int sock = socket(AF_INET6, SOCK_STREAM, 0);
130 if (sock < 0) {
131 state.SkipWithError(StringPrintf("socket() failed with errno=%d", errno).c_str());
132 break;
133 }
134
135 const Stopwatch stopwatch;
136
137 sockaddr_in6 server = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
138 if (auto ret = connect(sock, (sockaddr*) &server, sizeof(server))) {
139 state.SkipWithError(StringPrintf("connect() failed with errno=%d", errno).c_str());
140 close(sock);
141 break;
142 }
143
144 if (waitBetweenRuns) {
145 latencies[iterations] = stopwatch.timeTaken() * 1e6L;
146 state.SetIterationTime(latencies[iterations] / 1e9L);
147 std::this_thread::sleep_for(std::chrono::milliseconds(10));
148 ++iterations;
149 }
150
151 sockaddr_in6 client;
152 socklen_t clientlen = sizeof(client);
153 int accepted = accept(listensocket, (sockaddr *) &client, &clientlen);
154 if (accepted < 0) {
155 state.SkipWithError(StringPrintf("accept() failed with errno=%d", errno).c_str());
156 close(sock);
157 break;
158 }
159
160 close(accepted);
161 close(sock);
162 }
163 close(listensocket);
164 // ALOGI("Finished test on port = %d", port);
165
166 if (iterations > 0) {
167 latencies.resize(iterations);
168 sort(latencies.begin(), latencies.end());
169 state.SetLabel(StringPrintf("%lld", (long long) latencies[iterations * 9 / 10]));
170 }
171}
172
173static void run_at_reporting_level(decltype(ipv4_loopback) benchmarkFunction,
Michal Karpinskid46aa712016-10-13 10:04:36 +0100174 ::benchmark::State& state, const int reportingLevel,
Robin Lee2cf56172016-09-13 18:55:42 +0900175 const bool waitBetweenRuns) {
176 // Our master thread (thread_index == 0) will control setup and teardown for other threads.
177 const bool isMaster = (state.thread_index == 0);
178
179 // Previous values of env variables used by fwmarkclient (only read/written by master thread)
180 const std::string savedSettings[] = {
181 FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT,
182 FwmarkClient::ANDROID_FWMARK_METRICS_ONLY
183 };
184 std::map<std::string, std::string> prevSettings;
185
186 // SETUP
187 if (isMaster) {
188 for (const auto setting : savedSettings) {
189 const char* prevEnvStr = getenv(setting.c_str());
190 if (prevEnvStr != nullptr) {
191 prevSettings[setting.c_str()] = prevEnvStr;
192 }
193 }
194 switch (reportingLevel) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100195 case INetdEventListener::REPORTING_LEVEL_NONE:
Robin Lee2cf56172016-09-13 18:55:42 +0900196 setenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT, "", 1);
197 break;
Michal Karpinskid46aa712016-10-13 10:04:36 +0100198 case INetdEventListener::REPORTING_LEVEL_METRICS:
Robin Lee2cf56172016-09-13 18:55:42 +0900199 unsetenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT);
200 setenv(FwmarkClient::ANDROID_FWMARK_METRICS_ONLY, "", 1);
201 break;
Michal Karpinskid46aa712016-10-13 10:04:36 +0100202 case INetdEventListener::REPORTING_LEVEL_FULL:
Robin Lee2cf56172016-09-13 18:55:42 +0900203 unsetenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT);
204 unsetenv(FwmarkClient::ANDROID_FWMARK_METRICS_ONLY);
205 break;
206 }
207 }
208
209 // TEST
210 benchmarkFunction(state, waitBetweenRuns);
211
212 // TEARDOWN
213 if (isMaster) {
214 for (const auto setting : savedSettings) {
215 if (prevSettings.count(setting)) {
216 setenv(setting.c_str(), prevSettings[setting].c_str(), 1);
217 } else {
218 unsetenv(setting.c_str());
219 }
220 }
221 }
222}
223
224constexpr int MIN_THREADS = 1;
225constexpr int MAX_THREADS = 1;
226constexpr double MIN_TIME = 0.5 /* seconds */;
227
228static void ipv4_metrics_reporting_no_fwmark(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100229 run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_NONE, true);
Robin Lee2cf56172016-09-13 18:55:42 +0900230}
231BENCHMARK(ipv4_metrics_reporting_no_fwmark)->MinTime(MIN_TIME)->UseManualTime();
232
233// IPv4 metrics under low load
234static void ipv4_metrics_reporting_no_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100235 run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS, true);
Robin Lee2cf56172016-09-13 18:55:42 +0900236}
237BENCHMARK(ipv4_metrics_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
238
Robin Lee2cf56172016-09-13 18:55:42 +0900239static void ipv4_full_reporting_no_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100240 run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, true);
Robin Lee2cf56172016-09-13 18:55:42 +0900241}
242BENCHMARK(ipv4_full_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
Robin Lee2cf56172016-09-13 18:55:42 +0900243
244// IPv4 benchmarks under high load
245static void ipv4_metrics_reporting_high_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100246 run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS,
247 false);
Robin Lee2cf56172016-09-13 18:55:42 +0900248}
249BENCHMARK(ipv4_metrics_reporting_high_load)
250 ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
251
Robin Lee2cf56172016-09-13 18:55:42 +0900252static void ipv4_full_reporting_high_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100253 run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, false);
Robin Lee2cf56172016-09-13 18:55:42 +0900254}
255BENCHMARK(ipv4_full_reporting_high_load)
256 ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
Robin Lee2cf56172016-09-13 18:55:42 +0900257
258// IPv6 raw connect() without using fwmark
259static void ipv6_metrics_reporting_no_fwmark(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100260 run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_NONE, true);
Robin Lee2cf56172016-09-13 18:55:42 +0900261}
262BENCHMARK(ipv6_metrics_reporting_no_fwmark)->MinTime(MIN_TIME)->UseManualTime();
263
264// IPv6 metrics under low load
265static void ipv6_metrics_reporting_no_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100266 run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS, true);
Robin Lee2cf56172016-09-13 18:55:42 +0900267}
268BENCHMARK(ipv6_metrics_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
269
Robin Lee2cf56172016-09-13 18:55:42 +0900270static void ipv6_full_reporting_no_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100271 run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, true);
Robin Lee2cf56172016-09-13 18:55:42 +0900272}
273BENCHMARK(ipv6_full_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
Robin Lee2cf56172016-09-13 18:55:42 +0900274
275// IPv6 benchmarks under high load
276static void ipv6_metrics_reporting_high_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100277 run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS,
278 false);
Robin Lee2cf56172016-09-13 18:55:42 +0900279}
280BENCHMARK(ipv6_metrics_reporting_high_load)
281 ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
282
Robin Lee2cf56172016-09-13 18:55:42 +0900283static void ipv6_full_reporting_high_load(::benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100284 run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, false);
Robin Lee2cf56172016-09-13 18:55:42 +0900285}
286BENCHMARK(ipv6_full_reporting_high_load)
287 ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();