Commit | Line | Data |
---|---|---|
fec0f21c JA |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | ||
5 | #include "../fio.h" | |
6 | #include "../gettime.h" | |
9e52966e | 7 | #include "../fio_time.h" |
fec0f21c JA |
8 | #include "../verify.h" |
9 | ||
10 | #include "../crc/md5.h" | |
11 | #include "../crc/crc64.h" | |
12 | #include "../crc/crc32.h" | |
13 | #include "../crc/crc32c.h" | |
14 | #include "../crc/crc16.h" | |
15 | #include "../crc/crc7.h" | |
16 | #include "../crc/sha1.h" | |
17 | #include "../crc/sha256.h" | |
18 | #include "../crc/sha512.h" | |
844ea602 | 19 | #include "../crc/xxhash.h" |
fec0f21c | 20 | |
ea1f1da3 JA |
21 | #include "test.h" |
22 | ||
f7540869 JA |
23 | #define CHUNK 131072U |
24 | #define NR_CHUNKS 2048U | |
fec0f21c JA |
25 | |
26 | struct test_type { | |
27 | const char *name; | |
28 | unsigned int mask; | |
24ef7a61 | 29 | uint64_t (*fn)(void *, size_t); |
fec0f21c JA |
30 | }; |
31 | ||
32 | enum { | |
33 | T_MD5 = 1U << 0, | |
34 | T_CRC64 = 1U << 1, | |
35 | T_CRC32 = 1U << 2, | |
36 | T_CRC32C = 1U << 3, | |
37 | T_CRC16 = 1U << 4, | |
38 | T_CRC7 = 1U << 5, | |
39 | T_SHA1 = 1U << 6, | |
40 | T_SHA256 = 1U << 7, | |
41 | T_SHA512 = 1U << 8, | |
844ea602 | 42 | T_XXHASH = 1U << 9, |
fec0f21c JA |
43 | }; |
44 | ||
45 | static void randomize_buf(void *buf, unsigned int size, int seed) | |
46 | { | |
47 | struct frand_state state; | |
48 | ||
49 | init_rand_seed(&state, seed); | |
50 | fill_random_buf(&state, buf, size); | |
51 | } | |
52 | ||
24ef7a61 | 53 | static uint64_t t_md5(void *buf, size_t size) |
fec0f21c JA |
54 | { |
55 | uint32_t digest[4]; | |
56 | struct fio_md5_ctx ctx = { .hash = digest }; | |
57 | struct timeval s; | |
fec0f21c JA |
58 | int i; |
59 | ||
60 | fio_md5_init(&ctx); | |
61 | ||
fec0f21c JA |
62 | fio_gettime(&s, NULL); |
63 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 64 | fio_md5_update(&ctx, buf, size); |
fec0f21c | 65 | |
24ef7a61 | 66 | return utime_since_now(&s); |
fec0f21c JA |
67 | } |
68 | ||
24ef7a61 | 69 | static uint64_t t_crc64(void *buf, size_t size) |
fec0f21c JA |
70 | { |
71 | struct timeval s; | |
fec0f21c JA |
72 | int i; |
73 | ||
fec0f21c JA |
74 | fio_gettime(&s, NULL); |
75 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 76 | fio_crc64(buf, size); |
fec0f21c | 77 | |
24ef7a61 | 78 | return utime_since_now(&s); |
fec0f21c JA |
79 | } |
80 | ||
24ef7a61 | 81 | static uint64_t t_crc32(void *buf, size_t size) |
fec0f21c JA |
82 | { |
83 | struct timeval s; | |
fec0f21c JA |
84 | int i; |
85 | ||
fec0f21c JA |
86 | fio_gettime(&s, NULL); |
87 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 88 | fio_crc32(buf, size); |
fec0f21c | 89 | |
24ef7a61 | 90 | return utime_since_now(&s); |
fec0f21c JA |
91 | } |
92 | ||
24ef7a61 | 93 | static uint64_t t_crc32c(void *buf, size_t size) |
fec0f21c JA |
94 | { |
95 | struct timeval s; | |
fec0f21c JA |
96 | int i; |
97 | ||
fec0f21c JA |
98 | fio_gettime(&s, NULL); |
99 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 100 | fio_crc32c(buf, size); |
fec0f21c | 101 | |
24ef7a61 | 102 | return utime_since_now(&s); |
fec0f21c JA |
103 | } |
104 | ||
24ef7a61 | 105 | static uint64_t t_crc16(void *buf, size_t size) |
fec0f21c JA |
106 | { |
107 | struct timeval s; | |
fec0f21c JA |
108 | int i; |
109 | ||
fec0f21c JA |
110 | fio_gettime(&s, NULL); |
111 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 112 | fio_crc16(buf, size); |
fec0f21c | 113 | |
24ef7a61 | 114 | return utime_since_now(&s); |
fec0f21c JA |
115 | } |
116 | ||
24ef7a61 | 117 | static uint64_t t_crc7(void *buf, size_t size) |
fec0f21c JA |
118 | { |
119 | struct timeval s; | |
120 | uint64_t ret; | |
fec0f21c JA |
121 | int i; |
122 | ||
fec0f21c JA |
123 | fio_gettime(&s, NULL); |
124 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 125 | fio_crc7(buf, size); |
fec0f21c JA |
126 | |
127 | ret = utime_since_now(&s); | |
fec0f21c JA |
128 | return ret; |
129 | } | |
130 | ||
24ef7a61 | 131 | static uint64_t t_sha1(void *buf, size_t size) |
fec0f21c JA |
132 | { |
133 | uint32_t sha[5]; | |
134 | struct fio_sha1_ctx ctx = { .H = sha }; | |
135 | struct timeval s; | |
136 | uint64_t ret; | |
fec0f21c JA |
137 | int i; |
138 | ||
139 | fio_sha1_init(&ctx); | |
140 | ||
fec0f21c JA |
141 | fio_gettime(&s, NULL); |
142 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 143 | fio_sha1_update(&ctx, buf, size); |
fec0f21c JA |
144 | |
145 | ret = utime_since_now(&s); | |
fec0f21c JA |
146 | return ret; |
147 | } | |
148 | ||
24ef7a61 | 149 | static uint64_t t_sha256(void *buf, size_t size) |
fec0f21c JA |
150 | { |
151 | uint8_t sha[64]; | |
152 | struct fio_sha256_ctx ctx = { .buf = sha }; | |
153 | struct timeval s; | |
154 | uint64_t ret; | |
fec0f21c JA |
155 | int i; |
156 | ||
157 | fio_sha256_init(&ctx); | |
158 | ||
fec0f21c JA |
159 | fio_gettime(&s, NULL); |
160 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 161 | fio_sha256_update(&ctx, buf, size); |
fec0f21c JA |
162 | |
163 | ret = utime_since_now(&s); | |
fec0f21c JA |
164 | return ret; |
165 | } | |
166 | ||
24ef7a61 | 167 | static uint64_t t_sha512(void *buf, size_t size) |
fec0f21c JA |
168 | { |
169 | uint8_t sha[128]; | |
170 | struct fio_sha512_ctx ctx = { .buf = sha }; | |
171 | struct timeval s; | |
172 | uint64_t ret; | |
fec0f21c JA |
173 | int i; |
174 | ||
175 | fio_sha512_init(&ctx); | |
176 | ||
fec0f21c JA |
177 | fio_gettime(&s, NULL); |
178 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 179 | fio_sha512_update(&ctx, buf, size); |
fec0f21c JA |
180 | |
181 | ret = utime_since_now(&s); | |
fec0f21c JA |
182 | return ret; |
183 | } | |
184 | ||
24ef7a61 | 185 | static uint64_t t_xxhash(void *buf, size_t size) |
844ea602 JA |
186 | { |
187 | void *state; | |
188 | struct timeval s; | |
189 | uint64_t ret; | |
844ea602 JA |
190 | int i; |
191 | ||
192 | state = XXH32_init(0x8989); | |
193 | ||
844ea602 JA |
194 | fio_gettime(&s, NULL); |
195 | for (i = 0; i < NR_CHUNKS; i++) | |
24ef7a61 | 196 | XXH32_update(state, buf, size); |
844ea602 JA |
197 | |
198 | XXH32_digest(state); | |
199 | ret = utime_since_now(&s); | |
844ea602 JA |
200 | return ret; |
201 | } | |
202 | ||
fec0f21c JA |
203 | static struct test_type t[] = { |
204 | { | |
205 | .name = "md5", | |
206 | .mask = T_MD5, | |
207 | .fn = t_md5, | |
208 | }, | |
209 | { | |
210 | .name = "crc64", | |
211 | .mask = T_CRC64, | |
212 | .fn = t_crc64, | |
213 | }, | |
214 | { | |
215 | .name = "crc32", | |
216 | .mask = T_CRC32, | |
217 | .fn = t_crc32, | |
218 | }, | |
219 | { | |
220 | .name = "crc32c", | |
221 | .mask = T_CRC32C, | |
222 | .fn = t_crc32c, | |
223 | }, | |
224 | { | |
225 | .name = "crc16", | |
226 | .mask = T_CRC16, | |
227 | .fn = t_crc16, | |
228 | }, | |
229 | { | |
230 | .name = "crc7", | |
231 | .mask = T_CRC7, | |
232 | .fn = t_crc7, | |
233 | }, | |
234 | { | |
235 | .name = "sha1", | |
236 | .mask = T_SHA1, | |
237 | .fn = t_sha1, | |
238 | }, | |
239 | { | |
240 | .name = "sha256", | |
241 | .mask = T_SHA256, | |
242 | .fn = t_sha256, | |
243 | }, | |
244 | { | |
245 | .name = "sha512", | |
246 | .mask = T_SHA512, | |
247 | .fn = t_sha512, | |
248 | }, | |
844ea602 JA |
249 | { |
250 | .name = "xxhash", | |
251 | .mask = T_XXHASH, | |
252 | .fn = t_xxhash, | |
253 | }, | |
fec0f21c JA |
254 | { |
255 | .name = NULL, | |
256 | }, | |
257 | }; | |
258 | ||
259 | static unsigned int get_test_mask(const char *type) | |
260 | { | |
261 | char *ostr, *str = strdup(type); | |
262 | unsigned int mask; | |
263 | char *name; | |
264 | int i; | |
265 | ||
266 | ostr = str; | |
267 | mask = 0; | |
268 | while ((name = strsep(&str, ",")) != NULL) { | |
269 | for (i = 0; t[i].name; i++) { | |
f7540869 | 270 | if (!strcmp(t[i].name, name)) { |
fec0f21c JA |
271 | mask |= t[i].mask; |
272 | break; | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | free(ostr); | |
278 | return mask; | |
279 | } | |
280 | ||
782744ef JA |
281 | static int list_types(void) |
282 | { | |
283 | int i; | |
284 | ||
285 | for (i = 0; t[i].name; i++) | |
286 | printf("%s\n", t[i].name); | |
287 | ||
24ef7a61 | 288 | return 1; |
782744ef JA |
289 | } |
290 | ||
fec0f21c JA |
291 | int fio_crctest(const char *type) |
292 | { | |
293 | unsigned int test_mask = 0; | |
294 | uint64_t mb = CHUNK * NR_CHUNKS; | |
24ef7a61 JA |
295 | int i, first = 1; |
296 | void *buf; | |
fec0f21c JA |
297 | |
298 | crc32c_intel_probe(); | |
299 | ||
300 | if (!type) | |
301 | test_mask = ~0U; | |
782744ef JA |
302 | else if (!strcmp(type, "help") || !strcmp(type, "list")) |
303 | return list_types(); | |
fec0f21c JA |
304 | else |
305 | test_mask = get_test_mask(type); | |
306 | ||
24ef7a61 JA |
307 | if (!test_mask) { |
308 | fprintf(stderr, "fio: unknown hash `%s`. Available:\n", type); | |
309 | return list_types(); | |
310 | } | |
311 | ||
312 | buf = malloc(CHUNK); | |
313 | randomize_buf(buf, CHUNK, 0x8989); | |
314 | ||
fec0f21c JA |
315 | for (i = 0; t[i].name; i++) { |
316 | double mb_sec; | |
317 | uint64_t usec; | |
318 | ||
319 | if (!(t[i].mask & test_mask)) | |
320 | continue; | |
321 | ||
24ef7a61 JA |
322 | usec = t[i].fn(buf, CHUNK); |
323 | if (first) | |
324 | usec = t[i].fn(buf, CHUNK); | |
325 | ||
fec0f21c JA |
326 | mb_sec = (double) mb / (double) usec; |
327 | mb_sec /= (1.024 * 1.024); | |
31538d87 | 328 | printf("%s:\t%8.2f MB/sec\n", t[i].name, mb_sec); |
24ef7a61 | 329 | first = 0; |
fec0f21c | 330 | } |
782744ef | 331 | |
24ef7a61 | 332 | free(buf); |
fec0f21c JA |
333 | return 0; |
334 | } |