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 #define LOG_TAG "libhwbinder_benchmark"
18 
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 
23 #include <iostream>
24 
25 #include <log/log.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <benchmark/benchmark.h>
29 #include <hidl/Status.h>
30 #include <hidl/ServiceManagement.h>
31 
32 #include <android/hardware/tests/libhwbinder/1.0/IBenchmark.h>
33 
34 // libutils:
35 using android::OK;
36 using android::sp;
37 using android::status_t;
38 
39 // libhidl:
40 using android::hardware::defaultServiceManager;
41 using android::hardware::Return;
42 using android::hardware::Void;
43 using android::hardware::hidl_vec;
44 
45 // Standard library
46 using std::cerr;
47 using std::cout;
48 using std::endl;
49 using std::string;
50 using std::unique_ptr;
51 using std::vector;
52 
53 // Generated HIDL files
54 using android::hardware::tests::libhwbinder::V1_0::IBenchmark;
55 
56 const char gServiceName[] = "android.hardware.tests.libhwbinder.IBenchmark";
57 
startServer()58 static bool startServer() {
59     sp<IBenchmark> service = IBenchmark::getService(gServiceName, true);
60     status_t status = service->registerAsService(gServiceName);
61 
62     if (status != ::android::OK) {
63         ALOGE("Failed to register service %s.", gServiceName);
64         exit(EXIT_FAILURE);
65     }
66 
67     return 0;
68 }
69 
BM_sendVec(benchmark::State & state,sp<IBenchmark> service)70 static void BM_sendVec(benchmark::State& state, sp<IBenchmark> service) {
71     // Prepare data to IPC
72     hidl_vec<uint8_t> data_vec;
73     data_vec.resize(state.range(0));
74     for (int i = 0; i < state.range(0); i++) {
75        data_vec[i] = i % 256;
76     }
77     // Start running
78     while (state.KeepRunning()) {
79        service->sendVec(data_vec, [&] (const auto &/*res*/) {
80                });
81     }
82 }
83 
BM_sendVec_passthrough(benchmark::State & state)84 static void BM_sendVec_passthrough(benchmark::State& state) {
85     // getService automatically retries
86     sp<IBenchmark> service = IBenchmark::getService(gServiceName, true /* getStub */);
87     if (service == nullptr) {
88         state.SkipWithError("Failed to retrieve benchmark service.");
89     }
90     if (service->isRemote()) {
91         state.SkipWithError("Benchmark service is remote.");
92     }
93     BM_sendVec(state, service);
94 }
95 
BM_sendVec_binderize(benchmark::State & state)96 static void BM_sendVec_binderize(benchmark::State& state) {
97     // getService automatically retries
98     sp<IBenchmark> service = IBenchmark::getService(gServiceName);
99     if (service == nullptr) {
100         state.SkipWithError("Failed to retrieve benchmark service.");
101     }
102     if (!service->isRemote()) {
103         state.SkipWithError("Unable to fetch remote benchmark service.");
104     }
105     BM_sendVec(state, service);
106 }
107 
main(int argc,char * argv[])108 int main(int argc, char* argv []) {
109     android::hardware::details::setTrebleTestingOverride(true);
110 
111     enum HwBinderMode {
112         kBinderize = 0,
113         kPassthrough = 1,
114     };
115     HwBinderMode mode = HwBinderMode::kBinderize;
116 
117     // Parse arguments.
118     for (int i = 1; i < argc; i++) {
119         if (string(argv[i]) == "-m") {
120             if (!strcmp(argv[i + 1], "PASSTHROUGH")) {
121                 mode = HwBinderMode::kPassthrough;
122             }
123             break;
124         }
125     }
126     if (mode == HwBinderMode::kBinderize) {
127         BENCHMARK(BM_sendVec_binderize)->RangeMultiplier(2)->Range(4, 65536);
128     } else {
129         BENCHMARK(BM_sendVec_passthrough)->RangeMultiplier(2)->Range(4, 65536);
130     }
131 
132     ::benchmark::Initialize(&argc, argv);
133 
134     pid_t pid = fork();
135     if (pid == 0) {
136         // Child, start benchmarks
137         ::benchmark::RunSpecifiedBenchmarks();
138     } else {
139         startServer();
140         while (true) {
141             int stat, retval;
142             retval = wait(&stat);
143             if (retval == -1 && errno == ECHILD) {
144                 break;
145             }
146         }
147     };
148 }
149