• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "android/net/metrics/INetdEventListener.h"
38 
39 using android::base::StringPrintf;
40 using android::net::metrics::INetdEventListener;
41 
bindAndListen(int s)42 static 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 
ipv4_loopback(benchmark::State & state,const bool waitBetweenRuns)59 static 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 (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 
ipv6_loopback(benchmark::State & state,const bool waitBetweenRuns)116 static 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 (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 
run_at_reporting_level(decltype(ipv4_loopback) benchmarkFunction,::benchmark::State & state,const int reportingLevel,const bool waitBetweenRuns)173 static void run_at_reporting_level(decltype(ipv4_loopback) benchmarkFunction,
174                                    ::benchmark::State& state, const int reportingLevel,
175                                    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) {
195             case INetdEventListener::REPORTING_LEVEL_NONE:
196                 setenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT, "", 1);
197                 break;
198             case INetdEventListener::REPORTING_LEVEL_METRICS:
199                 unsetenv(FwmarkClient::ANDROID_NO_USE_FWMARK_CLIENT);
200                 setenv(FwmarkClient::ANDROID_FWMARK_METRICS_ONLY, "", 1);
201                 break;
202             case INetdEventListener::REPORTING_LEVEL_FULL:
203                 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 
224 constexpr int MIN_THREADS = 1;
225 constexpr int MAX_THREADS = 1;
226 constexpr double MIN_TIME = 0.5 /* seconds */;
227 
ipv4_metrics_reporting_no_fwmark(::benchmark::State & state)228 static void ipv4_metrics_reporting_no_fwmark(::benchmark::State& state) {
229     run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_NONE, true);
230 }
231 BENCHMARK(ipv4_metrics_reporting_no_fwmark)->MinTime(MIN_TIME)->UseManualTime();
232 
233 // IPv4 metrics under low load
ipv4_metrics_reporting_no_load(::benchmark::State & state)234 static void ipv4_metrics_reporting_no_load(::benchmark::State& state) {
235     run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS, true);
236 }
237 BENCHMARK(ipv4_metrics_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
238 
ipv4_full_reporting_no_load(::benchmark::State & state)239 static void ipv4_full_reporting_no_load(::benchmark::State& state) {
240     run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, true);
241 }
242 BENCHMARK(ipv4_full_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
243 
244 // IPv4 benchmarks under high load
ipv4_metrics_reporting_high_load(::benchmark::State & state)245 static void ipv4_metrics_reporting_high_load(::benchmark::State& state) {
246     run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS,
247             false);
248 }
249 BENCHMARK(ipv4_metrics_reporting_high_load)
250     ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
251 
ipv4_full_reporting_high_load(::benchmark::State & state)252 static void ipv4_full_reporting_high_load(::benchmark::State& state) {
253     run_at_reporting_level(ipv4_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, false);
254 }
255 BENCHMARK(ipv4_full_reporting_high_load)
256     ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
257 
258 // IPv6 raw connect() without using fwmark
ipv6_metrics_reporting_no_fwmark(::benchmark::State & state)259 static void ipv6_metrics_reporting_no_fwmark(::benchmark::State& state) {
260     run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_NONE, true);
261 }
262 BENCHMARK(ipv6_metrics_reporting_no_fwmark)->MinTime(MIN_TIME)->UseManualTime();
263 
264 // IPv6 metrics under low load
ipv6_metrics_reporting_no_load(::benchmark::State & state)265 static void ipv6_metrics_reporting_no_load(::benchmark::State& state) {
266     run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS, true);
267 }
268 BENCHMARK(ipv6_metrics_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
269 
ipv6_full_reporting_no_load(::benchmark::State & state)270 static void ipv6_full_reporting_no_load(::benchmark::State& state) {
271     run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, true);
272 }
273 BENCHMARK(ipv6_full_reporting_no_load)->MinTime(MIN_TIME)->UseManualTime();
274 
275 // IPv6 benchmarks under high load
ipv6_metrics_reporting_high_load(::benchmark::State & state)276 static void ipv6_metrics_reporting_high_load(::benchmark::State& state) {
277     run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_METRICS,
278             false);
279 }
280 BENCHMARK(ipv6_metrics_reporting_high_load)
281     ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
282 
ipv6_full_reporting_high_load(::benchmark::State & state)283 static void ipv6_full_reporting_high_load(::benchmark::State& state) {
284     run_at_reporting_level(ipv6_loopback, state, INetdEventListener::REPORTING_LEVEL_FULL, false);
285 }
286 BENCHMARK(ipv6_full_reporting_high_load)
287     ->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
288