blob: f8b168609c04210590a41feddb84235924735dcd [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 Honge0e53282021-09-23 18:37:21 -070028#include <binder/RpcTlsTestUtils.h>
Yifan Hongaecdd172021-09-21 14:53:13 -070029#include <binder/RpcTlsUtils.h>
30#include <binder/RpcTransportRaw.h>
31#include <binder/RpcTransportTls.h>
32#include <openssl/ssl.h>
Steven Morelandcda60852021-04-14 23:45:32 +000033
34#include <thread>
35
Steven Moreland656e9d12021-07-29 17:33:53 -070036#include <signal.h>
Steven Moreland1ebdc702021-07-28 18:46:15 -070037#include <sys/prctl.h>
Steven Morelandcda60852021-04-14 23:45:32 +000038#include <sys/types.h>
39#include <unistd.h>
40
41using android::BBinder;
Steven Moreland1ebdc702021-07-28 18:46:15 -070042using android::defaultServiceManager;
Steven Morelandcda60852021-04-14 23:45:32 +000043using android::IBinder;
44using android::interface_cast;
Steven Moreland1ebdc702021-07-28 18:46:15 -070045using android::IPCThreadState;
46using android::IServiceManager;
Steven Morelandcda60852021-04-14 23:45:32 +000047using android::OK;
Steven Moreland1ebdc702021-07-28 18:46:15 -070048using android::ProcessState;
Yifan Hongaecdd172021-09-21 14:53:13 -070049using android::RpcAuthPreSigned;
50using android::RpcCertificateFormat;
51using android::RpcCertificateVerifier;
Steven Morelandcda60852021-04-14 23:45:32 +000052using android::RpcServer;
Steven Morelandbdb53ab2021-05-05 17:57:41 +000053using android::RpcSession;
Yifan Hongaecdd172021-09-21 14:53:13 -070054using android::RpcTransportCtxFactory;
55using android::RpcTransportCtxFactoryRaw;
56using android::RpcTransportCtxFactoryTls;
Steven Morelandcda60852021-04-14 23:45:32 +000057using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070058using android::status_t;
59using android::statusToString;
Steven Moreland1ebdc702021-07-28 18:46:15 -070060using android::String16;
Steven Morelandcda60852021-04-14 23:45:32 +000061using android::binder::Status;
62
63class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
64 Status repeatString(const std::string& str, std::string* out) override {
65 *out = str;
66 return Status::ok();
67 }
Steven Morelandce240ce2021-08-05 13:02:37 -070068 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
69 *out = binder;
70 return Status::ok();
71 }
72 Status repeatBytes(const std::vector<uint8_t>& bytes, std::vector<uint8_t>* out) override {
73 *out = bytes;
Steven Morelandcda60852021-04-14 23:45:32 +000074 return Status::ok();
75 }
76};
77
Steven Moreland1ebdc702021-07-28 18:46:15 -070078enum Transport {
79 KERNEL,
80 RPC,
Yifan Hongaecdd172021-09-21 14:53:13 -070081 RPC_TLS,
Steven Moreland1ebdc702021-07-28 18:46:15 -070082};
Steven Morelandcda60852021-04-14 23:45:32 +000083
Steven Morelandce240ce2021-08-05 13:02:37 -070084static const std::initializer_list<int64_t> kTransportList = {
Steven Moreland1ebdc702021-07-28 18:46:15 -070085#ifdef __BIONIC__
Steven Morelandce240ce2021-08-05 13:02:37 -070086 Transport::KERNEL,
Steven Moreland1ebdc702021-07-28 18:46:15 -070087#endif
Yifan Hongaecdd172021-09-21 14:53:13 -070088 Transport::RPC,
89 Transport::RPC_TLS,
90};
91
92// Certificate validation happens during handshake and does not affect the result of benchmarks.
93// Skip certificate validation to simplify the setup process.
94class RpcCertificateVerifierNoOp : public RpcCertificateVerifier {
95public:
96 status_t verify(const SSL*, uint8_t*) override { return OK; }
97};
98
99std::unique_ptr<RpcTransportCtxFactory> makeFactoryTls() {
100 auto pkey = android::makeKeyPairForSelfSignedCert();
101 CHECK_NE(pkey.get(), nullptr);
102 auto cert = android::makeSelfSignedCert(pkey.get(), android::kCertValidSeconds);
103 CHECK_NE(cert.get(), nullptr);
104
105 auto verifier = std::make_shared<RpcCertificateVerifierNoOp>();
106 auto auth = std::make_unique<RpcAuthPreSigned>(std::move(pkey), std::move(cert));
107 return RpcTransportCtxFactoryTls::make(verifier, std::move(auth));
108}
Steven Moreland1ebdc702021-07-28 18:46:15 -0700109
110static sp<RpcSession> gSession = RpcSession::make();
Yifan Hongaecdd172021-09-21 14:53:13 -0700111static sp<RpcSession> gSessionTls = RpcSession::make(makeFactoryTls());
Steven Moreland1ebdc702021-07-28 18:46:15 -0700112#ifdef __BIONIC__
113static const String16 kKernelBinderInstance = String16(u"binderRpcBenchmark-control");
114static sp<IBinder> gKernelBinder;
115#endif
116
117static sp<IBinder> getBinderForOptions(benchmark::State& state) {
118 Transport transport = static_cast<Transport>(state.range(0));
119 switch (transport) {
120#ifdef __BIONIC__
121 case KERNEL:
122 return gKernelBinder;
123#endif
124 case RPC:
125 return gSession->getRootObject();
Yifan Hongaecdd172021-09-21 14:53:13 -0700126 case RPC_TLS:
127 return gSessionTls->getRootObject();
Steven Moreland1ebdc702021-07-28 18:46:15 -0700128 default:
129 LOG(FATAL) << "Unknown transport value: " << transport;
130 return nullptr;
Steven Morelandcda60852021-04-14 23:45:32 +0000131 }
132}
Steven Morelandcda60852021-04-14 23:45:32 +0000133
134void BM_pingTransaction(benchmark::State& state) {
Steven Moreland1ebdc702021-07-28 18:46:15 -0700135 sp<IBinder> binder = getBinderForOptions(state);
Steven Morelandcda60852021-04-14 23:45:32 +0000136
137 while (state.KeepRunning()) {
138 CHECK_EQ(OK, binder->pingBinder());
139 }
140}
Steven Morelandce240ce2021-08-05 13:02:37 -0700141BENCHMARK(BM_pingTransaction)->ArgsProduct({kTransportList});
Steven Morelandcda60852021-04-14 23:45:32 +0000142
Steven Morelandce240ce2021-08-05 13:02:37 -0700143void BM_repeatTwoPageString(benchmark::State& state) {
Steven Moreland1ebdc702021-07-28 18:46:15 -0700144 sp<IBinder> binder = getBinderForOptions(state);
145
Steven Morelandcda60852021-04-14 23:45:32 +0000146 sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
147 CHECK(iface != nullptr);
148
149 // Googlers might see go/another-look-at-aidl-hidl-perf
150 //
151 // When I checked in July 2019, 99.5% of AIDL transactions and 99.99% of HIDL
152 // transactions were less than one page in size (system wide during a test
153 // involving media and camera). This is why this diverges from
154 // binderThroughputTest and hwbinderThroughputTest. Future consideration - get
155 // this data on continuous integration. Here we are testing sending a
156 // transaction of twice this size. In other cases, we should focus on
157 // benchmarks of particular usecases. If individual binder transactions like
158 // the ones tested here are fast, then Android performance will be dominated
159 // by how many binder calls work together (and by factors like the scheduler,
160 // thermal throttling, core choice, etc..).
161 std::string str = std::string(getpagesize() * 2, 'a');
162 CHECK_EQ(str.size(), getpagesize() * 2);
163
164 while (state.KeepRunning()) {
165 std::string out;
166 Status ret = iface->repeatString(str, &out);
167 CHECK(ret.isOk()) << ret;
168 }
169}
Steven Morelandce240ce2021-08-05 13:02:37 -0700170BENCHMARK(BM_repeatTwoPageString)->ArgsProduct({kTransportList});
171
172void BM_throughputForTransportAndBytes(benchmark::State& state) {
173 sp<IBinder> binder = getBinderForOptions(state);
174 sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
175 CHECK(iface != nullptr);
176
177 std::vector<uint8_t> bytes = std::vector<uint8_t>(state.range(1));
178 for (size_t i = 0; i < bytes.size(); i++) {
179 bytes[i] = i % 256;
180 }
181
182 while (state.KeepRunning()) {
183 std::vector<uint8_t> out;
184 Status ret = iface->repeatBytes(bytes, &out);
185 CHECK(ret.isOk()) << ret;
186 }
187}
188BENCHMARK(BM_throughputForTransportAndBytes)
189 ->ArgsProduct({kTransportList,
190 {64, 1024, 2048, 4096, 8182, 16364, 32728, 65535, 65536, 65537}});
Steven Morelandcda60852021-04-14 23:45:32 +0000191
192void BM_repeatBinder(benchmark::State& state) {
Steven Morelandb9c49622021-08-10 17:39:10 -0700193 sp<IBinder> binder = getBinderForOptions(state);
Steven Morelandcda60852021-04-14 23:45:32 +0000194 CHECK(binder != nullptr);
195 sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
196 CHECK(iface != nullptr);
197
198 while (state.KeepRunning()) {
199 // force creation of a new address
200 sp<IBinder> binder = sp<BBinder>::make();
201
202 sp<IBinder> out;
203 Status ret = iface->repeatBinder(binder, &out);
204 CHECK(ret.isOk()) << ret;
205 }
206}
Steven Morelandce240ce2021-08-05 13:02:37 -0700207BENCHMARK(BM_repeatBinder)->ArgsProduct({kTransportList});
Steven Morelandcda60852021-04-14 23:45:32 +0000208
Yifan Hongaecdd172021-09-21 14:53:13 -0700209void forkRpcServer(const char* addr, const sp<RpcServer>& server) {
210 if (0 == fork()) {
211 prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
212 server->setRootObject(sp<MyBinderRpcBenchmark>::make());
213 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
214 CHECK_EQ(OK, server->setupUnixDomainServer(addr));
215 server->join();
216 exit(1);
217 }
218}
219
220void setupClient(const sp<RpcSession>& session, const char* addr) {
221 status_t status;
222 for (size_t tries = 0; tries < 5; tries++) {
223 usleep(10000);
224 status = session->setupUnixDomainClient(addr);
225 if (status == OK) break;
226 }
227 CHECK_EQ(status, OK) << "Could not connect: " << addr << ": " << statusToString(status).c_str();
228}
229
Steven Morelandcda60852021-04-14 23:45:32 +0000230int main(int argc, char** argv) {
231 ::benchmark::Initialize(&argc, argv);
232 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
233
Steven Moreland1ebdc702021-07-28 18:46:15 -0700234 std::cerr << "Tests suffixes:" << std::endl;
Steven Morelandce240ce2021-08-05 13:02:37 -0700235 std::cerr << "\t.../" << Transport::KERNEL << " is KERNEL" << std::endl;
236 std::cerr << "\t.../" << Transport::RPC << " is RPC" << std::endl;
Yifan Hongaecdd172021-09-21 14:53:13 -0700237 std::cerr << "\t.../" << Transport::RPC_TLS << " is RPC with TLS" << std::endl;
Steven Moreland656e9d12021-07-29 17:33:53 -0700238
Steven Moreland1ebdc702021-07-28 18:46:15 -0700239#ifdef __BIONIC__
240 if (0 == fork()) {
241 prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
242 CHECK_EQ(OK,
243 defaultServiceManager()->addService(kKernelBinderInstance,
244 sp<MyBinderRpcBenchmark>::make()));
245 IPCThreadState::self()->joinThreadPool();
Steven Moreland656e9d12021-07-29 17:33:53 -0700246 exit(1);
Steven Moreland1ebdc702021-07-28 18:46:15 -0700247 }
248
249 ProcessState::self()->setThreadPoolMaxThreadCount(1);
250 ProcessState::self()->startThreadPool();
251
252 gKernelBinder = defaultServiceManager()->waitForService(kKernelBinderInstance);
253 CHECK_NE(nullptr, gKernelBinder.get());
254#endif
255
Yifan Hongaecdd172021-09-21 14:53:13 -0700256 std::string tmp = getenv("TMPDIR") ?: "/tmp";
257
258 std::string addr = tmp + "/binderRpcBenchmark";
259 (void)unlink(addr.c_str());
260 forkRpcServer(addr.c_str(), RpcServer::make(RpcTransportCtxFactoryRaw::make()));
261 setupClient(gSession, addr.c_str());
262
263 std::string tlsAddr = tmp + "/binderRpcTlsBenchmark";
264 (void)unlink(tlsAddr.c_str());
265 forkRpcServer(tlsAddr.c_str(), RpcServer::make(makeFactoryTls()));
266 setupClient(gSessionTls, tlsAddr.c_str());
Steven Morelandcda60852021-04-14 23:45:32 +0000267
268 ::benchmark::RunSpecifiedBenchmarks();
Steven Moreland3ae982a2021-05-05 18:16:11 +0000269 return 0;
Steven Morelandcda60852021-04-14 23:45:32 +0000270}