blob: 0477d503950f24c687949846daf6d09fc3b5bd72 [file] [log] [blame]
Steven Morelandcda60852021-04-14 23:45:32 +00001/*
2 * Copyright (C) 2021 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 <BnBinderRpcBenchmark.h>
18#include <android-base/logging.h>
19#include <benchmark/benchmark.h>
20#include <binder/Binder.h>
Steven Moreland1ebdc702021-07-28 18:46:15 -070021#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
23#include <binder/ProcessState.h>
Yifan Hongaecdd172021-09-21 14:53:13 -070024#include <binder/RpcCertificateFormat.h>
25#include <binder/RpcCertificateVerifier.h>
Steven Morelandcda60852021-04-14 23:45:32 +000026#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000027#include <binder/RpcSession.h>
Yifan Hongaecdd172021-09-21 14:53:13 -070028#include <binder/RpcTlsUtils.h>
29#include <binder/RpcTransportRaw.h>
30#include <binder/RpcTransportTls.h>
31#include <openssl/ssl.h>
Steven Morelandcda60852021-04-14 23:45:32 +000032
33#include <thread>
34
Steven Moreland656e9d12021-07-29 17:33:53 -070035#include <signal.h>
Steven Moreland1ebdc702021-07-28 18:46:15 -070036#include <sys/prctl.h>
Steven Morelandcda60852021-04-14 23:45:32 +000037#include <sys/types.h>
38#include <unistd.h>
39
Yifan Hongaecdd172021-09-21 14:53:13 -070040#include "RpcAuthTesting.h"
41
Steven Morelandcda60852021-04-14 23:45:32 +000042using android::BBinder;
Steven Moreland1ebdc702021-07-28 18:46:15 -070043using android::defaultServiceManager;
Steven Morelandcda60852021-04-14 23:45:32 +000044using android::IBinder;
45using android::interface_cast;
Steven Moreland1ebdc702021-07-28 18:46:15 -070046using android::IPCThreadState;
47using android::IServiceManager;
Steven Morelandcda60852021-04-14 23:45:32 +000048using android::OK;
Steven Moreland1ebdc702021-07-28 18:46:15 -070049using android::ProcessState;
Yifan Hongaecdd172021-09-21 14:53:13 -070050using android::RpcAuthPreSigned;
51using android::RpcCertificateFormat;
52using android::RpcCertificateVerifier;
Steven Morelandcda60852021-04-14 23:45:32 +000053using android::RpcServer;
Steven Morelandbdb53ab2021-05-05 17:57:41 +000054using android::RpcSession;
Yifan Hongaecdd172021-09-21 14:53:13 -070055using android::RpcTransportCtxFactory;
56using android::RpcTransportCtxFactoryRaw;
57using android::RpcTransportCtxFactoryTls;
Steven Morelandcda60852021-04-14 23:45:32 +000058using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070059using android::status_t;
60using android::statusToString;
Steven Moreland1ebdc702021-07-28 18:46:15 -070061using android::String16;
Steven Morelandcda60852021-04-14 23:45:32 +000062using android::binder::Status;
63
64class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
65 Status repeatString(const std::string& str, std::string* out) override {
66 *out = str;
67 return Status::ok();
68 }
Steven Morelandce240ce2021-08-05 13:02:37 -070069 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
70 *out = binder;
71 return Status::ok();
72 }
73 Status repeatBytes(const std::vector<uint8_t>& bytes, std::vector<uint8_t>* out) override {
74 *out = bytes;
Steven Morelandcda60852021-04-14 23:45:32 +000075 return Status::ok();
76 }
77};
78
Steven Moreland1ebdc702021-07-28 18:46:15 -070079enum Transport {
80 KERNEL,
81 RPC,
Yifan Hongaecdd172021-09-21 14:53:13 -070082 RPC_TLS,
Steven Moreland1ebdc702021-07-28 18:46:15 -070083};
Steven Morelandcda60852021-04-14 23:45:32 +000084
Steven Morelandce240ce2021-08-05 13:02:37 -070085static const std::initializer_list<int64_t> kTransportList = {
Steven Moreland1ebdc702021-07-28 18:46:15 -070086#ifdef __BIONIC__
Steven Morelandce240ce2021-08-05 13:02:37 -070087 Transport::KERNEL,
Steven Moreland1ebdc702021-07-28 18:46:15 -070088#endif
Yifan Hongaecdd172021-09-21 14:53:13 -070089 Transport::RPC,
90 Transport::RPC_TLS,
91};
92
93// Certificate validation happens during handshake and does not affect the result of benchmarks.
94// Skip certificate validation to simplify the setup process.
95class RpcCertificateVerifierNoOp : public RpcCertificateVerifier {
96public:
97 status_t verify(const SSL*, uint8_t*) override { return OK; }
98};
99
100std::unique_ptr<RpcTransportCtxFactory> makeFactoryTls() {
101 auto pkey = android::makeKeyPairForSelfSignedCert();
102 CHECK_NE(pkey.get(), nullptr);
103 auto cert = android::makeSelfSignedCert(pkey.get(), android::kCertValidSeconds);
104 CHECK_NE(cert.get(), nullptr);
105
106 auto verifier = std::make_shared<RpcCertificateVerifierNoOp>();
107 auto auth = std::make_unique<RpcAuthPreSigned>(std::move(pkey), std::move(cert));
108 return RpcTransportCtxFactoryTls::make(verifier, std::move(auth));
109}
Steven Moreland1ebdc702021-07-28 18:46:15 -0700110
111static sp<RpcSession> gSession = RpcSession::make();
Yifan Hongaecdd172021-09-21 14:53:13 -0700112static sp<RpcSession> gSessionTls = RpcSession::make(makeFactoryTls());
Steven Moreland1ebdc702021-07-28 18:46:15 -0700113#ifdef __BIONIC__
114static const String16 kKernelBinderInstance = String16(u"binderRpcBenchmark-control");
115static sp<IBinder> gKernelBinder;
116#endif
117
118static sp<IBinder> getBinderForOptions(benchmark::State& state) {
119 Transport transport = static_cast<Transport>(state.range(0));
120 switch (transport) {
121#ifdef __BIONIC__
122 case KERNEL:
123 return gKernelBinder;
124#endif
125 case RPC:
126 return gSession->getRootObject();
Yifan Hongaecdd172021-09-21 14:53:13 -0700127 case RPC_TLS:
128 return gSessionTls->getRootObject();
Steven Moreland1ebdc702021-07-28 18:46:15 -0700129 default:
130 LOG(FATAL) << "Unknown transport value: " << transport;
131 return nullptr;
Steven Morelandcda60852021-04-14 23:45:32 +0000132 }
133}
Steven Morelandcda60852021-04-14 23:45:32 +0000134
135void BM_pingTransaction(benchmark::State& state) {
Steven Moreland1ebdc702021-07-28 18:46:15 -0700136 sp<IBinder> binder = getBinderForOptions(state);
Steven Morelandcda60852021-04-14 23:45:32 +0000137
138 while (state.KeepRunning()) {
139 CHECK_EQ(OK, binder->pingBinder());
140 }
141}
Steven Morelandce240ce2021-08-05 13:02:37 -0700142BENCHMARK(BM_pingTransaction)->ArgsProduct({kTransportList});
Steven Morelandcda60852021-04-14 23:45:32 +0000143
Steven Morelandce240ce2021-08-05 13:02:37 -0700144void BM_repeatTwoPageString(benchmark::State& state) {
Steven Moreland1ebdc702021-07-28 18:46:15 -0700145 sp<IBinder> binder = getBinderForOptions(state);
146
Steven Morelandcda60852021-04-14 23:45:32 +0000147 sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
148 CHECK(iface != nullptr);
149
150 // Googlers might see go/another-look-at-aidl-hidl-perf
151 //
152 // When I checked in July 2019, 99.5% of AIDL transactions and 99.99% of HIDL
153 // transactions were less than one page in size (system wide during a test
154 // involving media and camera). This is why this diverges from
155 // binderThroughputTest and hwbinderThroughputTest. Future consideration - get
156 // this data on continuous integration. Here we are testing sending a
157 // transaction of twice this size. In other cases, we should focus on
158 // benchmarks of particular usecases. If individual binder transactions like
159 // the ones tested here are fast, then Android performance will be dominated
160 // by how many binder calls work together (and by factors like the scheduler,
161 // thermal throttling, core choice, etc..).
162 std::string str = std::string(getpagesize() * 2, 'a');
163 CHECK_EQ(str.size(), getpagesize() * 2);
164
165 while (state.KeepRunning()) {
166 std::string out;
167 Status ret = iface->repeatString(str, &out);
168 CHECK(ret.isOk()) << ret;
169 }
170}
Steven Morelandce240ce2021-08-05 13:02:37 -0700171BENCHMARK(BM_repeatTwoPageString)->ArgsProduct({kTransportList});
172
173void BM_throughputForTransportAndBytes(benchmark::State& state) {
174 sp<IBinder> binder = getBinderForOptions(state);
175 sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
176 CHECK(iface != nullptr);
177
178 std::vector<uint8_t> bytes = std::vector<uint8_t>(state.range(1));
179 for (size_t i = 0; i < bytes.size(); i++) {
180 bytes[i] = i % 256;
181 }
182
183 while (state.KeepRunning()) {
184 std::vector<uint8_t> out;
185 Status ret = iface->repeatBytes(bytes, &out);
186 CHECK(ret.isOk()) << ret;
187 }
188}
189BENCHMARK(BM_throughputForTransportAndBytes)
190 ->ArgsProduct({kTransportList,
191 {64, 1024, 2048, 4096, 8182, 16364, 32728, 65535, 65536, 65537}});
Steven Morelandcda60852021-04-14 23:45:32 +0000192
193void BM_repeatBinder(benchmark::State& state) {
Steven Morelandb9c49622021-08-10 17:39:10 -0700194 sp<IBinder> binder = getBinderForOptions(state);
Steven Morelandcda60852021-04-14 23:45:32 +0000195 CHECK(binder != nullptr);
196 sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
197 CHECK(iface != nullptr);
198
199 while (state.KeepRunning()) {
200 // force creation of a new address
201 sp<IBinder> binder = sp<BBinder>::make();
202
203 sp<IBinder> out;
204 Status ret = iface->repeatBinder(binder, &out);
205 CHECK(ret.isOk()) << ret;
206 }
207}
Steven Morelandce240ce2021-08-05 13:02:37 -0700208BENCHMARK(BM_repeatBinder)->ArgsProduct({kTransportList});
Steven Morelandcda60852021-04-14 23:45:32 +0000209
Yifan Hongaecdd172021-09-21 14:53:13 -0700210void forkRpcServer(const char* addr, const sp<RpcServer>& server) {
211 if (0 == fork()) {
212 prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
213 server->setRootObject(sp<MyBinderRpcBenchmark>::make());
214 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
215 CHECK_EQ(OK, server->setupUnixDomainServer(addr));
216 server->join();
217 exit(1);
218 }
219}
220
221void setupClient(const sp<RpcSession>& session, const char* addr) {
222 status_t status;
223 for (size_t tries = 0; tries < 5; tries++) {
224 usleep(10000);
225 status = session->setupUnixDomainClient(addr);
226 if (status == OK) break;
227 }
228 CHECK_EQ(status, OK) << "Could not connect: " << addr << ": " << statusToString(status).c_str();
229}
230
Steven Morelandcda60852021-04-14 23:45:32 +0000231int main(int argc, char** argv) {
232 ::benchmark::Initialize(&argc, argv);
233 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
234
Steven Moreland1ebdc702021-07-28 18:46:15 -0700235 std::cerr << "Tests suffixes:" << std::endl;
Steven Morelandce240ce2021-08-05 13:02:37 -0700236 std::cerr << "\t.../" << Transport::KERNEL << " is KERNEL" << std::endl;
237 std::cerr << "\t.../" << Transport::RPC << " is RPC" << std::endl;
Yifan Hongaecdd172021-09-21 14:53:13 -0700238 std::cerr << "\t.../" << Transport::RPC_TLS << " is RPC with TLS" << std::endl;
Steven Moreland656e9d12021-07-29 17:33:53 -0700239
Steven Moreland1ebdc702021-07-28 18:46:15 -0700240#ifdef __BIONIC__
241 if (0 == fork()) {
242 prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
243 CHECK_EQ(OK,
244 defaultServiceManager()->addService(kKernelBinderInstance,
245 sp<MyBinderRpcBenchmark>::make()));
246 IPCThreadState::self()->joinThreadPool();
Steven Moreland656e9d12021-07-29 17:33:53 -0700247 exit(1);
Steven Moreland1ebdc702021-07-28 18:46:15 -0700248 }
249
250 ProcessState::self()->setThreadPoolMaxThreadCount(1);
251 ProcessState::self()->startThreadPool();
252
253 gKernelBinder = defaultServiceManager()->waitForService(kKernelBinderInstance);
254 CHECK_NE(nullptr, gKernelBinder.get());
255#endif
256
Yifan Hongaecdd172021-09-21 14:53:13 -0700257 std::string tmp = getenv("TMPDIR") ?: "/tmp";
258
259 std::string addr = tmp + "/binderRpcBenchmark";
260 (void)unlink(addr.c_str());
261 forkRpcServer(addr.c_str(), RpcServer::make(RpcTransportCtxFactoryRaw::make()));
262 setupClient(gSession, addr.c_str());
263
264 std::string tlsAddr = tmp + "/binderRpcTlsBenchmark";
265 (void)unlink(tlsAddr.c_str());
266 forkRpcServer(tlsAddr.c_str(), RpcServer::make(makeFactoryTls()));
267 setupClient(gSessionTls, tlsAddr.c_str());
Steven Morelandcda60852021-04-14 23:45:32 +0000268
269 ::benchmark::RunSpecifiedBenchmarks();
Steven Moreland3ae982a2021-05-05 18:16:11 +0000270 return 0;
Steven Morelandcda60852021-04-14 23:45:32 +0000271}