| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 1 | /* | 
|  | 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 Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> | 
|  | 22 | #include <binder/IServiceManager.h> | 
|  | 23 | #include <binder/ProcessState.h> | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 24 | #include <binder/RpcCertificateFormat.h> | 
|  | 25 | #include <binder/RpcCertificateVerifier.h> | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 26 | #include <binder/RpcServer.h> | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 27 | #include <binder/RpcSession.h> | 
| Yifan Hong | e0e5328 | 2021-09-23 18:37:21 -0700 | [diff] [blame] | 28 | #include <binder/RpcTlsTestUtils.h> | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 29 | #include <binder/RpcTlsUtils.h> | 
|  | 30 | #include <binder/RpcTransportRaw.h> | 
|  | 31 | #include <binder/RpcTransportTls.h> | 
|  | 32 | #include <openssl/ssl.h> | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | #include <thread> | 
|  | 35 |  | 
| Steven Moreland | 656e9d1 | 2021-07-29 17:33:53 -0700 | [diff] [blame] | 36 | #include <signal.h> | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 37 | #include <sys/prctl.h> | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 38 | #include <sys/types.h> | 
|  | 39 | #include <unistd.h> | 
|  | 40 |  | 
|  | 41 | using android::BBinder; | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 42 | using android::defaultServiceManager; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 43 | using android::IBinder; | 
|  | 44 | using android::interface_cast; | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 45 | using android::IPCThreadState; | 
|  | 46 | using android::IServiceManager; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 47 | using android::OK; | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 48 | using android::ProcessState; | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 49 | using android::RpcAuthPreSigned; | 
|  | 50 | using android::RpcCertificateFormat; | 
|  | 51 | using android::RpcCertificateVerifier; | 
| Yifan Hong | 9809add | 2021-09-23 19:06:58 -0700 | [diff] [blame] | 52 | using android::RpcCertificateVerifierNoOp; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 53 | using android::RpcServer; | 
| Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 54 | using android::RpcSession; | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 55 | using android::RpcTransportCtxFactory; | 
|  | 56 | using android::RpcTransportCtxFactoryRaw; | 
|  | 57 | using android::RpcTransportCtxFactoryTls; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 58 | using android::sp; | 
| Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 59 | using android::status_t; | 
|  | 60 | using android::statusToString; | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 61 | using android::String16; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 62 | using android::binder::Status; | 
|  | 63 |  | 
|  | 64 | class MyBinderRpcBenchmark : public BnBinderRpcBenchmark { | 
|  | 65 | Status repeatString(const std::string& str, std::string* out) override { | 
|  | 66 | *out = str; | 
|  | 67 | return Status::ok(); | 
|  | 68 | } | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 69 | 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 Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 75 | return Status::ok(); | 
|  | 76 | } | 
|  | 77 | }; | 
|  | 78 |  | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 79 | enum Transport { | 
|  | 80 | KERNEL, | 
|  | 81 | RPC, | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 82 | RPC_TLS, | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 83 | }; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 84 |  | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 85 | static const std::initializer_list<int64_t> kTransportList = { | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 86 | #ifdef __BIONIC__ | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 87 | Transport::KERNEL, | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 88 | #endif | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 89 | Transport::RPC, | 
|  | 90 | Transport::RPC_TLS, | 
|  | 91 | }; | 
|  | 92 |  | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 93 | std::unique_ptr<RpcTransportCtxFactory> makeFactoryTls() { | 
|  | 94 | auto pkey = android::makeKeyPairForSelfSignedCert(); | 
|  | 95 | CHECK_NE(pkey.get(), nullptr); | 
|  | 96 | auto cert = android::makeSelfSignedCert(pkey.get(), android::kCertValidSeconds); | 
|  | 97 | CHECK_NE(cert.get(), nullptr); | 
|  | 98 |  | 
| Yifan Hong | dc76765 | 2021-09-23 19:38:38 -0700 | [diff] [blame] | 99 | auto verifier = std::make_shared<RpcCertificateVerifierNoOp>(OK); | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 100 | auto auth = std::make_unique<RpcAuthPreSigned>(std::move(pkey), std::move(cert)); | 
|  | 101 | return RpcTransportCtxFactoryTls::make(verifier, std::move(auth)); | 
|  | 102 | } | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | static sp<RpcSession> gSession = RpcSession::make(); | 
| Yifan Hong | 9809add | 2021-09-23 19:06:58 -0700 | [diff] [blame] | 105 | // Certificate validation happens during handshake and does not affect the result of benchmarks. | 
|  | 106 | // Skip certificate validation to simplify the setup process. | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 107 | static sp<RpcSession> gSessionTls = RpcSession::make(makeFactoryTls()); | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 108 | #ifdef __BIONIC__ | 
|  | 109 | static const String16 kKernelBinderInstance = String16(u"binderRpcBenchmark-control"); | 
|  | 110 | static sp<IBinder> gKernelBinder; | 
|  | 111 | #endif | 
|  | 112 |  | 
|  | 113 | static sp<IBinder> getBinderForOptions(benchmark::State& state) { | 
|  | 114 | Transport transport = static_cast<Transport>(state.range(0)); | 
|  | 115 | switch (transport) { | 
|  | 116 | #ifdef __BIONIC__ | 
|  | 117 | case KERNEL: | 
|  | 118 | return gKernelBinder; | 
|  | 119 | #endif | 
|  | 120 | case RPC: | 
|  | 121 | return gSession->getRootObject(); | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 122 | case RPC_TLS: | 
|  | 123 | return gSessionTls->getRootObject(); | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 124 | default: | 
|  | 125 | LOG(FATAL) << "Unknown transport value: " << transport; | 
|  | 126 | return nullptr; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 127 | } | 
|  | 128 | } | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 129 |  | 
|  | 130 | void BM_pingTransaction(benchmark::State& state) { | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 131 | sp<IBinder> binder = getBinderForOptions(state); | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 132 |  | 
|  | 133 | while (state.KeepRunning()) { | 
|  | 134 | CHECK_EQ(OK, binder->pingBinder()); | 
|  | 135 | } | 
|  | 136 | } | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 137 | BENCHMARK(BM_pingTransaction)->ArgsProduct({kTransportList}); | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 138 |  | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 139 | void BM_repeatTwoPageString(benchmark::State& state) { | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 140 | sp<IBinder> binder = getBinderForOptions(state); | 
|  | 141 |  | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 142 | sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder); | 
|  | 143 | CHECK(iface != nullptr); | 
|  | 144 |  | 
|  | 145 | // Googlers might see go/another-look-at-aidl-hidl-perf | 
|  | 146 | // | 
|  | 147 | // When I checked in July 2019, 99.5% of AIDL transactions and 99.99% of HIDL | 
|  | 148 | // transactions were less than one page in size (system wide during a test | 
|  | 149 | // involving media and camera). This is why this diverges from | 
|  | 150 | // binderThroughputTest and hwbinderThroughputTest. Future consideration - get | 
|  | 151 | // this data on continuous integration. Here we are testing sending a | 
|  | 152 | // transaction of twice this size. In other cases, we should focus on | 
|  | 153 | // benchmarks of particular usecases. If individual binder transactions like | 
|  | 154 | // the ones tested here are fast, then Android performance will be dominated | 
|  | 155 | // by how many binder calls work together (and by factors like the scheduler, | 
|  | 156 | // thermal throttling, core choice, etc..). | 
|  | 157 | std::string str = std::string(getpagesize() * 2, 'a'); | 
|  | 158 | CHECK_EQ(str.size(), getpagesize() * 2); | 
|  | 159 |  | 
|  | 160 | while (state.KeepRunning()) { | 
|  | 161 | std::string out; | 
|  | 162 | Status ret = iface->repeatString(str, &out); | 
|  | 163 | CHECK(ret.isOk()) << ret; | 
|  | 164 | } | 
|  | 165 | } | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 166 | BENCHMARK(BM_repeatTwoPageString)->ArgsProduct({kTransportList}); | 
|  | 167 |  | 
|  | 168 | void BM_throughputForTransportAndBytes(benchmark::State& state) { | 
|  | 169 | sp<IBinder> binder = getBinderForOptions(state); | 
|  | 170 | sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder); | 
|  | 171 | CHECK(iface != nullptr); | 
|  | 172 |  | 
|  | 173 | std::vector<uint8_t> bytes = std::vector<uint8_t>(state.range(1)); | 
|  | 174 | for (size_t i = 0; i < bytes.size(); i++) { | 
|  | 175 | bytes[i] = i % 256; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | while (state.KeepRunning()) { | 
|  | 179 | std::vector<uint8_t> out; | 
|  | 180 | Status ret = iface->repeatBytes(bytes, &out); | 
|  | 181 | CHECK(ret.isOk()) << ret; | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 | BENCHMARK(BM_throughputForTransportAndBytes) | 
|  | 185 | ->ArgsProduct({kTransportList, | 
|  | 186 | {64, 1024, 2048, 4096, 8182, 16364, 32728, 65535, 65536, 65537}}); | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 187 |  | 
|  | 188 | void BM_repeatBinder(benchmark::State& state) { | 
| Steven Moreland | b9c4962 | 2021-08-10 17:39:10 -0700 | [diff] [blame] | 189 | sp<IBinder> binder = getBinderForOptions(state); | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 190 | CHECK(binder != nullptr); | 
|  | 191 | sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder); | 
|  | 192 | CHECK(iface != nullptr); | 
|  | 193 |  | 
|  | 194 | while (state.KeepRunning()) { | 
|  | 195 | // force creation of a new address | 
|  | 196 | sp<IBinder> binder = sp<BBinder>::make(); | 
|  | 197 |  | 
|  | 198 | sp<IBinder> out; | 
|  | 199 | Status ret = iface->repeatBinder(binder, &out); | 
|  | 200 | CHECK(ret.isOk()) << ret; | 
|  | 201 | } | 
|  | 202 | } | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 203 | BENCHMARK(BM_repeatBinder)->ArgsProduct({kTransportList}); | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 204 |  | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 205 | void forkRpcServer(const char* addr, const sp<RpcServer>& server) { | 
|  | 206 | if (0 == fork()) { | 
|  | 207 | prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay | 
|  | 208 | server->setRootObject(sp<MyBinderRpcBenchmark>::make()); | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 209 | CHECK_EQ(OK, server->setupUnixDomainServer(addr)); | 
|  | 210 | server->join(); | 
|  | 211 | exit(1); | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | void setupClient(const sp<RpcSession>& session, const char* addr) { | 
|  | 216 | status_t status; | 
|  | 217 | for (size_t tries = 0; tries < 5; tries++) { | 
|  | 218 | usleep(10000); | 
|  | 219 | status = session->setupUnixDomainClient(addr); | 
|  | 220 | if (status == OK) break; | 
|  | 221 | } | 
|  | 222 | CHECK_EQ(status, OK) << "Could not connect: " << addr << ": " << statusToString(status).c_str(); | 
|  | 223 | } | 
|  | 224 |  | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 225 | int main(int argc, char** argv) { | 
|  | 226 | ::benchmark::Initialize(&argc, argv); | 
|  | 227 | if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; | 
|  | 228 |  | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 229 | std::cerr << "Tests suffixes:" << std::endl; | 
| Steven Moreland | ce240ce | 2021-08-05 13:02:37 -0700 | [diff] [blame] | 230 | std::cerr << "\t.../" << Transport::KERNEL << " is KERNEL" << std::endl; | 
|  | 231 | std::cerr << "\t.../" << Transport::RPC << " is RPC" << std::endl; | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 232 | std::cerr << "\t.../" << Transport::RPC_TLS << " is RPC with TLS" << std::endl; | 
| Steven Moreland | 656e9d1 | 2021-07-29 17:33:53 -0700 | [diff] [blame] | 233 |  | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 234 | #ifdef __BIONIC__ | 
|  | 235 | if (0 == fork()) { | 
|  | 236 | prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay | 
|  | 237 | CHECK_EQ(OK, | 
|  | 238 | defaultServiceManager()->addService(kKernelBinderInstance, | 
|  | 239 | sp<MyBinderRpcBenchmark>::make())); | 
|  | 240 | IPCThreadState::self()->joinThreadPool(); | 
| Steven Moreland | 656e9d1 | 2021-07-29 17:33:53 -0700 | [diff] [blame] | 241 | exit(1); | 
| Steven Moreland | 1ebdc70 | 2021-07-28 18:46:15 -0700 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
|  | 244 | ProcessState::self()->setThreadPoolMaxThreadCount(1); | 
|  | 245 | ProcessState::self()->startThreadPool(); | 
|  | 246 |  | 
|  | 247 | gKernelBinder = defaultServiceManager()->waitForService(kKernelBinderInstance); | 
|  | 248 | CHECK_NE(nullptr, gKernelBinder.get()); | 
|  | 249 | #endif | 
|  | 250 |  | 
| Yifan Hong | aecdd17 | 2021-09-21 14:53:13 -0700 | [diff] [blame] | 251 | std::string tmp = getenv("TMPDIR") ?: "/tmp"; | 
|  | 252 |  | 
|  | 253 | std::string addr = tmp + "/binderRpcBenchmark"; | 
|  | 254 | (void)unlink(addr.c_str()); | 
|  | 255 | forkRpcServer(addr.c_str(), RpcServer::make(RpcTransportCtxFactoryRaw::make())); | 
|  | 256 | setupClient(gSession, addr.c_str()); | 
|  | 257 |  | 
|  | 258 | std::string tlsAddr = tmp + "/binderRpcTlsBenchmark"; | 
|  | 259 | (void)unlink(tlsAddr.c_str()); | 
|  | 260 | forkRpcServer(tlsAddr.c_str(), RpcServer::make(makeFactoryTls())); | 
|  | 261 | setupClient(gSessionTls, tlsAddr.c_str()); | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 262 |  | 
|  | 263 | ::benchmark::RunSpecifiedBenchmarks(); | 
| Steven Moreland | 3ae982a | 2021-05-05 18:16:11 +0000 | [diff] [blame] | 264 | return 0; | 
| Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 265 | } |