Zhuoyao Zhang | d09965d | 2016-08-24 15:18:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 <benchmark/benchmark.h> |
| 18 | |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <binder/ProcessState.h> |
| 21 | #include <utils/String16.h> |
| 22 | #include <utils/StrongPointer.h> |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <android/tests/binder/IBenchmark.h> |
| 29 | #include <android/tests/binder/BnBenchmark.h> |
| 30 | |
| 31 | // libutils: |
| 32 | using android::OK; |
| 33 | using android::sp; |
| 34 | using android::status_t; |
| 35 | using android::String16; |
| 36 | |
| 37 | // libbinder: |
| 38 | using android::getService; |
Zhuoyao Zhang | d09965d | 2016-08-24 15:18:22 -0700 | [diff] [blame] | 39 | using android::defaultServiceManager; |
| 40 | using android::ProcessState; |
| 41 | using android::binder::Status; |
| 42 | |
| 43 | // Standard library |
| 44 | using std::vector; |
| 45 | |
| 46 | // Generated AIDL files |
| 47 | using android::tests::binder::BnBenchmark; |
| 48 | using android::tests::binder::IBenchmark; |
| 49 | |
| 50 | const char kServiceName[] = "android.tests.binder.IBenchmark"; |
| 51 | |
| 52 | class BenchmarkServiceAidl : public BnBenchmark { |
| 53 | public: |
| 54 | BenchmarkServiceAidl() {} |
| 55 | virtual ~BenchmarkServiceAidl() = default; |
| 56 | |
| 57 | Status sendVec(const vector<uint8_t>& data, vector<uint8_t>* _aidl_return) { |
| 58 | *_aidl_return = data; |
| 59 | return Status::ok(); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | bool startServer() { |
| 64 | BenchmarkServiceAidl *service = new BenchmarkServiceAidl(); |
| 65 | defaultServiceManager()->addService(String16(kServiceName), |
| 66 | service); |
| 67 | ProcessState::self()->startThreadPool(); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static void BM_sendVec_binder(benchmark::State& state) { |
| 72 | sp<IBenchmark> service; |
| 73 | // Prepare data to IPC |
| 74 | vector<uint8_t> data_vec; |
| 75 | vector<uint8_t> data_return; |
Martijn Coenen | a0a7e1e | 2016-11-14 14:51:33 +0100 | [diff] [blame] | 76 | data_vec.resize(state.range(0)); |
| 77 | for (int i = 0; i < state.range(0); i++) { |
Zhuoyao Zhang | d09965d | 2016-08-24 15:18:22 -0700 | [diff] [blame] | 78 | data_vec[i] = i % 256; |
| 79 | } |
| 80 | // getService automatically retries |
| 81 | status_t status = getService(String16(kServiceName), &service); |
| 82 | if (status != OK) { |
| 83 | state.SkipWithError("Failed to retrieve benchmark service."); |
| 84 | } |
| 85 | // Start running |
| 86 | while (state.KeepRunning()) { |
| 87 | service->sendVec(data_vec, &data_return); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | BENCHMARK(BM_sendVec_binder)->RangeMultiplier(2)->Range(4, 65536); |
| 92 | |
| 93 | int main(int argc, char* argv []) { |
| 94 | ::benchmark::Initialize(&argc, argv); |
| 95 | |
| 96 | pid_t pid = fork(); |
| 97 | if (pid == 0) { |
| 98 | // Child, start benchmarks |
| 99 | ::benchmark::RunSpecifiedBenchmarks(); |
| 100 | } else { |
| 101 | int stat; |
| 102 | startServer(); |
| 103 | while (true) { |
| 104 | int stat, retval; |
| 105 | retval = wait(&stat); |
| 106 | if (retval == -1 && errno == ECHILD) { |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | }; |
| 111 | } |