|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2024 Intel Corporation |
| 4 | + * |
| 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + * |
| 8 | + */ |
| 9 | +#include "ipc_cache.h" |
| 10 | + |
| 11 | +#include <stdbool.h> |
| 12 | + |
| 13 | +#include "base_alloc_global.h" |
| 14 | +#include "utils_common.h" |
| 15 | +#include "utils_concurrency.h" |
| 16 | +#include "utils_log.h" |
| 17 | +#include "utlist.h" |
| 18 | + |
| 19 | +typedef struct ipc_handle_cache_entry_t { |
| 20 | + UT_hash_handle hh; |
| 21 | + struct ipc_handle_cache_entry_t *next, *prev; |
| 22 | + ipc_mmaped_handle_cache_key_t key; |
| 23 | + uint64_t ref_count; |
| 24 | + uint64_t handle_id; |
| 25 | + ipc_mmaped_handle_cache_value_t value; |
| 26 | +} ipc_handle_cache_entry_t; |
| 27 | + |
| 28 | +typedef struct ipc_handle_mmaped_cache_t { |
| 29 | + utils_mutex_t cache_lock; |
| 30 | + umf_ba_pool_t *cache_allocator; |
| 31 | + size_t max_size; |
| 32 | + size_t cur_size; |
| 33 | + ipc_handle_cache_entry_t *hash_table; |
| 34 | + ipc_handle_cache_entry_t *frequency_list; |
| 35 | +} ipc_handle_mmaped_cache_t; |
| 36 | + |
| 37 | +ipc_handle_mmaped_cache_handle_t IPC_MMAPED_CACHE = NULL; |
| 38 | + |
| 39 | +void umfIpcCacheInit(void) { |
| 40 | + ipc_handle_mmaped_cache_t *cache = umf_ba_global_alloc(sizeof(*cache)); |
| 41 | + if (!cache) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (NULL == utils_mutex_init(&(cache->cache_lock))) { |
| 46 | + LOG_ERR("Failed to initialize mutex for the IPC cache"); |
| 47 | + umf_ba_global_free(cache); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + cache->cache_allocator = umf_ba_create(sizeof(ipc_handle_cache_entry_t)); |
| 52 | + if (!cache->cache_allocator) { |
| 53 | + LOG_ERR("Failed to create IPC cache allocator"); |
| 54 | + umf_ba_global_free(cache); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + cache->max_size = 0; |
| 59 | + cache->cur_size = 0; |
| 60 | + cache->hash_table = NULL; |
| 61 | + cache->frequency_list = NULL; |
| 62 | + |
| 63 | + IPC_MMAPED_CACHE = cache; |
| 64 | +} |
| 65 | + |
| 66 | +void umfIpcCacheTearDown(void) { |
| 67 | + ipc_handle_mmaped_cache_handle_t cache = IPC_MMAPED_CACHE; |
| 68 | + IPC_MMAPED_CACHE = NULL; |
| 69 | + |
| 70 | + if (!cache) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + ipc_handle_cache_entry_t *entry, *tmp; |
| 75 | + HASH_ITER(hh, cache->hash_table, entry, tmp) { |
| 76 | + DL_DELETE(cache->frequency_list, entry); |
| 77 | + HASH_DEL(cache->hash_table, entry); |
| 78 | + cache->cur_size -= 1; |
| 79 | + umf_ba_free(cache->cache_allocator, entry); |
| 80 | + } |
| 81 | + HASH_CLEAR(hh, cache->hash_table); |
| 82 | + |
| 83 | + umf_ba_destroy(cache->cache_allocator); |
| 84 | + umf_ba_global_free(cache); |
| 85 | +} |
| 86 | + |
| 87 | +ipc_handle_mmaped_cache_handle_t umfIpcHandleMmapedCacheCreate(void) { |
| 88 | + return IPC_MMAPED_CACHE; |
| 89 | +} |
| 90 | + |
| 91 | +void umfIpcHandleMmapedCacheDestroy(ipc_handle_mmaped_cache_handle_t cache) { |
| 92 | + (void)cache; |
| 93 | +} |
| 94 | + |
| 95 | +umf_result_t |
| 96 | +umfIpcHandleMmapedCacheGet(ipc_handle_mmaped_cache_handle_t cache, |
| 97 | + const ipc_mmaped_handle_cache_key_t *key, |
| 98 | + uint64_t handle_id, |
| 99 | + ipc_handle_mmaped_cache_eviction_cb_t eviction_cb, |
| 100 | + ipc_mmaped_handle_cache_value_t **retEntry) { |
| 101 | + ipc_handle_cache_entry_t *entry = NULL; |
| 102 | + umf_result_t ret = UMF_RESULT_SUCCESS; |
| 103 | + bool evicted = false; |
| 104 | + ipc_mmaped_handle_cache_value_t evicted_value; |
| 105 | + |
| 106 | + if (!cache) { |
| 107 | + return UMF_RESULT_ERROR_INVALID_ARGUMENT; |
| 108 | + } |
| 109 | + |
| 110 | + utils_mutex_lock(&(cache->cache_lock)); |
| 111 | + |
| 112 | + HASH_FIND(hh, cache->hash_table, key, sizeof(*key), entry); |
| 113 | + if (entry && entry->handle_id == handle_id) { // cache hit |
| 114 | + // update frequency list |
| 115 | + DL_DELETE(cache->frequency_list, entry); |
| 116 | + DL_PREPEND(cache->frequency_list, entry); |
| 117 | + } else { //cache miss |
| 118 | + // Look for eviction candidate |
| 119 | + if (entry == NULL && cache->max_size != 0 && |
| 120 | + cache->cur_size >= cache->max_size) { |
| 121 | + entry = cache->frequency_list->prev; |
| 122 | + } |
| 123 | + |
| 124 | + if (entry) { // we have eviction candidate |
| 125 | + DL_DELETE(cache->frequency_list, entry); |
| 126 | + HASH_DEL(cache->hash_table, entry); |
| 127 | + cache->cur_size -= 1; |
| 128 | + evicted_value.mmaped_base_ptr = entry->value.mmaped_base_ptr; |
| 129 | + evicted_value.mmaped_size = entry->value.mmaped_size; |
| 130 | + evicted = true; |
| 131 | + } else { |
| 132 | + entry = umf_ba_alloc(cache->cache_allocator); |
| 133 | + if (!entry) { |
| 134 | + ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 135 | + goto exit; |
| 136 | + } |
| 137 | + if (NULL == utils_mutex_init(&(entry->value.mmap_lock))) { |
| 138 | + LOG_ERR("Failed to initialize mutex for the IPC cache entry"); |
| 139 | + umf_ba_global_free(entry); |
| 140 | + ret = UMF_RESULT_ERROR_UNKNOWN; |
| 141 | + goto exit; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + entry->key = *key; |
| 146 | + entry->ref_count = 0; |
| 147 | + entry->handle_id = handle_id; |
| 148 | + entry->value.mmaped_size = 0; |
| 149 | + entry->value.mmaped_base_ptr = NULL; |
| 150 | + |
| 151 | + HASH_ADD(hh, cache->hash_table, key, sizeof(entry->key), entry); |
| 152 | + DL_PREPEND(cache->frequency_list, entry); |
| 153 | + cache->cur_size += 1; |
| 154 | + } |
| 155 | + |
| 156 | +exit: |
| 157 | + if (ret == UMF_RESULT_SUCCESS) { |
| 158 | + utils_atomic_increment(&entry->ref_count); |
| 159 | + *retEntry = &entry->value; |
| 160 | + } |
| 161 | + |
| 162 | + utils_mutex_unlock(&(cache->cache_lock)); |
| 163 | + |
| 164 | + if (evicted) { |
| 165 | + eviction_cb(key, &evicted_value); |
| 166 | + } |
| 167 | + |
| 168 | + return ret; |
| 169 | +} |
0 commit comments