blob: 10912c7363132a366aaed3407a8c022794b675ff [file] [log] [blame]
Riley Andrews515aa0b2015-08-11 13:19:53 -07001#include <binder/Binder.h>
2#include <binder/IBinder.h>
3#include <binder/IPCThreadState.h>
4#include <binder/IServiceManager.h>
5#include <string>
6#include <cstring>
7#include <cstdlib>
8#include <cstdio>
9
10#include <iostream>
11#include <vector>
12#include <tuple>
13
14#include <unistd.h>
15#include <sys/wait.h>
16
17using namespace std;
18using namespace android;
19
20enum BinderWorkerServiceCode {
21 BINDER_NOP = IBinder::FIRST_CALL_TRANSACTION,
22};
23
24#define ASSERT_TRUE(cond) \
25do { \
26 if (!(cond)) {\
27 cerr << __func__ << ":" << __LINE__ << " condition:" << #cond << " failed\n" << endl; \
28 exit(EXIT_FAILURE); \
29 } \
30} while (0)
31
32class BinderWorkerService : public BBinder
33{
34public:
35 BinderWorkerService() {}
36 ~BinderWorkerService() {}
37 virtual status_t onTransact(uint32_t code,
38 const Parcel& data, Parcel* reply,
39 uint32_t flags = 0) {
40 (void)flags;
41 (void)data;
42 (void)reply;
43 switch (code) {
44 case BINDER_NOP:
45 return NO_ERROR;
46 default:
47 return UNKNOWN_TRANSACTION;
48 };
49 }
50};
51
Alice Ryhl4d46cd82023-07-28 15:11:11 +000052static uint64_t warn_latency = std::numeric_limits<uint64_t>::max();
53
54struct ProcResults {
55 vector<uint64_t> data;
56
57 ProcResults(size_t capacity) { data.reserve(capacity); }
58
59 void add_time(uint64_t time) { data.push_back(time); }
60 void combine_with(const ProcResults& append) {
61 data.insert(data.end(), append.data.begin(), append.data.end());
62 }
63 uint64_t worst() {
64 return *max_element(data.begin(), data.end());
65 }
66 void dump() {
67 if (data.size() == 0) {
68 // This avoids index-out-of-bounds below.
69 cout << "error: no data\n" << endl;
70 return;
71 }
72
73 size_t num_long_transactions = 0;
74 for (uint64_t elem : data) {
75 if (elem > warn_latency) {
76 num_long_transactions += 1;
77 }
78 }
79
80 if (num_long_transactions > 0) {
81 cout << (double)num_long_transactions / data.size() << "% of transactions took longer "
82 "than estimated max latency. Consider setting -m to be higher than "
83 << worst() / 1000 << " microseconds" << endl;
84 }
85
86 sort(data.begin(), data.end());
87
88 uint64_t total_time = 0;
89 for (uint64_t elem : data) {
90 total_time += elem;
91 }
92
93 double best = (double)data[0] / 1.0E6;
94 double worst = (double)data.back() / 1.0E6;
95 double average = (double)total_time / data.size() / 1.0E6;
96 cout << "average:" << average << "ms worst:" << worst << "ms best:" << best << "ms" << endl;
97
98 double percentile_50 = data[(50 * data.size()) / 100] / 1.0E6;
99 double percentile_90 = data[(90 * data.size()) / 100] / 1.0E6;
100 double percentile_95 = data[(95 * data.size()) / 100] / 1.0E6;
101 double percentile_99 = data[(99 * data.size()) / 100] / 1.0E6;
102 cout << "50%: " << percentile_50 << " ";
103 cout << "90%: " << percentile_90 << " ";
104 cout << "95%: " << percentile_95 << " ";
105 cout << "99%: " << percentile_99 << endl;
106 }
107};
108
Riley Andrews515aa0b2015-08-11 13:19:53 -0700109class Pipe {
110 int m_readFd;
111 int m_writeFd;
112 Pipe(int readFd, int writeFd) : m_readFd{readFd}, m_writeFd{writeFd} {}
113 Pipe(const Pipe &) = delete;
114 Pipe& operator=(const Pipe &) = delete;
115 Pipe& operator=(const Pipe &&) = delete;
116public:
117 Pipe(Pipe&& rval) noexcept {
118 m_readFd = rval.m_readFd;
119 m_writeFd = rval.m_writeFd;
120 rval.m_readFd = 0;
121 rval.m_writeFd = 0;
122 }
123 ~Pipe() {
124 if (m_readFd)
125 close(m_readFd);
126 if (m_writeFd)
127 close(m_writeFd);
128 }
129 void signal() {
130 bool val = true;
131 int error = write(m_writeFd, &val, sizeof(val));
132 ASSERT_TRUE(error >= 0);
133 };
134 void wait() {
135 bool val = false;
136 int error = read(m_readFd, &val, sizeof(val));
137 ASSERT_TRUE(error >= 0);
138 }
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000139 void send(const ProcResults& v) {
140 size_t num_elems = v.data.size();
141
142 int error = write(m_writeFd, &num_elems, sizeof(size_t));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700143 ASSERT_TRUE(error >= 0);
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000144
145 char* to_write = (char*)v.data.data();
146 size_t num_bytes = sizeof(uint64_t) * num_elems;
147
148 while (num_bytes > 0) {
149 int ret = write(m_writeFd, to_write, num_bytes);
150 ASSERT_TRUE(ret >= 0);
151 num_bytes -= ret;
152 to_write += ret;
153 }
Riley Andrews515aa0b2015-08-11 13:19:53 -0700154 }
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000155 void recv(ProcResults& v) {
156 size_t num_elems = 0;
157 int error = read(m_readFd, &num_elems, sizeof(size_t));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700158 ASSERT_TRUE(error >= 0);
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000159
160 v.data.resize(num_elems);
161 char* read_to = (char*)v.data.data();
162 size_t num_bytes = sizeof(uint64_t) * num_elems;
163
164 while (num_bytes > 0) {
165 int ret = read(m_readFd, read_to, num_bytes);
166 ASSERT_TRUE(ret >= 0);
167 num_bytes -= ret;
168 read_to += ret;
169 }
Riley Andrews515aa0b2015-08-11 13:19:53 -0700170 }
171 static tuple<Pipe, Pipe> createPipePair() {
172 int a[2];
173 int b[2];
174
175 int error1 = pipe(a);
176 int error2 = pipe(b);
177 ASSERT_TRUE(error1 >= 0);
178 ASSERT_TRUE(error2 >= 0);
179
180 return make_tuple(Pipe(a[0], b[1]), Pipe(b[0], a[1]));
181 }
182};
183
Riley Andrews515aa0b2015-08-11 13:19:53 -0700184String16 generateServiceName(int num)
185{
186 char num_str[32];
187 snprintf(num_str, sizeof(num_str), "%d", num);
188 String16 serviceName = String16("binderWorker") + String16(num_str);
189 return serviceName;
190}
191
Sherry Yang52d15402017-06-15 11:32:06 -0700192void worker_fx(int num,
193 int worker_count,
194 int iterations,
195 int payload_size,
196 bool cs_pair,
197 Pipe p)
Riley Andrews515aa0b2015-08-11 13:19:53 -0700198{
199 // Create BinderWorkerService and for go.
200 ProcessState::self()->startThreadPool();
201 sp<IServiceManager> serviceMgr = defaultServiceManager();
202 sp<BinderWorkerService> service = new BinderWorkerService;
203 serviceMgr->addService(generateServiceName(num), service);
204
205 srand(num);
206 p.signal();
207 p.wait();
208
Todd Kjosabd88832016-11-22 16:46:36 -0800209 // If client/server pairs, then half the workers are
210 // servers and half are clients
211 int server_count = cs_pair ? worker_count / 2 : worker_count;
212
Riley Andrews515aa0b2015-08-11 13:19:53 -0700213 // Get references to other binder services.
214 cout << "Created BinderWorker" << num << endl;
215 (void)worker_count;
216 vector<sp<IBinder> > workers;
Todd Kjosabd88832016-11-22 16:46:36 -0800217 for (int i = 0; i < server_count; i++) {
Riley Andrews515aa0b2015-08-11 13:19:53 -0700218 if (num == i)
219 continue;
Carlos Llamas8c513522023-09-23 20:24:20 +0000220 workers.push_back(serviceMgr->waitForService(generateServiceName(i)));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700221 }
222
Carlos Llamas60a90512023-09-24 15:48:09 +0000223 p.signal();
224 p.wait();
225
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000226 ProcResults results(iterations);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700227 chrono::time_point<chrono::high_resolution_clock> start, end;
Todd Kjosabd88832016-11-22 16:46:36 -0800228
Carlos Llamas625212e2023-09-24 16:16:18 +0000229 // Skip the benchmark if server of a cs_pair.
230 if (!(cs_pair && num < server_count)) {
231 for (int i = 0; i < iterations; i++) {
232 Parcel data, reply;
233 int target = cs_pair ? num % server_count : rand() % workers.size();
234 int sz = payload_size;
Riley Andrews515aa0b2015-08-11 13:19:53 -0700235
Carlos Llamas625212e2023-09-24 16:16:18 +0000236 while (sz >= sizeof(uint32_t)) {
237 data.writeInt32(0);
238 sz -= sizeof(uint32_t);
239 }
240 start = chrono::high_resolution_clock::now();
241 status_t ret = workers[target]->transact(BINDER_NOP, data, &reply);
242 end = chrono::high_resolution_clock::now();
Riley Andrews515aa0b2015-08-11 13:19:53 -0700243
Carlos Llamas625212e2023-09-24 16:16:18 +0000244 uint64_t cur_time = uint64_t(chrono::duration_cast<chrono::nanoseconds>(end - start).count());
245 results.add_time(cur_time);
246
247 if (ret != NO_ERROR) {
248 cout << "thread " << num << " failed " << ret << "i : " << i << endl;
249 exit(EXIT_FAILURE);
250 }
Riley Andrews515aa0b2015-08-11 13:19:53 -0700251 }
252 }
Todd Kjosabd88832016-11-22 16:46:36 -0800253
Riley Andrews515aa0b2015-08-11 13:19:53 -0700254 // Signal completion to master and wait.
255 p.signal();
256 p.wait();
257
258 // Send results to master and wait for go to exit.
259 p.send(results);
260 p.wait();
261
262 exit(EXIT_SUCCESS);
263}
264
Todd Kjosabd88832016-11-22 16:46:36 -0800265Pipe make_worker(int num, int iterations, int worker_count, int payload_size, bool cs_pair)
Riley Andrews515aa0b2015-08-11 13:19:53 -0700266{
267 auto pipe_pair = Pipe::createPipePair();
268 pid_t pid = fork();
269 if (pid) {
270 /* parent */
Yi Kong5436a872022-08-03 14:40:29 +0800271 return std::move(get<0>(pipe_pair));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700272 } else {
273 /* child */
Yi Kong5436a872022-08-03 14:40:29 +0800274 worker_fx(num, worker_count, iterations, payload_size, cs_pair,
275 std::move(get<1>(pipe_pair)));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700276 /* never get here */
Yi Kong5436a872022-08-03 14:40:29 +0800277 return std::move(get<0>(pipe_pair));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700278 }
279
280}
281
282void wait_all(vector<Pipe>& v)
283{
284 for (int i = 0; i < v.size(); i++) {
285 v[i].wait();
286 }
287}
288
289void signal_all(vector<Pipe>& v)
290{
291 for (int i = 0; i < v.size(); i++) {
292 v[i].signal();
293 }
294}
295
Sherry Yang52d15402017-06-15 11:32:06 -0700296void run_main(int iterations,
297 int workers,
298 int payload_size,
299 int cs_pair,
300 bool training_round=false)
Riley Andrews515aa0b2015-08-11 13:19:53 -0700301{
Riley Andrews515aa0b2015-08-11 13:19:53 -0700302 vector<Pipe> pipes;
Riley Andrews515aa0b2015-08-11 13:19:53 -0700303 // Create all the workers and wait for them to spawn.
304 for (int i = 0; i < workers; i++) {
Todd Kjosabd88832016-11-22 16:46:36 -0800305 pipes.push_back(make_worker(i, iterations, workers, payload_size, cs_pair));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700306 }
307 wait_all(pipes);
Carlos Llamas60a90512023-09-24 15:48:09 +0000308 // All workers have now been spawned and added themselves to service
309 // manager. Signal each worker to obtain a handle to the server workers from
310 // servicemanager.
311 signal_all(pipes);
312 // Wait for each worker to finish obtaining a handle to all server workers
313 // from servicemanager.
314 wait_all(pipes);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700315
Carlos Llamas60a90512023-09-24 15:48:09 +0000316 // Run the benchmark and wait for completion.
Riley Andrews515aa0b2015-08-11 13:19:53 -0700317 chrono::time_point<chrono::high_resolution_clock> start, end;
318 cout << "waiting for workers to complete" << endl;
319 start = chrono::high_resolution_clock::now();
320 signal_all(pipes);
321 wait_all(pipes);
322 end = chrono::high_resolution_clock::now();
323
324 // Calculate overall throughput.
325 double iterations_per_sec = double(iterations * workers) / (chrono::duration_cast<chrono::nanoseconds>(end - start).count() / 1.0E9);
326 cout << "iterations per sec: " << iterations_per_sec << endl;
327
328 // Collect all results from the workers.
329 cout << "collecting results" << endl;
330 signal_all(pipes);
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000331 ProcResults tot_results(0), tmp_results(0);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700332 for (int i = 0; i < workers; i++) {
Riley Andrews515aa0b2015-08-11 13:19:53 -0700333 pipes[i].recv(tmp_results);
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000334 tot_results.combine_with(tmp_results);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700335 }
Riley Andrews515aa0b2015-08-11 13:19:53 -0700336
337 // Kill all the workers.
338 cout << "killing workers" << endl;
339 signal_all(pipes);
340 for (int i = 0; i < workers; i++) {
341 int status;
342 wait(&status);
343 if (status != 0) {
344 cout << "nonzero child status" << status << endl;
345 }
346 }
Sherry Yang52d15402017-06-15 11:32:06 -0700347 if (training_round) {
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000348 // Sets warn_latency to 2 * worst from the training round.
349 warn_latency = 2 * tot_results.worst();
350 cout << "Max latency during training: " << tot_results.worst() / 1.0E6 << "ms" << endl;
Sherry Yang52d15402017-06-15 11:32:06 -0700351 } else {
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000352 tot_results.dump();
Sherry Yang52d15402017-06-15 11:32:06 -0700353 }
354}
355
356int main(int argc, char *argv[])
357{
358 int workers = 2;
359 int iterations = 10000;
360 int payload_size = 0;
361 bool cs_pair = false;
362 bool training_round = false;
Carlos Llamas9a85d302023-09-23 22:41:22 +0000363 int max_time_us;
Sherry Yang52d15402017-06-15 11:32:06 -0700364
365 // Parse arguments.
366 for (int i = 1; i < argc; i++) {
367 if (string(argv[i]) == "--help") {
368 cout << "Usage: binderThroughputTest [OPTIONS]" << endl;
369 cout << "\t-i N : Specify number of iterations." << endl;
370 cout << "\t-m N : Specify expected max latency in microseconds." << endl;
371 cout << "\t-p : Split workers into client/server pairs." << endl;
372 cout << "\t-s N : Specify payload size." << endl;
Carlos Llamasb21d7fd2023-09-23 21:20:51 +0000373 cout << "\t-t : Run training round." << endl;
Sherry Yang52d15402017-06-15 11:32:06 -0700374 cout << "\t-w N : Specify total number of workers." << endl;
375 return 0;
376 }
377 if (string(argv[i]) == "-w") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000378 if (i + 1 == argc) {
379 cout << "-w requires an argument\n" << endl;
380 exit(EXIT_FAILURE);
381 }
Sherry Yang52d15402017-06-15 11:32:06 -0700382 workers = atoi(argv[i+1]);
383 i++;
384 continue;
385 }
386 if (string(argv[i]) == "-i") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000387 if (i + 1 == argc) {
388 cout << "-i requires an argument\n" << endl;
389 exit(EXIT_FAILURE);
390 }
Sherry Yang52d15402017-06-15 11:32:06 -0700391 iterations = atoi(argv[i+1]);
392 i++;
393 continue;
394 }
395 if (string(argv[i]) == "-s") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000396 if (i + 1 == argc) {
397 cout << "-s requires an argument\n" << endl;
398 exit(EXIT_FAILURE);
399 }
Sherry Yang52d15402017-06-15 11:32:06 -0700400 payload_size = atoi(argv[i+1]);
401 i++;
Carlos Llamas71686da2023-09-23 20:34:58 +0000402 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700403 }
404 if (string(argv[i]) == "-p") {
405 // client/server pairs instead of spreading
406 // requests to all workers. If true, half
407 // the workers become clients and half servers
408 cs_pair = true;
Carlos Llamas71686da2023-09-23 20:34:58 +0000409 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700410 }
411 if (string(argv[i]) == "-t") {
412 // Run one training round before actually collecting data
413 // to get an approximation of max latency.
414 training_round = true;
Carlos Llamas71686da2023-09-23 20:34:58 +0000415 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700416 }
417 if (string(argv[i]) == "-m") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000418 if (i + 1 == argc) {
419 cout << "-m requires an argument\n" << endl;
420 exit(EXIT_FAILURE);
421 }
Sherry Yang52d15402017-06-15 11:32:06 -0700422 // Caller specified the max latency in microseconds.
423 // No need to run training round in this case.
Carlos Llamas9a85d302023-09-23 22:41:22 +0000424 max_time_us = atoi(argv[i+1]);
425 if (max_time_us <= 0) {
Sherry Yang52d15402017-06-15 11:32:06 -0700426 cout << "Max latency -m must be positive." << endl;
427 exit(EXIT_FAILURE);
428 }
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000429 warn_latency = max_time_us * 1000ull;
Carlos Llamas9a85d302023-09-23 22:41:22 +0000430 i++;
Carlos Llamas71686da2023-09-23 20:34:58 +0000431 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700432 }
433 }
434
435 if (training_round) {
436 cout << "Start training round" << endl;
437 run_main(iterations, workers, payload_size, cs_pair, training_round=true);
438 cout << "Completed training round" << endl << endl;
439 }
440
441 run_main(iterations, workers, payload_size, cs_pair);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700442 return 0;
443}