File: mr_cache_evict.c

package info (click to toggle)
libfabric 2.1.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,108 kB
  • sloc: ansic: 387,262; python: 3,171; sh: 2,555; makefile: 1,313; cpp: 617; perl: 474; ruby: 123; asm: 27
file content (850 lines) | stat: -rw-r--r-- 20,933 bytes parent folder | download
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
 * (C) Copyright 2020 Hewlett Packard Enterprise Development LP
 *
 * 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 <unistd.h>
#include <stddef.h>
#include <sys/mman.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <stdio.h>
#include <malloc.h>

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

/* Supported memory region types. */
enum alloc_type {
	MMAP,
	BRK,
	SBRK,
	CUDA,
	ROCR,
};

static void *reuse_addr = NULL;
static char err_buf[512];
static size_t mr_buf_size = 16384;

/* Given a time value, determine the expected cached time value. The assumption
 * is the cache value should at least have a CACHE_IMPROVEMENT_PERCENT time
 * improvement over the original time value.
 */
#define CACHE_IMPROVEMENT_PERCENT 80
#define CACHE_TIME_MAX_VALUE(time) ((time) / 100 * \
				    (100 - CACHE_IMPROVEMENT_PERCENT))

#define PAGEMAP_ENTRY_SIZE 8
#define PAGEMAP_PFN_PRESENT (1ULL << 63)
#define PAGEMAP_PFN_MASK ((1ULL << 55) - 1)

/* Function used to get physical address from a virtual address. Must be root
 * to read pagemap.
 */
static int virt_to_phys(const void *va_addr, uint64_t *phy_addr)
{
	int fd;
	int ret;
	uint64_t entry;
	ssize_t read_size;
	off_t seek_ret;
	off_t seek_offset;
	int page_size;

	fd = open("/proc/self/pagemap", O_RDONLY);
	if (fd == -1) {
		ret = -errno;
		FT_UNIT_STRERR(err_buf, "open of /proc/self/pagemap failed",
			       ret);
		return ret;
	}

	page_size = sysconf(_SC_PAGESIZE);
	if (page_size == -1) {
		ret = -errno;
		FT_UNIT_STRERR(err_buf, "sysconf(_SC_PAGESIZE) failed", ret);
		goto out;
	}

	/* Map virtual address to offset in pagemap. */
	seek_offset = (uintptr_t) va_addr / page_size * PAGEMAP_ENTRY_SIZE;

	seek_ret = lseek(fd, seek_offset, SEEK_SET);
	if (seek_ret == -1) {
		ret = -errno;
		FT_UNIT_STRERR(err_buf, "lseek failed", ret);
		goto out;
	}

	read_size = read(fd, &entry, sizeof(entry));
	if (read_size == -1) {
		ret = -errno;
		FT_UNIT_STRERR(err_buf, "read failed", ret);
		goto out;
	} else if (read_size != sizeof(entry)) {
		ret = -ENOSPC;
		FT_UNIT_STRERR(err_buf, "short read", ret);
		goto out;
	}

	if (entry & PAGEMAP_PFN_PRESENT) {
		ret = 0;
		*phy_addr = (entry & PAGEMAP_PFN_MASK) * page_size;
	} else {
		ret = -EFAULT;
		FT_UNIT_STRERR(err_buf, "Failed to find physical address", ret);
	}

out:
	close(fd);

	return ret;
}

/* Sbrk/brk allocations are only intended to support a single outstanding
 * allocation at a time. Extra handling of the program break is needed to make
 * sbrk/brk allocations more flexible including making allocations thread safe.
 */
static void sbrk_free(void *ptr)
{
	void *cur_brk = (void *) ((uint64_t) ptr + mr_buf_size);
	void *rewind_brk = ptr;

	FT_DEBUG("Resetting program break from %p to %p", cur_brk, rewind_brk);
	cur_brk = sbrk(-(intptr_t) mr_buf_size);
	if (cur_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return;
	}

	/* Verify the program break was reset to the expected location. */
	cur_brk = sbrk(0);
	if (cur_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return;
	}

	if (cur_brk != rewind_brk)
		FT_UNIT_STRERR(err_buf, "Failed to reset program break",
			       -ENOMEM);
}

