aboutsummaryrefslogtreecommitdiffstats
path: root/test/dring/test_dring.c
blob: 22d0db47eaa48a0285ca0cbd92493e2ef6ad92c2 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * Copyright (c) 2016  Intel Corporation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>

#include <rte_common.h>
#include <rte_log.h>
#include <rte_errno.h>
#include <rte_launch.h>
#include <rte_cycles.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_ring.h>
#include <tle_dring.h>
#include <rte_random.h>

#define OBJ_NUM		UINT16_MAX
#define ITER_NUM	(4 * OBJ_NUM)

enum {
	NONE,
	SINGLE,
	MULTI,
};

struct dring_arg {
	struct tle_dring *dr;
	struct rte_ring *r;
	uint32_t iter;
	int32_t enq_type;
	int32_t deq_type;
	uint32_t enq;
	uint32_t deq;
};

/*
 * free memory allocated for drbs and for the ring itself.
 */
static void
fini_drb_ring(struct rte_ring *r)
{
	struct tle_drb *drb;

	/* free drbs. */
	while (rte_ring_dequeue(r, (void **)&drb) == 0)
		free(drb);

	/* free ring. */
	free(r);
}

/*
 * allocate drbs for specified number of objects, put them into the ring.
 */
static struct rte_ring *
init_drb_ring(uint32_t num)
{
	uint32_t i, k, n;
	size_t sz, tsz;
	struct rte_ring *r;
	struct tle_drb *drb;

	/* allocate and initialise rte_ring. */

	n = rte_align32pow2(num);
	sz =  sizeof(*r) + n * sizeof(r->ring[0]);

	r = calloc(1, sz);
	if (r == NULL) {
		printf("%s:%d(%u) failed to allocate %zu bytes;\n",
			__func__, __LINE__, num, sz);
		return NULL;
	}

	rte_ring_init(r, __func__, n, 0);

	/* allocate drbs and put them into the ring. */

	tsz = sz;
	for (i = 0; i != num; i += k) {
		k =  rte_rand() % (UINT8_MAX + 1) + 1;
		k = RTE_MIN(k, num - i);
		sz = tle_drb_calc_size(k);
		drb = calloc(1, sz);
		if (drb == NULL) {
			printf("%s:%d(%u) %u-th iteration: "
				"failed to allocate %zu bytes;\n",
				__func__, __LINE__, num, i, sz);
			fini_drb_ring(r);
			return NULL;
		}
		drb->size = k;
		rte_ring_enqueue(r, drb);
		tsz += sz;
	}

	printf("%s(%u) total %zu bytes allocated, number of drbs: %u;\n",
		__func__, num, tsz, rte_ring_count(r));
	return r;
}

/*
 * Each enqueued object will contain:
 * [2-3]B: it's own sequence number.
 * [0-1]B: next object sequence number, or UINT16_MAX.
 */
static void
test_fill_obj(uintptr_t obj[], uint32_t num)
{
	uint32_t i;

	for (i = 0; i != num - 1; i++)
		obj[i] = i << 16 | (i + 1);

	obj[i] = i << 16 | UINT16_MAX;
}

static uint32_t
test_check_obj(uintptr_t obj[], uint32_t num)
{
	uint32_t i, h, l, oh, ol;

	h = obj[0] >> 16;
	l = obj[0] & UINT16_MAX;

	if (h + 1 != l && l != UINT16_MAX)
		return 0;

	if (l == UINT16_MAX)
		l = 0;

	for (i = 1; i != num; i++) {

		oh = obj[i] >> 16;
		ol = obj[i] & UINT16_MAX;

		if (l != oh || (oh + 1 != ol && ol != UINT16_MAX))
			return i;

		l = ol;
		if (l == UINT16_MAX)
			l = 0;
	}

	return num;
}

static int
test_dring_dequeue(struct tle_dring *dr, struct rte_ring *r, uint32_t num,
	int32_t type)
{
	uint32_t i, k, lc, n, t;
	struct tle_drb *drb[num];
	uintptr_t obj[num];

	lc = rte_lcore_id();
	k = num;

	/* dequeue objects. */
	if (type == SINGLE)
		n = tle_dring_sc_dequeue(dr, (const void **)obj, num, drb, &k);
	else if (type == MULTI)
		n = tle_dring_mc_dequeue(dr, (const void **)obj, num, drb, &k);
	else
		return -EINVAL;

	if (n == 0)
		return 0;

	/* check the data returned. */
	t = test_check_obj(obj, n);
	if (t != n) {
		printf("%s:%d(%p, %u) at lcore %u: invalid dequeued object, "
			"n=%u, idx=%u, obj=%#x, prev obj=%#x;\n",
			__func__, __LINE__, dr, num, lc, n, t,
			(uint32_t)obj[t], (t == 0) ? 0 : (uint32_t)obj[t - 1]);
		return -EFAULT;
	}

	/* check and free drbs. */
	for (i = 0; i != k; i++) {
		/* udata value for drb in use shouldn't be zero. */
		if (drb[i]->udata == NULL) {
			printf("error @ %s:%d(%p, %u) at lcore %u: "
				"erroneous drb@%p={udata=%p, size=%u,};\n",
				__func__, __LINE__, dr, num, lc, drb[i],
				drb[i]->udata, drb[i]->size);
			return -EFAULT;
		}
		drb[i]->udata = NULL;
		rte_ring_enqueue(r, drb[i]);
	}

	return n;
}

