]> code.ossystems Code Review - meta-freescale.git/blob
9f7709bb4f8685b89e52ae6aec29ad48f1d7eba4
[meta-freescale.git] /
1 From a7e42a9c9a2cd4b4ffd369ae57dfbd207536766a Mon Sep 17 00:00:00 2001
2 From: Cristian Stoica <cristian.stoica@nxp.com>
3 Date: Tue, 1 Nov 2016 12:01:33 +0200
4 Subject: [PATCH 088/104] merge sync and async benchmarks into a single program
5
6 Signed-off-by: Cristian Stoica <cristian.stoica@nxp.com>
7 ---
8  tests/Makefile             |   9 +-
9  tests/async_speed.c        | 463 --------------------------------------
10  tests/async_speed_multi.sh | 172 --------------
11  tests/run_crypto_tests.sh  |   2 +-
12  tests/speed.c              | 546 +++++++++++++++++++++++++++++++++++++++++++++
13  tests/speed_multi.sh       | 174 +++++++++++++++
14  tests/sync_speed.c         | 400 ---------------------------------
15  7 files changed, 725 insertions(+), 1041 deletions(-)
16  delete mode 100644 tests/async_speed.c
17  delete mode 100755 tests/async_speed_multi.sh
18  create mode 100644 tests/speed.c
19  create mode 100755 tests/speed_multi.sh
20  delete mode 100644 tests/sync_speed.c
21
22 diff --git a/tests/Makefile b/tests/Makefile
23 index 51469e8..88f5040 100644
24 --- a/tests/Makefile
25 +++ b/tests/Makefile
26 @@ -3,19 +3,18 @@ CFLAGS += -I.. $(CRYPTODEV_CFLAGS)
27  
28  comp_progs := cipher_comp hash_comp hmac_comp
29  
30 -hostprogs := cipher cipher-aead hmac sync_speed async_cipher async_hmac \
31 -       async_speed sha_speed hashcrypt_speed fullspeed cipher-gcm \
32 +hostprogs := cipher cipher-aead hmac async_cipher async_hmac \
33 +       speed sha_speed hashcrypt_speed fullspeed cipher-gcm \
34         cipher-aead-srtp $(comp_progs)
35  
36  example-cipher-objs := cipher.o
37  example-cipher-aead-objs := cipher-aead.o
38  example-hmac-objs := hmac.o
39 -example-speed-objs := sync_speed.o
40  example-fullspeed-objs := fullspeed.c
41  example-sha-speed-objs := sha_speed.c
42  example-async-cipher-objs := async_cipher.o
43  example-async-hmac-objs := async_hmac.o
44 -example-async-speed-objs := async_speed.o
45 +example-async-speed-objs := speed.o
46  example-hashcrypt-speed-objs := hashcrypt_speed.c
47  
48  prefix ?= /usr/local
49 @@ -38,7 +37,7 @@ install:
50         for prog in $(hostprogs); do \
51                 install -m 755 $$prog $(DESTDIR)/$(bindir)/tests_cryptodev/; \
52         done
53 -       install -m 755 async_speed_multi.sh  $(DESTDIR)/$(bindir)
54 +       install -m 755 speed_multi.sh  $(DESTDIR)/$(bindir)
55         install -m 755 run_crypto_tests.sh  $(DESTDIR)/$(bindir)
56  
57  clean:
58 diff --git a/tests/async_speed.c b/tests/async_speed.c
59 deleted file mode 100644
60 index d16d17e..0000000
61 --- a/tests/async_speed.c
62 +++ /dev/null
63 @@ -1,463 +0,0 @@
64 -/*  cryptodev_test - simple benchmark tool for cryptodev
65 - *
66 - *    Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
67 - *
68 - *  This program is free software; you can redistribute it and/or modify
69 - *  it under the terms of the GNU General Public License as published by
70 - *  the Free Software Foundation; either version 2 of the License, or
71 - *  (at your option) any later version.
72 - *
73 - *  This program is distributed in the hope that it will be useful,
74 - *  but WITHOUT ANY WARRANTY; without even the implied warranty of
75 - *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
76 - *  GNU General Public License for more details.
77 - *
78 - *  You should have received a copy of the GNU General Public License
79 - *  along with this program; if not, write to the Free Software
80 - *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
81 - */
82 -#include <errno.h>
83 -#include <fcntl.h>
84 -#include <poll.h>
85 -#include <stdio.h>
86 -#include <stdlib.h>
87 -#include <string.h>
88 -#include <sys/ioctl.h>
89 -#include <sys/time.h>
90 -#include <sys/types.h>
91 -#include <signal.h>
92 -#include <crypto/cryptodev.h>
93 -#include <stdbool.h>
94 -#include <unistd.h>
95 -#include <stdint.h>
96 -#include <inttypes.h>
97 -
98 -#ifdef ENABLE_ASYNC
99 -
100 -struct test_params {
101 -       bool tflag;
102 -       bool nflag;
103 -       bool mflag;
104 -       int tvalue;
105 -       int nvalue;
106 -};
107 -
108 -const char usage_str[] = "Usage: %s [OPTION]... <cipher>|<hash>\n"
109 -       "Run benchmark test for cipher or hash\n\n"
110 -       "  -t <secs>\t" "time to run each test (default 10 secs)\n"
111 -       "  -n <bytes>\t" "size of the test buffer\n"
112 -       "  -m\t\t" "output in a machine readable format\n"
113 -       "  -h\t\t" "show this help\n\n"
114 -       "Note: SEC driver is configured to support buffers smaller than 512K\n"
115 -;
116 -
117 -int run_null(int fdc, struct test_params tp);
118 -int run_aes_128_cbc(int fdc, struct test_params tp);
119 -int run_aes_256_xts(int fdc, struct test_params tp);
120 -int run_crc32c(int fdc, struct test_params tp);
121 -int run_sha1(int fdc, struct test_params tp);
122 -int run_sha256(int fdc, struct test_params tp);
123 -
124 -#define ALG_COUNT      6
125 -struct {
126 -       char *name;
127 -       int (*func)(int, struct test_params);
128 -} ciphers[ALG_COUNT] = {
129 -       {"null",        run_null},
130 -       {"aes-128-cbc", run_aes_128_cbc},
131 -       {"aes-256-xts", run_aes_256_xts},
132 -       {"crc32c",      run_crc32c},
133 -       {"sha1",        run_sha1},
134 -       {"sha256",      run_sha256},
135 -};
136 -
137 -static double udifftimeval(struct timeval start, struct timeval end)
138 -{
139 -       return (double)(end.tv_usec - start.tv_usec) +
140 -              (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
141 -}
142 -
143 -static volatile int must_finish;
144 -static volatile int must_exit;
145 -static struct pollfd pfd;
146 -
147 -static void alarm_handler(int signo)
148 -{
149 -        must_finish = 1;
150 -       pfd.events = POLLIN;
151 -}
152 -
153 -static void exit_handler(int signo)
154 -{
155 -       must_exit = 1;
156 -       printf("\nexit requested by user through ctrl+c \n");
157 -}
158 -
159 -static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
160 -
161 -static void value2human(uint64_t bytes, double time, double* data, double* speed,char* metric)
162 -{
163 -       int unit = 0;
164 -
165 -       *data = bytes;
166 -       while (*data > 1024 && units[unit + 1]) {
167 -               *data /= 1024;
168 -               unit++;
169 -       }
170 -       *speed = *data / time;
171 -       sprintf(metric, "%sB", units[unit]);
172 -}
173 -
174 -static void value2machine(uint64_t bytes, double time, double* speed)
175 -{
176 -       *speed = bytes / time;
177 -}
178 -
179 -int get_alignmask(int fdc, struct session_op *sess)
180 -{
181 -       int alignmask;
182 -       int min_alignmask = sizeof(void*) - 1;
183 -
184 -#ifdef CIOCGSESSINFO
185 -       struct session_info_op siop;
186 -
187 -       siop.ses = sess->ses;
188 -       if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
189 -               perror("ioctl(CIOCGSESSINFO)");
190 -               return -EINVAL;
191 -       }
192 -       alignmask = siop.alignmask;
193 -       if (alignmask < min_alignmask) {
194 -               alignmask = min_alignmask;
195 -       }
196 -#else
197 -       alignmask = 0;
198 -#endif
199 -
200 -       return alignmask;
201 -}
202 -
203 -int encrypt_data(int fdc, struct test_params tp, struct session_op *sess)
204 -{
205 -       struct crypt_op cop;
206 -       char *buffer[64], iv[32];
207 -       uint8_t mac[64][HASH_MAX_LEN];
208 -       static int val = 23;
209 -       struct timeval start, end;
210 -       uint64_t total = 0;
211 -       double secs, ddata, dspeed;
212 -       char metric[16];
213 -       int rc, wqueue = 0, bufidx = 0;
214 -       int alignmask;
215 -
216 -       memset(iv, 0x23, 32);
217 -
218 -       if (!tp.mflag) {
219 -               printf("\tBuffer size %d bytes: ", tp.nvalue);
220 -               fflush(stdout);
221 -       }
222 -
223 -       alignmask = get_alignmask(fdc, sess);
224 -       for (rc = 0; rc < 64; rc++) {
225 -               if (alignmask) {
226 -                       if (posix_memalign((void **)(buffer + rc), alignmask + 1, tp.nvalue)) {
227 -                               printf("posix_memalign() failed!\n");
228 -                               return 1;
229 -                       }
230 -               } else {
231 -                       if (!(buffer[rc] = malloc(tp.nvalue))) {
232 -                               perror("malloc()");
233 -                               return 1;
234 -                       }
235 -               }
236 -               memset(buffer[rc], val++, tp.nvalue);
237 -       }
238 -       pfd.fd = fdc;
239 -       pfd.events = POLLOUT | POLLIN;
240 -
241 -       must_finish = 0;
242 -       alarm(tp.tvalue);
243 -
244 -       gettimeofday(&start, NULL);
245 -       do {
246 -               if ((rc = poll(&pfd, 1, 100)) < 0) {
247 -                       if (errno & (ERESTART | EINTR))
248 -                               continue;
249 -                       fprintf(stderr, "errno = %d ", errno);
250 -                       perror("poll()");
251 -                       return 1;
252 -               }
253 -
254 -               if (pfd.revents & POLLOUT) {
255 -                       memset(&cop, 0, sizeof(cop));
256 -                       cop.ses = sess->ses;
257 -                       cop.len = tp.nvalue;
258 -                       cop.iv = (unsigned char *)iv;
259 -                       cop.op = COP_ENCRYPT;
260 -                       cop.src = cop.dst = (unsigned char *)buffer[bufidx];
261 -                       cop.mac = mac[bufidx];
262 -                       bufidx = (bufidx + 1) % 64;
263 -
264 -                       if (ioctl(fdc, CIOCASYNCCRYPT, &cop)) {
265 -                               perror("ioctl(CIOCASYNCCRYPT)");
266 -                               return 1;
267 -                       }
268 -                       wqueue++;
269 -               }
270 -               if (pfd.revents & POLLIN) {
271 -                       if (ioctl(fdc, CIOCASYNCFETCH, &cop)) {
272 -                               perror("ioctl(CIOCASYNCFETCH)");
273 -                               return 1;
274 -                       }
275 -                       wqueue--;
276 -                       total += cop.len;
277 -               }
278 -       } while(!must_finish || wqueue);
279 -       gettimeofday(&end, NULL);
280 -
281 -       secs = udifftimeval(start, end)/ 1000000.0;
282 -
283 -       if (tp.mflag) {
284 -               value2machine(total, secs, &dspeed);
285 -               printf("%" PRIu64 "\t%.2f\t%.2f\n", total, secs, dspeed);
286 -       } else {
287 -               value2human(total, secs, &ddata, &dspeed, metric);
288 -               printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
289 -               printf ("%.2f %s/sec\n", dspeed, metric);
290 -       }
291 -
292 -       for (rc = 0; rc < 64; rc++)
293 -               free(buffer[rc]);
294 -       return 0;
295 -}
296 -
297 -void usage(char *cmd_name)
298 -{
299 -       printf(usage_str, cmd_name);
300 -}
301 -
302 -int run_test(int id, struct test_params tp)
303 -{
304 -       int fd;
305 -       int fdc;
306 -       int err;
307 -
308 -       fd = open("/dev/crypto", O_RDWR, 0);
309 -       if (fd < 0) {
310 -               perror("open()");
311 -               return fd;
312 -       }
313 -       if (ioctl(fd, CRIOGET, &fdc)) {
314 -               perror("ioctl(CRIOGET)");
315 -               return -EINVAL;
316 -       }
317 -
318 -       if (!tp.mflag) {
319 -               fprintf(stderr, "Testing %s:\n", ciphers[id].name);
320 -       }
321 -       err = ciphers[id].func(fdc, tp);
322 -
323 -       close(fdc);
324 -       close(fd);
325 -
326 -       return err;
327 -}
328 -
329 -void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
330 -{
331 -       int i;
332 -
333 -       if (tp.nflag) {
334 -               encrypt_data(fdc, tp, sess);
335 -       } else {
336 -               for (i = 256; i <= (64 * 1024); i *= 2) {
337 -                       if (must_exit) {
338 -                               break;
339 -                       }
340 -
341 -                       tp.nvalue = i;
342 -                       if (encrypt_data(fdc, tp, sess)) {
343 -                               break;
344 -                       }
345 -               }
346 -       }
347 -}
348 -
349 -
350 -int run_null(int fdc, struct test_params tp)
351 -{
352 -       struct session_op sess;
353 -       char keybuf[32];
354 -
355 -       fprintf(stderr, "Testing NULL cipher: \n");
356 -       memset(&sess, 0, sizeof(sess));
357 -       sess.cipher = CRYPTO_NULL;
358 -       sess.keylen = 0;
359 -       sess.key = (unsigned char *)keybuf;
360 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
361 -               perror("ioctl(CIOCGSESSION)");
362 -               return -EINVAL;
363 -       }
364 -
365 -       do_test_vectors(fdc, tp, &sess);
366 -       return 0;
367 -}
368 -
369 -int run_aes_128_cbc(int fdc, struct test_params tp)
370 -{
371 -       struct session_op sess;
372 -       char keybuf[32];
373 -
374 -       memset(&sess, 0, sizeof(sess));
375 -       sess.cipher = CRYPTO_AES_CBC;
376 -       sess.keylen = 16;
377 -       memset(keybuf, 0x42, 16);
378 -       sess.key = (unsigned char *)keybuf;
379 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
380 -               perror("ioctl(CIOCGSESSION)");
381 -               return -EINVAL;
382 -       }
383 -
384 -       do_test_vectors(fdc, tp, &sess);
385 -       return 0;
386 -}
387 -
388 -int run_aes_256_xts(int fdc, struct test_params tp)
389 -{
390 -       struct session_op sess;
391 -       char keybuf[32];
392 -
393 -       memset(&sess, 0, sizeof(sess));
394 -       sess.cipher = CRYPTO_AES_XTS;
395 -       sess.keylen = 32;
396 -       memset(keybuf, 0x42, sess.keylen);
397 -       sess.key = (unsigned char *)keybuf;
398 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
399 -               perror("ioctl(CIOCGSESSION)");
400 -               return -EINVAL;
401 -       }
402 -
403 -       do_test_vectors(fdc, tp, &sess);
404 -       return 0;
405 -}
406 -
407 -int run_crc32c(int fdc, struct test_params tp)
408 -{
409 -       struct session_op sess;
410 -
411 -       memset(&sess, 0, sizeof(sess));
412 -       sess.mac = CRYPTO_CRC32C;
413 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
414 -               perror("ioctl(CIOCGSESSION)");
415 -               return 1;
416 -       }
417 -
418 -       do_test_vectors(fdc, tp, &sess);
419 -       return 0;
420 -}
421 -
422 -int run_sha1(int fdc, struct test_params tp)
423 -{
424 -       struct session_op sess;
425 -
426 -       memset(&sess, 0, sizeof(sess));
427 -       sess.mac = CRYPTO_SHA1;
428 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
429 -               perror("ioctl(CIOCGSESSION)");
430 -               return 1;
431 -       }
432 -
433 -       do_test_vectors(fdc, tp, &sess);
434 -       return 0;
435 -}
436 -
437 -int run_sha256(int fdc, struct test_params tp)
438 -{
439 -       struct session_op sess;
440 -
441 -       memset(&sess, 0, sizeof(sess));
442 -       sess.mac = CRYPTO_SHA2_256;
443 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
444 -               perror("ioctl(CIOCGSESSION)");
445 -               return 1;
446 -       }
447 -
448 -       do_test_vectors(fdc, tp, &sess);
449 -       return 0;
450 -}
451 -
452 -int main(int argc, char **argv)
453 -{
454 -       int err = 0;
455 -       int i;
456 -       int c;
457 -       bool alg_flag;
458 -       char *alg_name;
459 -       struct test_params tp;
460 -
461 -       tp.tflag = false;
462 -       tp.nflag = false;
463 -       tp.mflag = false;
464 -       alg_flag = false;
465 -       opterr = 0;
466 -       while ((c = getopt(argc, argv, "hn:t:m")) != -1) {
467 -               switch (c) {
468 -               case 'n':
469 -                       tp.nvalue = atoi(optarg);
470 -                       tp.nflag = true;
471 -                       break;
472 -               case 't':
473 -                       tp.tvalue = atoi(optarg);
474 -                       tp.tflag = true;
475 -                       break;
476 -               case 'm':
477 -                       tp.mflag = true;
478 -                       break;
479 -               case 'h': /* no break */
480 -               default:
481 -                       usage(argv[0]);
482 -                       exit(1);
483 -               }
484 -       }
485 -
486 -       /* the name of a specific test asked on the command line */
487 -       if (optind < argc) {
488 -               alg_name = argv[optind];
489 -               alg_flag = true;
490 -       }
491 -
492 -       /* default test time */
493 -       if (!tp.tflag) {
494 -               tp.tvalue = 5;
495 -       }
496 -
497 -       signal(SIGALRM, alarm_handler);
498 -       signal(SIGINT, exit_handler);
499 -
500 -       for (i = 0; i < ALG_COUNT; i++) {
501 -               if (must_exit) {
502 -                       break;
503 -               }
504 -
505 -               if (alg_flag) {
506 -                       if (strcmp(alg_name, ciphers[i].name) == 0) {
507 -                               err = run_test(i, tp);
508 -                       }
509 -               } else {
510 -                       err = run_test(i, tp);
511 -                       if (err != 0) {
512 -                               break;
513 -                       }
514 -               }
515 -       }
516 -
517 -       return err;
518 -}
519 -
520 -#else
521 -int
522 -main(int argc, char** argv)
523 -{
524 -       return (0);
525 -}
526 -#endif
527 diff --git a/tests/async_speed_multi.sh b/tests/async_speed_multi.sh
528 deleted file mode 100755
529 index 90f9b72..0000000
530 --- a/tests/async_speed_multi.sh
531 +++ /dev/null
532 @@ -1,172 +0,0 @@
533 -#!/bin/bash
534 -#
535 -#    Copyright 2016 NXP Semiconductors
536 -#
537 -#    This program is free software: you can redistribute it and/or modify
538 -#    it under the terms of the GNU General Public License as published by
539 -#    the Free Software Foundation, either version 2 of the License, or
540 -#    (at your option) any later version.
541 -#
542 -#    This program is distributed in the hope that it will be useful,
543 -#    but WITHOUT ANY WARRANTY; without even the implied warranty of
544 -#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
545 -#    GNU General Public License for more details.
546 -#
547 -#    You should have received a copy of the GNU General Public License
548 -#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
549 -
550 -
551 -
552 -# no user-configurable options below this line
553 -
554 -NUM_CORES=$(nproc)
555 -CMD_BIN="async_speed"
556 -OUT_BASENAME="async_speed"
557 -MPSTAT_OUT="mpstat_out"
558 -
559 -# A bigger hammer for mpstat to use ISO8601 time format (fixed in 11.2.2)
560 -export LC_TIME=en_GB.UTF-8 &> /dev/null
561 -
562 -
563 -function usage
564 -{
565 -cat << EOF
566 -Usage: `basename $0` [OPTIONS] <alg_name>
567 -
568 -  -m <threads>    number of threads to run with (defaults to number of cores)
569 -  -t <secs>       time to run each test (default 10 secs)
570 -  -n <bytes>      size of the test buffer (default 256 bytes)
571 -  -v              make output more verbose (default tabular)
572 -  -h              show this help
573 -
574 -alg_name: null, aes-128-cbc, aes-256-xts, sha1, sha256, crc32c
575 -
576 -Note: SEC driver is configured to support buffers smaller than 512K
577 -EOF
578 -}
579 -
580 -function SUM {
581 -       paste -sd+ - | bc -l
582 -}
583 -
584 -function get_cpu_idle
585 -{
586 -    header_line=$(grep %idle ${MPSTAT_OUT} | head -n 1 | sed 's/\s\+/ /g')
587 -    idle_column=$(echo $header_line | wc -w)
588 -    average_idle=$(grep Average ${MPSTAT_OUT} | sed 's/\s\+/ /g' | cut -d' ' -f ${idle_column} | tail -n 1)
589 -
590 -    echo $average_idle
591 -}
592 -
593 -function run_parallel
594 -{
595 -    trap control_c SIGINT
596 -
597 -    OPTIONS="-t $tvalue -n $nvalue -m"
598 -    CMD="$CMD_BIN $OPTIONS $alg_name"
599 -
600 -    (sleep 1; S_TIME_FORMAT=ISO mpstat 1 $(($tvalue-2))) &> $MPSTAT_OUT &
601 -    MPSTAT_PID=$!
602 -
603 -    PIDS=""
604 -    start=$(date +%s.%N)
605 -
606 -    for i in $(seq 0 $(($mvalue-1)))
607 -    do
608 -       CMD_OUT="${OUT_BASENAME}_${i}"
609 -
610 -       $CMD &> $CMD_OUT &
611 -       PID=$!
612 -       AFFINITY=$(($i % $NUM_CORES))
613 -       taskset -pc $AFFINITY $PID > /dev/null
614 -
615 -       PIDS="$PID $PIDS"
616 -    done
617 -
618 -    wait $PIDS
619 -    end=$(date +%s.%N)
620 -
621 -    wait $MPSTAT_PID
622 -
623 -    grep "ioctl" ${OUT_BASENAME}_* &> /dev/null
624 -    if (($? == 0))
625 -    then
626 -       echo "cryptodev is not built with -DENABLE_ASYNC flag"
627 -       exit 1
628 -    fi
629 -
630 -    runtime=$(echo "scale=2; ($end - $start) / 1" | bc -l )
631 -    total_data=$(cat ${OUT_BASENAME}_* | cut -f 1 | SUM)
632 -    avg_speed=$(echo "scale=2; $total_data / $runtime / 1000000000" | bc -l)
633 -    cpu_idle=$(get_cpu_idle)
634 -
635 -    if [ ! -z "$vvalue" ]
636 -    then
637 -       echo
638 -       echo "buffer size  :   $nvalue"
639 -       echo "running time :   $runtime"
640 -       echo "avg_speed    :   $avg_speed GB/s"
641 -       echo "all_cpu idle :   $cpu_idle %"
642 -       echo
643 -    else
644 -       echo -e "algorithm\t""threads\t""run time\t"\
645 -            "buffer size\t""GB/s\t""%cpu idle"
646 -       echo -e "${alg_name}\t${mvalue}\t${runtime}\t"\
647 -            "${nvalue}\t${avg_speed}\t${cpu_idle}%"
648 -    fi
649 -}
650 -
651 -function control_c
652 -{
653 -    killall async_speed > /dev/null
654 -    killall mpstat > /dev/null
655 -}
656 -
657 -function main
658 -{
659 -       [ ! -e "/dev/crypto" ] &&
660 -               (sudo modprobe cryptodev || modprobe cryptodev || exit 1)
661 -
662 -       $(which ${CMD_BIN} &> /dev/null)
663 -       if (($? != 0))
664 -       then
665 -               echo "${CMD_BIN} test is not installed"
666 -               exit 1
667 -       fi
668 -
669 -       rm -f ${OUT_BASENAME}_*
670 -       rm -f ${MPSTAT_OUT}
671 -
672 -       while getopts vhm:t:n: option
673 -       do
674 -               case "$option" in
675 -                       m) mvalue="$OPTARG";;
676 -                       t) tvalue="$OPTARG";;
677 -                       n) nvalue="$OPTARG";;
678 -                       v) vvalue="verbose";;
679 -                       *) usage $0; exit 1;;
680 -               esac
681 -       done
682 -
683 -       shift $((OPTIND-1))
684 -       alg_name=$1
685 -
686 -       [ -z "$tvalue" ] && tvalue=10         # 10 seconds per test by default
687 -       [ -z "$mvalue" ] && mvalue=$NUM_CORES # thread count defaults to nproc
688 -       [ -z "$nvalue" ] && nvalue=256        # 256 bytes default buffer size
689 -
690 -       [ "$tvalue" -lt 5 ] && tvalue=5
691 -
692 -       case "$alg_name" in
693 -           "null"    |\
694 -           "aes-128-cbc" |\
695 -           "aes-256-xts" |\
696 -           "sha1"    |\
697 -           "sha256"  |\
698 -           "crc32c"  ) run_parallel;;
699 -           * ) usage && exit 1;;
700 -       esac
701 -}
702 -
703 -main "$@"
704 -
705 diff --git a/tests/run_crypto_tests.sh b/tests/run_crypto_tests.sh
706 index 32ab8e2..e128637 100644
707 --- a/tests/run_crypto_tests.sh
708 +++ b/tests/run_crypto_tests.sh
709 @@ -16,7 +16,7 @@ do
710         do
711                 for bsize in ${BUF_SIZE}
712                 do
713 -                       async_speed_multi.sh -t 10 -n $bsize -m ${multi} ${alg_name} |
714 +                       speed_multi.sh -t 10 -n $bsize -m ${multi} ${alg_name} |
715                         tail -n 1
716                 done
717         done
718 diff --git a/tests/speed.c b/tests/speed.c
719 new file mode 100644
720 index 0000000..3b36db1
721 --- /dev/null
722 +++ b/tests/speed.c
723 @@ -0,0 +1,546 @@
724 +/*  cryptodev_test - simple benchmark tool for cryptodev
725 + *
726 + *    Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
727 + *
728 + *  This program is free software; you can redistribute it and/or modify
729 + *  it under the terms of the GNU General Public License as published by
730 + *  the Free Software Foundation; either version 2 of the License, or
731 + *  (at your option) any later version.
732 + *
733 + *  This program is distributed in the hope that it will be useful,
734 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
735 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
736 + *  GNU General Public License for more details.
737 + *
738 + *  You should have received a copy of the GNU General Public License
739 + *  along with this program; if not, write to the Free Software
740 + *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
741 + */
742 +#include <errno.h>
743 +#include <fcntl.h>
744 +#include <poll.h>
745 +#include <stdio.h>
746 +#include <stdlib.h>
747 +#include <string.h>
748 +#include <sys/ioctl.h>
749 +#include <sys/time.h>
750 +#include <sys/types.h>
751 +#include <signal.h>
752 +#include <crypto/cryptodev.h>
753 +#include <stdbool.h>
754 +#include <unistd.h>
755 +#include <stdint.h>
756 +#include <inttypes.h>
757 +
758 +
759 +struct test_params {
760 +       bool tflag;
761 +       bool nflag;
762 +       bool mflag;
763 +       bool aflag;
764 +       int tvalue;
765 +       int nvalue;
766 +};
767 +
768 +const char usage_str[] = "Usage: %s [OPTION]... <cipher>|<hash>\n"
769 +       "Run benchmark test for cipher or hash\n\n"
770 +       "  -t <secs>\t" "time to run each test (default 10 secs)\n"
771 +       "  -n <bytes>\t" "size of the test buffer\n"
772 +       "  -m\t\t" "output in a machine readable format\n"
773 +       "  -a\t\t" "run the async tests (default sync)\n"
774 +       "  -h\t\t" "show this help\n\n"
775 +       "Note: SEC driver is configured to support buffers smaller than 512K\n"
776 +;
777 +
778 +int run_null(int fdc, struct test_params tp);
779 +int run_aes_128_cbc(int fdc, struct test_params tp);
780 +int run_aes_256_xts(int fdc, struct test_params tp);
781 +int run_crc32c(int fdc, struct test_params tp);
782 +int run_sha1(int fdc, struct test_params tp);
783 +int run_sha256(int fdc, struct test_params tp);
784 +
785 +#define ALG_COUNT      6
786 +struct {
787 +       char *name;
788 +       int (*func)(int, struct test_params);
789 +} ciphers[ALG_COUNT] = {
790 +       {"null",        run_null},
791 +       {"aes-128-cbc", run_aes_128_cbc},
792 +       {"aes-256-xts", run_aes_256_xts},
793 +       {"crc32c",      run_crc32c},
794 +       {"sha1",        run_sha1},
795 +       {"sha256",      run_sha256},
796 +};
797 +
798 +static double udifftimeval(struct timeval start, struct timeval end)
799 +{
800 +       return (double)(end.tv_usec - start.tv_usec) +
801 +              (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
802 +}
803 +
804 +static volatile int must_finish;
805 +static volatile int must_exit;
806 +static struct pollfd pfd;
807 +
808 +static void alarm_handler(int signo)
809 +{
810 +        must_finish = 1;
811 +       pfd.events = POLLIN;
812 +}
813 +
814 +static void exit_handler(int signo)
815 +{
816 +       must_exit = 1;
817 +       printf("\nexit requested by user through ctrl+c \n");
818 +}
819 +
820 +static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
821 +
822 +static void value2human(uint64_t bytes, double time, double* data, double* speed,char* metric)
823 +{
824 +       int unit = 0;
825 +
826 +       *data = bytes;
827 +       while (*data > 1024 && units[unit + 1]) {
828 +               *data /= 1024;
829 +               unit++;
830 +       }
831 +       *speed = *data / time;
832 +       sprintf(metric, "%sB", units[unit]);
833 +}
834 +
835 +static void value2machine(uint64_t bytes, double time, double* speed)
836 +{
837 +       *speed = bytes / time;
838 +}
839 +
840 +int get_alignmask(int fdc, struct session_op *sess)
841 +{
842 +       int alignmask;
843 +       int min_alignmask = sizeof(void*) - 1;
844 +
845 +#ifdef CIOCGSESSINFO
846 +       struct session_info_op siop;
847 +
848 +       siop.ses = sess->ses;
849 +       if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
850 +               perror("ioctl(CIOCGSESSINFO)");
851 +               return -EINVAL;
852 +       }
853 +       alignmask = siop.alignmask;
854 +       if (alignmask < min_alignmask) {
855 +               alignmask = min_alignmask;
856 +       }
857 +#else
858 +       alignmask = 0;
859 +#endif
860 +
861 +       return alignmask;
862 +}
863 +
864 +int encrypt_async(int fdc, struct test_params tp, struct session_op *sess)
865 +{
866 +       struct crypt_op cop;
867 +       char *buffer[64], iv[32];
868 +       uint8_t mac[64][HASH_MAX_LEN];
869 +       static int val = 23;
870 +       struct timeval start, end;
871 +       uint64_t total = 0;
872 +       double secs, ddata, dspeed;
873 +       char metric[16];
874 +       int rc, wqueue = 0, bufidx = 0;
875 +       int alignmask;
876 +
877 +       memset(iv, 0x23, 32);
878 +
879 +       if (!tp.mflag) {
880 +               printf("\tBuffer size %d bytes: ", tp.nvalue);
881 +               fflush(stdout);
882 +       }
883 +
884 +       alignmask = get_alignmask(fdc, sess);
885 +       for (rc = 0; rc < 64; rc++) {
886 +               if (alignmask) {
887 +                       if (posix_memalign((void **)(buffer + rc), alignmask + 1, tp.nvalue)) {
888 +                               printf("posix_memalign() failed!\n");
889 +                               return 1;
890 +                       }
891 +               } else {
892 +                       if (!(buffer[rc] = malloc(tp.nvalue))) {
893 +                               perror("malloc()");
894 +                               return 1;
895 +                       }
896 +               }
897 +               memset(buffer[rc], val++, tp.nvalue);
898 +       }
899 +       pfd.fd = fdc;
900 +       pfd.events = POLLOUT | POLLIN;
901 +
902 +       must_finish = 0;
903 +       alarm(tp.tvalue);
904 +
905 +       gettimeofday(&start, NULL);
906 +       do {
907 +               if ((rc = poll(&pfd, 1, 100)) < 0) {
908 +                       if (errno & (ERESTART | EINTR))
909 +                               continue;
910 +                       fprintf(stderr, "errno = %d ", errno);
911 +                       perror("poll()");
912 +                       return 1;
913 +               }
914 +
915 +               if (pfd.revents & POLLOUT) {
916 +                       memset(&cop, 0, sizeof(cop));
917 +                       cop.ses = sess->ses;
918 +                       cop.len = tp.nvalue;
919 +                       cop.iv = (unsigned char *)iv;
920 +                       cop.op = COP_ENCRYPT;
921 +                       cop.src = cop.dst = (unsigned char *)buffer[bufidx];
922 +                       cop.mac = mac[bufidx];
923 +                       bufidx = (bufidx + 1) % 64;
924 +
925 +                       if (ioctl(fdc, CIOCASYNCCRYPT, &cop)) {
926 +                               perror("ioctl(CIOCASYNCCRYPT)");
927 +                               return 1;
928 +                       }
929 +                       wqueue++;
930 +               }
931 +               if (pfd.revents & POLLIN) {
932 +                       if (ioctl(fdc, CIOCASYNCFETCH, &cop)) {
933 +                               perror("ioctl(CIOCASYNCFETCH)");
934 +                               return 1;
935 +                       }
936 +                       wqueue--;
937 +                       total += cop.len;
938 +               }
939 +       } while(!must_finish || wqueue);
940 +       gettimeofday(&end, NULL);
941 +
942 +       secs = udifftimeval(start, end)/ 1000000.0;
943 +
944 +       if (tp.mflag) {
945 +               value2machine(total, secs, &dspeed);
946 +               printf("%" PRIu64 "\t%.2f\t%.2f\n", total, secs, dspeed);
947 +       } else {
948 +               value2human(total, secs, &ddata, &dspeed, metric);
949 +               printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
950 +               printf ("%.2f %s/sec\n", dspeed, metric);
951 +       }
952 +
953 +       for (rc = 0; rc < 64; rc++)
954 +               free(buffer[rc]);
955 +       return 0;
956 +}
957 +
958 +
959 +static int encrypt_sync(int fdc, struct test_params tp, struct session_op *sess)
960 +{
961 +       struct crypt_op cop;
962 +       char *buffer, iv[32];
963 +       char mac[HASH_MAX_LEN];
964 +       static int val = 23;
965 +       struct timeval start, end;
966 +       uint64_t total = 0;
967 +       double secs, ddata, dspeed;
968 +       char metric[16];
969 +       int alignmask;
970 +       int min_alignmask = sizeof(void*) - 1;
971 +
972 +       memset(iv, 0x23, 32);
973 +
974 +       if (!tp.mflag) {
975 +               printf("\tBuffer size %d bytes: ", tp.nvalue);
976 +               fflush(stdout);
977 +       }
978 +
979 +       alignmask = get_alignmask(fdc, sess);
980 +       if (alignmask) {
981 +               alignmask = ((alignmask < min_alignmask) ? min_alignmask : alignmask);
982 +               if (posix_memalign((void **)(&buffer), alignmask + 1, tp.nvalue)) {
983 +                       printf("posix_memalign() failed!\n");
984 +                       return 1;
985 +               }
986 +       } else {
987 +               if (!(buffer = malloc(tp.nvalue))) {
988 +                       perror("malloc()");
989 +                       return 1;
990 +               }
991 +       }
992 +       memset(buffer, val++, tp.nvalue);
993 +
994 +       must_finish = 0;
995 +       alarm(tp.tvalue);
996 +
997 +       gettimeofday(&start, NULL);
998 +       do {
999 +               memset(&cop, 0, sizeof(cop));
1000 +               cop.ses = sess->ses;
1001 +               cop.len = tp.nvalue;
1002 +               cop.iv = (unsigned char *)iv;
1003 +               cop.op = COP_ENCRYPT;
1004 +               cop.src = cop.dst = (unsigned char *)buffer;
1005 +               cop.mac = (unsigned char *)mac;
1006 +
1007 +               if (ioctl(fdc, CIOCCRYPT, &cop)) {
1008 +                       perror("ioctl(CIOCCRYPT)");
1009 +                       return 1;
1010 +               }
1011 +               total += cop.len;
1012 +       } while(!must_finish);
1013 +       gettimeofday(&end, NULL);
1014 +
1015 +       secs = udifftimeval(start, end)/ 1000000.0;
1016 +
1017 +       if (tp.mflag) {
1018 +               value2machine(total, secs, &dspeed);
1019 +               printf("%" PRIu64 "\t%.2f\t%.2f\n", total, secs, dspeed);
1020 +       } else {
1021 +               value2human(total, secs, &ddata, &dspeed, metric);
1022 +               printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
1023 +               printf ("%.2f %s/sec\n", dspeed, metric);
1024 +       }
1025 +
1026 +       free(buffer);
1027 +       return 0;
1028 +}
1029 +
1030 +void usage(char *cmd_name)
1031 +{
1032 +       printf(usage_str, cmd_name);
1033 +}
1034 +
1035 +int run_test(int id, struct test_params tp)
1036 +{
1037 +       int fd;
1038 +       int fdc;
1039 +       int err;
1040 +
1041 +       fd = open("/dev/crypto", O_RDWR, 0);
1042 +       if (fd < 0) {
1043 +               perror("open()");
1044 +               return fd;
1045 +       }
1046 +       if (ioctl(fd, CRIOGET, &fdc)) {
1047 +               perror("ioctl(CRIOGET)");
1048 +               return -EINVAL;
1049 +       }
1050 +
1051 +       if (!tp.mflag) {
1052 +               char *type;
1053 +               type = tp.aflag ? "async" : "sync";
1054 +
1055 +               fprintf(stderr, "Testing %s %s:\n", type, ciphers[id].name);
1056 +       }
1057 +       err = ciphers[id].func(fdc, tp);
1058 +
1059 +       close(fdc);
1060 +       close(fd);
1061 +
1062 +       return err;
1063 +}
1064 +
1065 +void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
1066 +{
1067 +       int i;
1068 +       int err;
1069 +
1070 +       if (tp.nflag) {
1071 +               if (tp.aflag) {
1072 +                       encrypt_async(fdc, tp, sess);
1073 +               } else {
1074 +                       encrypt_sync(fdc, tp, sess);
1075 +               }
1076 +       } else {
1077 +               for (i = 256; i <= (64 * 1024); i *= 2) {
1078 +                       if (must_exit) {
1079 +                               break;
1080 +                       }
1081 +
1082 +                       tp.nvalue = i;
1083 +                       if (tp.aflag) {
1084 +                               err = encrypt_async(fdc, tp, sess);
1085 +                       } else {
1086 +                               err = encrypt_sync(fdc, tp, sess);
1087 +                       }
1088 +
1089 +                       if (err != 0) {
1090 +                               break;
1091 +                       }
1092 +               }
1093 +       }
1094 +}
1095 +
1096 +
1097 +int run_null(int fdc, struct test_params tp)
1098 +{
1099 +       struct session_op sess;
1100 +       char keybuf[32];
1101 +
1102 +       fprintf(stderr, "Testing NULL cipher: \n");
1103 +       memset(&sess, 0, sizeof(sess));
1104 +       sess.cipher = CRYPTO_NULL;
1105 +       sess.keylen = 0;
1106 +       sess.key = (unsigned char *)keybuf;
1107 +       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1108 +               perror("ioctl(CIOCGSESSION)");
1109 +               return -EINVAL;
1110 +       }
1111 +
1112 +       do_test_vectors(fdc, tp, &sess);
1113 +       return 0;
1114 +}
1115 +
1116 +int run_aes_128_cbc(int fdc, struct test_params tp)
1117 +{
1118 +       struct session_op sess;
1119 +       char keybuf[32];
1120 +
1121 +       memset(&sess, 0, sizeof(sess));
1122 +       sess.cipher = CRYPTO_AES_CBC;
1123 +       sess.keylen = 16;
1124 +       memset(keybuf, 0x42, 16);
1125 +       sess.key = (unsigned char *)keybuf;
1126 +       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1127 +               perror("ioctl(CIOCGSESSION)");
1128 +               return -EINVAL;
1129 +       }
1130 +
1131 +       do_test_vectors(fdc, tp, &sess);
1132 +       return 0;
1133 +}
1134 +
1135 +int run_aes_256_xts(int fdc, struct test_params tp)
1136 +{
1137 +       struct session_op sess;
1138 +       char keybuf[32];
1139 +
1140 +       memset(&sess, 0, sizeof(sess));
1141 +       sess.cipher = CRYPTO_AES_XTS;
1142 +       sess.keylen = 32;
1143 +       memset(keybuf, 0x42, sess.keylen);
1144 +       sess.key = (unsigned char *)keybuf;
1145 +       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1146 +               perror("ioctl(CIOCGSESSION)");
1147 +               return -EINVAL;
1148 +       }
1149 +
1150 +       do_test_vectors(fdc, tp, &sess);
1151 +       return 0;
1152 +}
1153 +
1154 +int run_crc32c(int fdc, struct test_params tp)
1155 +{
1156 +       struct session_op sess;
1157 +
1158 +       memset(&sess, 0, sizeof(sess));
1159 +       sess.mac = CRYPTO_CRC32C;
1160 +       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1161 +               perror("ioctl(CIOCGSESSION)");
1162 +               return 1;
1163 +       }
1164 +
1165 +       do_test_vectors(fdc, tp, &sess);
1166 +       return 0;
1167 +}
1168 +
1169 +int run_sha1(int fdc, struct test_params tp)
1170 +{
1171 +       struct session_op sess;
1172 +
1173 +       memset(&sess, 0, sizeof(sess));
1174 +       sess.mac = CRYPTO_SHA1;
1175 +       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1176 +               perror("ioctl(CIOCGSESSION)");
1177 +               return 1;
1178 +       }
1179 +
1180 +       do_test_vectors(fdc, tp, &sess);
1181 +       return 0;
1182 +}
1183 +
1184 +int run_sha256(int fdc, struct test_params tp)
1185 +{
1186 +       struct session_op sess;
1187 +
1188 +       memset(&sess, 0, sizeof(sess));
1189 +       sess.mac = CRYPTO_SHA2_256;
1190 +       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1191 +               perror("ioctl(CIOCGSESSION)");
1192 +               return 1;
1193 +       }
1194 +
1195 +       do_test_vectors(fdc, tp, &sess);
1196 +       return 0;
1197 +}
1198 +
1199 +int main(int argc, char **argv)
1200 +{
1201 +       int err = 0;
1202 +       int i;
1203 +       int c;
1204 +       bool alg_flag;
1205 +       char *alg_name;
1206 +       struct test_params tp;
1207 +
1208 +       tp.tflag = false;
1209 +       tp.nflag = false;
1210 +       tp.mflag = false;
1211 +       tp.aflag = false;
1212 +       alg_flag = false;
1213 +       opterr = 0;
1214 +       while ((c = getopt(argc, argv, "ahn:t:m")) != -1) {
1215 +               switch (c) {
1216 +               case 'n':
1217 +                       tp.nvalue = atoi(optarg);
1218 +                       tp.nflag = true;
1219 +                       break;
1220 +               case 't':
1221 +                       tp.tvalue = atoi(optarg);
1222 +                       tp.tflag = true;
1223 +                       break;
1224 +               case 'm':
1225 +                       tp.mflag = true;
1226 +                       break;
1227 +               case 'a':
1228 +                       tp.aflag = true;
1229 +                       break;
1230 +               case 'h': /* no break */
1231 +               default:
1232 +                       usage(argv[0]);
1233 +                       exit(1);
1234 +               }
1235 +       }
1236 +
1237 +       /* the name of a specific test asked on the command line */
1238 +       if (optind < argc) {
1239 +               alg_name = argv[optind];
1240 +               alg_flag = true;
1241 +       }
1242 +
1243 +       /* default test time */
1244 +       if (!tp.tflag) {
1245 +               tp.tvalue = 5;
1246 +       }
1247 +
1248 +       signal(SIGALRM, alarm_handler);
1249 +       signal(SIGINT, exit_handler);
1250 +
1251 +       for (i = 0; i < ALG_COUNT; i++) {
1252 +               if (must_exit) {
1253 +                       break;
1254 +               }
1255 +
1256 +               if (alg_flag) {
1257 +                       if (strcmp(alg_name, ciphers[i].name) == 0) {
1258 +                               err = run_test(i, tp);
1259 +                       }
1260 +               } else {
1261 +                       err = run_test(i, tp);
1262 +                       if (err != 0) {
1263 +                               break;
1264 +                       }
1265 +               }
1266 +       }
1267 +
1268 +       return err;
1269 +}
1270 diff --git a/tests/speed_multi.sh b/tests/speed_multi.sh
1271 new file mode 100755
1272 index 0000000..b116483
1273 --- /dev/null
1274 +++ b/tests/speed_multi.sh
1275 @@ -0,0 +1,174 @@
1276 +#!/bin/bash
1277 +#
1278 +#    Copyright 2016 NXP Semiconductors
1279 +#
1280 +#    This program is free software: you can redistribute it and/or modify
1281 +#    it under the terms of the GNU General Public License as published by
1282 +#    the Free Software Foundation, either version 2 of the License, or
1283 +#    (at your option) any later version.
1284 +#
1285 +#    This program is distributed in the hope that it will be useful,
1286 +#    but WITHOUT ANY WARRANTY; without even the implied warranty of
1287 +#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1288 +#    GNU General Public License for more details.
1289 +#
1290 +#    You should have received a copy of the GNU General Public License
1291 +#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
1292 +
1293 +
1294 +
1295 +# no user-configurable options below this line
1296 +
1297 +NUM_CORES=$(nproc)
1298 +CMD_BIN="speed"
1299 +OUT_BASENAME="speed"
1300 +MPSTAT_OUT="mpstat_out"
1301 +
1302 +# A bigger hammer for mpstat to use ISO8601 time format (fixed in 11.2.2)
1303 +export LC_TIME=en_GB.UTF-8 &> /dev/null
1304 +
1305 +
1306 +function usage
1307 +{
1308 +cat << EOF
1309 +Usage: `basename $0` [OPTIONS] <alg_name>
1310 +
1311 +  -m <threads>    number of threads to run with (defaults to number of cores)
1312 +  -t <secs>       time to run each test (default 10 secs)
1313 +  -n <bytes>      size of the test buffer (default 256 bytes)
1314 +  -v              make output more verbose (default tabular)
1315 +  -a              run async version of the benchmark (default sync)
1316 +  -h              show this help
1317 +
1318 +alg_name: null, aes-128-cbc, aes-256-xts, sha1, sha256, crc32c
1319 +
1320 +Note: SEC driver is configured to support buffers smaller than 512K
1321 +EOF
1322 +}
1323 +
1324 +function SUM {
1325 +       paste -sd+ - | bc -l
1326 +}
1327 +
1328 +function get_cpu_idle
1329 +{
1330 +    header_line=$(grep %idle ${MPSTAT_OUT} | head -n 1 | sed 's/\s\+/ /g')
1331 +    idle_column=$(echo $header_line | wc -w)
1332 +    average_idle=$(grep Average ${MPSTAT_OUT} | sed 's/\s\+/ /g' | cut -d' ' -f ${idle_column} | tail -n 1)
1333 +
1334 +    echo $average_idle
1335 +}
1336 +
1337 +function run_parallel
1338 +{
1339 +    trap control_c SIGINT
1340 +
1341 +    OPTIONS="-t $tvalue -n $nvalue -m $aflag"
1342 +    CMD="$CMD_BIN $OPTIONS $alg_name"
1343 +
1344 +    (sleep 1; S_TIME_FORMAT=ISO mpstat 1 $(($tvalue-2))) &> $MPSTAT_OUT &
1345 +    MPSTAT_PID=$!
1346 +
1347 +    PIDS=""
1348 +    start=$(date +%s.%N)
1349 +
1350 +    for i in $(seq 0 $(($mvalue-1)))
1351 +    do
1352 +       CMD_OUT="${OUT_BASENAME}_${i}"
1353 +
1354 +       $CMD &> $CMD_OUT &
1355 +       PID=$!
1356 +       AFFINITY=$(($i % $NUM_CORES))
1357 +       taskset -pc $AFFINITY $PID > /dev/null
1358 +
1359 +       PIDS="$PID $PIDS"
1360 +    done
1361 +
1362 +    wait $PIDS
1363 +    end=$(date +%s.%N)
1364 +
1365 +    wait $MPSTAT_PID
1366 +
1367 +    grep "ioctl" ${OUT_BASENAME}_* &> /dev/null
1368 +    if (($? == 0))
1369 +    then
1370 +       echo "cryptodev is not built with -DENABLE_ASYNC flag"
1371 +       exit 1
1372 +    fi
1373 +
1374 +    runtime=$(echo "scale=2; ($end - $start) / 1" | bc -l )
1375 +    total_data=$(cat ${OUT_BASENAME}_* | cut -f 1 | SUM)
1376 +    avg_speed=$(echo "scale=2; $total_data / $runtime / 1000000000" | bc -l)
1377 +    cpu_idle=$(get_cpu_idle)
1378 +
1379 +    if [ ! -z "$vflag" ]
1380 +    then
1381 +       echo
1382 +       echo "buffer size  :   $nvalue"
1383 +       echo "running time :   $runtime"
1384 +       echo "avg_speed    :   $avg_speed GB/s"
1385 +       echo "all_cpu idle :   $cpu_idle %"
1386 +       echo
1387 +    else
1388 +       echo -e "algorithm\t""threads\t""run time\t"\
1389 +            "buffer size\t""GB/s\t""%cpu idle"
1390 +       echo -e "${alg_name}\t${mvalue}\t${runtime}\t"\
1391 +            "${nvalue}\t${avg_speed}\t${cpu_idle}%"
1392 +    fi
1393 +}
1394 +
1395 +function control_c
1396 +{
1397 +    killall $CMD_BIN > /dev/null
1398 +    killall mpstat > /dev/null
1399 +}
1400 +
1401 +function main
1402 +{
1403 +       [ ! -e "/dev/crypto" ] &&
1404 +               (sudo modprobe cryptodev || modprobe cryptodev || exit 1)
1405 +
1406 +       $(which ${CMD_BIN} &> /dev/null)
1407 +       if (($? != 0))
1408 +       then
1409 +               echo "${CMD_BIN} test is not installed"
1410 +               exit 1
1411 +       fi
1412 +
1413 +       rm -f ${OUT_BASENAME}_*
1414 +       rm -f ${MPSTAT_OUT}
1415 +
1416 +       while getopts avhm:t:n: option
1417 +       do
1418 +               case "$option" in
1419 +                       m) mvalue="$OPTARG";;
1420 +                       t) tvalue="$OPTARG";;
1421 +                       n) nvalue="$OPTARG";;
1422 +                       v) vflag="verbose";;
1423 +                       a) aflag="-a";;
1424 +                       *) usage $0; exit 1;;
1425 +               esac
1426 +       done
1427 +
1428 +       shift $((OPTIND-1))
1429 +       alg_name=$1
1430 +
1431 +       [ -z "$tvalue" ] && tvalue=10         # 10 seconds per test by default
1432 +       [ -z "$mvalue" ] && mvalue=$NUM_CORES # thread count defaults to nproc
1433 +       [ -z "$nvalue" ] && nvalue=256        # 256 bytes default buffer size
1434 +
1435 +       [ "$tvalue" -lt 5 ] && tvalue=5
1436 +
1437 +       case "$alg_name" in
1438 +           "null"    |\
1439 +           "aes-128-cbc" |\
1440 +           "aes-256-xts" |\
1441 +           "sha1"    |\
1442 +           "sha256"  |\
1443 +           "crc32c"  ) run_parallel;;
1444 +           * ) usage && exit 1;;
1445 +       esac
1446 +}
1447 +
1448 +main "$@"
1449 +
1450 diff --git a/tests/sync_speed.c b/tests/sync_speed.c
1451 deleted file mode 100644
1452 index ceae645..0000000
1453 --- a/tests/sync_speed.c
1454 +++ /dev/null
1455 @@ -1,400 +0,0 @@
1456 -/*  cryptodev_test - simple benchmark tool for cryptodev
1457 - *
1458 - *    Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
1459 - *
1460 - *  This program is free software; you can redistribute it and/or modify
1461 - *  it under the terms of the GNU General Public License as published by
1462 - *  the Free Software Foundation; either version 2 of the License, or
1463 - *  (at your option) any later version.
1464 - *
1465 - *  This program is distributed in the hope that it will be useful,
1466 - *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1467 - *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1468 - *  GNU General Public License for more details.
1469 - *
1470 - *  You should have received a copy of the GNU General Public License
1471 - *  along with this program; if not, write to the Free Software
1472 - *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1473 - */
1474 -#include <errno.h>
1475 -#include <fcntl.h>
1476 -#include <poll.h>
1477 -#include <stdio.h>
1478 -#include <stdlib.h>
1479 -#include <string.h>
1480 -#include <sys/ioctl.h>
1481 -#include <sys/time.h>
1482 -#include <sys/types.h>
1483 -#include <signal.h>
1484 -#include <crypto/cryptodev.h>
1485 -#include <stdbool.h>
1486 -#include <unistd.h>
1487 -
1488 -struct test_params {
1489 -       bool tflag;
1490 -       bool nflag;
1491 -       int tvalue;
1492 -       int nvalue;
1493 -};
1494 -
1495 -const char usage_str[] = "Usage: %s [OPTION]... <cipher>|<hash>\n"
1496 -       "Run benchmark test for cipher or hash\n\n"
1497 -       "  -t <secs>\t" "time to run each test (default 10 secs)\n"
1498 -       "  -n <bytes>\t" "size of the test buffer\n"
1499 -       "  -h\t\t" "show this help\n\n"
1500 -       "Note: SEC driver is configured to support buffers smaller than 512K\n"
1501 -;
1502 -
1503 -int run_null(int fdc, struct test_params tp);
1504 -int run_aes_128_cbc(int fdc, struct test_params tp);
1505 -int run_aes_256_xts(int fdc, struct test_params tp);
1506 -int run_crc32c(int fdc, struct test_params tp);
1507 -int run_sha1(int fdc, struct test_params tp);
1508 -int run_sha256(int fdc, struct test_params tp);
1509 -int get_alignmask(int fdc, struct session_op *sess);
1510 -
1511 -#define ALG_COUNT      6
1512 -struct {
1513 -       char *name;
1514 -       int (*func)(int, struct test_params);
1515 -} ciphers[ALG_COUNT] = {
1516 -       {"null",        run_null},
1517 -       {"aes-128-cbc", run_aes_128_cbc},
1518 -       {"aes-256-xts", run_aes_256_xts},
1519 -       {"crc32c",      run_crc32c},
1520 -       {"sha1",        run_sha1},
1521 -       {"sha256",      run_sha256},
1522 -};
1523 -
1524 -static double udifftimeval(struct timeval start, struct timeval end)
1525 -{
1526 -       return (double)(end.tv_usec - start.tv_usec) +
1527 -              (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
1528 -}
1529 -
1530 -static volatile int must_finish;
1531 -static volatile int must_exit;
1532 -
1533 -static void alarm_handler(int signo)
1534 -{
1535 -        must_finish = 1;
1536 -}
1537 -
1538 -static void exit_handler(int signo)
1539 -{
1540 -       must_exit = 1;
1541 -       printf("\nexit requested by user through ctrl+c \n");
1542 -}
1543 -
1544 -static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
1545 -
1546 -static void value2human(double bytes, double time, double* data, double* speed,char* metric)
1547 -{
1548 -       int unit = 0;
1549 -
1550 -       *data = bytes;
1551 -       while (*data > 1024 && units[unit + 1]) {
1552 -               *data /= 1024;
1553 -               unit++;
1554 -       }
1555 -       *speed = *data / time;
1556 -       sprintf(metric, "%sB", units[unit]);
1557 -}
1558 -
1559 -static int encrypt_data(int fdc, struct test_params tp, struct session_op *sess)
1560 -{
1561 -       struct crypt_op cop;
1562 -       char *buffer, iv[32];
1563 -       char mac[HASH_MAX_LEN];
1564 -       static int val = 23;
1565 -       struct timeval start, end;
1566 -       double total = 0;
1567 -       double secs, ddata, dspeed;
1568 -       char metric[16];
1569 -       int alignmask;
1570 -       int min_alignmask = sizeof(void*) - 1;
1571 -
1572 -       memset(iv, 0x23, 32);
1573 -
1574 -       printf("\tEncrypting in chunks of %d bytes: ", tp.nvalue);
1575 -       fflush(stdout);
1576 -
1577 -       alignmask = get_alignmask(fdc, sess);
1578 -       if (alignmask) {
1579 -               alignmask = ((alignmask < min_alignmask) ? min_alignmask : alignmask);
1580 -               if (posix_memalign((void **)(&buffer), alignmask + 1, tp.nvalue)) {
1581 -                       printf("posix_memalign() failed!\n");
1582 -                       return 1;
1583 -               }
1584 -       } else {
1585 -               if (!(buffer = malloc(tp.nvalue))) {
1586 -                       perror("malloc()");
1587 -                       return 1;
1588 -               }
1589 -       }
1590 -       memset(buffer, val++, tp.nvalue);
1591 -       
1592 -       must_finish = 0;
1593 -       alarm(tp.tvalue);
1594 -
1595 -       gettimeofday(&start, NULL);
1596 -       do {
1597 -               memset(&cop, 0, sizeof(cop));
1598 -               cop.ses = sess->ses;
1599 -               cop.len = tp.nvalue;
1600 -               cop.iv = (unsigned char *)iv;
1601 -               cop.op = COP_ENCRYPT;
1602 -               cop.src = cop.dst = (unsigned char *)buffer;
1603 -               cop.mac = (unsigned char *)mac;
1604 -
1605 -               if (ioctl(fdc, CIOCCRYPT, &cop)) {
1606 -                       perror("ioctl(CIOCCRYPT)");
1607 -                       return 1;
1608 -               }
1609 -               total += cop.len;
1610 -       } while(!must_finish);
1611 -       gettimeofday(&end, NULL);
1612 -
1613 -       secs = udifftimeval(start, end)/ 1000000.0;
1614 -
1615 -       value2human(total, secs, &ddata, &dspeed, metric);
1616 -       printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
1617 -       printf ("%.2f %s/sec\n", dspeed, metric);
1618 -
1619 -       free(buffer);
1620 -       return 0;
1621 -}
1622 -
1623 -void usage(char *cmd_name)
1624 -{
1625 -       printf(usage_str, cmd_name);
1626 -}
1627 -
1628 -int run_test(int id, struct test_params tp)
1629 -{
1630 -       int fd;
1631 -       int fdc;
1632 -
1633 -       fd = open("/dev/crypto", O_RDWR, 0);
1634 -       if (fd < 0) {
1635 -               perror("open()");
1636 -               return fd;
1637 -       }
1638 -       if (ioctl(fd, CRIOGET, &fdc)) {
1639 -               perror("ioctl(CRIOGET)");
1640 -               return -EINVAL;
1641 -       }
1642 -
1643 -       ciphers[id].func(fdc, tp);
1644 -
1645 -       close(fdc);
1646 -       close(fd);
1647 -
1648 -       return 0;
1649 -}
1650 -
1651 -int get_alignmask(int fdc, struct session_op *sess)
1652 -{
1653 -       int alignmask;
1654 -
1655 -#ifdef CIOCGSESSINFO
1656 -       struct session_info_op siop;
1657 -
1658 -       siop.ses = sess->ses;
1659 -       if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
1660 -               perror("ioctl(CIOCGSESSINFO)");
1661 -               return -EINVAL;
1662 -       }
1663 -       alignmask = siop.alignmask;
1664 -#else
1665 -       alignmask = 0;
1666 -#endif
1667 -
1668 -       return alignmask;
1669 -}
1670 -
1671 -void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
1672 -{
1673 -       int i;
1674 -
1675 -       if (tp.nflag) {
1676 -               encrypt_data(fdc, tp, sess);
1677 -       } else {
1678 -               for (i = 256; i <= (64 * 1024); i *= 2) {
1679 -                       if (must_exit)
1680 -                               break;
1681 -
1682 -                       tp.nvalue = i;
1683 -                       if (encrypt_data(fdc, tp, sess)) {
1684 -                               break;
1685 -                       }
1686 -               }
1687 -       }
1688 -}
1689 -
1690 -
1691 -int run_null(int fdc, struct test_params tp)
1692 -{
1693 -       struct session_op sess;
1694 -       char keybuf[32];
1695 -
1696 -       fprintf(stderr, "Testing NULL cipher: \n");
1697 -       memset(&sess, 0, sizeof(sess));
1698 -       sess.cipher = CRYPTO_NULL;
1699 -       sess.keylen = 0;
1700 -       sess.key = (unsigned char *)keybuf;
1701 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1702 -               perror("ioctl(CIOCGSESSION)");
1703 -               return -EINVAL;
1704 -       }
1705 -
1706 -       do_test_vectors(fdc, tp, &sess);
1707 -       return 0;
1708 -}
1709 -
1710 -int run_aes_128_cbc(int fdc, struct test_params tp)
1711 -{
1712 -       struct session_op sess;
1713 -       char keybuf[32];
1714 -
1715 -       fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
1716 -       memset(&sess, 0, sizeof(sess));
1717 -       sess.cipher = CRYPTO_AES_CBC;
1718 -       sess.keylen = 16;
1719 -       memset(keybuf, 0x42, 16);
1720 -       sess.key = (unsigned char *)keybuf;
1721 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1722 -               perror("ioctl(CIOCGSESSION)");
1723 -               return -EINVAL;
1724 -       }
1725 -
1726 -       do_test_vectors(fdc, tp, &sess);
1727 -       return 0;
1728 -}
1729 -
1730 -int run_aes_256_xts(int fdc, struct test_params tp)
1731 -{
1732 -       struct session_op sess;
1733 -       char keybuf[32];
1734 -
1735 -       fprintf(stderr, "\nTesting AES-256-XTS cipher: \n");
1736 -       memset(&sess, 0, sizeof(sess));
1737 -       sess.cipher = CRYPTO_AES_XTS;
1738 -       sess.keylen = 32;
1739 -       memset(keybuf, 0x42, sess.keylen);
1740 -       sess.key = (unsigned char *)keybuf;
1741 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1742 -               perror("ioctl(CIOCGSESSION)");
1743 -               return -EINVAL;
1744 -       }
1745 -
1746 -       do_test_vectors(fdc, tp, &sess);
1747 -       return 0;
1748 -}
1749 -
1750 -int run_crc32c(int fdc, struct test_params tp)
1751 -{
1752 -       struct session_op sess;
1753 -
1754 -       fprintf(stderr, "\nTesting CRC32C hash: \n");
1755 -       memset(&sess, 0, sizeof(sess));
1756 -       sess.mac = CRYPTO_CRC32C;
1757 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1758 -               perror("ioctl(CIOCGSESSION)");
1759 -               return 1;
1760 -       }
1761 -
1762 -       do_test_vectors(fdc, tp, &sess);
1763 -       return 0;
1764 -}
1765 -
1766 -int run_sha1(int fdc, struct test_params tp)
1767 -{
1768 -       struct session_op sess;
1769 -
1770 -       fprintf(stderr, "\nTesting SHA-1 hash: \n");
1771 -       memset(&sess, 0, sizeof(sess));
1772 -       sess.mac = CRYPTO_SHA1;
1773 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1774 -               perror("ioctl(CIOCGSESSION)");
1775 -               return 1;
1776 -       }
1777 -
1778 -       do_test_vectors(fdc, tp, &sess);
1779 -       return 0;
1780 -}
1781 -
1782 -int run_sha256(int fdc, struct test_params tp)
1783 -{
1784 -       struct session_op sess;
1785 -
1786 -       fprintf(stderr, "\nTesting SHA2-256 hash: \n");
1787 -       memset(&sess, 0, sizeof(sess));
1788 -       sess.mac = CRYPTO_SHA2_256;
1789 -       if (ioctl(fdc, CIOCGSESSION, &sess)) {
1790 -               perror("ioctl(CIOCGSESSION)");
1791 -               return 1;
1792 -       }
1793 -
1794 -       do_test_vectors(fdc, tp, &sess);
1795 -       return 0;
1796 -}
1797 -
1798 -int main(int argc, char **argv)
1799 -{
1800 -       int i;
1801 -       int c;
1802 -       bool alg_flag;
1803 -       char *alg_name;
1804 -       struct test_params tp;
1805 -
1806 -       tp.tflag = false;
1807 -       tp.nflag = false;
1808 -       alg_flag = false;
1809 -       opterr = 0;
1810 -       while ((c = getopt(argc, argv, "hn:t:")) != -1) {
1811 -               switch (c) {
1812 -               case 'n':
1813 -                       tp.nvalue = atoi(optarg);
1814 -                       tp.nflag = true;
1815 -                       break;
1816 -               case 't':
1817 -                       tp.tvalue = atoi(optarg);
1818 -                       tp.tflag = true;
1819 -                       break;
1820 -               case 'h': /* no break */
1821 -               default:
1822 -                       usage(argv[0]);
1823 -                       exit(1);
1824 -               }
1825 -       }
1826 -
1827 -       /* the name of a specific test asked on the command line */
1828 -       if (optind < argc) {
1829 -               alg_name = argv[optind];
1830 -               alg_flag = true;
1831 -       }
1832 -
1833 -       /* default test time */
1834 -       if (!tp.tflag) {
1835 -               tp.tvalue = 5;
1836 -       }
1837 -
1838 -       signal(SIGALRM, alarm_handler);
1839 -       signal(SIGINT, exit_handler);
1840 -
1841 -       for (i = 0; i < ALG_COUNT; i++) {
1842 -               if (must_exit)
1843 -                       break;
1844 -
1845 -               if (alg_flag) {
1846 -                       if (strcmp(alg_name, ciphers[i].name) == 0) {
1847 -                               run_test(i, tp);
1848 -                       }
1849 -               } else {
1850 -                       run_test(i, tp);
1851 -               }
1852 -       }
1853 -
1854 -       return 0;
1855 -}
1856 -- 
1857 2.10.2
1858