Debian Codegraph @

speed-test/cryptoperf-base.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2015 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #include "cryptoperf.h"
21
22 /**
23 * Execute one particular test and record the timing information
24 * @test: test case definition
25 *
26 * result: 0 on success, error otherwise
27 */
28 int cp_exec_test(struct cp_test *test)
29 {
30 uint64_t testduration = 0;
31 uint64_t nano = 1;
32 unsigned int i = 0;
33 struct cp_test_param *params = test->test_params;
34
35 nano = nano << 32;
36
37 testduration = nano * test->exectime;
38
39 if (params->exectime)
40 testduration = nano * params->exectime;
41
42 if (test->init_test) {
43 int ret = test->init_test(test);
44 if (ret) {
45 printf(DRIVER_NAME": initialization for %s failed\n",
46 test->testname);
47 return ret;
48 }
49 }
50
51 dbg("Starting test %s for %lu seconds\n", test->testname,
52 (unsigned long)(testduration / nano));
53 test->results.totaltime = 0;
54 test->results.rounds = 0;
55 test->results.byteperop = test->exec_test(test);
56 if (params->aio)
57 test->results.byteperop *= params->aio;
58
59 /* prime the test */
60 for (i = 0; i < 10; i++)
61 test->results.chunksize = test->exec_test(test);
62
63 while (test->results.totaltime < testduration) {
64 struct timespec start;
65 struct timespec end;
66
67 cp_get_nstime(&start);
68 test->results.chunksize = test->exec_test(test);
69 cp_get_nstime(&end);
70 test->results.totaltime +=
71 (cp_ts2u64(&end) - cp_ts2u64(&start));
72 test->results.rounds++;
73 }
74 dbg("Finished test %s for %lu ns with %lu rounds\n", test->testname,
75 (unsigned long)test->results.totaltime,
76 (unsigned long)test->results.rounds);
77
78 if (test->fini_test)
79 test->fini_test(test);
80
81 dbg("Finished test %s\n", test->testname);
82
83 return 0;
84 }
85
86 /*
87 * Convert an integer value into a string value that displays the integer
88 * in either bytes, kB, or MB
89 *
90 * @bytes value to convert -- input
91 * @str already allocated buffer for converted string -- output
92 * @strlen size of str
93 */
94 static void cp_bytes2string(uint64_t bytes, char *str, size_t strlen)
95 {
96 if (1UL<<30 < bytes) {
97 uint64_t abs = (bytes>>30);
98 uint64_t part = ((bytes - (abs<<30)) / (10000000));
99 snprintf(str, strlen, "%lu.%lu GB", (unsigned long)abs,
100 (unsigned long)part);
101 return;
102
103 } else if (1UL<<20 < bytes) {
104 uint64_t abs = (bytes>>20);
105 uint64_t part = ((bytes - (abs<<20)) / (10000));
106 snprintf(str, strlen, "%lu.%lu MB", (unsigned long)abs,
107 (unsigned long)part);
108 return;
109 } else if (1UL<<10 < bytes) {
110 uint64_t abs = (bytes>>10);
111 uint64_t part = ((bytes - (abs<<10)) / (10));
112 snprintf(str, strlen, "%lu.%lu kB", (unsigned long)abs,
113 (unsigned long)part);
114 return;
115 }
116 snprintf(str, strlen, "%lu B", (unsigned long)bytes);
117 str[strlen] = '\0';
118 }
119
120 /*
121 * Format the test results nicely
122 *
123 * This function should be called after a test execution to present
124 * the test results properly formatted
125 *
126 * @test test definition
127 *
128 * result: pointer to newly allocated character string with formatted output.
129 * The caller must free that pointer.
130 */
131 char *cp_print_status(struct cp_test *test, int raw)
132 {
133 char *str = NULL;
134 uint64_t processed_bytes = test->results.rounds * test->results.byteperop;
135 uint64_t totaltime = test->results.totaltime>>32;
136 uint64_t ops = 0;
137
138 str = calloc(1, 121);
139 if (!str)
140 return str;
141
142 if (!totaltime) {
143 snprintf(str, 120, "%-35s | untested\n", test->testname);
144 return str;
145 }
146
147 ops = test->results.rounds / totaltime;
148
149 if (raw) {
150 snprintf(str, 120, "%s,%s,%lu,%lu,%lu",
151 test->testname,
152 test->enc ? "e" : "d",
153 (unsigned long)test->results.chunksize,
154 (unsigned long)(processed_bytes/totaltime),
155 (unsigned long)ops);
156 } else {
157 #define VALLEN 23
158 char byteseconds[VALLEN + 1];
159
160 memset(byteseconds, 0, sizeof(byteseconds));
161 cp_bytes2string((processed_bytes / totaltime), byteseconds,
162 VALLEN);
163 snprintf(str, 120, "%-24s|%s|%8lu bytes|%*s/s|%lu ops/s",
164 test->testname,
165 test->enc ? "e" : "d",
166 (unsigned long)test->results.chunksize,
167 VALLEN,
168 byteseconds,
169 (unsigned long)ops);
170 }
171
172 return str;
173 }
174
175 int cp_read_random(unsigned char *buf, size_t buflen)
176 {
177 int fd = 0;
178 ssize_t ret = 0;
179 size_t len = 0;
180
181 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
182 if(0 > fd)
183 return fd;
184 do {
185 ret = read(fd, (buf + len), (buflen - len));
186 if(0 < ret)
187 len += (size_t)ret;
188 } while ((0 < ret || EINTR == errno || ERESTART == errno)
189 && buflen > len);
190
191 close(fd);
192
193 if(buflen == len)
194 return SUCCESS;
195 else
196 return FAILURE;
197 }