blob: b97c21322fb52a0468deb224ba30ee8abf89b7e0 [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#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"
36
37/* Misc auxilary functions */
38void tee_errx(const char *msg, TEEC_Result res)
39{
40 fprintf(stderr, "%s: 0x%08x\n", msg, res);
41 exit(1);
42}
43
44void tee_check_res(TEEC_Result res, const char *errmsg)
45{
46 if (res != TEEC_SUCCESS)
47 tee_errx(errmsg, res);
48}
49
50const char *bench_str_src(uint64_t source)
51{
52 switch (source) {
53 case TEE_BENCH_CORE:
54 return "CORE";
55 case TEE_BENCH_KMOD:
56 return "KMOD";
57 case TEE_BENCH_CLIENT:
58 return "CLIENT";
59 case TEE_BENCH_UTEE:
60 return "UTEE";
61 case TEE_BENCH_DUMB_TA:
62 return "DUMB_TA";
63 default:
64 return "???";
65 }
66}
67
68void print_line(void)
69{
70 int n = 115;
71
72 while (n-- > 0)
73 printf("=");
74 printf("\n");
75}
76
77void alloc_argv(int argc, char *argv[], char **new_argv[])
78{
79 char *res, *base;
80 char path[PATH_MAX];
81 char **testapp_argv;
82
83 res = realpath(argv[1], path);
84 if (!res)
85 _exit(EXIT_FAILURE);
86
87 *new_argv = malloc(argc * sizeof(void *));
88 if (!(*new_argv))
89 _exit(EXIT_FAILURE);
90
91 testapp_argv = *new_argv;
92 testapp_argv[argc-1] = NULL;
93 base = basename(path);
94
95 testapp_argv[0] = malloc(strlen(base) + 1);
96 if (!testapp_argv[0])
97 _exit(EXIT_FAILURE);
98
99 memcpy(testapp_argv[0], base, strlen(base) + 1);
100
101 for (int i = 2; i < argc; i++) {
102 size_t length = strlen(argv[i]) + 1;
103
104 testapp_argv[i - 1] = malloc(length);
105 if (!testapp_argv[i - 1])
106 _exit(EXIT_FAILURE);
107
108 memcpy(testapp_argv[i-1], argv[i], length);
109 }
110}
111
112void dealloc_argv(int new_argc, char **new_argv)
113{
114 for (int i = 0; i < new_argc; ++i)
115 free(new_argv[i]);
116
117 free(new_argv);
118}
119
120uint32_t get_cores(void)
121{
122 return sysconf(_SC_NPROCESSORS_ONLN);
123}