blob: 403363cee261c996aa5cb7acae6d47014e3a11e1 [file] [log] [blame]
Igor Opaniukab88c952017-02-14 13:22:54 +02001/*
2 * Copyright (c) 2016, Linaro Limited
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <errno.h>
29#include <fcntl.h>
30#include <inttypes.h>
31#include <limits.h>
32#include <libgen.h>
33#include <pthread.h>
34#include <string.h>
35#include <stdio.h>
36#include <stdbool.h>
37#include <stdlib.h>
38#include <sys/types.h>
39#include <sys/mman.h>
40#include <sys/stat.h>
41#include <sys/wait.h>
42#include <unistd.h>
Igor Opaniukf1f3fd02017-09-14 15:34:56 +030043#include <yaml.h>
Igor Opaniukab88c952017-02-14 13:22:54 +020044
45#include "benchmark_aux.h"
Igor Opaniukf1f3fd02017-09-14 15:34:56 +030046#include "common.h"
Igor Opaniukab88c952017-02-14 13:22:54 +020047
Igor Opaniukf1f3fd02017-09-14 15:34:56 +030048#define MAX_SCALAR 20
49static struct tee_ts_global *bench_ts_global;
Igor Opaniukab88c952017-02-14 13:22:54 +020050
51static const TEEC_UUID pta_benchmark_uuid = PTA_BENCHMARK_UUID;
Igor Opaniukab88c952017-02-14 13:22:54 +020052static TEEC_SharedMemory ts_buf_shm = {
53 .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
54};
Igor Opaniukab88c952017-02-14 13:22:54 +020055static TEEC_Context ctx;
56static TEEC_Session sess;
57
Igor Opaniukf1f3fd02017-09-14 15:34:56 +030058static volatile sig_atomic_t is_running;
59static yaml_emitter_t emitter;
60
61
62void sigint_handler(int data)
63{
64 (void)data;
65
66 is_running = 0;
67}
68
Igor Opaniukab88c952017-02-14 13:22:54 +020069static void open_bench_pta(void)
70{
71 TEEC_Result res;
72 uint32_t err_origin;
73
74 res = TEEC_InitializeContext(NULL, &ctx);
75 tee_check_res(res, "TEEC_InitializeContext");
76
77 res = TEEC_OpenSession(&ctx, &sess, &pta_benchmark_uuid,
78 TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
79 tee_check_res(res, "TEEC_OpenSession");
80}
81
82static void close_bench_pta(void)
83{
84 /* release benchmark timestamp shm */
85 TEEC_ReleaseSharedMemory(&ts_buf_shm);
86 TEEC_CloseSession(&sess);
87 TEEC_FinalizeContext(&ctx);
88}
89
90static void init_ts_global(void *ts_global, uint32_t cores)
91{
Yves Leflochb31789d2017-08-01 14:51:18 +020092 unsigned int i;
Igor Opaniukab88c952017-02-14 13:22:54 +020093 struct tee_ts_cpu_buf *cpu_buf;
94
95 /* init global timestamp buffer */
96 bench_ts_global = (struct tee_ts_global *)ts_global;
97 bench_ts_global->cores = cores;
98
99 /* init per-cpu timestamp buffers */
Yves Leflochb31789d2017-08-01 14:51:18 +0200100 for (i = 0; i < cores; i++) {
Igor Opaniukab88c952017-02-14 13:22:54 +0200101 cpu_buf = &bench_ts_global->cpu_buf[i];
102 memset(cpu_buf, 0, sizeof(struct tee_ts_cpu_buf));
103 }
104}
105
106static void register_bench_buf(uint32_t cores)
107{
108 TEEC_Result res;
109 TEEC_Operation op = { 0 };
110 uint32_t ret_orig;
111
112 ts_buf_shm.size = sizeof(struct tee_ts_global) +
113 sizeof(struct tee_ts_cpu_buf) * cores;
114
115 /* allocate global timestamp buffer */
116 res = TEEC_AllocateSharedMemory(&ctx, &ts_buf_shm);
117 tee_check_res(res, "TEEC_AllocateSharedMemory");
118
119 init_ts_global(ts_buf_shm.buffer, cores);
120
121 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
122 TEEC_NONE, TEEC_NONE, TEEC_NONE);
123 op.params[0].memref.parent = &ts_buf_shm;
124
125 TEEC_InvokeCommand(&sess, BENCHMARK_CMD_REGISTER_MEMREF,
126 &op, &ret_orig);
127 tee_check_res(res, "TEEC_InvokeCommand");
128}
129
130static void unregister_bench(void)
131{
132 TEEC_Result res;
133 TEEC_Operation op = { 0 };
134 uint32_t ret_orig;
135
136 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
137 TEEC_NONE, TEEC_NONE, TEEC_NONE);
138
139 res = TEEC_InvokeCommand(&sess, BENCHMARK_CMD_UNREGISTER,
140 &op, &ret_orig);
141 tee_check_res(res, "TEEC_InvokeCommand");
142}
143
144static void usage(char *progname)
145{
146 fprintf(stderr, "Call latency benchmark tool for OP-TEE\n\n");
147 fprintf(stderr, "Usage:\n");
148 fprintf(stderr, " %s -h\n", progname);
149 fprintf(stderr, " %s host_app [host_app_args]\n", progname);
150 fprintf(stderr, "Options:\n");
151 fprintf(stderr, " -h Print this help and exit\n");
152 fprintf(stderr, " host_app Path to host app to benchmark\n");
153 fprintf(stderr, " host_app_args Original host app args\n");
154}
155
156static int timestamp_pop(struct tee_ts_cpu_buf *cpu_buf,
157 struct tee_time_st *ts)
158{
159 uint64_t ts_tail;
160
161 if (!cpu_buf && !ts)
162 return RING_BADPARM;
163
164 if (cpu_buf->tail >= cpu_buf->head)
165 return RING_NODATA;
166
167 ts_tail = cpu_buf->tail++;
168 *ts = cpu_buf->stamps[ts_tail & TEE_BENCH_MAX_MASK];
169
170 return 0;
171}
172
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300173static bool init_emitter(FILE *ts_file)
174{
175 yaml_event_t event;
176
177 if (!yaml_emitter_initialize(&emitter))
178 ERROR_RETURN_FALSE("Can't initialize YAML emitter");
179
180 yaml_emitter_set_canonical(&emitter, 0);
181 yaml_emitter_set_unicode(&emitter, 1);
182 yaml_emitter_set_output_file(&emitter, ts_file);
183
184 /* Stream start */
185 if (!yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING))
186 ERROR_GOTO(emitter_delete,
187 "Failed to initialize YAML stream start event");
188 if (!yaml_emitter_emit(&emitter, &event))
189 ERROR_GOTO(emitter_delete, "Failed to emit YAML stream start event");
190
191 /* Document start */
192 if (!yaml_document_start_event_initialize(&event,
193 NULL, NULL, NULL, YAML_IMPLICIT))
194 ERROR_GOTO(emitter_delete,
195 "Failed to initialize YAML document start event");
196 if (!yaml_emitter_emit(&emitter, &event))
197 ERROR_GOTO(emitter_delete, "Failed to emit YAML doc start event");
198
199 /* Mapping start */
200 if (!yaml_mapping_start_event_initialize(&event,
201 NULL, NULL , YAML_IMPLICIT,
202 YAML_ANY_SEQUENCE_STYLE))
203 ERROR_GOTO(emitter_delete,
204 "Failed to initialize YAML mapping start event");
205 if (!yaml_emitter_emit(&emitter, &event))
206 ERROR_GOTO(emitter_delete,
207 "Failed to emit YAML sequence mapping event");
208 /* Key timestamps */
209 yaml_scalar_event_initialize(&event, NULL, NULL,
210 (yaml_char_t *)"timestamps", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE);
211 if (!yaml_emitter_emit(&emitter, &event))
212 ERROR_RETURN_FALSE("Failed to emit YAML scalar");
213
214 /* Sequence start */
215 if (!yaml_sequence_start_event_initialize(&event,
216 NULL, NULL , YAML_IMPLICIT,
217 YAML_ANY_SEQUENCE_STYLE))
218 ERROR_GOTO(emitter_delete,
219 "Failed to initialize YAML sequence start event");
220 if (!yaml_emitter_emit(&emitter, &event))
221 ERROR_GOTO(emitter_delete, "Failed to emit YAML sequence start event");
222
223 return true;
224emitter_delete:
225 yaml_emitter_delete(&emitter);
226 return false;
227}
228
229static void deinit_emitter()
230{
231 yaml_event_t event;
232
233 /* Sequence cmd */
234 if (!yaml_sequence_end_event_initialize(&event))
235 ERROR_GOTO(emitter_delete,
236 "Failed to initialize YAML sequence end event");
237 if (!yaml_emitter_emit(&emitter, &event))
238 ERROR_GOTO(emitter_delete, "Failed to emit YAML sequence end event");
239
240 /* Mapping end */
241 if (!yaml_mapping_end_event_initialize(&event))
242 ERROR_GOTO(emitter_delete,
243 "Failed to initialize YAML mapping end event");
244 if (!yaml_emitter_emit(&emitter, &event))
245 ERROR_GOTO(emitter_delete,
246 "Failed to emit YAML mapping end event");
247
248 /* Document end */
249 if (!yaml_document_end_event_initialize(&event, 0))
250 ERROR_GOTO(emitter_delete,
251 "Failed to initialize YAML document end event");
252 if (!yaml_emitter_emit(&emitter, &event))
253 ERROR_GOTO(emitter_delete, "Failed to emit YAML doc end event");
254
255 /* Stream end */
256 if (!yaml_stream_end_event_initialize(&event))
257 ERROR_GOTO(emitter_delete,
258 "Error occured while initialising YAML stream end event");
259 if (!yaml_emitter_emit(&emitter, &event))
260 ERROR_GOTO(emitter_delete, "Failed to emit YAML stream end event");
261
262emitter_delete:
263 yaml_emitter_delete(&emitter);
264}
265
266static bool fill_map(char *var, char *value)
267{
268 yaml_event_t event;
269
270 yaml_scalar_event_initialize(&event, NULL, NULL,
271 (yaml_char_t *)var, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE);
272 if (!yaml_emitter_emit(&emitter, &event))
273 ERROR_RETURN_FALSE("Failed to emit YAML scalar");
274
275 yaml_scalar_event_initialize(&event, NULL, NULL,
276 (yaml_char_t *)value, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE);
277 if (!yaml_emitter_emit(&emitter, &event))
278 ERROR_RETURN_FALSE("Failed to emit YAML scalar");
279
280 return true;
281}
282
283static bool fill_timestamp(uint32_t core, uint64_t count, uint64_t addr,
284 const char *subsystem)
285{
286 yaml_event_t event;
287 char data[MAX_SCALAR];
288
289 /* Mapping start */
290 if (!yaml_mapping_start_event_initialize(&event,
291 NULL, NULL , YAML_IMPLICIT,
292 YAML_ANY_SEQUENCE_STYLE))
293 ERROR_RETURN_FALSE("Failed to initialize YAML mapping start event");
294 if (!yaml_emitter_emit(&emitter, &event))
295 ERROR_RETURN_FALSE("Failed to emit YAML mapping start event");
296
297 snprintf(data, MAX_SCALAR, "%" PRIu32, core);
298 fill_map("core", data);
299
300 snprintf(data, MAX_SCALAR, "%" PRIu64, count);
301 fill_map("counter", data);
302
303 snprintf(data, MAX_SCALAR, "0x%" PRIx64, addr);
304 fill_map("address", data);
305
306 snprintf(data, MAX_SCALAR, "%s", subsystem);
307 fill_map("component", data);
308
309 /* Mapping end */
310 if (!yaml_mapping_end_event_initialize(&event))
311 ERROR_RETURN_FALSE("Failed to initialize YAML mapping end event");
312 if (!yaml_emitter_emit(&emitter, &event))
313 ERROR_RETURN_FALSE("Failed to emit YAML mapping end event");
314
315 return true;
316}
317
318/*
319 * Consume all timestamps from per-cpu ringbuffers and put everything into
320 * the yaml file.
321 */
Igor Opaniukab88c952017-02-14 13:22:54 +0200322static void *ts_consumer(void *arg)
323{
Yves Leflochb31789d2017-08-01 14:51:18 +0200324 unsigned int i;
325 int ret;
Igor Opaniukab88c952017-02-14 13:22:54 +0200326 bool ts_received = false;
327 uint32_t cores;
328 struct tee_time_st ts_data;
329 FILE *ts_file;
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300330 char *tsfile_path = arg;
Igor Opaniukab88c952017-02-14 13:22:54 +0200331
Igor Opaniukab88c952017-02-14 13:22:54 +0200332 if (!tsfile_path)
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300333 ERROR_GOTO(exit, "Wrong timestamp file path");
Igor Opaniukab88c952017-02-14 13:22:54 +0200334
335 cores = get_cores();
336 if (!cores)
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300337 ERROR_GOTO(exit, "Can't receive amount of avalable cores");
Igor Opaniukab88c952017-02-14 13:22:54 +0200338
339 ts_file = fopen(tsfile_path, "w");
340 if (!ts_file)
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300341 ERROR_GOTO(exit, "Can't open timestamp file");
342
343 if (!init_emitter(ts_file))
344 ERROR_GOTO(file_close, "Error occured in emitter initialization");
Igor Opaniukab88c952017-02-14 13:22:54 +0200345
346 while (is_running) {
347 ts_received = false;
348 for (i = 0; i < cores; i++) {
349 ret = timestamp_pop(&bench_ts_global->cpu_buf[i],
350 &ts_data);
351 if (!ret) {
352 ts_received = true;
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300353 DBG("Timestamp: core = %u; tick = %lld; pc = 0x%"
354 PRIx64 ";system = %s",
Igor Opaniukab88c952017-02-14 13:22:54 +0200355 i, ts_data.cnt, ts_data.addr,
356 bench_str_src(ts_data.src));
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300357 if (!fill_timestamp(i, ts_data.cnt,
358 ts_data.addr, bench_str_src(ts_data.src)))
359 ERROR_GOTO(deinit_yaml, "Adding timestamp failed");
360
Igor Opaniukab88c952017-02-14 13:22:54 +0200361 }
362 }
363
Yves Leflochb31789d2017-08-01 14:51:18 +0200364 if (!ts_received) {
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300365 if (is_running) {
366 DBG("yielding...");
Igor Opaniukab88c952017-02-14 13:22:54 +0200367 sched_yield();
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300368 } else {
369 ERROR_GOTO(deinit_yaml,
370 "No new data in the per-cpu ringbuffers, closing ts file");
371 }
Yves Leflochb31789d2017-08-01 14:51:18 +0200372 }
Igor Opaniukab88c952017-02-14 13:22:54 +0200373 }
374
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300375deinit_yaml:
376 deinit_emitter();
Igor Opaniukab88c952017-02-14 13:22:54 +0200377file_close:
378 fclose(ts_file);
379exit:
380 return NULL;
381}
382
383int main(int argc, char *argv[])
384{
385 int i;
386 int status;
387 pid_t pid;
388 char testapp_path[PATH_MAX];
389 char **testapp_argv;
390 char *res;
391 char *tsfile_path;
392 uint32_t cores;
393 pthread_t consumer_thread;
394
395 if (argc == 1) {
396 usage(argv[0]);
397 return 0;
398 }
399
400 /* Parse command line */
401 for (i = 1; i < argc; i++) {
402 if (!strcmp(argv[i], "-h")) {
403 usage(argv[0]);
404 return 0;
405 }
406 }
407
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300408 signal(SIGINT, sigint_handler);
409
410 INFO("1. Opening Benchmark Static TA...");
Igor Opaniukab88c952017-02-14 13:22:54 +0200411 open_bench_pta();
412
413 cores = get_cores();
414 if (!cores)
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300415 ERROR_EXIT("Receiving amount of active cores failed");
Igor Opaniukab88c952017-02-14 13:22:54 +0200416
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300417 INFO("2. Allocating per-core buffers, cores detected = %d",
Igor Opaniukab88c952017-02-14 13:22:54 +0200418 cores);
419 register_bench_buf(cores);
420
421 res = realpath(argv[1], testapp_path);
422 if (!res)
423 tee_errx("Failed to get realpath", EXIT_FAILURE);
424
425 alloc_argv(argc, argv, &testapp_argv);
426
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300427 INFO("3. Starting origin host app %s ...", testapp_path);
Igor Opaniukab88c952017-02-14 13:22:54 +0200428
429 /* fork/exec here */
430 pid = fork();
431
432 if (pid == -1) {
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300433 DBG("fork() failed");
434 ERROR_EXIT("Starting origin host application failed.");
Igor Opaniukab88c952017-02-14 13:22:54 +0200435 } else if (pid > 0) {
436 is_running = 1;
437
438 tsfile_path = malloc(strlen(testapp_path) +
439 strlen(TSFILE_NAME_SUFFIX) + 1);
440 if (!tsfile_path)
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300441 ERROR_EXIT("Memory allocation failed for timestamp file path.");
Igor Opaniukab88c952017-02-14 13:22:54 +0200442
443 tsfile_path[0] = '\0';
444 strcat(tsfile_path, testapp_path);
445 strcat(tsfile_path, TSFILE_NAME_SUFFIX);
446
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300447 INFO("Dumping timestamps to %s ...", tsfile_path);
Igor Opaniukab88c952017-02-14 13:22:54 +0200448 print_line();
449
450 if (pthread_create(&consumer_thread, NULL,
451 ts_consumer, tsfile_path)) {
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300452 DBG( "Error creating ts consumer thread");
453 ERROR_EXIT("Can't start process of reading timestamps");
Igor Opaniukab88c952017-02-14 13:22:54 +0200454 }
455 /* wait for child app exits */
456 waitpid(pid, &status, 0);
457 is_running = 0;
458
459 /* wait for our consumer thread terminate */
460 if (pthread_join(consumer_thread, NULL)) {
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300461 DBG("Error joining thread");
462 ERROR_EXIT("Something went wrong while consuming timestamps");
Igor Opaniukab88c952017-02-14 13:22:54 +0200463 }
464 }
465 else {
466 execvp(testapp_path, testapp_argv);
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300467 DBG("execve() failed");
468 ERROR_EXIT("Starting origin host application failed");
Igor Opaniukab88c952017-02-14 13:22:54 +0200469 }
470
Igor Opaniukf1f3fd02017-09-14 15:34:56 +0300471 INFO("4. Done benchmark");
Igor Opaniukab88c952017-02-14 13:22:54 +0200472
473 dealloc_argv(argc-1, testapp_argv);
474 unregister_bench();
475 close_bench_pta();
476 return 0;
477}