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