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