From 500ae49da154b0054998f283d907712936a5393a Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@gnutls.org>
Date: Sun, 4 Jul 2010 10:46:37 +0200
Subject: [PATCH 069/291] cryptodev: Benchmark time to have rational waiting
 time in slow engines.

---
 drivers/staging/crypto/cryptodev/examples/speed.c | 105 +++++++++++++++-------
 1 file changed, 72 insertions(+), 33 deletions(-)

diff --git a/drivers/staging/crypto/cryptodev/examples/speed.c b/drivers/staging/crypto/cryptodev/examples/speed.c
index 77d6d29f035e..7757070d5f8d 100644
--- a/drivers/staging/crypto/cryptodev/examples/speed.c
+++ b/drivers/staging/crypto/cryptodev/examples/speed.c
@@ -23,50 +23,71 @@
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <signal.h>
 #include "../cryptodev.h"
 
-#if 1
-#define TEST_ALGO	CRYPTO_AES_CBC
-#define TEST_KEYLEN	32
-#else
-#define TEST_ALGO	CRYPTO_NULL
-#define TEST_KEYLEN	0
-#endif
+static double udifftimeval(struct timeval start, struct timeval end)
+{
+	return (double)(end.tv_usec - start.tv_usec) +
+	       (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
+}
+
+static int must_finish = 0;
 
-/* transcode a total of 1GB */
-#define TOT_LEN (1024 * 1024 * 1024)
+static void alarm_handler(int signo)
+{
+        must_finish = 1;
+}
 
-static long udifftimeval(struct timeval start, struct timeval end)
+static void value2human(double bytes, double time, double* data, double* speed,char* metric)
 {
-	return (end.tv_usec - start.tv_usec) +
-	       (end.tv_sec - start.tv_sec) * 1000 * 1000 + 0.5;
+        if (bytes > 1000 && bytes < 1000*1000) {
+                *data = ((double)bytes)/1000;
+                *speed = *data/time;
+                strcpy(metric, "Kb");
+                return;
+        } else if (bytes >= 1000*1000 && bytes < 1000*1000*1000) {
+                *data = ((double)bytes)/(1000*1000);
+                *speed = *data/time;
+                strcpy(metric, "Mb");
+                return;
+        } else if (bytes >= 1000*1000*1000) {
+                *data = ((double)bytes)/(1000*1000*1000);
+                *speed = *data/time;
+                strcpy(metric, "Gb");
+                return;
+        } else {
+                *data = (double)bytes;
+                *speed = *data/time;
+                strcpy(metric, "bytes");
+                return;
+        }
 }
 
-int encrypt_data(struct session_op *sess, int fdc, int len, int chunksize, int flags)
+
+int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
 {
 	struct crypt_op cop;
 	char *buffer, iv[32];
-	int i, buffercount;
 	static int val = 23;
 	struct timeval start, end;
-
-	if (len % chunksize) {
-		printf("got milk?!\n");
-		return 1;
-	}
-
-	buffercount = len / chunksize;
+	double total = 0;
+	double secs, ddata, dspeed;
+	char metric[16];
 
 	buffer = malloc(chunksize);
 	memset(iv, 0x23, 32);
 
-	printf("Encrypting %d x %d bytes: ", buffercount, chunksize);
+	printf("\tEncrypting in chunks of %d bytes: ", chunksize);
 	fflush(stdout);
 
 	memset(buffer, val++, chunksize);
 
+	must_finish = 0;
+	alarm(5);
+
 	gettimeofday(&start, NULL);
-	for (i = 0; i < buffercount; i++) {
+	do {
 		memset(&cop, 0, sizeof(cop));
 		cop.ses = sess->ses;
 		cop.len = chunksize;
@@ -79,11 +100,16 @@ int encrypt_data(struct session_op *sess, int fdc, int len, int chunksize, int f
 			perror("ioctl(CIOCCRYPT)");
 			return 1;
 		}
-	}
+		total+=chunksize;
+	} while(must_finish==0);
 	gettimeofday(&end, NULL);
 
-	printf("done in %.5f seconds - %.5fMB/s\n", (double)udifftimeval(start, end) / 1000000.0,
-			(double)len / ((double)udifftimeval(start, end) / 1000000.0) / 1024 / 1024);
+	secs = udifftimeval(start, end)/ 1000000.0;
+	
+	value2human(total, secs, &ddata, &dspeed, metric);
+	printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
+	printf ("%.2f %s/sec\n", dspeed, metric);
+
 	return 0;
 }
 
@@ -93,6 +119,8 @@ int main(void)
 	struct session_op sess;
 	char keybuf[32];
 
+	signal(SIGALRM, alarm_handler);
+
 	if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
 		perror("open()");
 		return 1;
@@ -101,24 +129,35 @@ int main(void)
 		perror("ioctl(CRIOGET)");
 		return 1;
 	}
+	
+	fprintf(stderr, "Testing NULL cipher: \n");
 	memset(&sess, 0, sizeof(sess));
-	sess.cipher = TEST_ALGO;
-	sess.keylen = TEST_KEYLEN;
-	memset(keybuf, 0x42, 32);
+	sess.cipher = CRYPTO_NULL;
+	sess.keylen = 0;
 	sess.key = (unsigned char *)keybuf;
 	if (ioctl(fdc, CIOCGSESSION, &sess)) {
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
 
-	printf("Standard operation:\n");
 	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, TOT_LEN, i, 0))
+		if (encrypt_data(&sess, fdc, i, 0))
 			break;
 	}
-	printf("Zero-Copy operation:\n");
+
+	fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
+	memset(&sess, 0, sizeof(sess));
+	sess.cipher = CRYPTO_AES_CBC;
+	sess.keylen = 16;
+	memset(keybuf, 0x42, 16);
+	sess.key = (unsigned char *)keybuf;
+	if (ioctl(fdc, CIOCGSESSION, &sess)) {
+		perror("ioctl(CIOCGSESSION)");
+		return 1;
+	}
+
 	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, TOT_LEN, i, COP_FLAG_ZCOPY))
+		if (encrypt_data(&sess, fdc, i, 0))
 			break;
 	}
 
-- 
2.1.0

