xref: /aosp_15_r20/system/libhwbinder/vts/performance/Benchmark_binder.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 #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 using android::IBinder;
37 using android::BBinder;
38 
39 // libbinder:
40 using android::getService;
41 using android::defaultServiceManager;
42 using android::ProcessState;
43 using android::binder::Status;
44 
45 // Standard library
46 using std::vector;
47 
48 // Generated AIDL files
49 using android::tests::binder::BnBenchmark;
50 using android::tests::binder::IBenchmark;
51 
52 const char kServiceName[] = "android.tests.binder.IBenchmark";
53 
54 class BenchmarkServiceAidl : public BnBenchmark {
55  public:
BenchmarkServiceAidl()56     BenchmarkServiceAidl() {}
57     virtual ~BenchmarkServiceAidl() = default;
58 
sendVec(const vector<uint8_t> & data,vector<uint8_t> * _aidl_return)59     Status sendVec(const vector<uint8_t>& data, vector<uint8_t>* _aidl_return) {
60         *_aidl_return = data;
61         return Status::ok();
62     }
sendBinderVec(const vector<sp<IBinder>> & data,vector<sp<IBinder>> * _aidl_return)63     Status sendBinderVec(const vector<sp<IBinder>>& data, vector<sp<IBinder>>* _aidl_return) {
64         *_aidl_return = data;
65         return Status::ok();
66     }
67 };
68 
startServer()69 bool startServer() {
70     BenchmarkServiceAidl *service = new BenchmarkServiceAidl();
71     // Tells the kernel to spawn zero threads, but startThreadPool() below will still spawn one.
72     ProcessState::self()->setThreadPoolMaxThreadCount(0);
73     defaultServiceManager()->addService(String16(kServiceName),
74                                         service);
75     ProcessState::self()->startThreadPool();
76     return 0;
77 }
78 
BM_sendVec_binder(benchmark::State & state)79 static void BM_sendVec_binder(benchmark::State& state) {
80     sp<IBenchmark> service;
81     // Prepare data to IPC
82     vector<uint8_t> data_vec;
83     vector<uint8_t> data_return;
84     data_vec.resize(state.range(0));
85     for (int i = 0; i < state.range(0); i++) {
86        data_vec[i] = i % 256;
87     }
88     // getService automatically retries
89     status_t status = getService(String16(kServiceName), &service);
90     if (status != OK) {
91         state.SkipWithError("Failed to retrieve benchmark service.");
92     }
93     // Start running
94     while (state.KeepRunning()) {
95        service->sendVec(data_vec, &data_return);
96     }
97 }
98 
99 BENCHMARK(BM_sendVec_binder)->RangeMultiplier(2)->Range(4, 65536);
100 
BM_sendBinderVec_binder(benchmark::State & state)101 static void BM_sendBinderVec_binder(benchmark::State& state) {
102     sp<IBenchmark> service;
103     // Prepare data to IPC
104     vector<sp<IBinder>> data_vec;
105     vector<sp<IBinder>> data_return;
106     data_vec.resize(state.range(0));
107     for (int i = 0; i < state.range(0); i++) {
108        data_vec[i] = sp<BBinder>::make();
109     }
110     // getService automatically retries
111     status_t status = getService(String16(kServiceName), &service);
112     if (status != OK) {
113         state.SkipWithError("Failed to retrieve benchmark service.");
114     }
115     // Start running
116     while (state.KeepRunning()) {
117        service->sendBinderVec(data_vec, &data_return);
118     }
119 }
120 
121 BENCHMARK(BM_sendBinderVec_binder)->RangeMultiplier(2)->Range(4, 65536);
122 
main(int argc,char * argv[])123 int main(int argc, char* argv []) {
124     ::benchmark::Initialize(&argc, argv);
125 
126     pid_t pid = fork();
127     if (pid == 0) {
128         // Child, start benchmarks
129         ::benchmark::RunSpecifiedBenchmarks();
130     } else {
131         startServer();
132         while (true) {
133             int stat, retval;
134             retval = wait(&stat);
135             if (retval == -1 && errno == ECHILD) {
136                 break;
137             }
138         }
139     };
140 }
141