blob: 8045e7e03f791fba1da4734e363629e6262ec2ab [file] [log] [blame]
Igor Opaniuk136644a2016-09-13 13:40:56 +03001/*
2 * Copyright (c) 2015, 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 <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <math.h>
32#include <stdint.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <strings.h>
37#include <time.h>
38#include <unistd.h>
39
40#include <adbg.h>
41#include <tee_client_api.h>
42#include "sha_perf.h"
43
44#define _verbose(lvl, ...) \
45 do { \
46 if (verbosity >= lvl) { \
47 printf(__VA_ARGS__); \
48 fflush(stdout); \
49 } \
50 } while (0)
51
52#define verbose(...) _verbose(1, __VA_ARGS__)
53#define vverbose(...) _verbose(2, __VA_ARGS__)
54
55/*
56 * TEE client stuff
57 */
58
59static TEEC_Context ctx;
60static TEEC_Session sess;
61static TEEC_SharedMemory in_shm = {
62 .flags = TEEC_MEM_INPUT
63};
64static TEEC_SharedMemory out_shm = {
65 .flags = TEEC_MEM_OUTPUT
66};
67
68static void errx(const char *msg, TEEC_Result res)
69{
70 fprintf(stderr, "%s: 0x%08x", msg, res);
71 exit (1);
72}
73
74static void check_res(TEEC_Result res, const char *errmsg)
75{
76 if (res != TEEC_SUCCESS)
77 errx(errmsg, res);
78}
79
80static void open_ta(void)
81{
82 TEEC_Result res;
83 TEEC_UUID uuid = TA_SHA_PERF_UUID;
84 uint32_t err_origin;
85
86 res = TEEC_InitializeContext(NULL, &ctx);
87 check_res(res,"TEEC_InitializeContext");
88
89 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
90 NULL, &err_origin);
91 check_res(res,"TEEC_OpenSession");
92}
93
94/*
95 * Statistics
96 *
97 * We want to compute min, max, mean and standard deviation of processing time
98 */
99
100struct statistics {
101 int n;
102 double m;
103 double M2;
104 double min;
105 double max;
106 int initialized;
107};
108
109/* Take new sample into account (Knuth/Welford algorithm) */
110static void update_stats(struct statistics *s, uint64_t t)
111{
112 double x = (double)t;
113 double delta = x - s->m;
114
115 s->n++;
116 s->m += delta/s->n;
117 s->M2 += delta*(x - s->m);
118 if (!s->initialized) {
119 s->min = s->max = x;
120 s->initialized = 1;
121 } else {
122 if (s->min > x)
123 s->min = x;
124 if (s->max < x)
125 s->max = x;
126 }
127}
128
129static double stddev(struct statistics *s)
130{
131 if (s->n < 2)
132 return NAN;
133 return sqrt(s->M2/s->n);
134}
135
136static const char *algo_str(uint32_t algo)
137{
138 switch (algo) {
139 case TA_SHA_SHA1:
140 return "SHA1";
141 case TA_SHA_SHA224:
142 return "SHA224";
143 case TA_SHA_SHA256:
144 return "SHA256";
145 case TA_SHA_SHA384:
146 return "SHA384";
147 case TA_SHA_SHA512:
148 return "SHA512";
149 default:
150 return "???";
151 }
152}
153
154static int hash_size(uint32_t algo)
155{
156 switch (algo) {
157 case TA_SHA_SHA1:
158 return 20;
159 case TA_SHA_SHA224:
160 return 28;
161 case TA_SHA_SHA256:
162 return 32;
163 case TA_SHA_SHA384:
164 return 48;
165 case TA_SHA_SHA512:
166 return 64;
167 default:
168 return 0;
169 }
170}
171
172#define _TO_STR(x) #x
173#define TO_STR(x) _TO_STR(x)
174
175
176static void alloc_shm(size_t sz, uint32_t algo, int offset)
177{
178 TEEC_Result res;
179
180 in_shm.buffer = NULL;
181 in_shm.size = sz + offset;
182 res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
183 check_res(res, "TEEC_AllocateSharedMemory");
184
185 out_shm.buffer = NULL;
186 out_shm.size = hash_size(algo);
187 res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
188 check_res(res, "TEEC_AllocateSharedMemory");
189}
190
191static void free_shm(void)
192{
193 TEEC_ReleaseSharedMemory(&in_shm);
194 TEEC_ReleaseSharedMemory(&out_shm);
195}
196
197static ssize_t read_random(void *in, size_t rsize)
198{
199 static int rnd;
200 ssize_t s;
201
202 if (!rnd) {
203 rnd = open("/dev/urandom", O_RDONLY);
204 if (rnd < 0) {
205 perror("open");
206 return 1;
207 }
208 }
209 s = read(rnd, in, rsize);
210 if (s < 0) {
211 perror("read");
212 return 1;
213 }
214 if ((size_t)s != rsize) {
215 printf("read: requested %zu bytes, got %zd\n",
216 rsize, s);
217 }
218 return 0;
219}
220
221static long get_current_time(struct timespec *ts)
222{
223 if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
224 perror("clock_gettime");
225 exit(1);
226 }
227 return 0;
228}
229
230static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
231{
232 uint64_t ns = 0;
233
234 if (end->tv_nsec < start->tv_nsec) {
235 ns += 1000000000 * (end->tv_sec - start->tv_sec - 1);
236 ns += 1000000000 - start->tv_nsec + end->tv_nsec;
237 } else {
238 ns += 1000000000 * (end->tv_sec - start->tv_sec);
239 ns += end->tv_nsec - start->tv_nsec;
240 }
241 return ns;
242}
243
244static uint64_t run_test_once(void *in, size_t size, int random_in, TEEC_Operation *op)
245{
246 struct timespec t0, t1;
247 TEEC_Result res;
248 uint32_t ret_origin;
249
250 if (random_in)
251 read_random(in, size);
252 get_current_time(&t0);
253 res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op,
254 &ret_origin);
255 check_res(res, "TEEC_InvokeCommand");
256 get_current_time(&t1);
257
258 return timespec_diff_ns(&t0, &t1);
259}
260
261static void prepare_op(int algo)
262{
263 TEEC_Result res;
264 uint32_t ret_origin;
265 TEEC_Operation op;
266
267 memset(&op, 0, sizeof(op));
268 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
269 TEEC_NONE, TEEC_NONE);
270 op.params[0].value.a = algo;
271 res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PREPARE_OP, &op,
272 &ret_origin);
273 check_res(res, "TEEC_InvokeCommand");
274}
275
276static void do_warmup(int warmup)
277{
278 struct timespec t0, t;
279 int i;
280
281 get_current_time(&t0);
282 do {
283 for (i = 0; i < 100000; i++)
284 ;
285 get_current_time(&t);
286 } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
287}
288
289static const char *yesno(int v)
290{
291 return (v ? "yes" : "no");
292}
293
294static double mb_per_sec(size_t size, double usec)
295{
296 return (1000000000/usec)*((double)size/(1024*1024));
297}
298
299/* Hash test: buffer of size byte. Run test n times.
300 * Entry point for running SHA benchmark
301 * Params:
302 * algo - Algorithm
303 * size - Buffer size
304 * n - Number of measurements
305 * l - Amount of inner loops
306 * random_in - Get input from /dev/urandom
307 * offset - Buffer offset wrt. alloc-ed address
308 * warmup - Start with a-second busy loop
309 * verbosity - Verbosity level
310 * */
311extern void sha_perf_run_test(int algo, size_t size, unsigned int n,
312 unsigned int l, int random_in, int offset,
313 int warmup, int verbosity)
314{
315 uint64_t t;
316 struct statistics stats;
317 TEEC_Operation op;
318 int n0 = n;
319 struct timespec ts;
320
321 vverbose("sha-perf version %s\n", TO_STR(VERSION));
322 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
323 perror("clock_getres");
324 return;
325 }
326 vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 +
327 ts.tv_nsec);
328
329 open_ta();
330 prepare_op(algo);
331
332
333 alloc_shm(size, algo, offset);
334
335 if (!random_in)
336 memset((uint8_t *)in_shm.buffer + offset, 0, size);
337
338 memset(&op, 0, sizeof(op));
339 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
340 TEEC_MEMREF_PARTIAL_OUTPUT,
341 TEEC_VALUE_INPUT, TEEC_NONE);
342 op.params[0].memref.parent = &in_shm;
343 op.params[0].memref.offset = 0;
344 op.params[0].memref.size = size + offset;
345 op.params[1].memref.parent = &out_shm;
346 op.params[1].memref.offset = 0;
347 op.params[1].memref.size = hash_size(algo);
348 op.params[2].value.a = l;
349 op.params[2].value.b = offset;
350
351 verbose("Starting test: %s, size=%zu bytes, ",
352 algo_str(algo), size);
353 verbose("random=%s, ", yesno(random_in));
354 verbose("unaligned=%s, ", yesno(offset));
355 verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
356
357 if (warmup)
358 do_warmup(warmup);
359
360 memset(&stats, 0, sizeof(stats));
361 while (n-- > 0) {
362 t = run_test_once((uint8_t *)in_shm.buffer + offset, size, random_in, &op);
363 update_stats(&stats, t);
364 if (n % (n0/10) == 0)
365 vverbose("#");
366 }
367 vverbose("\n");
368 printf("min=%gμs max=%gμs mean=%gμs stddev=%gμs (%gMiB/s)\n",
369 stats.min/1000, stats.max/1000, stats.m/1000,
370 stddev(&stats)/1000, mb_per_sec(size, stats.m));
371 free_shm();
372}
373
374static void usage(const char *progname,
375 /* Default params */
376 int algo, size_t size, int warmup, int l, int n)
377{
378 fprintf(stderr, "SHA performance testing tool for OP-TEE (%s)\n\n",
379 TO_STR(VERSION));
380 fprintf(stderr, "Usage:\n");
381 fprintf(stderr, " %s -h\n", progname);
382 fprintf(stderr, " %s [-v] [-a algo] ", progname);
383 fprintf(stderr, "[-s bufsize] [-r] [-n loops] [-l iloops] ");
384 fprintf(stderr, "[-w warmup_time]\n");
385 fprintf(stderr, "Options:\n");
386 fprintf(stderr, " -h Print this help and exit\n");
387 fprintf(stderr, " -l Inner loop iterations (TA hashes ");
388 fprintf(stderr, "the buffer <x> times) [%u]\n", l);
389 fprintf(stderr, " -a Algorithm (SHA1, SHA224, SHA256, SHA384, ");
390 fprintf(stderr, "SHA512) [%s]\n", algo_str(algo));
391 fprintf(stderr, " -n Outer loop iterations [%u]\n", n);
392 fprintf(stderr, " -r Get input data from /dev/urandom ");
393 fprintf(stderr, "(otherwise use zero-filled buffer)\n");
394 fprintf(stderr, " -s Buffer size (process <x> bytes at a time) ");
395 fprintf(stderr, "[%zu]\n", size);
396 fprintf(stderr, " -u Use unaligned buffer (odd address)\n");
397 fprintf(stderr, " -v Be verbose (use twice for greater effect)\n");
398 fprintf(stderr, " -w Warm-up time in seconds: execute a busy ");
399 fprintf(stderr, "loop before the test\n");
400 fprintf(stderr, " to mitigate the effects of cpufreq etc. ");
401 fprintf(stderr, "[%u]\n", warmup);
402}
403
404#define NEXT_ARG(i) \
405 do { \
406 if (++i == argc) { \
407 fprintf(stderr, "%s: %s: missing argument\n", \
408 argv[0], argv[i-1]); \
409 return 1; \
410 } \
411 } while (0);
412
413
414
415extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
416{
417 int i;
418
419 /* Command line params */
420 size_t size = 1024; /* Buffer size (-s) */
421 unsigned int n = 5000; /* Number of measurements (-n) */
422 unsigned int l = 1; /* Inner loops (-l) */
423 int verbosity = 0; /* Verbosity (-v) */
424 int algo = TA_SHA_SHA1; /* Algorithm (-a) */
425 int random_in = 0; /* Get input data from /dev/urandom (-r) */
426 int warmup = 2; /* Start with a 2-second busy loop (-w) */
427 int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */
428
429
430 /* Parse command line */
431 for (i = 1; i < argc; i++) {
432 if (!strcmp(argv[i], "-h")) {
433 usage(argv[0], algo, size, warmup, l, n);
434 return 0;
435 }
436 }
437 for (i = 1; i < argc; i++) {
438 if (!strcmp(argv[i], "-l")) {
439 NEXT_ARG(i);
440 l = atoi(argv[i]);
441 } else if (!strcmp(argv[i], "-a")) {
442 NEXT_ARG(i);
443 if (!strcasecmp(argv[i], "SHA1"))
444 algo = TA_SHA_SHA1;
445 else if (!strcasecmp(argv[i], "SHA224"))
446 algo = TA_SHA_SHA224;
447 else if (!strcasecmp(argv[i], "SHA256"))
448 algo = TA_SHA_SHA256;
449 else if (!strcasecmp(argv[i], "SHA384"))
450 algo = TA_SHA_SHA384;
451 else if (!strcasecmp(argv[i], "SHA512"))
452 algo = TA_SHA_SHA512;
453 else {
454 fprintf(stderr, "%s, invalid algorithm\n",
455 argv[0]);
456 usage(argv[0], algo, size, warmup, l, n);
457 return 1;
458 }
459 } else if (!strcmp(argv[i], "-n")) {
460 NEXT_ARG(i);
461 n = atoi(argv[i]);
462 } else if (!strcmp(argv[i], "-r")) {
463 random_in = 1;
464 } else if (!strcmp(argv[i], "-s")) {
465 NEXT_ARG(i);
466 size = atoi(argv[i]);
467 } else if (!strcmp(argv[i], "-u")) {
468 offset = 1;
469 } else if (!strcmp(argv[i], "-v")) {
470 verbosity++;
471 } else if (!strcmp(argv[i], "-w")) {
472 NEXT_ARG(i);
473 warmup = atoi(argv[i]);
474 } else {
475 fprintf(stderr, "%s: invalid argument: %s\n",
476 argv[0], argv[i]);
477 usage(argv[0], algo, size, warmup, l, n);
478 return 1;
479 }
480 }
481
482 sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
483
484 return 0;
485}