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