aboutsummaryrefslogtreecommitdiff
path: root/test/performance/odp_random.c
blob: 7083349fff7d29ee5136e33d74e9dc9ddb05a52d (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
/* Copyright (c) 2021, Nokia
 *
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#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 MB (1024ull * 1024ull)

/* Command line options */
typedef struct {
	int num_threads;
	uint32_t size;
	uint32_t rounds;
} options_t;

static options_t options;
static const options_t options_def = {
	.num_threads = 1,
	.size = 256,
	.rounds = 100000,
};

static void print_usage(void)
{
	printf("\n"
	       "random data performance test\n"
	       "\n"
	       "Usage: odp_random [options]\n"
	       "\n"
	       "  -t, --threads Number of worker threads (default %u)\n"
	       "  -s, --size    Size of buffer in bytes (default %u)\n"
	       "  -r, --rounds  Number of test rounds (default %u)\n"
	       "                Rounded down to nearest multiple of 8\n"
	       "  -h, --help    This help\n"
	       "\n",
	       options_def.num_threads, options_def.size, options_def.rounds);
}

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

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

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

	options = options_def;

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

		if (opt == -1)
			break;

		switch (opt) {
		case 't':
			options.num_threads = atol(optarg);
			break;
		case 's':
			options.size = atol(optarg);
			break;
		case 'r':
			options.rounds = 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;
	}

	return ret;
}

const char *shm_name = "odp_random_test";

typedef struct test_shm_t {
	odp_barrier_t barrier;
	odp_random_kind_t type;
	uint64_t nsec[ODP_THREAD_COUNT_MAX];
} test_shm_t;

static test_shm_t *shm_lookup(void)
{
	test_shm_t *shm = NULL;
	odp_shm_t shm_hdl = odp_shm_lookup(shm_name);

	if (shm_hdl != ODP_SHM_INVALID)
		shm = (test_shm_t *)odp_shm_addr(shm_hdl);

	return shm;
}

static int test_random(void *p)
{
	(void)p;

	uint8_t *buf, *data;
	const unsigned long page = ODP_PAGE_SIZE;
	odp_time_t start;
	uint64_t nsec;
	test_shm_t *shm = shm_lookup();

	if (!shm) {
		ODPH_ERR("Failed to look up shm %s\n", shm_name);
		exit(EXIT_FAILURE);
	}

	/* One extra page for alignment. */
	buf = (uint8_t *)malloc(options.size + page);

	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));

	odp_barrier_wait(&shm->barrier);
	start = odp_time_local();

	for (uint32_t i = 0; i < options.rounds; i++) {
		uint32_t pos = 0;

		while (pos < options.size) {
			int32_t n = odp_random_data(data + pos,
						    options.size - pos,
						    shm->type);

			if (n < 0) {
				ODPH_ERR("odp_random_data() failed\n");
				exit(EXIT_FAILURE);
			}

			pos += n;
		}
	}

	nsec = odp_time_diff_ns(odp_time_local(), start);
	shm->nsec[odp_thread_id()] = nsec;
	free(buf);

	return 0;
}

static int test_random_test(void *p)
{
	(void)p;

	uint8_t *buf, *data;
	const unsigned long page = ODP_PAGE_SIZE;
	odp_time_t start;
	uint64_t nsec;
	uint64_t seed = 0;
	test_shm_t *shm = shm_lookup();

	if (!shm) {
		ODPH_ERR("Failed to look up shm %s\n", shm_name);
		exit(EXIT_FAILURE);
	}

	/* One extra page for alignment. */
	buf = (uint8_t *)malloc(options.size + page);

	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));

	odp_barrier_wait(&shm->barrier);
	start = odp_time_local();

	for (uint32_t i = 0; i < options.rounds; i++) {
		uint32_t pos = 0;

		while (pos < options.size) {
			int32_t n = odp_random_test_data(data + pos,
							 options.size - pos,
							 &seed);

			if (n < 0) {
				ODPH_ERR("odp_random_data() failed\n");
				exit(EXIT_FAILURE);
			}

			pos += n;
		}
	}

	nsec = odp_time_diff_ns(odp_time_local(), start);
	shm->nsec[odp_thread_id()] = nsec;
	free(buf);

	return 0;
}

