blob: b899c34e95f80c1d3203999ee0f1979d0ac9a35a [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
Todd Kjosabd88832016-11-22 16:46:36 -0800226 // Run the benchmark if client
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000227 ProcResults results(iterations);
228
Riley Andrews515aa0b2015-08-11 13:19:53 -0700229 chrono::time_point<chrono::high_resolution_clock> start, end;
Todd Kjosabd88832016-11-22 16:46:36 -0800230 for (int i = 0; (!cs_pair || num >= server_count) && i < iterations; i++) {
Riley Andrews515aa0b2015-08-11 13:19:53 -0700231 Parcel data, reply;
Todd Kjosabd88832016-11-22 16:46:36 -0800232 int target = cs_pair ? num % server_count : rand() % workers.size();
Sherry Yang52d15402017-06-15 11:32:06 -0700233 int sz = payload_size;
Todd Kjosabd88832016-11-22 16:46:36 -0800234
Todd Kjose61c9b12017-10-03 14:04:39 -0700235 while (sz >= sizeof(uint32_t)) {
Sherry Yang52d15402017-06-15 11:32:06 -0700236 data.writeInt32(0);
237 sz -= sizeof(uint32_t);
238 }
Riley Andrews515aa0b2015-08-11 13:19:53 -0700239 start = chrono::high_resolution_clock::now();
240 status_t ret = workers[target]->transact(BINDER_NOP, data, &reply);
241 end = chrono::high_resolution_clock::now();
242
243 uint64_t cur_time = uint64_t(chrono::duration_cast<chrono::nanoseconds>(end - start).count());
244 results.add_time(cur_time);
245
246 if (ret != NO_ERROR) {
247 cout << "thread " << num << " failed " << ret << "i : " << i << endl;
248 exit(EXIT_FAILURE);
249 }
250 }
Todd Kjosabd88832016-11-22 16:46:36 -0800251
Riley Andrews515aa0b2015-08-11 13:19:53 -0700252 // Signal completion to master and wait.
253 p.signal();
254 p.wait();
255
256 // Send results to master and wait for go to exit.
257 p.send(results);
258 p.wait();
259
260 exit(EXIT_SUCCESS);
261}
262
Todd Kjosabd88832016-11-22 16:46:36 -0800263Pipe make_worker(int num, int iterations, int worker_count, int payload_size, bool cs_pair)
Riley Andrews515aa0b2015-08-11 13:19:53 -0700264{
265 auto pipe_pair = Pipe::createPipePair();
266 pid_t pid = fork();
267 if (pid) {
268 /* parent */
Yi Kong5436a872022-08-03 14:40:29 +0800269 return std::move(get<0>(pipe_pair));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700270 } else {
271 /* child */
Yi Kong5436a872022-08-03 14:40:29 +0800272 worker_fx(num, worker_count, iterations, payload_size, cs_pair,
273 std::move(get<1>(pipe_pair)));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700274 /* never get here */
Yi Kong5436a872022-08-03 14:40:29 +0800275 return std::move(get<0>(pipe_pair));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700276 }
277
278}
279
280void wait_all(vector<Pipe>& v)
281{
282 for (int i = 0; i < v.size(); i++) {
283 v[i].wait();
284 }
285}
286
287void signal_all(vector<Pipe>& v)
288{
289 for (int i = 0; i < v.size(); i++) {
290 v[i].signal();
291 }
292}
293
Sherry Yang52d15402017-06-15 11:32:06 -0700294void run_main(int iterations,
295 int workers,
296 int payload_size,
297 int cs_pair,
298 bool training_round=false)
Riley Andrews515aa0b2015-08-11 13:19:53 -0700299{
Riley Andrews515aa0b2015-08-11 13:19:53 -0700300 vector<Pipe> pipes;
Riley Andrews515aa0b2015-08-11 13:19:53 -0700301 // Create all the workers and wait for them to spawn.
302 for (int i = 0; i < workers; i++) {
Todd Kjosabd88832016-11-22 16:46:36 -0800303 pipes.push_back(make_worker(i, iterations, workers, payload_size, cs_pair));
Riley Andrews515aa0b2015-08-11 13:19:53 -0700304 }
305 wait_all(pipes);
Carlos Llamas60a90512023-09-24 15:48:09 +0000306 // All workers have now been spawned and added themselves to service
307 // manager. Signal each worker to obtain a handle to the server workers from
308 // servicemanager.
309 signal_all(pipes);
310 // Wait for each worker to finish obtaining a handle to all server workers
311 // from servicemanager.
312 wait_all(pipes);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700313
Carlos Llamas60a90512023-09-24 15:48:09 +0000314 // Run the benchmark and wait for completion.
Riley Andrews515aa0b2015-08-11 13:19:53 -0700315 chrono::time_point<chrono::high_resolution_clock> start, end;
316 cout << "waiting for workers to complete" << endl;
317 start = chrono::high_resolution_clock::now();
318 signal_all(pipes);
319 wait_all(pipes);
320 end = chrono::high_resolution_clock::now();
321
322 // Calculate overall throughput.
323 double iterations_per_sec = double(iterations * workers) / (chrono::duration_cast<chrono::nanoseconds>(end - start).count() / 1.0E9);
324 cout << "iterations per sec: " << iterations_per_sec << endl;
325
326 // Collect all results from the workers.
327 cout << "collecting results" << endl;
328 signal_all(pipes);
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000329 ProcResults tot_results(0), tmp_results(0);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700330 for (int i = 0; i < workers; i++) {
Riley Andrews515aa0b2015-08-11 13:19:53 -0700331 pipes[i].recv(tmp_results);
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000332 tot_results.combine_with(tmp_results);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700333 }
Riley Andrews515aa0b2015-08-11 13:19:53 -0700334
335 // Kill all the workers.
336 cout << "killing workers" << endl;
337 signal_all(pipes);
338 for (int i = 0; i < workers; i++) {
339 int status;
340 wait(&status);
341 if (status != 0) {
342 cout << "nonzero child status" << status << endl;
343 }
344 }
Sherry Yang52d15402017-06-15 11:32:06 -0700345 if (training_round) {
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000346 // Sets warn_latency to 2 * worst from the training round.
347 warn_latency = 2 * tot_results.worst();
348 cout << "Max latency during training: " << tot_results.worst() / 1.0E6 << "ms" << endl;
Sherry Yang52d15402017-06-15 11:32:06 -0700349 } else {
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000350 tot_results.dump();
Sherry Yang52d15402017-06-15 11:32:06 -0700351 }
352}
353
354int main(int argc, char *argv[])
355{
356 int workers = 2;
357 int iterations = 10000;
358 int payload_size = 0;
359 bool cs_pair = false;
360 bool training_round = false;
Carlos Llamas9a85d302023-09-23 22:41:22 +0000361 int max_time_us;
Sherry Yang52d15402017-06-15 11:32:06 -0700362
363 // Parse arguments.
364 for (int i = 1; i < argc; i++) {
365 if (string(argv[i]) == "--help") {
366 cout << "Usage: binderThroughputTest [OPTIONS]" << endl;
367 cout << "\t-i N : Specify number of iterations." << endl;
368 cout << "\t-m N : Specify expected max latency in microseconds." << endl;
369 cout << "\t-p : Split workers into client/server pairs." << endl;
370 cout << "\t-s N : Specify payload size." << endl;
Carlos Llamasb21d7fd2023-09-23 21:20:51 +0000371 cout << "\t-t : Run training round." << endl;
Sherry Yang52d15402017-06-15 11:32:06 -0700372 cout << "\t-w N : Specify total number of workers." << endl;
373 return 0;
374 }
375 if (string(argv[i]) == "-w") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000376 if (i + 1 == argc) {
377 cout << "-w requires an argument\n" << endl;
378 exit(EXIT_FAILURE);
379 }
Sherry Yang52d15402017-06-15 11:32:06 -0700380 workers = atoi(argv[i+1]);
381 i++;
382 continue;
383 }
384 if (string(argv[i]) == "-i") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000385 if (i + 1 == argc) {
386 cout << "-i requires an argument\n" << endl;
387 exit(EXIT_FAILURE);
388 }
Sherry Yang52d15402017-06-15 11:32:06 -0700389 iterations = atoi(argv[i+1]);
390 i++;
391 continue;
392 }
393 if (string(argv[i]) == "-s") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000394 if (i + 1 == argc) {
395 cout << "-s requires an argument\n" << endl;
396 exit(EXIT_FAILURE);
397 }
Sherry Yang52d15402017-06-15 11:32:06 -0700398 payload_size = atoi(argv[i+1]);
399 i++;
Carlos Llamas71686da2023-09-23 20:34:58 +0000400 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700401 }
402 if (string(argv[i]) == "-p") {
403 // client/server pairs instead of spreading
404 // requests to all workers. If true, half
405 // the workers become clients and half servers
406 cs_pair = true;
Carlos Llamas71686da2023-09-23 20:34:58 +0000407 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700408 }
409 if (string(argv[i]) == "-t") {
410 // Run one training round before actually collecting data
411 // to get an approximation of max latency.
412 training_round = true;
Carlos Llamas71686da2023-09-23 20:34:58 +0000413 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700414 }
415 if (string(argv[i]) == "-m") {
Alice Ryhlb39f05a2024-01-05 13:10:12 +0000416 if (i + 1 == argc) {
417 cout << "-m requires an argument\n" << endl;
418 exit(EXIT_FAILURE);
419 }
Sherry Yang52d15402017-06-15 11:32:06 -0700420 // Caller specified the max latency in microseconds.
421 // No need to run training round in this case.
Carlos Llamas9a85d302023-09-23 22:41:22 +0000422 max_time_us = atoi(argv[i+1]);
423 if (max_time_us <= 0) {
Sherry Yang52d15402017-06-15 11:32:06 -0700424 cout << "Max latency -m must be positive." << endl;
425 exit(EXIT_FAILURE);
426 }
Alice Ryhl4d46cd82023-07-28 15:11:11 +0000427 warn_latency = max_time_us * 1000ull;
Carlos Llamas9a85d302023-09-23 22:41:22 +0000428 i++;
Carlos Llamas71686da2023-09-23 20:34:58 +0000429 continue;
Sherry Yang52d15402017-06-15 11:32:06 -0700430 }
431 }
432
433 if (training_round) {
434 cout << "Start training round" << endl;
435 run_main(iterations, workers, payload_size, cs_pair, training_round=true);
436 cout << "Completed training round" << endl << endl;
437 }
438
439 run_main(iterations, workers, payload_size, cs_pair);
Riley Andrews515aa0b2015-08-11 13:19:53 -0700440 return 0;
441}