blob: aeda03ba3571066618abb7ae28f58aab85e4cc12 [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>
Steven Morelandcda60852021-04-14 23:45:32 +000024#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000025#include <binder/RpcSession.h>
Steven Morelandcda60852021-04-14 23:45:32 +000026
27#include <thread>
28
Steven Moreland656e9d12021-07-29 17:33:53 -070029#include <signal.h>
Steven Moreland1ebdc702021-07-28 18:46:15 -070030#include <sys/prctl.h>
Steven Morelandcda60852021-04-14 23:45:32 +000031#include <sys/types.h>
32#include <unistd.h>
33
34using android::BBinder;
Steven Moreland1ebdc702021-07-28 18:46:15 -070035using android::defaultServiceManager;
Steven Morelandcda60852021-04-14 23:45:32 +000036using android::IBinder;
37using android::interface_cast;
Steven Moreland1ebdc702021-07-28 18:46:15 -070038using android::IPCThreadState;
39using android::IServiceManager;
Steven Morelandcda60852021-04-14 23:45:32 +000040using android::OK;
Steven Moreland1ebdc702021-07-28 18:46:15 -070041using android::ProcessState;
Steven Morelandcda60852021-04-14 23:45:32 +000042using android::RpcServer;
Steven Morelandbdb53ab2021-05-05 17:57:41 +000043using android::RpcSession;
Steven Morelandcda60852021-04-14 23:45:32 +000044using android::sp;
Steven Moreland1ebdc702021-07-28 18:46:15 -070045using android::String16;
Steven Morelandcda60852021-04-14 23:45:32 +000046using android::binder::Status;
47
48class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
49 Status repeatString(const std::string& str, std::string* out) override {
50 *out = str;
51 return Status::ok();
52 }
Steven Morelandce240ce2021-08-05 13:02:37 -070053 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 Morelandcda60852021-04-14 23:45:32 +000059 return Status::ok();
60 }
61};
62
Steven Moreland1ebdc702021-07-28 18:46:15 -070063enum Transport {
64 KERNEL,
65 RPC,
66};
Steven Morelandcda60852021-04-14 23:45:32 +000067
Steven Morelandce240ce2021-08-05 13:02:37 -070068static const std::initializer_list<int64_t> kTransportList = {
Steven Moreland1ebdc702021-07-28 18:46:15 -070069#ifdef __BIONIC__
Steven Morelandce240ce2021-08-05 13:02:37 -070070 Transport::KERNEL,
Steven Moreland1ebdc702021-07-28 18:46:15 -070071#endif
Steven Morelandce240ce2021-08-05 13:02:37 -070072 Transport::RPC};
Steven Moreland1ebdc702021-07-28 18:46:15 -070073
74static sp<RpcSession> gSession = RpcSession::make();
75#ifdef __BIONIC__
76static const String16 kKernelBinderInstance = String16(u"binderRpcBenchmark-control");
77static sp<IBinder> gKernelBinder;
78#endif
79
80static 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 Morelandcda60852021-04-14 23:45:32 +000092 }
93}
Steven Morelandcda60852021-04-14 23:45:32 +000094
95void BM_pingTransaction(benchmark::State& state) {
Steven Moreland1ebdc702021-07-28 18:46:15 -070096 sp<IBinder> binder = getBinderForOptions(state);
Steven Morelandcda60852021-04-14 23:45:32 +000097
98 while (state.KeepRunning()) {
99 CHECK_EQ(OK, binder->pingBinder());
100 }
101}
Steven Morelandce240ce2021-08-05 13:02:37 -0700102BENCHMARK(BM_pingTransaction)->ArgsProduct({kTransportList});
Steven Morelandcda60852021-04-14 23:45:32 +0000103
Steven Morelandce240ce2021-08-05 13:02:37 -0700104void BM_repeatTwoPageString(benchmark::State& state) {
Steven Moreland1ebdc702021-07-28 18:46:15 -0700105 sp<IBinder> binder = getBinderForOptions(state);
106
Steven Morelandcda60852021-04-14 23:45:32 +0000107 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 Morelandce240ce2021-08-05 13:02:37 -0700131BENCHMARK(BM_repeatTwoPageString)->ArgsProduct({kTransportList});
132
133void 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}
149BENCHMARK(BM_throughputForTransportAndBytes)
150 ->ArgsProduct({kTransportList,
151 {64, 1024, 2048, 4096, 8182, 16364, 32728, 65535, 65536, 65537}});
Steven Morelandcda60852021-04-14 23:45:32 +0000152
153void BM_repeatBinder(benchmark::State& state) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000154 sp<IBinder> binder = gSession->getRootObject();
Steven Morelandcda60852021-04-14 23:45:32 +0000155 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 Morelandce240ce2021-08-05 13:02:37 -0700168BENCHMARK(BM_repeatBinder)->ArgsProduct({kTransportList});
Steven Morelandcda60852021-04-14 23:45:32 +0000169
170int 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 Moreland1ebdc702021-07-28 18:46:15 -0700177 std::cerr << "Tests suffixes:" << std::endl;
Steven Morelandce240ce2021-08-05 13:02:37 -0700178 std::cerr << "\t.../" << Transport::KERNEL << " is KERNEL" << std::endl;
179 std::cerr << "\t.../" << Transport::RPC << " is RPC" << std::endl;
Steven Moreland1ebdc702021-07-28 18:46:15 -0700180
Steven Moreland656e9d12021-07-29 17:33:53 -0700181 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 Moreland1ebdc702021-07-28 18:46:15 -0700191#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 Moreland656e9d12021-07-29 17:33:53 -0700198 exit(1);
Steven Moreland1ebdc702021-07-28 18:46:15 -0700199 }
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 Morelandcda60852021-04-14 23:45:32 +0000208 for (size_t tries = 0; tries < 5; tries++) {
209 usleep(10000);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000210 if (gSession->setupUnixDomainClient(addr.c_str())) goto success;
Steven Morelandcda60852021-04-14 23:45:32 +0000211 }
212 LOG(FATAL) << "Could not connect.";
213success:
214
215 ::benchmark::RunSpecifiedBenchmarks();
Steven Moreland3ae982a2021-05-05 18:16:11 +0000216 return 0;
Steven Morelandcda60852021-04-14 23:45:32 +0000217}