Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 108db786 authored by Steven Moreland's avatar Steven Moreland Committed by Automerger Merge Worker
Browse files

Merge "binderRpcBenchmark: device tests kernel as ctrl" am: 3e0eada7 am: 63b047d9

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1781529

Change-Id: Ib73cc7719ee663a792a723adc9feb8dc45c59915
parents f010abc2 63b047d9
Loading
Loading
Loading
Loading
+64 −12
Original line number Original line Diff line number Diff line
@@ -18,21 +18,30 @@
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <benchmark/benchmark.h>
#include <benchmark/benchmark.h>
#include <binder/Binder.h>
#include <binder/Binder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <binder/RpcServer.h>
#include <binder/RpcServer.h>
#include <binder/RpcSession.h>
#include <binder/RpcSession.h>


#include <thread>
#include <thread>


#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>


using android::BBinder;
using android::BBinder;
using android::defaultServiceManager;
using android::IBinder;
using android::IBinder;
using android::interface_cast;
using android::interface_cast;
using android::IPCThreadState;
using android::IServiceManager;
using android::OK;
using android::OK;
using android::ProcessState;
using android::RpcServer;
using android::RpcServer;
using android::RpcSession;
using android::RpcSession;
using android::sp;
using android::sp;
using android::String16;
using android::binder::Status;
using android::binder::Status;


class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
@@ -46,28 +55,51 @@ class MyBinderRpcBenchmark : public BnBinderRpcBenchmark {
    }
    }
};
};


static sp<RpcSession> gSession = RpcSession::make();
enum Transport {
    KERNEL,
    RPC,
};


void BM_getRootObject(benchmark::State& state) {
static void EachTransport(benchmark::internal::Benchmark* b) {
    while (state.KeepRunning()) {
#ifdef __BIONIC__
        CHECK(gSession->getRootObject() != nullptr);
    b->Args({Transport::KERNEL});
#endif
    b->Args({Transport::RPC});
}

static sp<RpcSession> gSession = RpcSession::make();
#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 gSession->getRootObject();
        default:
            LOG(FATAL) << "Unknown transport value: " << transport;
            return nullptr;
    }
    }
}
}
BENCHMARK(BM_getRootObject);


void BM_pingTransaction(benchmark::State& state) {
void BM_pingTransaction(benchmark::State& state) {
    sp<IBinder> binder = gSession->getRootObject();
    sp<IBinder> binder = getBinderForOptions(state);
    CHECK(binder != nullptr);


    while (state.KeepRunning()) {
    while (state.KeepRunning()) {
        CHECK_EQ(OK, binder->pingBinder());
        CHECK_EQ(OK, binder->pingBinder());
    }
    }
}
}
BENCHMARK(BM_pingTransaction);
BENCHMARK(BM_pingTransaction)->Apply(EachTransport);


void BM_repeatString(benchmark::State& state) {
void BM_repeatString(benchmark::State& state) {
    sp<IBinder> binder = gSession->getRootObject();
    sp<IBinder> binder = getBinderForOptions(state);
    CHECK(binder != nullptr);

    sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
    sp<IBinderRpcBenchmark> iface = interface_cast<IBinderRpcBenchmark>(binder);
    CHECK(iface != nullptr);
    CHECK(iface != nullptr);


@@ -92,7 +124,7 @@ void BM_repeatString(benchmark::State& state) {
        CHECK(ret.isOk()) << ret;
        CHECK(ret.isOk()) << ret;
    }
    }
}
}
BENCHMARK(BM_repeatString);
BENCHMARK(BM_repeatString)->Apply(EachTransport);


void BM_repeatBinder(benchmark::State& state) {
void BM_repeatBinder(benchmark::State& state) {
    sp<IBinder> binder = gSession->getRootObject();
    sp<IBinder> binder = gSession->getRootObject();
@@ -109,7 +141,7 @@ void BM_repeatBinder(benchmark::State& state) {
        CHECK(ret.isOk()) << ret;
        CHECK(ret.isOk()) << ret;
    }
    }
}
}
BENCHMARK(BM_repeatBinder);
BENCHMARK(BM_repeatBinder)->Apply(EachTransport);


int main(int argc, char** argv) {
int main(int argc, char** argv) {
    ::benchmark::Initialize(&argc, argv);
    ::benchmark::Initialize(&argc, argv);
@@ -118,6 +150,26 @@ int main(int argc, char** argv) {
    std::string addr = std::string(getenv("TMPDIR") ?: "/tmp") + "/binderRpcBenchmark";
    std::string addr = std::string(getenv("TMPDIR") ?: "/tmp") + "/binderRpcBenchmark";
    (void)unlink(addr.c_str());
    (void)unlink(addr.c_str());


    std::cerr << "Tests suffixes:" << std::endl;
    std::cerr << "\t\\" << Transport::KERNEL << " is KERNEL" << std::endl;
    std::cerr << "\t\\" << Transport::RPC << " is RPC" << std::endl;

#ifdef __BIONIC__
    if (0 == fork()) {
        prctl(PR_SET_PDEATHSIG, SIGHUP); // racey, okay
        CHECK_EQ(OK,
                 defaultServiceManager()->addService(kKernelBinderInstance,
                                                     sp<MyBinderRpcBenchmark>::make()));
        IPCThreadState::self()->joinThreadPool();
    }

    ProcessState::self()->setThreadPoolMaxThreadCount(1);
    ProcessState::self()->startThreadPool();

    gKernelBinder = defaultServiceManager()->waitForService(kKernelBinderInstance);
    CHECK_NE(nullptr, gKernelBinder.get());
#endif

    std::thread([addr]() {
    std::thread([addr]() {
        sp<RpcServer> server = RpcServer::make();
        sp<RpcServer> server = RpcServer::make();
        server->setRootObject(sp<MyBinderRpcBenchmark>::make());
        server->setRootObject(sp<MyBinderRpcBenchmark>::make());