/* * Copyright (C) 2016 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 #include #include #include #include #include #include #include #include #include // libutils: using android::OK; using android::sp; using android::status_t; using android::String16; // libbinder: using android::getService; using android::defaultServiceManager; using android::ProcessState; using android::binder::Status; // Standard library using std::vector; // Generated AIDL files using android::tests::binder::BnBenchmark; using android::tests::binder::IBenchmark; const char kServiceName[] = "android.tests.binder.IBenchmark"; class BenchmarkServiceAidl : public BnBenchmark { public: BenchmarkServiceAidl() {} virtual ~BenchmarkServiceAidl() = default; Status sendVec(const vector& data, vector* _aidl_return) { *_aidl_return = data; return Status::ok(); } }; bool startServer() { BenchmarkServiceAidl *service = new BenchmarkServiceAidl(); // Tells the kernel to spawn zero threads, but startThreadPool() below will still spawn one. ProcessState::self()->setThreadPoolMaxThreadCount(0); defaultServiceManager()->addService(String16(kServiceName), service); ProcessState::self()->startThreadPool(); return 0; } static void BM_sendVec_binder(benchmark::State& state) { sp service; // Prepare data to IPC vector data_vec; vector data_return; data_vec.resize(state.range(0)); for (int i = 0; i < state.range(0); i++) { data_vec[i] = i % 256; } // getService automatically retries status_t status = getService(String16(kServiceName), &service); if (status != OK) { state.SkipWithError("Failed to retrieve benchmark service."); } // Start running while (state.KeepRunning()) { service->sendVec(data_vec, &data_return); } } BENCHMARK(BM_sendVec_binder)->RangeMultiplier(2)->Range(4, 65536); int main(int argc, char* argv []) { ::benchmark::Initialize(&argc, argv); pid_t pid = fork(); if (pid == 0) { // Child, start benchmarks ::benchmark::RunSpecifiedBenchmarks(); } else { startServer(); while (true) { int stat, retval; retval = wait(&stat); if (retval == -1 && errno == ECHILD) { break; } } }; }