static void *sbrk_alloc(void)
{
	void *prev_brk;
	void *cur_brk;

	prev_brk = sbrk((intptr_t) mr_buf_size);
	if (prev_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return NULL;
	}

	/* Determine the size of the newly allocated buffer. If this operation
	 * fails, memory is leaked.
	 */
	cur_brk = sbrk(0);
	if (cur_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return NULL;
	}

	FT_DEBUG("Moved program break from %p to %p", prev_brk, cur_brk);

	return prev_brk;
}

static void brk_free(void *ptr)
{
	void *cur_brk = (void *) ((uint64_t) ptr + mr_buf_size);
	void *rewind_brk = ptr;
	int ret;

	FT_DEBUG("Resetting program break from %p to %p", cur_brk, rewind_brk);
	ret = brk(rewind_brk);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "brk failed", -errno);
		return;
	}

	/* Verify the program break was reset the the expected location. */
	cur_brk = sbrk(0);
	if (cur_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return;
	}

	if (cur_brk != rewind_brk)
		FT_UNIT_STRERR(err_buf, "Failed to reset program break",
			       -ENOMEM);
}

static void *brk_alloc(void)
{
	void *prev_brk;
	void *cur_brk;
	int ret;

	/* Use sbrk to determine the current program break. This is needed to
	 * determine the brk allocation size.
	 */
	prev_brk = sbrk(0);
	if (prev_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return NULL;
	}

	cur_brk = (void *) ((intptr_t) prev_brk + mr_buf_size);
	ret = brk(cur_brk);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "brk failed", -errno);
		return NULL;
	}

	/* Determine the size of the newly allocated buffer. If this operation
	 * fails, memory is leaked.
	 */
	cur_brk = sbrk(0);
	if (cur_brk == (void *) -1) {
		FT_UNIT_STRERR(err_buf, "sbrk failed", -errno);
		return NULL;
	}

	FT_DEBUG("Moved program break from %p to %p", prev_brk, cur_brk);

	return prev_brk;
}

/* Mmap allocations are only intended to support a single outstanding
 * allocation at a time. Extra handling of the mmap reuse address needs to occur
 * to make mmap allocations more flexible including making allocations thread
 * safe.
 */
static void mmap_free(void *ptr)
{
	if (munmap(ptr, mr_buf_size))
		FT_UNIT_STRERR(err_buf, "munmap failed", -errno);
}

static void *mmap_alloc(void)
{
	void *ptr;
	int flags = MAP_ANONYMOUS | MAP_PRIVATE;

	/* If a reuse address is defined, request MAP_FIXED to require the mmap
	 * allocation to reuse this address.
	 */
	if (reuse_addr)
		flags |= MAP_FIXED;

	ptr = mmap(reuse_addr, mr_buf_size, PROT_READ | PROT_WRITE, flags, -1,
		   0);
	if (ptr == MAP_FAILED) {
		FT_UNIT_STRERR(err_buf, "mmap failed", -errno);
		return NULL;
	}

	/* Cache this virtual address to reuse for future allocations. */
	reuse_addr = ptr;

	return ptr;
}

static void rocr_free(void *ptr)
{
	ft_hmem_free(FI_HMEM_ROCR, ptr);
}

static void *rocr_malloc(void)
{
	int ret;
	void *ptr;

	ret = ft_hmem_alloc(FI_HMEM_ROCR, 0, &ptr, mr_buf_size);
	if (ret)
		return NULL;
	return ptr;
}


static void cuda_free(void *ptr)
{
	ft_hmem_free(FI_HMEM_CUDA, ptr);
}

static void *cuda_malloc(void)
{
	int ret;
	void *ptr;

	ret = ft_hmem_alloc(FI_HMEM_CUDA, 0, &ptr, mr_buf_size);
	if (ret)
		return NULL;
	return ptr;
}

