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
6 Signed-off-by: Cristian Stoica <cristian.stoica@nxp.com>
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
22 diff --git a/tests/Makefile b/tests/Makefile
23 index 51469e8..88f5040 100644
26 @@ -3,19 +3,18 @@ CFLAGS += -I.. $(CRYPTODEV_CFLAGS)
28 comp_progs := cipher_comp hash_comp hmac_comp
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)
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
49 @@ -38,7 +37,7 @@ install:
50 for prog in $(hostprogs); do \
51 install -m 755 $$prog $(DESTDIR)/$(bindir)/tests_cryptodev/; \
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)
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
64 -/* cryptodev_test - simple benchmark tool for cryptodev
66 - * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
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.
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.
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
88 -#include <sys/ioctl.h>
89 -#include <sys/time.h>
90 -#include <sys/types.h>
92 -#include <crypto/cryptodev.h>
96 -#include <inttypes.h>
100 -struct test_params {
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"
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);
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},
137 -static double udifftimeval(struct timeval start, struct timeval end)
139 - return (double)(end.tv_usec - start.tv_usec) +
140 - (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
143 -static volatile int must_finish;
144 -static volatile int must_exit;
145 -static struct pollfd pfd;
147 -static void alarm_handler(int signo)
150 - pfd.events = POLLIN;
153 -static void exit_handler(int signo)
156 - printf("\nexit requested by user through ctrl+c \n");
159 -static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
161 -static void value2human(uint64_t bytes, double time, double* data, double* speed,char* metric)
166 - while (*data > 1024 && units[unit + 1]) {
170 - *speed = *data / time;
171 - sprintf(metric, "%sB", units[unit]);
174 -static void value2machine(uint64_t bytes, double time, double* speed)
176 - *speed = bytes / time;
179 -int get_alignmask(int fdc, struct session_op *sess)
182 - int min_alignmask = sizeof(void*) - 1;
184 -#ifdef CIOCGSESSINFO
185 - struct session_info_op siop;
187 - siop.ses = sess->ses;
188 - if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
189 - perror("ioctl(CIOCGSESSINFO)");
192 - alignmask = siop.alignmask;
193 - if (alignmask < min_alignmask) {
194 - alignmask = min_alignmask;
203 -int encrypt_data(int fdc, struct test_params tp, struct session_op *sess)
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;
213 - int rc, wqueue = 0, bufidx = 0;
216 - memset(iv, 0x23, 32);
219 - printf("\tBuffer size %d bytes: ", tp.nvalue);
223 - alignmask = get_alignmask(fdc, sess);
224 - for (rc = 0; rc < 64; rc++) {
226 - if (posix_memalign((void **)(buffer + rc), alignmask + 1, tp.nvalue)) {
227 - printf("posix_memalign() failed!\n");
231 - if (!(buffer[rc] = malloc(tp.nvalue))) {
232 - perror("malloc()");
236 - memset(buffer[rc], val++, tp.nvalue);
239 - pfd.events = POLLOUT | POLLIN;
244 - gettimeofday(&start, NULL);
246 - if ((rc = poll(&pfd, 1, 100)) < 0) {
247 - if (errno & (ERESTART | EINTR))
249 - fprintf(stderr, "errno = %d ", errno);
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;
264 - if (ioctl(fdc, CIOCASYNCCRYPT, &cop)) {
265 - perror("ioctl(CIOCASYNCCRYPT)");
270 - if (pfd.revents & POLLIN) {
271 - if (ioctl(fdc, CIOCASYNCFETCH, &cop)) {
272 - perror("ioctl(CIOCASYNCFETCH)");
278 - } while(!must_finish || wqueue);
279 - gettimeofday(&end, NULL);
281 - secs = udifftimeval(start, end)/ 1000000.0;
284 - value2machine(total, secs, &dspeed);
285 - printf("%" PRIu64 "\t%.2f\t%.2f\n", total, secs, dspeed);
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);
292 - for (rc = 0; rc < 64; rc++)
297 -void usage(char *cmd_name)
299 - printf(usage_str, cmd_name);
302 -int run_test(int id, struct test_params tp)
308 - fd = open("/dev/crypto", O_RDWR, 0);
313 - if (ioctl(fd, CRIOGET, &fdc)) {
314 - perror("ioctl(CRIOGET)");
319 - fprintf(stderr, "Testing %s:\n", ciphers[id].name);
321 - err = ciphers[id].func(fdc, tp);
329 -void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
334 - encrypt_data(fdc, tp, sess);
336 - for (i = 256; i <= (64 * 1024); i *= 2) {
342 - if (encrypt_data(fdc, tp, sess)) {
350 -int run_null(int fdc, struct test_params tp)
352 - struct session_op sess;
355 - fprintf(stderr, "Testing NULL cipher: \n");
356 - memset(&sess, 0, sizeof(sess));
357 - sess.cipher = CRYPTO_NULL;
359 - sess.key = (unsigned char *)keybuf;
360 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
361 - perror("ioctl(CIOCGSESSION)");
365 - do_test_vectors(fdc, tp, &sess);
369 -int run_aes_128_cbc(int fdc, struct test_params tp)
371 - struct session_op sess;
374 - memset(&sess, 0, sizeof(sess));
375 - sess.cipher = CRYPTO_AES_CBC;
377 - memset(keybuf, 0x42, 16);
378 - sess.key = (unsigned char *)keybuf;
379 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
380 - perror("ioctl(CIOCGSESSION)");
384 - do_test_vectors(fdc, tp, &sess);
388 -int run_aes_256_xts(int fdc, struct test_params tp)
390 - struct session_op sess;
393 - memset(&sess, 0, sizeof(sess));
394 - sess.cipher = CRYPTO_AES_XTS;
396 - memset(keybuf, 0x42, sess.keylen);
397 - sess.key = (unsigned char *)keybuf;
398 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
399 - perror("ioctl(CIOCGSESSION)");
403 - do_test_vectors(fdc, tp, &sess);
407 -int run_crc32c(int fdc, struct test_params tp)
409 - struct session_op sess;
411 - memset(&sess, 0, sizeof(sess));
412 - sess.mac = CRYPTO_CRC32C;
413 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
414 - perror("ioctl(CIOCGSESSION)");
418 - do_test_vectors(fdc, tp, &sess);
422 -int run_sha1(int fdc, struct test_params tp)
424 - struct session_op sess;
426 - memset(&sess, 0, sizeof(sess));
427 - sess.mac = CRYPTO_SHA1;
428 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
429 - perror("ioctl(CIOCGSESSION)");
433 - do_test_vectors(fdc, tp, &sess);
437 -int run_sha256(int fdc, struct test_params tp)
439 - struct session_op sess;
441 - memset(&sess, 0, sizeof(sess));
442 - sess.mac = CRYPTO_SHA2_256;
443 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
444 - perror("ioctl(CIOCGSESSION)");
448 - do_test_vectors(fdc, tp, &sess);
452 -int main(int argc, char **argv)
459 - struct test_params tp;
466 - while ((c = getopt(argc, argv, "hn:t:m")) != -1) {
469 - tp.nvalue = atoi(optarg);
473 - tp.tvalue = atoi(optarg);
479 - case 'h': /* no break */
486 - /* the name of a specific test asked on the command line */
487 - if (optind < argc) {
488 - alg_name = argv[optind];
492 - /* default test time */
497 - signal(SIGALRM, alarm_handler);
498 - signal(SIGINT, exit_handler);
500 - for (i = 0; i < ALG_COUNT; i++) {
506 - if (strcmp(alg_name, ciphers[i].name) == 0) {
507 - err = run_test(i, tp);
510 - err = run_test(i, tp);
522 -main(int argc, char** argv)
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
535 -# Copyright 2016 NXP Semiconductors
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.
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.
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/>.
552 -# no user-configurable options below this line
555 -CMD_BIN="async_speed"
556 -OUT_BASENAME="async_speed"
557 -MPSTAT_OUT="mpstat_out"
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
566 -Usage: `basename $0` [OPTIONS] <alg_name>
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)
574 -alg_name: null, aes-128-cbc, aes-256-xts, sha1, sha256, crc32c
576 -Note: SEC driver is configured to support buffers smaller than 512K
581 - paste -sd+ - | bc -l
584 -function get_cpu_idle
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)
593 -function run_parallel
595 - trap control_c SIGINT
597 - OPTIONS="-t $tvalue -n $nvalue -m"
598 - CMD="$CMD_BIN $OPTIONS $alg_name"
600 - (sleep 1; S_TIME_FORMAT=ISO mpstat 1 $(($tvalue-2))) &> $MPSTAT_OUT &
604 - start=$(date +%s.%N)
606 - for i in $(seq 0 $(($mvalue-1)))
608 - CMD_OUT="${OUT_BASENAME}_${i}"
612 - AFFINITY=$(($i % $NUM_CORES))
613 - taskset -pc $AFFINITY $PID > /dev/null
623 - grep "ioctl" ${OUT_BASENAME}_* &> /dev/null
626 - echo "cryptodev is not built with -DENABLE_ASYNC flag"
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)
635 - if [ ! -z "$vvalue" ]
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 %"
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}%"
653 - killall async_speed > /dev/null
654 - killall mpstat > /dev/null
659 - [ ! -e "/dev/crypto" ] &&
660 - (sudo modprobe cryptodev || modprobe cryptodev || exit 1)
662 - $(which ${CMD_BIN} &> /dev/null)
665 - echo "${CMD_BIN} test is not installed"
669 - rm -f ${OUT_BASENAME}_*
670 - rm -f ${MPSTAT_OUT}
672 - while getopts vhm:t:n: option
675 - m) mvalue="$OPTARG";;
676 - t) tvalue="$OPTARG";;
677 - n) nvalue="$OPTARG";;
678 - v) vvalue="verbose";;
679 - *) usage $0; exit 1;;
683 - shift $((OPTIND-1))
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
690 - [ "$tvalue" -lt 5 ] && tvalue=5
692 - case "$alg_name" in
698 - "crc32c" ) run_parallel;;
699 - * ) usage && exit 1;;
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
711 for bsize in ${BUF_SIZE}
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} |
718 diff --git a/tests/speed.c b/tests/speed.c
720 index 0000000..3b36db1
724 +/* cryptodev_test - simple benchmark tool for cryptodev
726 + * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
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.
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.
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
748 +#include <sys/ioctl.h>
749 +#include <sys/time.h>
750 +#include <sys/types.h>
752 +#include <crypto/cryptodev.h>
753 +#include <stdbool.h>
756 +#include <inttypes.h>
759 +struct test_params {
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"
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);
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},
798 +static double udifftimeval(struct timeval start, struct timeval end)
800 + return (double)(end.tv_usec - start.tv_usec) +
801 + (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
804 +static volatile int must_finish;
805 +static volatile int must_exit;
806 +static struct pollfd pfd;
808 +static void alarm_handler(int signo)
811 + pfd.events = POLLIN;
814 +static void exit_handler(int signo)
817 + printf("\nexit requested by user through ctrl+c \n");
820 +static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
822 +static void value2human(uint64_t bytes, double time, double* data, double* speed,char* metric)
827 + while (*data > 1024 && units[unit + 1]) {
831 + *speed = *data / time;
832 + sprintf(metric, "%sB", units[unit]);
835 +static void value2machine(uint64_t bytes, double time, double* speed)
837 + *speed = bytes / time;
840 +int get_alignmask(int fdc, struct session_op *sess)
843 + int min_alignmask = sizeof(void*) - 1;
845 +#ifdef CIOCGSESSINFO
846 + struct session_info_op siop;
848 + siop.ses = sess->ses;
849 + if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
850 + perror("ioctl(CIOCGSESSINFO)");
853 + alignmask = siop.alignmask;
854 + if (alignmask < min_alignmask) {
855 + alignmask = min_alignmask;
864 +int encrypt_async(int fdc, struct test_params tp, struct session_op *sess)
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;
874 + int rc, wqueue = 0, bufidx = 0;
877 + memset(iv, 0x23, 32);
880 + printf("\tBuffer size %d bytes: ", tp.nvalue);
884 + alignmask = get_alignmask(fdc, sess);
885 + for (rc = 0; rc < 64; rc++) {
887 + if (posix_memalign((void **)(buffer + rc), alignmask + 1, tp.nvalue)) {
888 + printf("posix_memalign() failed!\n");
892 + if (!(buffer[rc] = malloc(tp.nvalue))) {
893 + perror("malloc()");
897 + memset(buffer[rc], val++, tp.nvalue);
900 + pfd.events = POLLOUT | POLLIN;
905 + gettimeofday(&start, NULL);
907 + if ((rc = poll(&pfd, 1, 100)) < 0) {
908 + if (errno & (ERESTART | EINTR))
910 + fprintf(stderr, "errno = %d ", errno);
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;
925 + if (ioctl(fdc, CIOCASYNCCRYPT, &cop)) {
926 + perror("ioctl(CIOCASYNCCRYPT)");
931 + if (pfd.revents & POLLIN) {
932 + if (ioctl(fdc, CIOCASYNCFETCH, &cop)) {
933 + perror("ioctl(CIOCASYNCFETCH)");
939 + } while(!must_finish || wqueue);
940 + gettimeofday(&end, NULL);
942 + secs = udifftimeval(start, end)/ 1000000.0;
945 + value2machine(total, secs, &dspeed);
946 + printf("%" PRIu64 "\t%.2f\t%.2f\n", total, secs, dspeed);
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);
953 + for (rc = 0; rc < 64; rc++)
959 +static int encrypt_sync(int fdc, struct test_params tp, struct session_op *sess)
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;
970 + int min_alignmask = sizeof(void*) - 1;
972 + memset(iv, 0x23, 32);
975 + printf("\tBuffer size %d bytes: ", tp.nvalue);
979 + alignmask = get_alignmask(fdc, sess);
981 + alignmask = ((alignmask < min_alignmask) ? min_alignmask : alignmask);
982 + if (posix_memalign((void **)(&buffer), alignmask + 1, tp.nvalue)) {
983 + printf("posix_memalign() failed!\n");
987 + if (!(buffer = malloc(tp.nvalue))) {
988 + perror("malloc()");
992 + memset(buffer, val++, tp.nvalue);
997 + gettimeofday(&start, NULL);
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;
1007 + if (ioctl(fdc, CIOCCRYPT, &cop)) {
1008 + perror("ioctl(CIOCCRYPT)");
1012 + } while(!must_finish);
1013 + gettimeofday(&end, NULL);
1015 + secs = udifftimeval(start, end)/ 1000000.0;
1018 + value2machine(total, secs, &dspeed);
1019 + printf("%" PRIu64 "\t%.2f\t%.2f\n", total, secs, dspeed);
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);
1030 +void usage(char *cmd_name)
1032 + printf(usage_str, cmd_name);
1035 +int run_test(int id, struct test_params tp)
1041 + fd = open("/dev/crypto", O_RDWR, 0);
1046 + if (ioctl(fd, CRIOGET, &fdc)) {
1047 + perror("ioctl(CRIOGET)");
1053 + type = tp.aflag ? "async" : "sync";
1055 + fprintf(stderr, "Testing %s %s:\n", type, ciphers[id].name);
1057 + err = ciphers[id].func(fdc, tp);
1065 +void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
1072 + encrypt_async(fdc, tp, sess);
1074 + encrypt_sync(fdc, tp, sess);
1077 + for (i = 256; i <= (64 * 1024); i *= 2) {
1084 + err = encrypt_async(fdc, tp, sess);
1086 + err = encrypt_sync(fdc, tp, sess);
1097 +int run_null(int fdc, struct test_params tp)
1099 + struct session_op sess;
1102 + fprintf(stderr, "Testing NULL cipher: \n");
1103 + memset(&sess, 0, sizeof(sess));
1104 + sess.cipher = CRYPTO_NULL;
1106 + sess.key = (unsigned char *)keybuf;
1107 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
1108 + perror("ioctl(CIOCGSESSION)");
1112 + do_test_vectors(fdc, tp, &sess);
1116 +int run_aes_128_cbc(int fdc, struct test_params tp)
1118 + struct session_op sess;
1121 + memset(&sess, 0, sizeof(sess));
1122 + sess.cipher = CRYPTO_AES_CBC;
1124 + memset(keybuf, 0x42, 16);
1125 + sess.key = (unsigned char *)keybuf;
1126 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
1127 + perror("ioctl(CIOCGSESSION)");
1131 + do_test_vectors(fdc, tp, &sess);
1135 +int run_aes_256_xts(int fdc, struct test_params tp)
1137 + struct session_op sess;
1140 + memset(&sess, 0, sizeof(sess));
1141 + sess.cipher = CRYPTO_AES_XTS;
1143 + memset(keybuf, 0x42, sess.keylen);
1144 + sess.key = (unsigned char *)keybuf;
1145 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
1146 + perror("ioctl(CIOCGSESSION)");
1150 + do_test_vectors(fdc, tp, &sess);
1154 +int run_crc32c(int fdc, struct test_params tp)
1156 + struct session_op sess;
1158 + memset(&sess, 0, sizeof(sess));
1159 + sess.mac = CRYPTO_CRC32C;
1160 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
1161 + perror("ioctl(CIOCGSESSION)");
1165 + do_test_vectors(fdc, tp, &sess);
1169 +int run_sha1(int fdc, struct test_params tp)
1171 + struct session_op sess;
1173 + memset(&sess, 0, sizeof(sess));
1174 + sess.mac = CRYPTO_SHA1;
1175 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
1176 + perror("ioctl(CIOCGSESSION)");
1180 + do_test_vectors(fdc, tp, &sess);
1184 +int run_sha256(int fdc, struct test_params tp)
1186 + struct session_op sess;
1188 + memset(&sess, 0, sizeof(sess));
1189 + sess.mac = CRYPTO_SHA2_256;
1190 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
1191 + perror("ioctl(CIOCGSESSION)");
1195 + do_test_vectors(fdc, tp, &sess);
1199 +int main(int argc, char **argv)
1206 + struct test_params tp;
1214 + while ((c = getopt(argc, argv, "ahn:t:m")) != -1) {
1217 + tp.nvalue = atoi(optarg);
1221 + tp.tvalue = atoi(optarg);
1230 + case 'h': /* no break */
1237 + /* the name of a specific test asked on the command line */
1238 + if (optind < argc) {
1239 + alg_name = argv[optind];
1243 + /* default test time */
1248 + signal(SIGALRM, alarm_handler);
1249 + signal(SIGINT, exit_handler);
1251 + for (i = 0; i < ALG_COUNT; i++) {
1257 + if (strcmp(alg_name, ciphers[i].name) == 0) {
1258 + err = run_test(i, tp);
1261 + err = run_test(i, tp);
1270 diff --git a/tests/speed_multi.sh b/tests/speed_multi.sh
1271 new file mode 100755
1272 index 0000000..b116483
1274 +++ b/tests/speed_multi.sh
1278 +# Copyright 2016 NXP Semiconductors
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.
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.
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/>.
1295 +# no user-configurable options below this line
1299 +OUT_BASENAME="speed"
1300 +MPSTAT_OUT="mpstat_out"
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
1309 +Usage: `basename $0` [OPTIONS] <alg_name>
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)
1318 +alg_name: null, aes-128-cbc, aes-256-xts, sha1, sha256, crc32c
1320 +Note: SEC driver is configured to support buffers smaller than 512K
1325 + paste -sd+ - | bc -l
1328 +function get_cpu_idle
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)
1334 + echo $average_idle
1337 +function run_parallel
1339 + trap control_c SIGINT
1341 + OPTIONS="-t $tvalue -n $nvalue -m $aflag"
1342 + CMD="$CMD_BIN $OPTIONS $alg_name"
1344 + (sleep 1; S_TIME_FORMAT=ISO mpstat 1 $(($tvalue-2))) &> $MPSTAT_OUT &
1348 + start=$(date +%s.%N)
1350 + for i in $(seq 0 $(($mvalue-1)))
1352 + CMD_OUT="${OUT_BASENAME}_${i}"
1354 + $CMD &> $CMD_OUT &
1356 + AFFINITY=$(($i % $NUM_CORES))
1357 + taskset -pc $AFFINITY $PID > /dev/null
1363 + end=$(date +%s.%N)
1367 + grep "ioctl" ${OUT_BASENAME}_* &> /dev/null
1370 + echo "cryptodev is not built with -DENABLE_ASYNC flag"
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)
1379 + if [ ! -z "$vflag" ]
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 %"
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}%"
1397 + killall $CMD_BIN > /dev/null
1398 + killall mpstat > /dev/null
1403 + [ ! -e "/dev/crypto" ] &&
1404 + (sudo modprobe cryptodev || modprobe cryptodev || exit 1)
1406 + $(which ${CMD_BIN} &> /dev/null)
1409 + echo "${CMD_BIN} test is not installed"
1413 + rm -f ${OUT_BASENAME}_*
1414 + rm -f ${MPSTAT_OUT}
1416 + while getopts avhm:t:n: option
1419 + m) mvalue="$OPTARG";;
1420 + t) tvalue="$OPTARG";;
1421 + n) nvalue="$OPTARG";;
1422 + v) vflag="verbose";;
1424 + *) usage $0; exit 1;;
1428 + shift $((OPTIND-1))
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
1435 + [ "$tvalue" -lt 5 ] && tvalue=5
1437 + case "$alg_name" in
1443 + "crc32c" ) run_parallel;;
1444 + * ) usage && exit 1;;
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
1456 -/* cryptodev_test - simple benchmark tool for cryptodev
1458 - * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
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.
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.
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
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>
1488 -struct test_params {
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"
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);
1511 -#define ALG_COUNT 6
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},
1524 -static double udifftimeval(struct timeval start, struct timeval end)
1526 - return (double)(end.tv_usec - start.tv_usec) +
1527 - (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
1530 -static volatile int must_finish;
1531 -static volatile int must_exit;
1533 -static void alarm_handler(int signo)
1538 -static void exit_handler(int signo)
1541 - printf("\nexit requested by user through ctrl+c \n");
1544 -static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
1546 -static void value2human(double bytes, double time, double* data, double* speed,char* metric)
1551 - while (*data > 1024 && units[unit + 1]) {
1555 - *speed = *data / time;
1556 - sprintf(metric, "%sB", units[unit]);
1559 -static int encrypt_data(int fdc, struct test_params tp, struct session_op *sess)
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;
1567 - double secs, ddata, dspeed;
1570 - int min_alignmask = sizeof(void*) - 1;
1572 - memset(iv, 0x23, 32);
1574 - printf("\tEncrypting in chunks of %d bytes: ", tp.nvalue);
1577 - alignmask = get_alignmask(fdc, sess);
1579 - alignmask = ((alignmask < min_alignmask) ? min_alignmask : alignmask);
1580 - if (posix_memalign((void **)(&buffer), alignmask + 1, tp.nvalue)) {
1581 - printf("posix_memalign() failed!\n");
1585 - if (!(buffer = malloc(tp.nvalue))) {
1586 - perror("malloc()");
1590 - memset(buffer, val++, tp.nvalue);
1595 - gettimeofday(&start, NULL);
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;
1605 - if (ioctl(fdc, CIOCCRYPT, &cop)) {
1606 - perror("ioctl(CIOCCRYPT)");
1610 - } while(!must_finish);
1611 - gettimeofday(&end, NULL);
1613 - secs = udifftimeval(start, end)/ 1000000.0;
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);
1623 -void usage(char *cmd_name)
1625 - printf(usage_str, cmd_name);
1628 -int run_test(int id, struct test_params tp)
1633 - fd = open("/dev/crypto", O_RDWR, 0);
1638 - if (ioctl(fd, CRIOGET, &fdc)) {
1639 - perror("ioctl(CRIOGET)");
1643 - ciphers[id].func(fdc, tp);
1651 -int get_alignmask(int fdc, struct session_op *sess)
1655 -#ifdef CIOCGSESSINFO
1656 - struct session_info_op siop;
1658 - siop.ses = sess->ses;
1659 - if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
1660 - perror("ioctl(CIOCGSESSINFO)");
1663 - alignmask = siop.alignmask;
1671 -void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
1676 - encrypt_data(fdc, tp, sess);
1678 - for (i = 256; i <= (64 * 1024); i *= 2) {
1683 - if (encrypt_data(fdc, tp, sess)) {
1691 -int run_null(int fdc, struct test_params tp)
1693 - struct session_op sess;
1696 - fprintf(stderr, "Testing NULL cipher: \n");
1697 - memset(&sess, 0, sizeof(sess));
1698 - sess.cipher = CRYPTO_NULL;
1700 - sess.key = (unsigned char *)keybuf;
1701 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
1702 - perror("ioctl(CIOCGSESSION)");
1706 - do_test_vectors(fdc, tp, &sess);
1710 -int run_aes_128_cbc(int fdc, struct test_params tp)
1712 - struct session_op sess;
1715 - fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
1716 - memset(&sess, 0, sizeof(sess));
1717 - sess.cipher = CRYPTO_AES_CBC;
1719 - memset(keybuf, 0x42, 16);
1720 - sess.key = (unsigned char *)keybuf;
1721 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
1722 - perror("ioctl(CIOCGSESSION)");
1726 - do_test_vectors(fdc, tp, &sess);
1730 -int run_aes_256_xts(int fdc, struct test_params tp)
1732 - struct session_op sess;
1735 - fprintf(stderr, "\nTesting AES-256-XTS cipher: \n");
1736 - memset(&sess, 0, sizeof(sess));
1737 - sess.cipher = CRYPTO_AES_XTS;
1739 - memset(keybuf, 0x42, sess.keylen);
1740 - sess.key = (unsigned char *)keybuf;
1741 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
1742 - perror("ioctl(CIOCGSESSION)");
1746 - do_test_vectors(fdc, tp, &sess);
1750 -int run_crc32c(int fdc, struct test_params tp)
1752 - struct session_op sess;
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)");
1762 - do_test_vectors(fdc, tp, &sess);
1766 -int run_sha1(int fdc, struct test_params tp)
1768 - struct session_op sess;
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)");
1778 - do_test_vectors(fdc, tp, &sess);
1782 -int run_sha256(int fdc, struct test_params tp)
1784 - struct session_op sess;
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)");
1794 - do_test_vectors(fdc, tp, &sess);
1798 -int main(int argc, char **argv)
1804 - struct test_params tp;
1810 - while ((c = getopt(argc, argv, "hn:t:")) != -1) {
1813 - tp.nvalue = atoi(optarg);
1817 - tp.tvalue = atoi(optarg);
1820 - case 'h': /* no break */
1827 - /* the name of a specific test asked on the command line */
1828 - if (optind < argc) {
1829 - alg_name = argv[optind];
1833 - /* default test time */
1838 - signal(SIGALRM, alarm_handler);
1839 - signal(SIGINT, exit_handler);
1841 - for (i = 0; i < ALG_COUNT; i++) {
1846 - if (strcmp(alg_name, ciphers[i].name) == 0) {