File: eq_test.c

package info (click to toggle)
mpich 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,828 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,468; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (642 lines) | stat: -rw-r--r-- 14,208 bytes parent folder | download | duplicates (4)
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
 * Copyright (c) 2013-2015 Intel Corporation.  All rights reserved.
 * Copyright (c) 2014-2016 Cisco Systems, Inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <poll.h>
#include <string.h>

#include <rdma/fi_errno.h>

#include "unit_common.h"
#include "shared.h"

static char err_buf[512];

static int
create_eq(size_t size, uint64_t flags, enum fi_wait_obj wait_obj)
{
	struct fi_eq_attr eq_attr;

	memset(&eq_attr, 0, sizeof(eq_attr));
	eq_attr.size = size;
	eq_attr.flags = flags;
	eq_attr.wait_obj = wait_obj;

	return fi_eq_open(fabric, &eq_attr, &eq, NULL);
}

/*
 * Tests:
 * - test open and close of EQ over a range of sizes
 */
static int
eq_open_close()
{
	int i;
	int ret;
	int size;
	int testret;

	testret = FAIL;

	for (i = 0; i < 17; ++i) {
		size = 1 << i;
		ret = create_eq(size, 0, FI_WAIT_UNSPEC);
		if (ret != 0) {
			sprintf(err_buf, "fi_eq_open(%d, 0, FI_WAIT_UNSPEC) = %d, %s",
					size, ret, fi_strerror(-ret));
			goto fail;
		}

		ret = fi_close(&eq->fid);
		if (ret != 0) {
			sprintf(err_buf, "close(eq) = %d, %s", ret, fi_strerror(-ret));
			goto fail;
		}
		eq = NULL;
	}
	testret = PASS;

fail:
	eq = NULL;
	return TEST_RET_VAL(ret, testret);
}

/*
 * Tests:
 * - writing to EQ
 * - reading from EQ with and without FI_PEEK
 * - underflow read
 */