/* Generic allocation/deallocation function. Only a single allocation of any
 * type should be outstanding.
 */
static void mem_free(void *ptr, enum alloc_type type)
{
	switch (type) {
	case SBRK:
		sbrk_free(ptr);
		break;
	case MMAP:
		mmap_free(ptr);
		break;
	case BRK:
		brk_free(ptr);
		break;
	case CUDA:
		cuda_free(ptr);
		break;
	case ROCR:
		rocr_free(ptr);
		break;
	default:
		return;
	}

	FT_DEBUG("Memory freed: va=%p", ptr);
}

static enum fi_hmem_iface alloc_type_to_iface(enum alloc_type type)
{
	switch (type) {
	case CUDA:
		return FI_HMEM_CUDA;
	case ROCR:
		return FI_HMEM_ROCR;
	default:
		return FI_HMEM_SYSTEM;
	}
}

/* User defined global mr_buf_size controls allocation size. */
static void *mem_alloc(enum alloc_type type)
{
	uint64_t phys_addr = 0;
	void *ptr;
	int ret;

	switch (type) {
	case SBRK:
		ptr = sbrk_alloc();
		break;
	case MMAP:
		ptr = mmap_alloc();
		break;
	case BRK:
		ptr = brk_alloc();
		break;
	case CUDA:
		ptr = cuda_malloc();
		break;
	case ROCR:
		ptr = rocr_malloc();
		break;
	default:
		return NULL;
	}

	if (ptr) {
		if (geteuid() == 0 &&
		    alloc_type_to_iface(type) == FI_HMEM_SYSTEM) {
			/* Perform a write to the buffer to ensure the kernel
			 * has faulted in a page for this allocation. This will
			 * help prevent virt_to_phys() from returning an error
			 * due to no PFN.
			 */
			*(uint8_t *) ptr = 0;
			ret = virt_to_phys(ptr, &phys_addr);
			if (ret)
				FT_DEBUG("virt_to_phys() failed: %s",
					 fi_strerror(-ret));
		}

		FT_DEBUG("Memory allocated: va=%p size=%lu phys_addr=0x%lx",
			 ptr, mr_buf_size, phys_addr);
	}

	return ptr;
}

/* MR registration function which returns the MR and the elapsed time, in
 * nanoseconds, to register the MR.
 */
static int mr_register(const void *buf, struct fid_mr **mr, int64_t *elapsed,
		       enum fi_hmem_iface iface)
{
	int ret;
	const struct iovec iov = {
		.iov_base = (void *) buf,
		.iov_len = mr_buf_size,
	};
	struct fi_mr_attr mr_attr = {
		.mr_iov = &iov,
		.iov_count = 1,
		.access = ft_info_to_mr_access(fi),
		.requested_key = FT_MR_KEY,
		.iface = iface,
	};

	ft_start();
	ret = fi_mr_regattr(domain, &mr_attr, 0, mr);
	ft_stop();

	if (ret != FI_SUCCESS) {
		FT_UNIT_STRERR(err_buf, "fi_mr_reg failed", -errno);
		return -errno;
	}

	*elapsed = get_elapsed(&start, &end, NANO);

	return 0;
}

