Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 1 | /* |
| 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 Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 43 | #include <yaml.h> |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 44 | |
| 45 | #include "benchmark_aux.h" |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 46 | #include "common.h" |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 47 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 48 | #define MAX_SCALAR 20 |
| 49 | static struct tee_ts_global *bench_ts_global; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 50 | |
| 51 | static const TEEC_UUID pta_benchmark_uuid = PTA_BENCHMARK_UUID; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 52 | static TEEC_Context ctx; |
| 53 | static TEEC_Session sess; |
| 54 | |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 55 | static sig_atomic_t is_running; |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 56 | static yaml_emitter_t emitter; |
| 57 | |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 58 | struct consumer_param { |
| 59 | pid_t child_pid; |
| 60 | char *ts_filepath; |
| 61 | }; |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 62 | void sigint_handler(int data) |
| 63 | { |
| 64 | (void)data; |
| 65 | |
| 66 | is_running = 0; |
| 67 | } |
| 68 | |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 69 | static 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 | |
| 82 | static void close_bench_pta(void) |
| 83 | { |
| 84 | /* release benchmark timestamp shm */ |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 85 | TEEC_CloseSession(&sess); |
| 86 | TEEC_FinalizeContext(&ctx); |
| 87 | } |
| 88 | |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 89 | static void alloc_bench_buf(uint32_t cores) |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 90 | { |
| 91 | TEEC_Result res; |
| 92 | TEEC_Operation op = { 0 }; |
| 93 | uint32_t ret_orig; |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 94 | intptr_t paddr_ts_buf = 0; |
| 95 | size_t size; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 96 | |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 97 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, |
| 98 | TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 99 | |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 100 | op.params[1].value.a = cores; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 101 | |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 102 | res = TEEC_InvokeCommand(&sess, BENCHMARK_CMD_REGISTER_MEMREF, |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 103 | &op, &ret_orig); |
| 104 | tee_check_res(res, "TEEC_InvokeCommand"); |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 105 | |
| 106 | paddr_ts_buf = op.params[0].value.a; |
| 107 | size = op.params[0].value.b; |
| 108 | |
ChrisG55 | ecba70d | 2018-12-06 11:52:29 +0100 | [diff] [blame^] | 109 | INFO("ts buffer paddr = %" PRIiPTR ", size = %zu\n", paddr_ts_buf, size); |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 110 | if (paddr_ts_buf) { |
| 111 | |
| 112 | bench_ts_global = mmap_paddr(paddr_ts_buf, size); |
| 113 | if (!bench_ts_global) |
| 114 | ERROR_EXIT("Failed to allocate timestamp buffer"); |
| 115 | } else { |
| 116 | ERROR_EXIT("Failed to allocate timestamp buffer"); |
| 117 | } |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 120 | static void free_bench_buf(void) |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 121 | { |
| 122 | TEEC_Result res; |
| 123 | TEEC_Operation op = { 0 }; |
| 124 | uint32_t ret_orig; |
| 125 | |
Igor Opaniuk | 845a783 | 2018-06-06 14:06:10 +0300 | [diff] [blame] | 126 | DBG("Freeing benchmark buffer."); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 127 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, |
| 128 | TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 129 | |
| 130 | res = TEEC_InvokeCommand(&sess, BENCHMARK_CMD_UNREGISTER, |
| 131 | &op, &ret_orig); |
| 132 | tee_check_res(res, "TEEC_InvokeCommand"); |
| 133 | } |
| 134 | |
| 135 | static void usage(char *progname) |
| 136 | { |
| 137 | fprintf(stderr, "Call latency benchmark tool for OP-TEE\n\n"); |
| 138 | fprintf(stderr, "Usage:\n"); |
| 139 | fprintf(stderr, " %s -h\n", progname); |
| 140 | fprintf(stderr, " %s host_app [host_app_args]\n", progname); |
| 141 | fprintf(stderr, "Options:\n"); |
| 142 | fprintf(stderr, " -h Print this help and exit\n"); |
| 143 | fprintf(stderr, " host_app Path to host app to benchmark\n"); |
| 144 | fprintf(stderr, " host_app_args Original host app args\n"); |
| 145 | } |
| 146 | |
| 147 | static int timestamp_pop(struct tee_ts_cpu_buf *cpu_buf, |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 148 | struct tee_time_st *ts) |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 149 | { |
| 150 | uint64_t ts_tail; |
| 151 | |
| 152 | if (!cpu_buf && !ts) |
| 153 | return RING_BADPARM; |
| 154 | |
| 155 | if (cpu_buf->tail >= cpu_buf->head) |
| 156 | return RING_NODATA; |
| 157 | |
| 158 | ts_tail = cpu_buf->tail++; |
| 159 | *ts = cpu_buf->stamps[ts_tail & TEE_BENCH_MAX_MASK]; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 164 | static bool init_emitter(FILE *ts_file) |
| 165 | { |
| 166 | yaml_event_t event; |
| 167 | |
| 168 | if (!yaml_emitter_initialize(&emitter)) |
| 169 | ERROR_RETURN_FALSE("Can't initialize YAML emitter"); |
| 170 | |
| 171 | yaml_emitter_set_canonical(&emitter, 0); |
| 172 | yaml_emitter_set_unicode(&emitter, 1); |
| 173 | yaml_emitter_set_output_file(&emitter, ts_file); |
| 174 | |
| 175 | /* Stream start */ |
| 176 | if (!yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING)) |
| 177 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 178 | "Failed to initialize YAML stream start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 179 | if (!yaml_emitter_emit(&emitter, &event)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 180 | ERROR_GOTO(emitter_delete, |
| 181 | "Failed to emit YAML stream start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 182 | |
| 183 | /* Document start */ |
| 184 | if (!yaml_document_start_event_initialize(&event, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 185 | NULL, NULL, NULL, YAML_IMPLICIT)) |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 186 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 187 | "Failed to initialize YAML document start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 188 | if (!yaml_emitter_emit(&emitter, &event)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 189 | ERROR_GOTO(emitter_delete, |
| 190 | "Failed to emit YAML doc start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 191 | |
| 192 | /* Mapping start */ |
| 193 | if (!yaml_mapping_start_event_initialize(&event, |
| 194 | NULL, NULL , YAML_IMPLICIT, |
| 195 | YAML_ANY_SEQUENCE_STYLE)) |
| 196 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 197 | "Failed to initialize YAML mapping start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 198 | if (!yaml_emitter_emit(&emitter, &event)) |
| 199 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 200 | "Failed to emit YAML sequence mapping event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 201 | /* Key timestamps */ |
| 202 | yaml_scalar_event_initialize(&event, NULL, NULL, |
| 203 | (yaml_char_t *)"timestamps", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE); |
| 204 | if (!yaml_emitter_emit(&emitter, &event)) |
| 205 | ERROR_RETURN_FALSE("Failed to emit YAML scalar"); |
| 206 | |
| 207 | /* Sequence start */ |
| 208 | if (!yaml_sequence_start_event_initialize(&event, |
| 209 | NULL, NULL , YAML_IMPLICIT, |
| 210 | YAML_ANY_SEQUENCE_STYLE)) |
| 211 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 212 | "Failed to initialize YAML sequence start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 213 | if (!yaml_emitter_emit(&emitter, &event)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 214 | ERROR_GOTO(emitter_delete, |
| 215 | "Failed to emit YAML sequence start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 216 | |
| 217 | return true; |
| 218 | emitter_delete: |
| 219 | yaml_emitter_delete(&emitter); |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | static void deinit_emitter() |
| 224 | { |
| 225 | yaml_event_t event; |
| 226 | |
| 227 | /* Sequence cmd */ |
| 228 | if (!yaml_sequence_end_event_initialize(&event)) |
| 229 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 230 | "Failed to initialize YAML sequence end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 231 | if (!yaml_emitter_emit(&emitter, &event)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 232 | ERROR_GOTO(emitter_delete, |
| 233 | "Failed to emit YAML sequence end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 234 | |
| 235 | /* Mapping end */ |
| 236 | if (!yaml_mapping_end_event_initialize(&event)) |
| 237 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 238 | "Failed to initialize YAML mapping end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 239 | if (!yaml_emitter_emit(&emitter, &event)) |
| 240 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 241 | "Failed to emit YAML mapping end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 242 | |
| 243 | /* Document end */ |
| 244 | if (!yaml_document_end_event_initialize(&event, 0)) |
| 245 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 246 | "Failed to initialize YAML document end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 247 | if (!yaml_emitter_emit(&emitter, &event)) |
| 248 | ERROR_GOTO(emitter_delete, "Failed to emit YAML doc end event"); |
| 249 | |
| 250 | /* Stream end */ |
| 251 | if (!yaml_stream_end_event_initialize(&event)) |
| 252 | ERROR_GOTO(emitter_delete, |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 253 | "Failed to initialise YAML stream end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 254 | if (!yaml_emitter_emit(&emitter, &event)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 255 | ERROR_GOTO(emitter_delete, |
| 256 | "Failed to emit YAML stream end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 257 | |
| 258 | emitter_delete: |
| 259 | yaml_emitter_delete(&emitter); |
| 260 | } |
| 261 | |
| 262 | static bool fill_map(char *var, char *value) |
| 263 | { |
| 264 | yaml_event_t event; |
| 265 | |
| 266 | yaml_scalar_event_initialize(&event, NULL, NULL, |
| 267 | (yaml_char_t *)var, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE); |
| 268 | if (!yaml_emitter_emit(&emitter, &event)) |
| 269 | ERROR_RETURN_FALSE("Failed to emit YAML scalar"); |
| 270 | |
| 271 | yaml_scalar_event_initialize(&event, NULL, NULL, |
| 272 | (yaml_char_t *)value, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE); |
| 273 | if (!yaml_emitter_emit(&emitter, &event)) |
| 274 | ERROR_RETURN_FALSE("Failed to emit YAML scalar"); |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | static bool fill_timestamp(uint32_t core, uint64_t count, uint64_t addr, |
| 280 | const char *subsystem) |
| 281 | { |
| 282 | yaml_event_t event; |
| 283 | char data[MAX_SCALAR]; |
| 284 | |
| 285 | /* Mapping start */ |
| 286 | if (!yaml_mapping_start_event_initialize(&event, |
| 287 | NULL, NULL , YAML_IMPLICIT, |
| 288 | YAML_ANY_SEQUENCE_STYLE)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 289 | ERROR_RETURN_FALSE( |
| 290 | "Failed to initialize YAML mapping start event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 291 | if (!yaml_emitter_emit(&emitter, &event)) |
| 292 | ERROR_RETURN_FALSE("Failed to emit YAML mapping start event"); |
| 293 | |
| 294 | snprintf(data, MAX_SCALAR, "%" PRIu32, core); |
| 295 | fill_map("core", data); |
| 296 | |
| 297 | snprintf(data, MAX_SCALAR, "%" PRIu64, count); |
| 298 | fill_map("counter", data); |
| 299 | |
| 300 | snprintf(data, MAX_SCALAR, "0x%" PRIx64, addr); |
| 301 | fill_map("address", data); |
| 302 | |
| 303 | snprintf(data, MAX_SCALAR, "%s", subsystem); |
| 304 | fill_map("component", data); |
| 305 | |
| 306 | /* Mapping end */ |
| 307 | if (!yaml_mapping_end_event_initialize(&event)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 308 | ERROR_RETURN_FALSE( |
| 309 | "Failed to initialize YAML mapping end event"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 310 | if (!yaml_emitter_emit(&emitter, &event)) |
| 311 | ERROR_RETURN_FALSE("Failed to emit YAML mapping end event"); |
| 312 | |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Consume all timestamps from per-cpu ringbuffers and put everything into |
| 318 | * the yaml file. |
| 319 | */ |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 320 | static void *ts_consumer(void *arg) |
| 321 | { |
Yves Lefloch | b31789d | 2017-08-01 14:51:18 +0200 | [diff] [blame] | 322 | unsigned int i; |
| 323 | int ret; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 324 | bool ts_received = false; |
| 325 | uint32_t cores; |
| 326 | struct tee_time_st ts_data; |
| 327 | FILE *ts_file; |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 328 | struct consumer_param *prm = (struct consumer_param *)arg; |
| 329 | char *tsfile_path = prm->ts_filepath; |
| 330 | pid_t child_pid = prm->child_pid; |
| 331 | size_t teec_dyn_addr = 0; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 332 | |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 333 | if (!tsfile_path) |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 334 | ERROR_GOTO(exit, "Wrong timestamp file path"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 335 | |
| 336 | cores = get_cores(); |
| 337 | if (!cores) |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 338 | ERROR_GOTO(exit, "Can't receive amount of avalable cores"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 339 | |
| 340 | ts_file = fopen(tsfile_path, "w"); |
| 341 | if (!ts_file) |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 342 | ERROR_GOTO(exit, "Can't open timestamp file"); |
| 343 | |
| 344 | if (!init_emitter(ts_file)) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 345 | ERROR_GOTO(file_close, |
| 346 | "Error occurred in emitter initialization"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 347 | |
| 348 | while (is_running) { |
| 349 | ts_received = false; |
| 350 | for (i = 0; i < cores; i++) { |
| 351 | ret = timestamp_pop(&bench_ts_global->cpu_buf[i], |
| 352 | &ts_data); |
| 353 | if (!ret) { |
| 354 | ts_received = true; |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 355 | DBG("Timestamp: core = %u; tick = %lld; " |
| 356 | "pc = 0x%" PRIx64 "; system = %s", |
| 357 | i, ts_data.cnt, ts_data.addr, |
| 358 | bench_str_src(ts_data.src)); |
Igor Opaniuk | 845a783 | 2018-06-06 14:06:10 +0300 | [diff] [blame] | 359 | do { |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 360 | teec_dyn_addr = get_library_load_offset |
| 361 | (child_pid, |
| 362 | LIBTEEC_NAME); |
Igor Opaniuk | 845a783 | 2018-06-06 14:06:10 +0300 | [diff] [blame] | 363 | |
| 364 | } while (!teec_dyn_addr && is_running); |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 365 | if (ts_data.src == TEE_BENCH_CLIENT) { |
| 366 | DBG("ts_addr = %llx, teec_addr = %x", |
| 367 | ts_data.addr, teec_dyn_addr); |
| 368 | ts_data.addr -= teec_dyn_addr; |
| 369 | } |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 370 | if (!fill_timestamp(i, ts_data.cnt, |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 371 | ts_data.addr, |
| 372 | bench_str_src(ts_data.src))) |
| 373 | ERROR_GOTO(deinit_yaml, |
| 374 | "Adding timestamp failed"); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 375 | |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Yves Lefloch | b31789d | 2017-08-01 14:51:18 +0200 | [diff] [blame] | 379 | if (!ts_received) { |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 380 | if (is_running) { |
| 381 | DBG("yielding..."); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 382 | sched_yield(); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 383 | } else { |
| 384 | ERROR_GOTO(deinit_yaml, |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 385 | "No new data in the per-cpu ringbuffers" |
| 386 | ); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 387 | } |
Yves Lefloch | b31789d | 2017-08-01 14:51:18 +0200 | [diff] [blame] | 388 | } |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 391 | deinit_yaml: |
| 392 | deinit_emitter(); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 393 | file_close: |
| 394 | fclose(ts_file); |
| 395 | exit: |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | int main(int argc, char *argv[]) |
| 400 | { |
| 401 | int i; |
| 402 | int status; |
| 403 | pid_t pid; |
| 404 | char testapp_path[PATH_MAX]; |
| 405 | char **testapp_argv; |
| 406 | char *res; |
| 407 | char *tsfile_path; |
| 408 | uint32_t cores; |
| 409 | pthread_t consumer_thread; |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 410 | struct consumer_param prm; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 411 | |
| 412 | if (argc == 1) { |
| 413 | usage(argv[0]); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* Parse command line */ |
| 418 | for (i = 1; i < argc; i++) { |
| 419 | if (!strcmp(argv[i], "-h")) { |
| 420 | usage(argv[0]); |
| 421 | return 0; |
| 422 | } |
| 423 | } |
| 424 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 425 | signal(SIGINT, sigint_handler); |
| 426 | |
| 427 | INFO("1. Opening Benchmark Static TA..."); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 428 | open_bench_pta(); |
| 429 | |
| 430 | cores = get_cores(); |
| 431 | if (!cores) |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 432 | ERROR_EXIT("Receiving amount of active cores failed"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 433 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 434 | INFO("2. Allocating per-core buffers, cores detected = %d", |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 435 | cores); |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 436 | alloc_bench_buf(cores); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 437 | |
| 438 | res = realpath(argv[1], testapp_path); |
| 439 | if (!res) |
| 440 | tee_errx("Failed to get realpath", EXIT_FAILURE); |
| 441 | |
| 442 | alloc_argv(argc, argv, &testapp_argv); |
| 443 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 444 | INFO("3. Starting origin host app %s ...", testapp_path); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 445 | |
| 446 | /* fork/exec here */ |
| 447 | pid = fork(); |
| 448 | |
| 449 | if (pid == -1) { |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 450 | DBG("fork() failed"); |
| 451 | ERROR_EXIT("Starting origin host application failed."); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 452 | } else if (pid > 0) { |
| 453 | is_running = 1; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 454 | tsfile_path = malloc(strlen(testapp_path) + |
| 455 | strlen(TSFILE_NAME_SUFFIX) + 1); |
| 456 | if (!tsfile_path) |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 457 | ERROR_EXIT("Memory allocation failed the file path."); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 458 | |
| 459 | tsfile_path[0] = '\0'; |
| 460 | strcat(tsfile_path, testapp_path); |
| 461 | strcat(tsfile_path, TSFILE_NAME_SUFFIX); |
| 462 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 463 | INFO("Dumping timestamps to %s ...", tsfile_path); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 464 | print_line(); |
| 465 | |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 466 | prm.child_pid = pid; |
| 467 | prm.ts_filepath = tsfile_path; |
| 468 | |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 469 | if (pthread_create(&consumer_thread, NULL, |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 470 | ts_consumer, &prm)) { |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 471 | DBG( "Error creating ts consumer thread"); |
| 472 | ERROR_EXIT("Can't start process of reading timestamps"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 473 | } |
| 474 | /* wait for child app exits */ |
| 475 | waitpid(pid, &status, 0); |
Igor Opaniuk | 845a783 | 2018-06-06 14:06:10 +0300 | [diff] [blame] | 476 | DBG("Origin host application finished executing"); |
| 477 | |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 478 | is_running = 0; |
| 479 | |
| 480 | /* wait for our consumer thread terminate */ |
| 481 | if (pthread_join(consumer_thread, NULL)) { |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 482 | DBG("Error joining thread"); |
Igor Opaniuk | 66e23bf | 2018-02-06 19:18:59 +0200 | [diff] [blame] | 483 | ERROR_EXIT("Couldn't start consuming timestamps"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | else { |
| 487 | execvp(testapp_path, testapp_argv); |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 488 | DBG("execve() failed"); |
| 489 | ERROR_EXIT("Starting origin host application failed"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 492 | INFO("4. Done benchmark"); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 493 | |
| 494 | dealloc_argv(argc-1, testapp_argv); |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 495 | free_bench_buf(); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 496 | close_bench_pta(); |
| 497 | return 0; |
| 498 | } |