static int
eq_write_read_self()
{
	struct fi_eq_entry entry;
	uint32_t event;
	int testret;
	int ret;
	int i;

	testret = FAIL;

	ret = create_eq(32, FI_WRITE, FI_WAIT_NONE);
	if (ret != 0) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	/* Insert some events */
	for (i = 0; i < 5; ++i) {
		if (i & 1) {
			entry.fid = &fabric->fid;
		} else {
			entry.fid = &eq->fid;
		}
		entry.context = (void *)(uintptr_t)i;
		ret = fi_eq_write(eq, FI_NOTIFY, &entry, sizeof(entry), 0);
		if (ret != sizeof(entry)) {
			sprintf(err_buf, "fi_eq_write ret=%d, %s", ret, fi_strerror(-ret));
			goto fail;
		}
	}

	/* Now read them back, peeking first at each one */
	for (i = 0; i < 10; ++i) {
		event = ~0;
		memset(&entry, 0, sizeof(entry));
		ret = fi_eq_read(eq, &event, &entry, sizeof(entry),
				(i & 1) ? 0 : FI_PEEK);
		if (ret != sizeof(entry)) {
			sprintf(err_buf, "fi_eq_read ret=%d, %s", ret, fi_strerror(-ret));
			goto fail;
		}

		if (event != FI_NOTIFY) {
			sprintf(err_buf, "iter %d: event = %d, should be %d\n", i, event,
					FI_NOTIFY);
			goto fail;
		}

		if ((int)(uintptr_t)entry.context != i / 2) {
			sprintf(err_buf, "iter %d: context mismatch %d != %d", i,
					(int)(uintptr_t)entry.context, i / 2);
			goto fail;
		}

		if (entry.fid != ((i & 2) ? &fabric->fid : &eq->fid)) {
			sprintf(err_buf, "iter %d: fid mismatch %p != %p", i,
					entry.fid, ((i & 2) ? &fabric->fid : &eq->fid));
			goto fail;
		}
	}

	/* queue is now empty */
	ret = fi_eq_read(eq, &event, &entry, sizeof(entry), 0);
	if (ret != -FI_EAGAIN) {
		sprintf(err_buf, "fi_eq_read of empty EQ returned %d", ret);
		goto fail;
	}
	testret = PASS;

fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

/*
 * Tests:
 * - eq size test
 */
static int
eq_size_verify()
{
	struct fi_eq_entry entry;
	int testret;
	int ret;
	int i;

	testret = FAIL;

	ret = create_eq(32, FI_WRITE, FI_WAIT_NONE);
	if (ret != 0) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	/* Insert some events */
	for (i = 0; i < 32; ++i) {
		entry.fid = &fabric->fid;
		entry.context = (void *)(uintptr_t)i;
		ret = fi_eq_write(eq, FI_NOTIFY, &entry, sizeof(entry), 0);
		if (ret != sizeof(entry)) {
			sprintf(err_buf, "fi_eq_write ret=%d, %s", ret, fi_strerror(-ret));
			goto fail;
		}
	}

	testret = PASS;

fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

/*
 * Tests:
 * - extracting FD from EQ with FI_WAIT_FD
 * - wait on fd with nothing pending
 * - wait on fd with event pending
 */
static int
eq_wait_fd_poll()
{
	int fd;
	struct fi_eq_entry entry;
	struct pollfd pfd;
	struct fid *fids[1];
	int testret;
	int ret;

	testret = FAIL;

	ret = create_eq(32, FI_WRITE, FI_WAIT_FD);
	if (ret != 0) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	ret = fi_control(&eq->fid, FI_GETWAIT, &fd);
	if (ret != 0) {
		sprintf(err_buf, "fi_control ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	fids[0] = &eq->fid;
	if ((ret = fi_trywait(fabric, fids, 1)) != FI_SUCCESS) {
		sprintf(err_buf, "fi_trywait ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	pfd.fd = fd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 0);
	if (ret < 0) {
		sprintf(err_buf, "poll errno=%d, %s", errno, fi_strerror(-errno));
		goto fail;
	}
	if (ret > 0) {
		sprintf(err_buf, "poll returned %d, should be 0", ret);
		goto fail;
	}

	/* write an event */
	entry.fid = &eq->fid;
	entry.context = eq;
	ret = fi_eq_write(eq, FI_NOTIFY, &entry, sizeof(entry), 0);
	if (ret != sizeof(entry)) {
		sprintf(err_buf, "fi_eq_write ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	pfd.fd = fd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 0);
	if (ret < 0) {
		sprintf(err_buf, "poll errno=%d, %s", errno, fi_strerror(-errno));
		goto fail;
	}
	if (ret != 1) {
		sprintf(err_buf, "poll returned %d, should be 1", ret);
		goto fail;
	}

	testret = PASS;
fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

static int insert_events(size_t count)
{
	struct fi_eq_entry entry;
	size_t i;
	int ret;

	for (i = 0; i < count; i++) {
		ret = fi_eq_write(eq, FI_NOTIFY, &entry, sizeof(entry), 0);
		if (ret != sizeof(entry)) {
			sprintf(err_buf, "fi_eq_write ret=%d, %s", ret,
					fi_strerror(-ret));
			return ret;
		}
	}

	return FI_SUCCESS;
}

static int read_events(size_t count, uint64_t flags)
{
	struct fi_eq_entry entry;
	uint32_t event;
	size_t i;
	int ret;

	for (i = 0; i < count; i++) {
		memset(&entry, 0, sizeof(entry));
		event = 0;

		ret = fi_eq_read(eq, &event, &entry, sizeof(entry), flags);
		if (ret != sizeof(entry)) {
			sprintf(err_buf, "fi_eq_read ret=%d, %s", ret,
					fi_strerror(-ret));
			return ret;
		}

		if (event != FI_NOTIFY) {
			sprintf(err_buf, "iter %zu: event = %d, should be %d\n",
					i, event, FI_NOTIFY);
			return -FI_EOTHER;
		}
	}

	return FI_SUCCESS;
}

static int sread_event(int timeout, uint64_t flags)
{
	struct fi_eq_entry entry;
	int64_t elapsed;
	uint32_t event;
	int ret;

	ft_start();
	ret = fi_eq_sread(eq, &event, &entry, sizeof(entry), timeout, flags);
	ft_stop();
	if (ret != sizeof(entry)) {
		sprintf(err_buf, "fi_eq_sread returned %d, %s", ret,
				fi_strerror(-ret));
		return ret;
	}

	/* check timeout accuracy */
	elapsed = get_elapsed(&start, &end, MILLI);
	if (elapsed > (int) (timeout * 1.25)) {
		sprintf(err_buf, "fi_eq_sread slept %d ms, expected %d",
				(int) elapsed, timeout);
		return -FI_EOTHER;
	}

	return FI_SUCCESS;
}

/* Make sure the peeking works fine on normal read. */
static int eq_wait_read_peek(void)
{
	int testret;
	int ret;

	testret = FAIL;

	ret = create_eq(32, FI_WRITE, FI_WAIT_NONE);
	if (ret) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret,
				fi_strerror(-ret));
		goto fail;
	}

	ret = insert_events(5);
	if (ret)
		goto fail;

	ret = read_events(5, FI_PEEK);
	if (ret)
		goto fail;

	ret = read_events(5, 0);
	if (ret)
		goto fail;

	testret = PASS;
fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

/* Check that the peeking doesn't affect the waiting. If the peek invalidly
 * clears the FD, then this will fail.
 */
static int eq_wait_sread_peek(void)
{
	int testret;
	int ret;

	testret = FAIL;

	ret = create_eq(32, FI_WRITE, FI_WAIT_FD);
	if (ret) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret,
				fi_strerror(-ret));
		goto fail;
	}

	/* Write an event */
	ret = insert_events(1);
	if (ret)
		goto fail;

	/* Make sure we can read the event */
	ret = sread_event(2000, 0);
	if (ret)
		goto fail;

	/* Write another event */
	ret = insert_events(1);
	if (ret)
		goto fail;

	/* Peek at the event */
	ret = sread_event(2000, FI_PEEK);
	if (ret)
		goto fail;

	/* Make sure the event is still available for reading */
	ret = sread_event(2000, 0);
	if (ret)
		goto fail;

	testret = PASS;
fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

/*
 * Tests:
 * - sread with event pending
 * - sread with no event pending
 */
static int
eq_wait_fd_sread()
{
	struct fi_eq_entry entry;
	uint32_t event;
	int64_t elapsed;
	int testret;
	int ret;

	testret = FAIL;

	ret = create_eq(32, FI_WRITE, FI_WAIT_FD);
	if (ret != 0) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	/* timed sread on empty EQ, 2s timeout */
	ft_start();
	ret = fi_eq_sread(eq, &event, &entry, sizeof(entry), 2000, 0);
	ft_stop();
	if (ret != -FI_EAGAIN) {
		sprintf(err_buf, "fi_eq_read of empty EQ returned %d", ret);
		goto fail;
	}

	/* check timeout accuracy */
	elapsed = get_elapsed(&start, &end, MILLI);
	if (elapsed < 1500 || elapsed > 2500) {
		sprintf(err_buf, "fi_eq_sread slept %d ms, expected 2000",
				(int)elapsed);
		goto fail;
	}

	/* write an event */
	entry.fid = &eq->fid;
	entry.context = eq;
	ret = fi_eq_write(eq, FI_NOTIFY, &entry, sizeof(entry), 0);
	if (ret != sizeof(entry)) {
		sprintf(err_buf, "fi_eq_write ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	event = ~0;
	memset(&entry, 0, sizeof(entry));
	/* timed sread on EQ with event, 2s timeout */
	ft_start();
	ret = fi_eq_sread(eq, &event, &entry, sizeof(entry), 2000, 0);
	ft_stop();
	if (ret != sizeof(entry)) {
		sprintf(err_buf, "fi_eq_read ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	/* check that no undue waiting occurred */
	elapsed = get_elapsed(&start, &end, MILLI);
	if (elapsed > 5) {
		sprintf(err_buf, "fi_eq_sread slept %d ms, expected immediate return",
				(int)elapsed);
		goto fail;
	}

	if (event != FI_NOTIFY) {
		sprintf(err_buf, "fi_eq_sread: event = %d, should be %d\n", event,
				FI_NOTIFY);
		goto fail;
	}
	if (entry.fid != &eq->fid) {
		sprintf(err_buf, "fi_eq_sread: fid mismatch: %p should be %p\n",
				entry.fid, &eq->fid);
		goto fail;
	}
	if (entry.context != eq) {
		sprintf(err_buf, "fi_eq_sread: context mismatch: %p should be %p\n",
				entry.context, eq);
		goto fail;
	}

	testret = PASS;
fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

static int
eq_readerr_ret_eagain()
{
	int testret;
	int ret;
	struct fi_eq_err_entry err_entry;

	testret = FAIL;

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

	/* create EQ  */
	ret = create_eq(32, FI_WRITE, FI_WAIT_UNSPEC);
	if (ret) {
		sprintf(err_buf, "fi_eq_open ret=%d, %s", ret, fi_strerror(-ret));
		goto fail;
	}

	ret = insert_events(5);
	if (ret)
		goto fail;

	ret = read_events(5, 0);
	if (ret)
		goto fail;

	/* Error handling  */
	ret = fi_eq_readerr(eq, &err_entry, 0);
	if (ret == -FI_EAGAIN)
		testret = PASS;
	else
		sprintf(err_buf, ", fi_eq_readerr returned: %d: %s, ", ret,
				fi_eq_strerror(eq, err_entry.prov_errno, err_entry.err_data, NULL, 0));

fail:
	FT_CLOSE_FID(eq);
	return TEST_RET_VAL(ret, testret);
}

struct test_entry test_array[] = {
	TEST_ENTRY(eq_open_close, "Test open and close of EQ for various sizes"),
	TEST_ENTRY(eq_write_read_self, "Test writing and reading to EQ"),
	TEST_ENTRY(eq_size_verify, "Test EQ supports writing # of entries = size"),
	TEST_ENTRY(eq_wait_fd_poll, "Test polling on fd extracted from EQ"),
	TEST_ENTRY(eq_wait_fd_sread, "Test EQ sread"),
	TEST_ENTRY(eq_wait_read_peek, "Test EQ read with FI_PEEK"),
	TEST_ENTRY(eq_wait_sread_peek, "Test EQ sread with FI_PEEK"),
	TEST_ENTRY(eq_readerr_ret_eagain, "Test EQ readerr with FI_EAGAIN"),
	{ NULL, "" }
};

static void usage(char *name)
{
	ft_unit_usage(name, "Unit test for Event Queue (EQ)");
}

int main(int argc, char **argv)
{
	int op, ret;
	int failed;

	hints = fi_allocinfo();
	if (!hints)
		return EXIT_FAILURE;

	while ((op = getopt(argc, argv, FAB_OPTS "h")) != -1) {
		switch (op) {
		default:
			ft_parseinfo(op, optarg, hints, &opts);
			break;
		case '?':
		case 'h':
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}

	hints->mode = FI_CONTEXT | FI_CONTEXT2 | FI_MSG_PREFIX | FI_ASYNC_IOV |
		FI_RX_CQ_DATA | FI_NOTIFY_FLAGS_ONLY | FI_RESTRICTED_COMP |
		FI_BUFFERED_RECV;
	hints->domain_attr->mode = FI_RESTRICTED_COMP;
	hints->domain_attr->mr_mode = ~(FI_MR_BASIC | FI_MR_SCALABLE);

	ret = fi_getinfo(FT_FIVERSION, NULL, 0, 0, hints, &fi);
	if (ret) {
		FT_PRINTERR("fi_getinfo", ret);
		goto err;
	}

	ret = fi_fabric(fi->fabric_attr, &fabric, NULL);
	if (ret) {
		FT_PRINTERR("fi_getinfo", ret);
		goto err;
	}

	printf("Testing EQs on fabric %s\n", fi->fabric_attr->name);

	failed = run_tests(test_array, err_buf);
	if (failed > 0) {
		printf("Summary: %d tests failed\n", failed);
	} else {
		printf("Summary: all tests passed\n");
	}

err:
	ft_free_res();
	return ret ? ft_exit_code(ret) : (failed > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
}