/* Run a test verifing the eviction MR cache entries. The following is how the
 * test works:
 * 1. Prime CPU caches by registering a priming MR. This MR is not used for
 *    cache measurements.
 *
 * 2. Allocate a buffer using mem_alloc() with either MMAP, BRK, or SBRK. The
 *    mem_alloc() allocator is constructed to return the same virtual address
 *    during buffer reallocation.
 *
 * 3. Measure MR registration time of the mem_alloc() buffer. Since this buffer
 *    has not been previously registered, the elapsed time for this MR
 *    registration should be long. This is referred to as the initial MR
 *    registration time.
 *
 * 4. Measure MR registration time of the mem_alloc() buffer again. Since this
 *    buffer has been previously registered, the elapsed time for this MR
 *    registration should be significantly less than the initial MR registration
 *    time. If the registration time is not significantly less, it is assumed
 *    the provider does not support MR caching, and the test will exit. This
 *    elapsed time is referred to as the cached MR registration time.
 *
 * 5. If the provider supports caching, the mem_alloc() buffer is freed and
 *    reallocated. Measures are inplace to have the reallocated mem_alloc()
 *    buffer return the same virtual address. During this this time, the
 *    provider's MR cache should experience an eviction.
 *
 * 6. Measure MR registration time of the mem_alloc() buffer a third time. Since
 *    the provider should have experienced a MR cache eviction, the elapsed time
 *    for this MR registration should not be significantly less than the initial
 *    MR registration time. If this allocation is significantly less, it is
 *    assumed this MR registration incorrectly found a cached MR entry. This
 *    elapsed time is referred to as the reallocated MR registration time.
 */
static int mr_cache_test(enum alloc_type type)
{
	void *prime_buf = NULL;
	struct fid_mr *prime_mr = NULL;
	void *buf = NULL;
	struct fid_mr *mr = NULL;
	int64_t mr_reg_time;
	struct fid_mr *cached_mr = NULL;
	int64_t cached_mr_reg_time;
	struct fid_mr *realloc_mr = NULL;
	int64_t realloc_mr_reg_time;
	int ret;
	void *prev_buf;
	int testret = FAIL;
	enum fi_hmem_iface iface = alloc_type_to_iface(type);

	/* Reallocate the domain to reset the MR cache. */
	if (!domain) {
		ret = -EINVAL;
		FT_UNIT_STRERR(err_buf, "no domain allocated", ret);
		goto cleanup;
	}

	ret = fi_close(&domain->fid);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "Failed to close the domain", ret);
		domain = NULL;
		goto cleanup;
	}

	ret = fi_domain(fabric, fi, &domain, NULL);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "fi_domain failed", ret);
		domain = NULL;
		goto cleanup;
	}

	/* A priming MR registration is used to ensure the first timed MR
	 * registration does not take into account the setting up of CPU caches.
	 */
	switch (iface) {
	case FI_HMEM_CUDA:
		prime_buf = cuda_malloc();
		if (!prime_buf) {
			ret = -ENOMEM;
			FT_UNIT_STRERR(err_buf, "cuda_malloc failed", ret);
			goto cleanup;
		}
		break;

	case FI_HMEM_ROCR:
		prime_buf = rocr_malloc();
		if (!prime_buf) {
			ret = -ENOMEM;
			FT_UNIT_STRERR(err_buf, "rocr_malloc failed", ret);
			goto cleanup;
		}
		break;

	/* Add test case for this once neuron has a memory monitor cache. */
	case FI_HMEM_NEURON:
		ret = -FI_ENOSYS;
		goto cleanup;
		break;

	default:
		prime_buf = malloc(mr_buf_size);
		if (!prime_buf) {
			ret = -ENOMEM;
			FT_UNIT_STRERR(err_buf, "malloc failed", ret);
			goto cleanup;
		}
		break;
	}

	ret = mr_register(prime_buf, &prime_mr, &mr_reg_time, iface);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "mr_register failed", ret);
		goto cleanup;
	}

	/* Perform initial MR registration. MR registration elapsed time is
	 * recorded for future comparision.
	 */
	buf = mem_alloc(type);
	if (!buf) {
		ret = -ENOMEM;
		FT_UNIT_STRERR(err_buf, "mem_alloc failed", ret);
		goto cleanup;
	}

	ret = mr_register(buf, &mr, &mr_reg_time, iface);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "mr_register failed", ret);
		goto cleanup;
	}

	FT_DEBUG("Initial MR registration time: %ld nsecs", mr_reg_time);

	/* Perform another allocation using the same buffer. This should hit the
	 * MR cache.
	 */
	ret = mr_register(buf, &cached_mr, &cached_mr_reg_time, iface);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "mr_register failed", ret);
		goto cleanup;
	}

	FT_DEBUG("Cached MR registration time: %ld nsecs", cached_mr_reg_time);

	/* If cached allocation is not within the expected duration, assume the
	 * provider does not support MR caching.
	 */
	if (cached_mr_reg_time > CACHE_TIME_MAX_VALUE(mr_reg_time)) {
		ret = -FI_ENOSYS;
		sprintf(err_buf, "Assuming MR cache not enabled by provider");
		goto cleanup;
	}

	/* Free the buffer without freeing the MR. This should result in the MR
	 * cache evicting/invalidating the MR entry. The buffer will then be
	 * reallocated and re-registered. The newly registered MR should not
	 * have been cached.
	 */
	prev_buf = buf;
	mem_free(buf, type);

	buf = mem_alloc(type);
	if (!buf) {
		ret = -ENOMEM;
		FT_UNIT_STRERR(err_buf, "mem_alloc failed", ret);
		goto cleanup;
	}

	/* We NEED the same pointer to be returned for this test to be valid. */
	if (buf != prev_buf) {
		ret = -EFAULT;
		FT_UNIT_STRERR(err_buf,
			       "Failed to reallocate same virtual address",
			       ret);
		goto cleanup;
	}

	/* Verify reallocated MR registration time is close to the initial MR
	 * registration time and greater than the cached MR registration time.
	 */
	ret = mr_register(buf, &realloc_mr, &realloc_mr_reg_time, iface);
	if (ret) {
		FT_UNIT_STRERR(err_buf, "mr_register failed", ret);
		goto cleanup;
	}

	FT_DEBUG("Reallocated MR registration time: %ld nsecs",
		 realloc_mr_reg_time);

	if (realloc_mr_reg_time <= CACHE_TIME_MAX_VALUE(mr_reg_time)) {
		ret = -EEXIST;
		FT_UNIT_STRERR(err_buf,
			       "Reallocated MR registration time too low. "
			       "Cached MR may have been incorrectly used.",
			       ret);
	} else {
		testret = PASS;
	}

