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 | #include <libgen.h> |
| 28 | #include <linux/limits.h> |
| 29 | #include <math.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include "benchmark_aux.h" |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 36 | #include "common.h" |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 37 | |
| 38 | /* Misc auxilary functions */ |
| 39 | void tee_errx(const char *msg, TEEC_Result res) |
| 40 | { |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 41 | ERROR_EXIT("%s: 0x%08x\n", msg, res); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void tee_check_res(TEEC_Result res, const char *errmsg) |
| 45 | { |
| 46 | if (res != TEEC_SUCCESS) |
| 47 | tee_errx(errmsg, res); |
| 48 | } |
| 49 | |
| 50 | const char *bench_str_src(uint64_t source) |
| 51 | { |
| 52 | switch (source) { |
| 53 | case TEE_BENCH_CORE: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 54 | return "core"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 55 | case TEE_BENCH_KMOD: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 56 | return "kmodule"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 57 | case TEE_BENCH_CLIENT: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 58 | return "libteec"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 59 | case TEE_BENCH_UTEE: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 60 | return "libutee"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 61 | default: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 62 | return "-"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | void print_line(void) |
| 67 | { |
| 68 | int n = 115; |
| 69 | |
| 70 | while (n-- > 0) |
| 71 | printf("="); |
| 72 | printf("\n"); |
| 73 | } |
| 74 | |
| 75 | void alloc_argv(int argc, char *argv[], char **new_argv[]) |
| 76 | { |
| 77 | char *res, *base; |
| 78 | char path[PATH_MAX]; |
| 79 | char **testapp_argv; |
l00176142 | d1cfd65 | 2017-11-23 00:52:28 +0800 | [diff] [blame^] | 80 | int i; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 81 | |
| 82 | res = realpath(argv[1], path); |
| 83 | if (!res) |
| 84 | _exit(EXIT_FAILURE); |
| 85 | |
| 86 | *new_argv = malloc(argc * sizeof(void *)); |
| 87 | if (!(*new_argv)) |
| 88 | _exit(EXIT_FAILURE); |
| 89 | |
| 90 | testapp_argv = *new_argv; |
| 91 | testapp_argv[argc-1] = NULL; |
| 92 | base = basename(path); |
| 93 | |
| 94 | testapp_argv[0] = malloc(strlen(base) + 1); |
| 95 | if (!testapp_argv[0]) |
| 96 | _exit(EXIT_FAILURE); |
| 97 | |
| 98 | memcpy(testapp_argv[0], base, strlen(base) + 1); |
| 99 | |
l00176142 | d1cfd65 | 2017-11-23 00:52:28 +0800 | [diff] [blame^] | 100 | for (i = 2; i < argc; i++) { |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 101 | size_t length = strlen(argv[i]) + 1; |
| 102 | |
| 103 | testapp_argv[i - 1] = malloc(length); |
| 104 | if (!testapp_argv[i - 1]) |
| 105 | _exit(EXIT_FAILURE); |
| 106 | |
| 107 | memcpy(testapp_argv[i-1], argv[i], length); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void dealloc_argv(int new_argc, char **new_argv) |
| 112 | { |
l00176142 | d1cfd65 | 2017-11-23 00:52:28 +0800 | [diff] [blame^] | 113 | int i; |
| 114 | for (i = 0; i < new_argc; ++i) |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 115 | free(new_argv[i]); |
| 116 | |
| 117 | free(new_argv); |
| 118 | } |
| 119 | |
| 120 | uint32_t get_cores(void) |
| 121 | { |
| 122 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 123 | } |