summaryrefslogtreecommitdiff
path: root/crc/test.c
blob: b57f07a4d718f3b623920528d74cde69740f7820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../gettime.h"
#include "../fio_time.h"
#include "../lib/rand.h"
#include "../os/os.h"

#include "../crc/md5.h"
#include "../crc/crc64.h"
#include "../crc/crc32.h"
#include "../crc/crc32c.h"
#include "../crc/crc16.h"
#include "../crc/crc7.h"
#include "../crc/sha1.h"
#include "../crc/sha256.h"
#include "../crc/sha512.h"
#include "../crc/sha3.h"
#include "../crc/xxhash.h"
#include "../crc/murmur3.h"
#include "../crc/fnv.h"
#include "../hash.h"

#include "test.h"

#define CHUNK		131072U
#define NR_CHUNKS	  2048U

struct test_type {
	const char *name;
	unsigned int mask;
	void (*fn)(struct test_type *, void *, size_t);
	uint32_t output;
};

enum {
	T_MD5		= 1U << 0,
	T_CRC64		= 1U << 1,
	T_CRC32		= 1U << 2,
	T_CRC32C	= 1U << 3,
	T_CRC16		= 1U << 4,
	T_CRC7		= 1U << 5,
	T_SHA1		= 1U << 6,
	T_SHA256	= 1U << 7,
	T_SHA512	= 1U << 8,
	T_XXHASH	= 1U << 9,
	T_MURMUR3	= 1U << 10,
	T_JHASH		= 1U << 11,
	T_FNV		= 1U << 12,
	T_SHA3_224	= 1U << 13,
	T_SHA3_256	= 1U << 14,
	T_SHA3_384	= 1U << 15,
	T_SHA3_512	= 1U << 16,
};

static void t_md5(struct test_type *t, void *buf, size_t size)
{
	uint32_t digest[4];
	struct fio_md5_ctx ctx = { .hash = digest };
	int i;

	fio_md5_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_md5_update(&ctx, buf, size);
		fio_md5_final(&ctx);
	}
}

static void t_crc64(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += fio_crc64(buf, size);
}

static void t_crc32(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += fio_crc32(buf, size);
}

static void t_crc32c(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += fio_crc32c(buf, size);
}

static void t_crc16(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += fio_crc16(buf, size);
}

static void t_crc7(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += fio_crc7(buf, size);
}

static void t_sha1(struct test_type *t, void *buf, size_t size)
{
	uint32_t sha[5];
	struct fio_sha1_ctx ctx = { .H = sha };
	int i;

	fio_sha1_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_sha1_update(&ctx, buf, size);
		fio_sha1_final(&ctx);
	}
}

static void t_sha256(struct test_type *t, void *buf, size_t size)
{
	uint8_t sha[64];
	struct fio_sha256_ctx ctx = { .buf = sha };
	int i;

	fio_sha256_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_sha256_update(&ctx, buf, size);
		fio_sha256_final(&ctx);
	}
}

static void t_sha512(struct test_type *t, void *buf, size_t size)
{
	uint8_t sha[128];
	struct fio_sha512_ctx ctx = { .buf = sha };
	int i;

	fio_sha512_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++)
		fio_sha512_update(&ctx, buf, size);
}

static void t_sha3_224(struct test_type *t, void *buf, size_t size)
{
	uint8_t sha[SHA3_224_DIGEST_SIZE];
	struct fio_sha3_ctx ctx = { .sha = sha };
	int i;

	fio_sha3_224_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_sha3_update(&ctx, buf, size);
		fio_sha3_final(&ctx);
	}
}

static void t_sha3_256(struct test_type *t, void *buf, size_t size)
{
	uint8_t sha[SHA3_256_DIGEST_SIZE];
	struct fio_sha3_ctx ctx = { .sha = sha };
	int i;

	fio_sha3_256_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_sha3_update(&ctx, buf, size);
		fio_sha3_final(&ctx);
	}
}

static void t_sha3_384(struct test_type *t, void *buf, size_t size)
{
	uint8_t sha[SHA3_384_DIGEST_SIZE];
	struct fio_sha3_ctx ctx = { .sha = sha };
	int i;

	fio_sha3_384_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_sha3_update(&ctx, buf, size);
		fio_sha3_final(&ctx);
	}
}

static void t_sha3_512(struct test_type *t, void *buf, size_t size)
{
	uint8_t sha[SHA3_512_DIGEST_SIZE];
	struct fio_sha3_ctx ctx = { .sha = sha };
	int i;

	fio_sha3_512_init(&ctx);

	for (i = 0; i < NR_CHUNKS; i++) {
		fio_sha3_update(&ctx, buf, size);
		fio_sha3_final(&ctx);
	}
}

static void t_murmur3(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += murmurhash3(buf, size, 0x8989);
}