static int
test_dring_enqueue(struct tle_dring *dr, struct rte_ring *r, uint32_t num,
	int32_t type)
{
	uint32_t i, j, k, lc, nb;
	struct tle_drb *drb[num];
	uintptr_t obj[num];

	lc = rte_lcore_id();

	/* prepare drbs to enqueue up to *num* objects. */
	for (i = 0, j = 0; i != num; i += k, j++) {

		if (rte_ring_dequeue(r, (void **)&drb[j]) != 0)
			break;

		/* udata value for unused drb should be zero. */
		if (drb[j]->udata != NULL) {
			printf("error @ %s:%d(%p, %u) at lcore %u: "
				"erroneous drb@%p={udata=%p, size=%u,};\n",
				__func__, __LINE__, dr, num, lc, drb[j],
				drb[j]->udata, drb[j]->size);
			return -EFAULT;
		}

		/* update udata value with current lcore id. */
		drb[j]->udata = (void *)(uintptr_t)(lc + 1);
		k = drb[j]->size;
		k = RTE_MIN(k, num - i);
	}

	/* no free drbs left. */
	if (i == 0)
		return 0;

	/* fill objects to enqueue. */
	test_fill_obj(obj, i);

	/* enqueue into the dring. */
	nb = j;
	if (type == SINGLE)
		k = tle_dring_sp_enqueue(dr, (const void **)obj, i, drb, &nb);
	else if (type == MULTI)
		k = tle_dring_mp_enqueue(dr, (const void **)obj, i, drb, &nb);
	else
		return -EINVAL;

	if (k != i) {
		printf("%s:%d(%p, %p, %u): failed to enqueue %u objects;\n",
			__func__, __LINE__, dr, r, num, i);
	}

	/* free unused drbs */
	for (i = j - nb; i != j; i++) {
		if ((uintptr_t)drb[i]->udata != lc + 1) {
			printf("error @ %s:%d(%p, %u) at lcore %u: "
				"erroneous drb@%p={udata=%p, size=%u,};\n",
				__func__, __LINE__, dr, num, lc, drb[i],
				drb[i]->udata, drb[i]->size);
			return -EFAULT;
		}
		drb[i]->udata = NULL;
		rte_ring_enqueue(r, drb[i]);
	}

	return k;
}

static int
test_dring_enq_deq(struct dring_arg *arg)
{
	int32_t rc;
	uint32_t i, lc, n;

	rc = 0;
	arg->enq = 0;
	arg->deq = 0;
	lc = rte_lcore_id();

	for (i = 0; i != arg->iter; i++) {

		/* try to enqueue random number of objects. */
		if (arg->enq_type != NONE) {
			n = rte_rand() % (UINT8_MAX + 1);
			rc = test_dring_enqueue(arg->dr, arg->r, n,
				arg->enq_type);
			if (rc < 0)
				break;
			arg->enq += rc;
		}

		/* try to dequeue random number of objects. */
		if (arg->deq_type != NONE) {
			n = rte_rand() % (UINT8_MAX + 1);
			rc = test_dring_dequeue(arg->dr, arg->r, n,
				arg->deq_type);
			if (rc < 0)
				break;
			arg->deq += rc;
		}
	}

	if (rc < 0)
		return rc;

	/* dequeue remaining objects. */
	while (arg->deq_type != NONE && arg->enq != arg->deq) {

		/* try to dequeue random number of objects. */
		n = rte_rand() % (UINT8_MAX + 1) + 1;
		rc = test_dring_dequeue(arg->dr, arg->r, n, arg->deq_type);
		if (rc <= 0)
			break;
		arg->deq += rc;
	}

	printf("%s:%d(lcore=%u, enq_type=%d, deq_type=%d): "
		"%u objects enqueued, %u objects dequeued\n",
		__func__, __LINE__, lc, arg->enq_type, arg->deq_type,
		arg->enq, arg->deq);
	return 0;
}

/*
 * enqueue/dequeue by single thread.
 */
