xref: /isa-l_crypto/sha256_mb/sha256_mb_vs_ossl_perf.c (revision 487b772bd076f8313189acea961fedffe88eec4b)
1 /**********************************************************************
2   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <openssl/sha.h>
33 #include "sha256_mb.h"
34 #include "test.h"
35 
36 // Set number of outstanding jobs
37 #define TEST_BUFS 32
38 
39 #ifndef GT_L3_CACHE
40 #define GT_L3_CACHE 32 * 1024 * 1024 /* some number > last level cache */
41 #endif
42 
43 #if !defined(COLD_TEST) && !defined(TEST_CUSTOM)
44 // Cached test, loop many times over small dataset
45 #define TEST_LEN      4 * 1024
46 #define TEST_LOOPS    4000
47 #define TEST_TYPE_STR "_warm"
48 #elif defined(COLD_TEST)
49 // Uncached test.  Pull from large mem base.
50 #define TEST_LEN      (GT_L3_CACHE / TEST_BUFS)
51 #define TEST_LOOPS    20
52 #define TEST_TYPE_STR "_cold"
53 #endif
54 
55 #define TEST_MEM TEST_LEN *TEST_BUFS *TEST_LOOPS
56 
57 /* Reference digest global to reduce stack usage */
58 static uint8_t digest_ssl[TEST_BUFS][4 * ISAL_SHA256_DIGEST_NWORDS];
59 
60 int
main(void)61 main(void)
62 {
63         ISAL_SHA256_HASH_CTX_MGR *mgr = NULL;
64         ISAL_SHA256_HASH_CTX ctxpool[TEST_BUFS], *ctx = NULL;
65         unsigned char *bufs[TEST_BUFS];
66         uint32_t i, j, t, fail = 0;
67         struct perf start, stop;
68 
69         for (i = 0; i < TEST_BUFS; i++) {
70                 bufs[i] = (unsigned char *) calloc((size_t) TEST_LEN, 1);
71                 if (bufs[i] == NULL) {
72                         printf("calloc failed test aborted\n");
73                         return 1;
74                 }
75                 // Init ctx contents
76                 isal_hash_ctx_init(&ctxpool[i]);
77                 ctxpool[i].user_data = (void *) ((uint64_t) i);
78         }
79 
80         int ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA256_HASH_CTX_MGR));
81         if (ret) {
82                 printf("alloc error: Fail");
83                 return -1;
84         }
85         ret = isal_sha256_ctx_mgr_init(mgr);
86         if (ret)
87                 return 1;
88 
89         // Start OpenSSL tests
90         perf_start(&start);
91         for (t = 0; t < TEST_LOOPS; t++) {
92                 for (i = 0; i < TEST_BUFS; i++)
93                         SHA256(bufs[i], TEST_LEN, digest_ssl[i]);
94         }
95         perf_stop(&stop);
96 
97         printf("sha256_openssl" TEST_TYPE_STR ": ");
98         perf_print(stop, start, (long long) TEST_LEN * i * t);
99 
100         // Start mb tests
101         perf_start(&start);
102         for (t = 0; t < TEST_LOOPS; t++) {
103                 for (i = 0; i < TEST_BUFS; i++) {
104                         ret = isal_sha256_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], TEST_LEN,
105                                                          ISAL_HASH_ENTIRE);
106                         if (ret)
107                                 return 1;
108                 }
109 
110                 do {
111                         ret = isal_sha256_ctx_mgr_flush(mgr, &ctx);
112                         if (ret)
113                                 return 1;
114                 } while (ctx != NULL);
115         }
116         perf_stop(&stop);
117 
118         printf("multibinary_sha256" TEST_TYPE_STR ": ");
119         perf_print(stop, start, (long long) TEST_LEN * i * t);
120 
121         for (i = 0; i < TEST_BUFS; i++) {
122                 for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
123                         if (ctxpool[i].job.result_digest[j] !=
124                             to_be32(((uint32_t *) digest_ssl[i])[j])) {
125                                 fail++;
126                                 printf("Test%d, digest%d fail %08X <=> %08X\n", i, j,
127                                        ctxpool[i].job.result_digest[j],
128                                        to_be32(((uint32_t *) digest_ssl[i])[j]));
129                         }
130                 }
131         }
132 
133         printf("Multi-buffer sha256 test complete %d buffers of %d B with "
134                "%d iterations\n",
135                TEST_BUFS, TEST_LEN, TEST_LOOPS);
136 
137         if (fail)
138                 printf("Test failed function check %d\n", fail);
139         else
140                 printf(" multibinary_sha256_ossl_perf: Pass\n");
141 
142         return fail;
143 }
144