static void t_jhash(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += jhash(buf, size, 0x8989);
}

static void t_fnv(struct test_type *t, void *buf, size_t size)
{
	int i;

	for (i = 0; i < NR_CHUNKS; i++)
		t->output += fnv(buf, size, 0x8989);
}

static void t_xxhash(struct test_type *t, void *buf, size_t size)
{
	void *state;
	int i;

	state = XXH32_init(0x8989);

	for (i = 0; i < NR_CHUNKS; i++)
		XXH32_update(state, buf, size);

	t->output = XXH32_digest(state);
}

static struct test_type t[] = {
	{
		.name = "md5",
		.mask = T_MD5,
		.fn = t_md5,
	},
	{
		.name = "crc64",
		.mask = T_CRC64,
		.fn = t_crc64,
	},
	{
		.name = "crc32",
		.mask = T_CRC32,
		.fn = t_crc32,
	},
	{
		.name = "crc32c",
		.mask = T_CRC32C,
		.fn = t_crc32c,
	},
	{
		.name = "crc16",
		.mask = T_CRC16,
		.fn = t_crc16,
	},
	{
		.name = "crc7",
		.mask = T_CRC7,
		.fn = t_crc7,
	},
	{
		.name = "sha1",
		.mask = T_SHA1,
		.fn = t_sha1,
	},
	{
		.name = "sha256",
		.mask = T_SHA256,
		.fn = t_sha256,
	},
	{
		.name = "sha512",
		.mask = T_SHA512,
		.fn = t_sha512,
	},
	{
		.name = "xxhash",
		.mask = T_XXHASH,
		.fn = t_xxhash,
	},
	{
		.name = "murmur3",
		.mask = T_MURMUR3,
		.fn = t_murmur3,
	},
	{
		.name = "jhash",
		.mask = T_JHASH,
		.fn = t_jhash,
	},
	{
		.name = "fnv",
		.mask = T_FNV,
		.fn = t_fnv,
	},
	{
		.name = "sha3-224",
		.mask = T_SHA3_224,
		.fn = t_sha3_224,
	},
	{
		.name = "sha3-256",
		.mask = T_SHA3_256,
		.fn = t_sha3_256,
	},
	{
		.name = "sha3-384",
		.mask = T_SHA3_384,
		.fn = t_sha3_384,
	},
	{
		.name = "sha3-512",
		.mask = T_SHA3_512,
		.fn = t_sha3_512,
	},
	{
		.name = NULL,
	},
};

static unsigned int get_test_mask(const char *type)
{
	char *ostr, *str = strdup(type);
	unsigned int mask;
	char *name;
	int i;

	ostr = str;
	mask = 0;
	while ((name = strsep(&str, ",")) != NULL) {
		for (i = 0; t[i].name; i++) {
			if (!strcmp(t[i].name, name)) {
				mask |= t[i].mask;
				break;
			}
		}
	}

	free(ostr);
	return mask;
}

static int list_types(void)
{
	int i;

	for (i = 0; t[i].name; i++)
		printf("%s\n", t[i].name);

	return 1;
}

int fio_crctest(const char *type)
{
	unsigned int test_mask = 0;
	uint64_t mb = CHUNK * NR_CHUNKS;
	struct frand_state state;
	int i, first = 1;
	void *buf;

	crc32c_arm64_probe();
	crc32c_intel_probe();

	if (!type)
		test_mask = ~0U;
	else if (!strcmp(type, "help") || !strcmp(type, "list"))
		return list_types();
	else
		test_mask = get_test_mask(type);

	if (!test_mask) {
		fprintf(stderr, "fio: unknown hash `%s`. Available:\n", type);
		return list_types();
	}

	buf = malloc(CHUNK);
	init_rand_seed(&state, 0x8989, 0);
	fill_random_buf(&state, buf, CHUNK);

	for (i = 0; t[i].name; i++) {
		struct timespec ts;
		double mb_sec;
		uint64_t usec;
		char pre[3];

		if (!(t[i].mask & test_mask))
			continue;

		/*
		 * For first run, make sure CPUs are spun up and that
		 * we've touched the data.
		 */
		if (first) {
			usec_spin(100000);
			t[i].fn(&t[i], buf, CHUNK);
		}

		fio_gettime(&ts, NULL);
		t[i].fn(&t[i], buf, CHUNK);
		usec = utime_since_now(&ts);

		if (usec) {
			mb_sec = (double) mb / (double) usec;
			mb_sec /= (1.024 * 1.024);
			if (strlen(t[i].name) >= 7)
				sprintf(pre, "\t");
			else
				sprintf(pre, "\t\t");
			printf("%s:%s%8.2f MiB/sec\n", t[i].name, pre, mb_sec);
		} else
			printf("%s:inf MiB/sec\n", t[i].name);
		first = 0;
	}

	free(buf);
	return 0;
}