static void test_type(odp_instance_t instance, test_shm_t *shm,
		      odp_random_kind_t type)
{
	memset(shm, 0, sizeof(test_shm_t));
	shm->type = type;
	odp_barrier_init(&shm->barrier, options.num_threads);

	odp_cpumask_t cpumask;
	odph_thread_common_param_t thr_common;
	odph_thread_param_t thr_param;
	odph_thread_t thr_worker[options.num_threads];

	if (odp_cpumask_default_worker(&cpumask, options.num_threads) !=
	    options.num_threads) {
		ODPH_ERR("Failed to get default CPU mask.\n");
		exit(EXIT_FAILURE);
	}

	odph_thread_common_param_init(&thr_common);
	thr_common.instance = instance;
	thr_common.cpumask = &cpumask;
	thr_common.share_param = 1;

	odph_thread_param_init(&thr_param);
	thr_param.thr_type = ODP_THREAD_WORKER;
	thr_param.start = test_random;

	if (type == (odp_random_kind_t)-1)
		thr_param.start = test_random_test;

	memset(&thr_worker, 0, sizeof(thr_worker));

	if (odph_thread_create(thr_worker, &thr_common, &thr_param,
			       options.num_threads) != options.num_threads) {
		ODPH_ERR("Failed to create worker threads.\n");
		exit(EXIT_FAILURE);
	}

	if (odph_thread_join(thr_worker, options.num_threads) !=
	    options.num_threads) {
		ODPH_ERR("Failed to join worker threads.\n");
		exit(EXIT_FAILURE);
	}

	double mb, seconds, nsec = 0;

	for (int i = 0; i < ODP_THREAD_COUNT_MAX; i++)
		nsec += shm->nsec[i];

	nsec /= options.num_threads;

	switch (type) {
	case ODP_RANDOM_BASIC:
		printf("ODP_RANDOM_BASIC\n");
		break;
	case ODP_RANDOM_CRYPTO:
		printf("ODP_RANDOM_CRYPTO\n");
		break;
	case ODP_RANDOM_TRUE:
		printf("ODP_RANDOM_TRUE\n");
		break;
	default:
		printf("odp_random_test_data\n");
	}

	printf("--------------------\n");
	printf("threads: %d  size: %u B  rounds: %u  ", options.num_threads,
	       options.size, options.rounds);
	mb = (uint64_t)options.num_threads * (uint64_t)options.size *
	     (uint64_t)options.rounds;
	mb /= MB;
	seconds = (double)nsec / (double)ODP_TIME_SEC_IN_NS;
	printf("MB: %.3f  seconds: %.3f  ", mb, seconds);
	printf("MB/s: %.3f  ", mb / seconds);
	printf("MB/s/thread: %.3f", mb / seconds / (double)options.num_threads);
	printf("\n\n");
}

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();

	test_shm_t *shm = NULL;
	odp_shm_t shm_hdl = odp_shm_reserve(shm_name, sizeof(test_shm_t), 64,
					    ODP_SHM_SW_ONLY);

	if (shm_hdl != ODP_SHM_INVALID)
		shm = (test_shm_t *)odp_shm_addr(shm_hdl);

	if (!shm) {
		ODPH_ERR("Failed to reserve shm %s\n", shm_name);
		exit(EXIT_FAILURE);
	}

	switch (odp_random_max_kind()) {
	case ODP_RANDOM_TRUE:
		test_type(instance, shm, ODP_RANDOM_TRUE);
		/* fall through */
	case ODP_RANDOM_CRYPTO:
		test_type(instance, shm, ODP_RANDOM_CRYPTO);
		/* fall through */
	default:
		test_type(instance, shm, ODP_RANDOM_BASIC);
		test_type(instance, shm, -1);
	}

	if (odp_shm_free(shm_hdl)) {
		ODPH_ERR("odp_shm_free() failed\n");
		exit(EXIT_FAILURE);
	}

	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;
}