1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <BnBinderRpcBenchmark.h>
#include <android-base/logging.h>
#include <benchmark/benchmark.h>
#include <binder/Binder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <binder/RpcCertificateFormat.h>
#include <binder/RpcCertificateVerifier.h>
#include <binder/RpcServer.h>
#include <binder/RpcSession.h>
#include <binder/RpcTlsTestUtils.h>
#include <binder/RpcTlsUtils.h>
#include <binder/RpcTransportRaw.h>
#include <binder/RpcTransportTls.h>
#include <openssl/ssl.h>
#include <thread>
#include <signal.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <unistd.h>
using android::BBinder;
using android::defaultServiceManager;
using android::IBinder;
using android::interface_cast;
using android::IPCThreadState;
using android::IServiceManager;
using android::OK;
using android::ProcessState;
using android::RpcAuthPreSigned;
using android::RpcCertificateFormat;
using android::RpcCertificateVerifier;
using android::RpcCertificateVerifierNoOp;
using android::RpcServer;
using android::RpcSession;
using android::RpcTransportCtxFactory;
using android::RpcTransportCtxFactoryRaw;
using android::RpcTransportCtxFactoryTls;
using android::sp;
using android::status_t;
using android::statusToString;
using android::String16;
using android::binder::Status;
class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
Status repeatString(const std::string& str, std::string* out) override {
*out = str;
return Status::ok();
}
Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
*out = binder;
return Status::ok();
}
Status repeatBytes(const std::vector<uint8_t>& bytes, std::vector<uint8_t>* out) override {
*out = bytes;
return Status::ok();
}
class CountedBinder : public BBinder {
public:
CountedBinder(const sp<MyBinderRpcBenchmark>& parent) : mParent(parent) {
std::lock_guard<std::mutex> l(mParent->mCountMutex);
mParent->mBinderCount++;
// std::cout << "Count + is now " << mParent->mBinderCount << std::endl;
}
~CountedBinder() {
{
std::lock_guard<std::mutex> l(mParent->mCountMutex);
mParent->mBinderCount--;
// std::cout << "Count - is now " << mParent->mBinderCount << std::endl;
// skip notify
if (mParent->mBinderCount != 0) return;
}
mParent->mCountCv.notify_one();
}
private:
sp<MyBinderRpcBenchmark> mParent;
};
Status gimmeBinder(sp<IBinder>* out) override {
*out = sp<CountedBinder>::make(sp<MyBinderRpcBenchmark>::fromExisting(this));
return Status::ok();
}
Status waitGimmesDestroyed() override {
std::unique_lock<std::mutex> l(mCountMutex);
mCountCv.wait(l, [&] { return mBinderCount == 0; });
return Status::ok();
}
friend class CountedBinder;
std::mutex mCountMutex;
std::condition_variable mCountCv;
size_t mBinderCount;
};
enum Transport {
KERNEL,
RPC,
RPC_TLS,
};
static const std::initializer_list<int64_t> kTransportList = {
#ifdef __BIONIC__
Transport::KERNEL,
#endif
Transport::RPC,
Transport::RPC_TLS,
};
std::unique_ptr<RpcTransportCtxFactory> makeFactoryTls() {
auto pkey = android::makeKeyPairForSelfSignedCert();
CHECK_NE(pkey.get(), nullptr);
auto cert = android::makeSelfSignedCert(pkey.get(), android::kCertValidSeconds);
CHECK_NE(cert.get(), nullptr);
auto verifier = std::make_shared<RpcCertificateVerifierNoOp>(OK);
auto auth = std::make_unique<RpcAuthPreSigned>(std::move(pkey), std::move(cert));
return RpcTransportCtxFactoryTls::make(verifier, std::move(auth));
}
static sp<RpcSession> gSession = RpcSession::make();
static sp<IBinder> gRpcBinder;
// Certificate validation happens during handshake and does not affect the result of benchmarks.
// Skip certificate validation to simplify the setup process.
static sp<RpcSession> gSessionTls = RpcSession::make(makeFactoryTls());
static sp<IBinder> gRpcTlsBinder;
#ifdef __BIONIC__
static const String16 kKernelBinderInstance = String16(u"binderRpcBenchmark-control");
static sp<IBinder> gKernelBinder;
#endif
static sp<IBinder> getBinderForOptions(benchmark::State& state) {
Transport transport = static_cast<Transport>(state.range(0));
switch (transport) {
#ifdef __BIONIC__
case KERNEL:
return gKernelBinder;
#endif
case RPC:
return gRpcBinder;
case RPC_TLS:
return gRpcTlsBinder;
default:
LOG(FATAL) << "Unknown transport value: " << transport;
return nullptr;
}
}
static void SetLabel(benchmark::State& state) {
Transport transport = static_cast<Transport>(state.range(0));
switch (transport) {
#ifdef __BIONIC__
case KERNEL:
state.SetLabel("kernel");
break;
#endif
case RPC:
state.SetLabel("rpc");
break;
case RPC_TLS:
state.SetLabel("rpc_tls");
break;
default:
LOG(FATAL) << "Unknown transport value: " << transport;
}
}
void BM_pingTransaction(benchmark::State& state) {
sp<IBinder> binder = getBinderForOptions(state);
while (state.KeepRunning()) {
CHECK_EQ(OK, binder->pingBinder());
}
SetLabel(state);
}
BENCHMARK(BM_pingTransaction)->ArgsProduct({kTransportList});
void BM_repeatTwoPageString(benchmark::State& state) {
sp<IBinder> binder = getBinderForOptions(state);
sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
CHECK(iface != nullptr);
// Googlers might see go/another-look-at-aidl-hidl-perf
//
// When I checked in July 2019, 99.5% of AIDL transactions and 99.99% of HIDL
// transactions were less than one page in size (system wide during a test
// involving media and camera). This is why this diverges from
// binderThroughputTest and hwbinderThroughputTest. Future consideration - get
// this data on continuous integration. Here we are testing sending a
// transaction of twice this size. In other cases, we should focus on
// benchmarks of particular usecases. If individual binder transactions like
// the ones tested here are fast, then Android performance will be dominated
// by how many binder calls work together (and by factors like the scheduler,
// thermal throttling, core choice, etc..).
std::string str = std::string(getpagesize() * 2, 'a');
CHECK_EQ(str.size(), getpagesize() * 2);
while (state.KeepRunning()) {
std::string out;
Status ret = iface->repeatString(str, &out);
CHECK(ret.isOk()) << ret;
}
SetLabel(state);
}
BENCHMARK(BM_repeatTwoPageString)->ArgsProduct({kTransportList});
void BM_throughputForTransportAndBytes(benchmark::State& state) {
sp<IBinder> binder = getBinderForOptions(state);
sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
CHECK(iface != nullptr);
std::vector<uint8_t> bytes = std::vector<uint8_t>(state.range(1));
for (size_t i = 0; i < bytes.size(); i++) {
bytes[i] = i % 256;
}
while (state.KeepRunning()) {
std::vector<uint8_t> out;
Status ret = iface->repeatBytes(bytes, &out);
CHECK(ret.isOk()) << ret;
}
SetLabel(state);
}
BENCHMARK(BM_throughputForTransportAndBytes)
->ArgsProduct({kTransportList,
{64, 1024, 2048, 4096, 8182, 16364, 32728, 65535, 65536, 65537}});
void BM_collectProxies(benchmark::State& state) {
sp<IBinder> binder = getBinderForOptions(state);
sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
CHECK(iface != nullptr);
const size_t kNumIters = state.range(1);
while (state.KeepRunning()) {
std::vector<sp<IBinder>> out;
out.resize(kNumIters);
for (size_t i = 0; i < kNumIters; i++) {
Status ret = iface->gimmeBinder(&out[i]);
CHECK(ret.isOk()) << ret;
}
out.clear();
// we are using a thread up to wait, so make a call to
// force all refcounts to be updated first - current
// binder behavior means we really don't need to wait,
// so code which is waiting is really there to protect
// against any future changes that could delay destruction
android::IInterface::asBinder(iface)->pingBinder();
iface->waitGimmesDestroyed();
}
SetLabel(state);
}
BENCHMARK(BM_collectProxies)->ArgsProduct({kTransportList, {10, 100, 1000, 5000, 10000, 20000}});
void BM_repeatBinder(benchmark::State& state) {
sp<IBinder> binder = getBinderForOptions(state);
CHECK(binder != nullptr);
sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
CHECK(iface != nullptr);
while (state.KeepRunning()) {
// force creation of a new address
sp<IBinder> binder = sp<BBinder>::make();
sp<IBinder> out;
Status ret = iface->repeatBinder(binder, &out);
CHECK(ret.isOk()) << ret;
}
SetLabel(state);
}
BENCHMARK(BM_repeatBinder)->ArgsProduct({kTransportList});
void forkRpcServer(const char* addr, const sp<RpcServer>& server) {
if (0 == fork()) {
prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
server->setRootObject(sp<MyBinderRpcBenchmark>::make());
CHECK_EQ(OK, server->setupUnixDomainServer(addr));
server->join();
exit(1);
}
}
void setupClient(const sp<RpcSession>& session, const char* addr) {
status_t status;
for (size_t tries = 0; tries < 5; tries++) {
usleep(10000);
status = session->setupUnixDomainClient(addr);
if (status == OK) break;
}
CHECK_EQ(status, OK) << "Could not connect: " << addr << ": " << statusToString(status).c_str();
}
int main(int argc, char** argv) {
::benchmark::Initialize(&argc, argv);
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
#ifdef __BIONIC__
if (0 == fork()) {
prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
CHECK_EQ(OK,
defaultServiceManager()->addService(kKernelBinderInstance,
sp<MyBinderRpcBenchmark>::make()));
IPCThreadState::self()->joinThreadPool();
exit(1);
}
ProcessState::self()->setThreadPoolMaxThreadCount(1);
ProcessState::self()->startThreadPool();
gKernelBinder = defaultServiceManager()->waitForService(kKernelBinderInstance);
CHECK_NE(nullptr, gKernelBinder.get());
#endif
std::string tmp = getenv("TMPDIR") ?: "/tmp";
std::string addr = tmp + "/binderRpcBenchmark";
(void)unlink(addr.c_str());
forkRpcServer(addr.c_str(), RpcServer::make(RpcTransportCtxFactoryRaw::make()));
setupClient(gSession, addr.c_str());
gRpcBinder = gSession->getRootObject();
std::string tlsAddr = tmp + "/binderRpcTlsBenchmark";
(void)unlink(tlsAddr.c_str());
forkRpcServer(tlsAddr.c_str(), RpcServer::make(makeFactoryTls()));
setupClient(gSessionTls, tlsAddr.c_str());
gRpcTlsBinder = gSessionTls->getRootObject();
::benchmark::RunSpecifiedBenchmarks();
return 0;
}
|