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