static int
test_dring_st(void)
{
	int32_t rc;
	struct rte_ring *r;
	struct tle_dring dr;
	struct dring_arg arg;

	printf("%s started;\n", __func__);

	tle_dring_reset(&dr);
	r = init_drb_ring(OBJ_NUM);
	if (r == NULL)
		return -ENOMEM;

	tle_dring_dump(stdout, 1, &dr);

	memset(&arg, 0, sizeof(arg));
	arg.dr = &dr;
	arg.r = r;
	arg.iter = ITER_NUM;
	arg.enq_type = SINGLE;
	arg.deq_type = SINGLE;
	rc = test_dring_enq_deq(&arg);

	rc = (rc != 0) ? rc : (arg.enq != arg.deq);
	printf("%s finished with status: %s(%d);\n",
		__func__, strerror(-rc), rc);

	tle_dring_dump(stdout, rc != 0, &dr);
	fini_drb_ring(r);

	return rc;
}

static int
test_dring_worker(void *arg)
{
	struct dring_arg *p;

	p = (struct dring_arg *)arg;
	return test_dring_enq_deq(p);
}

/*
 * enqueue/dequeue by multiple threads.
 */
static int
test_dring_mt(int32_t master_enq_type, int32_t master_deq_type,
	int32_t slave_enq_type, int32_t slave_deq_type)
{
	int32_t rc;
	uint32_t lc;
	uint64_t deq, enq;
	struct rte_ring *r;
	struct tle_dring dr;
	struct dring_arg arg[RTE_MAX_LCORE];

	tle_dring_reset(&dr);
	r = init_drb_ring(OBJ_NUM);
	if (r == NULL)
		return -ENOMEM;

	memset(arg, 0, sizeof(arg));

	/* launch on all slaves */
	RTE_LCORE_FOREACH_SLAVE(lc) {
		arg[lc].dr = &dr;
		arg[lc].r = r;
		arg[lc].iter = ITER_NUM;
		arg[lc].enq_type = slave_enq_type;
		arg[lc].deq_type = slave_deq_type;
		rte_eal_remote_launch(test_dring_worker, &arg[lc], lc);
	}

	/* launch on master */
	lc = rte_lcore_id();
	arg[lc].dr = &dr;
	arg[lc].r = r;
	arg[lc].iter = ITER_NUM;
	arg[lc].enq_type = master_enq_type;
	arg[lc].deq_type = master_deq_type;
	rc = test_dring_worker(&arg[lc]);
	enq = arg[lc].enq;
	deq = arg[lc].deq;

	/* wait for slaves. */
	RTE_LCORE_FOREACH_SLAVE(lc) {
		rc |= rte_eal_wait_lcore(lc);
		enq += arg[lc].enq;
		deq += arg[lc].deq;
	}

	printf("%s:%d: total %" PRIu64 " objects enqueued, %"
		PRIu64 " objects dequeued\n",
		__func__, __LINE__, enq, deq);

	rc = (rc != 0) ? rc : (enq != deq);
	if (rc != 0)
		tle_dring_dump(stdout, 1, &dr);

	fini_drb_ring(r);
	return rc;
}

static int
test_dring_mp_mc(void)
{
	int32_t rc;

	printf("%s started;\n", __func__);
	rc = test_dring_mt(MULTI, MULTI, MULTI, MULTI);
	printf("%s finished with status: %s(%d);\n",
		__func__, strerror(-rc), rc);
	return rc;
}

static int
test_dring_mp_sc(void)
{
	int32_t rc;

	printf("%s started;\n", __func__);
	rc = test_dring_mt(MULTI, SINGLE, MULTI, NONE);
	printf("%s finished with status: %s(%d);\n",
		__func__, strerror(-rc), rc);
	return rc;
}

static int
test_dring_sp_mc(void)
{
	int32_t rc;

	printf("%s started;\n", __func__);
	rc = test_dring_mt(SINGLE, MULTI, NONE, MULTI);
	printf("%s finished with status: %s(%d);\n",
		__func__, strerror(-rc), rc);
	return rc;
}

static int
test_dring(void)
{
	int32_t rc;

	rc = test_dring_st();
	if (rc != 0)
		return rc;

	rc = test_dring_mp_mc();
	if (rc != 0)
		return rc;

	rc = test_dring_mp_sc();
	if (rc != 0)
		return rc;

	rc = test_dring_sp_mc();
	if (rc != 0)
		return rc;

	return 0;
}

int
main(int argc, char *argv[])
{
	int32_t rc;

	rc = rte_eal_init(argc, argv);
	if (rc < 0)
		rte_exit(EXIT_FAILURE,
			"%s: rte_eal_init failed with error code: %d\n",
			__func__, rc);

	rc = test_dring();
	if (rc != 0)
		printf("TEST FAILED\n");
	else
		printf("TEST OK\n");

	return rc;
}