xref: /aosp_15_r20/system/libhwbinder/vts/performance/Benchmark.cpp (revision 77b80299c8bdfeca3ae6d0ce27ae1ad3db289be3)
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     sp<IBenchmark> service = IBenchmark::getService(gServiceName, true /* getStub */);
86     if (service == nullptr) {
87         state.SkipWithError("Failed to retrieve benchmark service.");
88     }
89     if (service->isRemote()) {
90         state.SkipWithError("Benchmark service is remote.");
91     }
92     BM_sendVec(state, service);
93 }
94 
BM_sendVec_binderize(benchmark::State & state)95 static void BM_sendVec_binderize(benchmark::State& state) {
96     android::hardware::details::waitForHwService(IBenchmark::descriptor, gServiceName);
97     sp<IBenchmark> service = IBenchmark::getService(gServiceName);
98     if (service == nullptr) {
99         state.SkipWithError("Failed to retrieve benchmark service.");
100     }
101     if (!service->isRemote()) {
102         state.SkipWithError("Unable to fetch remote benchmark service.");
103     }
104     BM_sendVec(state, service);
105 }
106 
main(int argc,char * argv[])107 int main(int argc, char* argv []) {
108     android::hardware::details::setTrebleTestingOverride(true);
109 
110     enum HwBinderMode {
111         kBinderize = 0,
112         kPassthrough = 1,
113     };
114     HwBinderMode mode = HwBinderMode::kBinderize;
115 
116     // Parse arguments.
117     for (int i = 1; i < argc; i++) {
118         if (string(argv[i]) == "-m") {
119             if (!strcmp(argv[i + 1], "PASSTHROUGH")) {
120                 mode = HwBinderMode::kPassthrough;
121             }
122             break;
123         }
124     }
125     if (mode == HwBinderMode::kBinderize) {
126         BENCHMARK(BM_sendVec_binderize)->RangeMultiplier(2)->Range(4, 65536);
127     } else {
128         BENCHMARK(BM_sendVec_passthrough)->RangeMultiplier(2)->Range(4, 65536);
129     }
130 
131     ::benchmark::Initialize(&argc, argv);
132 
133     pid_t pid = fork();
134     if (pid == 0) {
135         // Child, start benchmarks
136         ::benchmark::RunSpecifiedBenchmarks();
137     } else {
138         startServer();
139         while (true) {
140             int stat, retval;
141             retval = wait(&stat);
142             if (retval == -1 && errno == ECHILD) {
143                 break;
144             }
145         }
146     };
147 }
148