cleanup:
	if (realloc_mr)
		FT_CLOSE_FID(realloc_mr);

	if (cached_mr)
		FT_CLOSE_FID(cached_mr);

	if (mr)
		FT_CLOSE_FID(mr);

	if (buf)
		mem_free(buf, type);

	if (prime_mr)
		FT_CLOSE_FID(prime_mr);

	if (prime_buf) {
		switch (iface) {
		case FI_HMEM_CUDA:
			cuda_free(prime_buf);
			break;

		case FI_HMEM_ROCR:
			rocr_free(prime_buf);
			break;

		default:
			free(prime_buf);
			break;
		}
	}

	return TEST_RET_VAL(ret, testret);
}

/* Run tests using MMAP, BRK, and SBRK. */
static int mr_cache_mmap_test(void)
{
	return mr_cache_test(MMAP);
}

static int mr_cache_brk_test(void)
{
	return mr_cache_test(BRK);
}

static int mr_cache_sbrk_test(void)
{
	return mr_cache_test(SBRK);
}

static int mr_cache_cuda_test(void)
{
	int ret;

	if (!(opts.options & FT_OPT_ENABLE_HMEM)) {
		sprintf(err_buf, "FI_HMEM support not requested");
		return SKIPPED;
	}

	ret = ft_hmem_init(FI_HMEM_CUDA);
	if (ret) {
		sprintf(err_buf, "ft_hmem_init(FI_HMEM_CUDA) failed");
		return TEST_RET_VAL(ret, FAIL);
	}

	ret = mr_cache_test(CUDA);

	ft_hmem_cleanup(FI_HMEM_CUDA);

	return ret;
}

static int mr_cache_rocr_test(void)
{
	int ret;

	if (!(opts.options & FT_OPT_ENABLE_HMEM)) {
		sprintf(err_buf, "FI_HMEM support not requested");
		return SKIPPED;
	}

	ret = ft_hmem_init(FI_HMEM_ROCR);
	if (ret) {
		sprintf(err_buf, "ft_hmem_init(FI_HMEM_ROCR) failed");
		return TEST_RET_VAL(ret, FAIL);
	}

	ret = mr_cache_test(ROCR);

	ft_hmem_cleanup(FI_HMEM_ROCR);

	return ret;
}

