aboutsummaryrefslogtreecommitdiff
path: root/test/performance/odp_crc.c
blob: 1b631c691b25e99b18d1efde3da395182bf0e943 (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2021 Nokia
 */

/**
 * @example odp_crc.c
 *
 * Performance test application for CRC hash APIs
 *
 * @cond _ODP_HIDE_FROM_DOXYGEN_
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <getopt.h>

#include <odp_api.h>
#include <odp/helper/odph_api.h>

#define KB 1024ull
#define MB (1024ull * 1024ull)

/* Command line options */
typedef struct {
	uint32_t size;
	uint32_t rounds;
	uint32_t offset;
	uint32_t test;
} options_t;

static options_t options;
static const options_t options_def = {
	.size = 16,
	.rounds = 10000,
	.offset = 0,
	.test = 0,
};

static void print_usage(void)
{
	printf("\n"
	       "CRC performance test\n"
	       "\n"
	       "Usage: odp_crc_perf [options]\n"
	       "\n"
	       "  -s, --size    Size of buffer in KB (default %u)\n"
	       "  -r, --rounds  Number of test rounds (default %u)\n"
	       "                Rounded down to nearest multiple of 8\n"
	       "  -o, --offset  Offset of data (default %u)\n"
	       "  -t, --test    Which API to test (default %u)\n"
	       "                0: both\n"
	       "                1: odp_hash_crc32c\n"
	       "                2: odp_hash_crc32\n"
	       "  -h, --help    This help\n"
	       "\n",
	       options_def.size, options_def.rounds, options_def.offset,
	       options.test);
}

static int parse_options(int argc, char *argv[])
{
	int opt;
	int long_index;
	int ret = 0;

	static const struct option longopts[] = {
		{ "size", required_argument, NULL, 's' },
		{ "rounds", required_argument, NULL, 'r' },
		{ "offset", required_argument, NULL, 'o' },
		{ "test", required_argument, NULL, 't' },
		{ "help", no_argument, NULL, 'h' },
		{ NULL, 0, NULL, 0 }
	};

	static const char *shortopts = "+s:r:o:t:h";

	options = options_def;

	while (1) {
		opt = getopt_long(argc, argv, shortopts, longopts, &long_index);

		if (opt == -1)
			break;

		switch (opt) {
		case 's':
			options.size = atol(optarg);
			break;
		case 'r':
			options.rounds = atol(optarg);
			break;
		case 'o':
			options.offset = atol(optarg);
			break;
		case 't':
			options.test = atol(optarg);
			break;
		case 'h':
			/* fall through */
		default:
			print_usage();
			ret = -1;
			break;
		}
	}

	if (options.size < 1) {
		ODPH_ERR("Invalid size: %" PRIu32 "\n", options.size);
		return -1;
	}

	if (options.offset > 4 * KB) {
		ODPH_ERR("Invalid offset: %" PRIu32 "\n", options.offset);
		return -1;
	}

	if (options.test > 2) {
		ODPH_ERR("Invalid API to test: %" PRIu32 "\n", options.test);
		return -1;
	}

	return ret;
}

static void report(uint64_t nsec)
{
	uint64_t size = (uint64_t)options.size * KB;
	uint32_t rounds = options.rounds & ~7ul;
	double mb, seconds;

	printf("size: %d KB  rounds: %d  offset: %d  ",
	       options.size, rounds, options.offset);
	mb = (double)(size * (uint64_t)rounds) / (double)MB;
	seconds = (double)nsec / (double)ODP_TIME_SEC_IN_NS;
	printf("MB: %.3f  seconds: %.3f  ", mb, seconds);
	printf("MB/s: %.3f", mb / seconds);
	printf("\n\n");
}

static uint64_t measure_crc32c(uint8_t *data, uint32_t size)
{
	void *p = data + options.offset;
	uint32_t crc = 1;
	volatile uint32_t v;
	odp_time_t start = odp_time_local();

	for (uint32_t i = 0; i < options.rounds / 8; i++) {
		crc ^= odp_hash_crc32c(p, size, crc);
		crc ^= odp_hash_crc32c(p, size, crc);
		crc ^= odp_hash_crc32c(p, size, crc);
		crc ^= odp_hash_crc32c(p, size, crc);

		crc ^= odp_hash_crc32c(p, size, crc);
		crc ^= odp_hash_crc32c(p, size, crc);
		crc ^= odp_hash_crc32c(p, size, crc);
		crc ^= odp_hash_crc32c(p, size, crc);
	}

	/* Make sure that crc is not optimized out. */
	v = crc;

	/* Quell "unused" warning. */
	(void)v;

	return odp_time_diff_ns(odp_time_local(), start);
}

static void test_odp_hash_crc32c(uint8_t *data)
{
	uint64_t size = (uint64_t)options.size * KB;
	uint64_t nsec;

	/* Warm-up. */
	measure_crc32c(data, size);

	/* Actual measurement. */
	nsec = measure_crc32c(data, size);

	report(nsec);
}

static uint64_t measure_crc32(uint8_t *data, uint32_t size)
{
	void *p = data + options.offset;
	uint32_t crc = 1;
	volatile uint32_t v;
	odp_time_t start = odp_time_local();

	for (uint32_t i = 0; i < options.rounds / 8; i++) {
		crc ^= odp_hash_crc32(p, size, crc);
		crc ^= odp_hash_crc32(p, size, crc);
		crc ^= odp_hash_crc32(p, size, crc);
		crc ^= odp_hash_crc32(p, size, crc);

		crc ^= odp_hash_crc32(p, size, crc);
		crc ^= odp_hash_crc32(p, size, crc);
		crc ^= odp_hash_crc32(p, size, crc);
		crc ^= odp_hash_crc32(p, size, crc);
	}

	/* Make sure that crc is not optimized out. */
	v = crc;

	/* Quell "unused" warning. */
	(void)v;

	return odp_time_diff_ns(odp_time_local(), start);
}

static void test_odp_hash_crc32(uint8_t *data)
{
	uint64_t size = (uint64_t)options.size * KB;
	uint64_t nsec;

	/* Warm-up. */
	measure_crc32(data, size);

	/* Actual measurement. */
	nsec = measure_crc32(data, size);

	report(nsec);
}

int main(int argc, char **argv)
{
	odp_instance_t instance;
	odp_init_t init;

	if (parse_options(argc, argv))
		exit(EXIT_FAILURE);

	/* List features not to be used */
	odp_init_param_init(&init);
	init.not_used.feat.cls = 1;
	init.not_used.feat.compress = 1;
	init.not_used.feat.crypto = 1;
	init.not_used.feat.ipsec = 1;
	init.not_used.feat.schedule = 1;
	init.not_used.feat.stash = 1;
	init.not_used.feat.timer = 1;
	init.not_used.feat.tm = 1;

	/* Init ODP before calling anything else */
	if (odp_init_global(&instance, &init, NULL)) {
		ODPH_ERR("Global init failed.\n");
		exit(EXIT_FAILURE);
	}

	/* Init this thread */
	if (odp_init_local(instance, ODP_THREAD_CONTROL)) {
		ODPH_ERR("Local init failed.\n");
		exit(EXIT_FAILURE);
	}

	odp_sys_info_print();

	uint8_t *buf, *data;
	uint32_t size = options.size * KB;
	uint64_t seed = 1;
	const unsigned long page = 4 * KB;

	/* One extra page for alignment, another one for offset. */
	buf = (uint8_t *)malloc(size + page * 2);

	if (!buf) {
		ODPH_ERR("Memory allocation failed.\n");
		exit(EXIT_FAILURE);
	}

	/* Align to start of page. */
	data = (uint8_t *)(((uintptr_t)buf + (page - 1)) & ~(page - 1));

	if (odp_random_test_data(data, size, &seed) != (int32_t)size) {
		ODPH_ERR("odp_random_test_data() failed.\n");
		exit(EXIT_FAILURE);
	}

	if (options.test == 0 || options.test == 1) {
		printf("odp_hash_crc32c\n"
		       "---------------\n");
		test_odp_hash_crc32c(data);
	}

	if (options.test == 0 || options.test == 2) {
		printf("odp_hash_crc32\n"
		       "--------------\n");
		test_odp_hash_crc32(data);
	}

	free(buf);

	if (odp_term_local()) {
		ODPH_ERR("Local terminate failed.\n");
		exit(EXIT_FAILURE);
	}

	if (odp_term_global(instance)) {
		ODPH_ERR("Global terminate failed.\n");
		exit(EXIT_FAILURE);
	}

	return 0;
}