int nccl_ofi_mr_cache_insert_entry()

in src/nccl_ofi_mr.cpp [150:224]


int nccl_ofi_mr_cache_insert_entry(nccl_ofi_mr_cache_t *cache,
				   nccl_ofi_mr_ckey_ref ckey,
				   void *handle)
{
	uintptr_t page_addr;
	size_t pages;
	int ret = 0;

	compute_page_address((uintptr_t)nccl_ofi_mr_ckey_baseaddr(ckey),
	                     nccl_ofi_mr_ckey_len(ckey),
	                     (uintptr_t)cache->system_page_size,
	                     &page_addr,
	                     &pages);

	for (size_t slot = 0;; slot++) {
		assert(slot <= cache->used && slot <= cache->size);

		if (slot == cache->used ||
		    page_addr < cache->slots[slot]->addr) {
			/* cache missed */

			/* grow the cache if needed */
			if (cache->used == cache->size) {
				ret = nccl_ofi_mr_cache_grow(cache);
				if (ret != 0) {
					goto out;
				}
			}

			assert(cache->slots);
			memmove(cache->slots + slot + 1,
				cache->slots + slot,
				(cache->used - slot) *
					sizeof(nccl_ofi_reg_entry_t *));
			cache->slots[slot] = (nccl_ofi_reg_entry_t *)calloc(
				1,
				sizeof(nccl_ofi_reg_entry_t));
			if (!cache->slots[slot]) {
				NCCL_OFI_WARN("Failed to allocate new slot");
				ret = -ENOMEM;
				goto out;
			}

			nccl_ofi_reg_entry_t *entry = cache->slots[slot];

			entry->addr = page_addr;
			entry->pages = pages;
			entry->refcnt = 1;
			entry->handle = handle;

			cache->used++;
			NCCL_OFI_TRACE(NCCL_NET,
			               "Inserted MR handle %p for %ld(%s) in cache slot %zu",
			               handle,
			               nccl_ofi_mr_ckey_baseaddr(ckey),
			               nccl_ofi_mr_ckey_type_str(ckey),
			               slot);
			goto out;
		} else if ((page_addr >= cache->slots[slot]->addr) &&
			   ((page_addr - cache->slots[slot]->addr) /
				    cache->system_page_size +
			    pages) <= cache->slots[slot]->pages) {
			/* cache hit */
			NCCL_OFI_WARN("Entry already exists for input (%s) base %lu size %zu",
			              nccl_ofi_mr_ckey_type_str(ckey),
			              nccl_ofi_mr_ckey_baseaddr(ckey),
			              nccl_ofi_mr_ckey_len(ckey));
			ret = -EEXIST;
			goto out;
		}
	}

out:
	return ret;
}