struct test_entry test_array[] = {
	TEST_ENTRY(mr_cache_mmap_test, "MR cache eviction test using MMAP"),
	TEST_ENTRY(mr_cache_brk_test, "MR cache eviction test using BRK"),
	TEST_ENTRY(mr_cache_sbrk_test, "MR cache eviction test using SBRK"),
	TEST_ENTRY(mr_cache_cuda_test, "MR cache eviction test using CUDA"),
	TEST_ENTRY(mr_cache_rocr_test, "MR cache eviction test using ROCR"),
	{ NULL, "" }
};

static void usage(char *name)
{
	ft_unit_usage(name,
		"Test a provider's ability to evict MR cache entries.\n"
		"Evictions are verified using MMAP, BRK, SBRK, CUDA and ROCR\n"
		"allocations. FI_HMEM support must be enabled to run CUDA and\n"
		"ROCR tests.\n\n"
		"With debug enabled, when running as root, the physical \n"
		"address of the first page of the MMAP, BRK, and SBRK \n"
		"allocation is returned. This can be used to verify the \n"
		"underlying physical memory changes between MMAP, BRK, and \n"
		"SBRK allocations. When running as non-root, the reported \n"
		"physical address is always zero.");
	FT_PRINT_OPTS_USAGE("-s <bytes>", "Memory region size to be tested.");
	FT_PRINT_OPTS_USAGE("-H", "Enable provider FI_HMEM support");
}

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

	/* Force malloc to use mmap by setting M_MMAP_THRESHOLD to 1. This
	 * allows for this application to control the program break. Note that
	 * not all operating systems may support this call. Thus, failure of
	 * mallopt() is not treated as an error. But, this could impact the
	 * results of the test.
	 */
	ret = mallopt(M_MMAP_THRESHOLD, 1);
	if (ret != 1)
		FT_PRINTERR("Failed to set M_MMAP_THRESHOLD to 1. "
			    "System may not support M_MMAP_THRESHOLD. "
			    "Proceeding with test.", -EINVAL);

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

	while ((op = getopt(argc, argv, FAB_OPTS "h" "s:")) != -1) {
		switch (op) {
		default:
			ft_parseinfo(op, optarg, hints, &opts);
			break;
		case 's':
			errno = 0;
			mr_buf_size = strtoul(optarg, NULL, 10);
			if (mr_buf_size == 0)
				ret = -EINVAL;
			else if (mr_buf_size == ULONG_MAX && errno)
				ret = -errno;
			else
				ret = 0;

			if (ret) {
				FT_PRINTERR("Invalid memory region size", ret);
				goto out;
			}
			break;
		case '?':
		case 'h':
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}

	hints->mode = ~0;
	hints->domain_attr->mode = ~0;
	hints->domain_attr->mr_mode = ~OFI_MR_DEPRECATED;
	hints->caps |= FI_MSG | FI_RMA;

	if (opts.options & FT_OPT_ENABLE_HMEM)
		hints->caps |= FI_HMEM;

	ret = fi_getinfo(FT_FIVERSION, NULL, 0, 0, hints, &fi);
	if (ret) {
		hints->caps &= ~FI_RMA;
		ret = fi_getinfo(FT_FIVERSION, NULL, 0, 0, hints, &fi);
		if (ret) {
			FT_PRINTERR("fi_getinfo", ret);
			goto out;
		}
	}

	if (!ft_info_to_mr_access(fi))
		goto out;

	if (fi->domain_attr->mr_iov_limit == 0) {
		ret = -EINVAL;
		FT_PRINTERR("mr_iov_limit not set", ret);
		goto out;
	}

	ret = ft_open_fabric_res();
	if (ret)
		goto out;

	printf("Testing MR cache on fabric %s domain %s\n",
	       fi->fabric_attr->name, fi->domain_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");

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