diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2005-06-22 16:29:03 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2005-06-22 16:29:03 -0400 |
commit | 6a17944ca12229036a6d8d48be1b5eb51204fcf8 (patch) | |
tree | f3728d31d288746b85cd50883648a078c42b1850 /crypto | |
parent | dce907c00ff246a1fbb2b619964753ebc046591d (diff) |
[CRYPTO]: Use CPU cycle counters in tcrypt
After using this facility for a while to test my changes to the
cipher crypt() layer, I realised that I should've listend to Dave
and made this thing use CPU cycle counters :) As it is it's too
jittery for me to feel safe about relying on the results.
So here is a patch to make it use CPU cycles by default but fall
back to jiffies if the user specifies a non-zero sec value.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/tcrypt.c | 116 |
1 files changed, 95 insertions, 21 deletions
diff --git a/crypto/tcrypt.c b/crypto/tcrypt.c index 401d25ac214f..bd7524cfff33 100644 --- a/crypto/tcrypt.c +++ b/crypto/tcrypt.c | |||
@@ -27,6 +27,8 @@ | |||
27 | #include <linux/highmem.h> | 27 | #include <linux/highmem.h> |
28 | #include <linux/moduleparam.h> | 28 | #include <linux/moduleparam.h> |
29 | #include <linux/jiffies.h> | 29 | #include <linux/jiffies.h> |
30 | #include <linux/timex.h> | ||
31 | #include <linux/interrupt.h> | ||
30 | #include "tcrypt.h" | 32 | #include "tcrypt.h" |
31 | 33 | ||
32 | /* | 34 | /* |
@@ -60,7 +62,7 @@ static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 }; | |||
60 | /* | 62 | /* |
61 | * Used by test_cipher_speed() | 63 | * Used by test_cipher_speed() |
62 | */ | 64 | */ |
63 | static unsigned int sec = 10; | 65 | static unsigned int sec; |
64 | 66 | ||
65 | static int mode; | 67 | static int mode; |
66 | static char *xbuf; | 68 | static char *xbuf; |
@@ -426,6 +428,88 @@ out: | |||
426 | crypto_free_tfm(tfm); | 428 | crypto_free_tfm(tfm); |
427 | } | 429 | } |
428 | 430 | ||
431 | static int test_cipher_jiffies(struct crypto_tfm *tfm, int enc, char *p, | ||
432 | int blen, int sec) | ||
433 | { | ||
434 | struct scatterlist sg[8]; | ||
435 | unsigned long start, end; | ||
436 | int bcount; | ||
437 | int ret; | ||
438 | |||
439 | sg[0].page = virt_to_page(p); | ||
440 | sg[0].offset = offset_in_page(p); | ||
441 | sg[0].length = blen; | ||
442 | |||
443 | for (start = jiffies, end = start + sec * HZ, bcount = 0; | ||
444 | time_before(jiffies, end); bcount++) { | ||
445 | if (enc) | ||
446 | ret = crypto_cipher_encrypt(tfm, sg, sg, blen); | ||
447 | else | ||
448 | ret = crypto_cipher_decrypt(tfm, sg, sg, blen); | ||
449 | |||
450 | if (ret) | ||
451 | return ret; | ||
452 | } | ||
453 | |||
454 | printk("%d operations in %d seconds (%ld bytes)\n", | ||
455 | bcount, sec, (long)bcount * blen); | ||
456 | return 0; | ||
457 | } | ||
458 | |||
459 | static int test_cipher_cycles(struct crypto_tfm *tfm, int enc, char *p, | ||
460 | int blen) | ||
461 | { | ||
462 | struct scatterlist sg[8]; | ||
463 | unsigned long cycles = 0; | ||
464 | int ret = 0; | ||
465 | int i; | ||
466 | |||
467 | sg[0].page = virt_to_page(p); | ||
468 | sg[0].offset = offset_in_page(p); | ||
469 | sg[0].length = blen; | ||
470 | |||
471 | local_bh_disable(); | ||
472 | local_irq_disable(); | ||
473 | |||
474 | /* Warm-up run. */ | ||
475 | for (i = 0; i < 4; i++) { | ||
476 | if (enc) | ||
477 | ret = crypto_cipher_encrypt(tfm, sg, sg, blen); | ||
478 | else | ||
479 | ret = crypto_cipher_decrypt(tfm, sg, sg, blen); | ||
480 | |||
481 | if (ret) | ||
482 | goto out; | ||
483 | } | ||
484 | |||
485 | /* The real thing. */ | ||
486 | for (i = 0; i < 8; i++) { | ||
487 | cycles_t start, end; | ||
488 | |||
489 | start = get_cycles(); | ||
490 | if (enc) | ||
491 | ret = crypto_cipher_encrypt(tfm, sg, sg, blen); | ||
492 | else | ||
493 | ret = crypto_cipher_decrypt(tfm, sg, sg, blen); | ||
494 | end = get_cycles(); | ||
495 | |||
496 | if (ret) | ||
497 | goto out; | ||
498 | |||
499 | cycles += end - start; | ||
500 | } | ||
501 | |||
502 | out: | ||
503 | local_irq_enable(); | ||
504 | local_bh_enable(); | ||
505 | |||
506 | if (ret == 0) | ||
507 | printk("1 operation in %lu cycles (%d bytes)\n", | ||
508 | (cycles + 4) / 8, blen); | ||
509 | |||
510 | return ret; | ||
511 | } | ||
512 | |||
429 | static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec, | 513 | static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec, |
430 | struct cipher_testvec *template, | 514 | struct cipher_testvec *template, |
431 | unsigned int tcount, struct cipher_speed *speed) | 515 | unsigned int tcount, struct cipher_speed *speed) |
@@ -433,8 +517,6 @@ static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec, | |||
433 | unsigned int ret, i, j, iv_len; | 517 | unsigned int ret, i, j, iv_len; |
434 | unsigned char *key, *p, iv[128]; | 518 | unsigned char *key, *p, iv[128]; |
435 | struct crypto_tfm *tfm; | 519 | struct crypto_tfm *tfm; |
436 | struct scatterlist sg[8]; | ||
437 | unsigned long start, bcount; | ||
438 | const char *e, *m; | 520 | const char *e, *m; |
439 | 521 | ||
440 | if (enc == ENCRYPT) | 522 | if (enc == ENCRYPT) |
@@ -492,25 +574,16 @@ static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec, | |||
492 | crypto_cipher_set_iv(tfm, iv, iv_len); | 574 | crypto_cipher_set_iv(tfm, iv, iv_len); |
493 | } | 575 | } |
494 | 576 | ||
495 | for (start = jiffies, bcount = 0; | 577 | if (sec) |
496 | ((jiffies - start) / HZ) < sec; bcount++) { | 578 | ret = test_cipher_jiffies(tfm, enc, p, speed[i].blen, |
497 | sg[0].page = virt_to_page(p); | 579 | sec); |
498 | sg[0].offset = offset_in_page(p); | 580 | else |
499 | sg[0].length = speed[i].blen; | 581 | ret = test_cipher_cycles(tfm, enc, p, speed[i].blen); |
500 | 582 | ||
501 | if (enc) | 583 | if (ret) { |
502 | ret = crypto_cipher_encrypt(tfm, sg, sg, speed[i].blen); | 584 | printk("%s() failed flags=%x\n", e, tfm->crt_flags); |
503 | else | 585 | break; |
504 | ret = crypto_cipher_decrypt(tfm, sg, sg, speed[i].blen); | ||
505 | |||
506 | if (ret) { | ||
507 | printk("%s () failed flags=%x\n", e, tfm->crt_flags); | ||
508 | goto out; | ||
509 | } | ||
510 | } | 586 | } |
511 | |||
512 | printk("%lu operations in %u seconds (%lu bytes)\n", | ||
513 | bcount, sec, bcount * speed[i].blen); | ||
514 | } | 587 | } |
515 | 588 | ||
516 | out: | 589 | out: |
@@ -1063,7 +1136,8 @@ module_exit(fini); | |||
1063 | 1136 | ||
1064 | module_param(mode, int, 0); | 1137 | module_param(mode, int, 0); |
1065 | module_param(sec, uint, 0); | 1138 | module_param(sec, uint, 0); |
1066 | MODULE_PARM_DESC(sec, "Length in seconds of speed tests"); | 1139 | MODULE_PARM_DESC(sec, "Length in seconds of speed tests " |
1140 | "(defaults to zero which uses CPU cycles instead)"); | ||
1067 | 1141 | ||
1068 | MODULE_LICENSE("GPL"); | 1142 | MODULE_LICENSE("GPL"); |
1069 | MODULE_DESCRIPTION("Quick & dirty crypto testing module"); | 1143 | MODULE_DESCRIPTION("Quick & dirty crypto testing module"); |