• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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:
BenchmarkServiceAidl()54     BenchmarkServiceAidl() {}
55     virtual ~BenchmarkServiceAidl() = default;
56 
sendVec(const vector<uint8_t> & data,vector<uint8_t> * _aidl_return)57     Status sendVec(const vector<uint8_t>& data, vector<uint8_t>* _aidl_return) {
58         *_aidl_return = data;
59         return Status::ok();
60     }
61 };
62 
startServer()63 bool startServer() {
64     BenchmarkServiceAidl *service = new BenchmarkServiceAidl();
65     // Tells the kernel to spawn zero threads, but startThreadPool() below will still spawn one.
66     ProcessState::self()->setThreadPoolMaxThreadCount(0);
67     defaultServiceManager()->addService(String16(kServiceName),
68                                         service);
69     ProcessState::self()->startThreadPool();
70     return 0;
71 }
72 
BM_sendVec_binder(benchmark::State & state)73 static void BM_sendVec_binder(benchmark::State& state) {
74     sp<IBenchmark> service;
75     // Prepare data to IPC
76     vector<uint8_t> data_vec;
77     vector<uint8_t> data_return;
78     data_vec.resize(state.range(0));
79     for (int i = 0; i < state.range(0); i++) {
80        data_vec[i] = i % 256;
81     }
82     // getService automatically retries
83     status_t status = getService(String16(kServiceName), &service);
84     if (status != OK) {
85         state.SkipWithError("Failed to retrieve benchmark service.");
86     }
87     // Start running
88     while (state.KeepRunning()) {
89        service->sendVec(data_vec, &data_return);
90     }
91 }
92 
93 BENCHMARK(BM_sendVec_binder)->RangeMultiplier(2)->Range(4, 65536);
94 
main(int argc,char * argv[])95 int main(int argc, char* argv []) {
96     ::benchmark::Initialize(&argc, argv);
97 
98     pid_t pid = fork();
99     if (pid == 0) {
100         // Child, start benchmarks
101         ::benchmark::RunSpecifiedBenchmarks();
102     } else {
103         startServer();
104         while (true) {
105             int stat, retval;
106             retval = wait(&stat);
107             if (retval == -1 && errno == ECHILD) {
108                 break;
109             }
110         }
111     };
112 }
113