|
| 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 | +ipc_handle_mmaped_cache_handle_t umfIpcHandleMmapedCacheCreate(void) { |
| 20 | + ipc_handle_mmaped_cache_t *cache = umf_ba_global_alloc(sizeof(*cache)); |
| 21 | + if (!cache) { |
| 22 | + return NULL; |
| 23 | + } |
| 24 | + |
| 25 | + if (NULL == utils_mutex_init(&(cache->cache_lock))) { |
| 26 | + LOG_ERR("Failed to initialize mutex for the IPC cache"); |
| 27 | + umf_ba_global_free(cache); |
| 28 | + return NULL; |
| 29 | + } |
| 30 | + |
| 31 | + cache->cache_allocator = umf_ba_create(sizeof(ipc_handle_cache_entry_t)); |
| 32 | + if (!cache->cache_allocator) { |
| 33 | + LOG_ERR("Failed to create IPC cache allocator"); |
| 34 | + umf_ba_global_free(cache); |
| 35 | + return NULL; |
| 36 | + } |
| 37 | + |
| 38 | + cache->max_size = 0; |
| 39 | + cache->cur_size = 0; |
| 40 | + cache->hash_table = NULL; |
| 41 | + cache->frequency_list = NULL; |
| 42 | + |
| 43 | + return cache; |
| 44 | +} |
| 45 | + |
| 46 | +void umfIpcHandleMmapedCacheDestroy(ipc_handle_mmaped_cache_handle_t cache) { |
| 47 | + if (!cache) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + ipc_handle_cache_entry_t *entry, *tmp; |
| 52 | + HASH_ITER(hh, cache->hash_table, entry, tmp) { |
| 53 | + DL_DELETE(cache->frequency_list, entry); |
| 54 | + HASH_DEL(cache->hash_table, entry); |
| 55 | + cache->cur_size -= 1; |
| 56 | + umf_ba_free(cache->cache_allocator, entry); |
| 57 | + } |
| 58 | + HASH_CLEAR(hh, cache->hash_table); |
| 59 | + |
| 60 | + umf_ba_destroy(cache->cache_allocator); |
| 61 | + umf_ba_global_free(cache); |
| 62 | +} |
| 63 | + |
| 64 | +umf_result_t |
| 65 | +umfIpcHandleMmapedCacheGet(ipc_handle_mmaped_cache_handle_t cache, |
| 66 | + const ipc_mmaped_handle_cache_key_t *key, |
| 67 | + uint64_t handle_id, |
| 68 | + ipc_handle_mmaped_cache_eviction_cb_t eviction_cb, |
| 69 | + ipc_mmaped_handle_cache_value_t **retEntry) { |
| 70 | + ipc_handle_cache_entry_t *entry = NULL; |
| 71 | + umf_result_t ret = UMF_RESULT_SUCCESS; |
| 72 | + bool evicted = false; |
| 73 | + ipc_mmaped_handle_cache_value_t evicted_value; |
| 74 | + |
| 75 | + if (!cache) { |
| 76 | + return UMF_RESULT_ERROR_INVALID_ARGUMENT; |
| 77 | + } |
| 78 | + |
| 79 | + utils_mutex_lock(&(cache->cache_lock)); |
| 80 | + |
| 81 | + HASH_FIND(hh, cache->hash_table, key, sizeof(*key), entry); |
| 82 | + if (entry && entry->handle_id == handle_id) { // cache hit |
| 83 | + // update frequency list |
| 84 | + DL_DELETE(cache->frequency_list, entry); |
| 85 | + DL_PREPEND(cache->frequency_list, entry); |
| 86 | + } else { //cache miss |
| 87 | + // Look for eviction candidate |
| 88 | + if (entry == NULL && cache->max_size != 0 && |
| 89 | + cache->cur_size >= cache->max_size) { |
| 90 | + entry = cache->frequency_list->prev; |
| 91 | + } |
| 92 | + |
| 93 | + if (entry) { // we have eviction candidate |
| 94 | + DL_DELETE(cache->frequency_list, entry); |
| 95 | + HASH_DEL(cache->hash_table, entry); |
| 96 | + cache->cur_size -= 1; |
| 97 | + evicted_value.mmaped_base_ptr = entry->value.mmaped_base_ptr; |
| 98 | + evicted_value.mmaped_size = entry->value.mmaped_size; |
| 99 | + evicted = true; |
| 100 | + } else { |
| 101 | + entry = umf_ba_alloc(cache->cache_allocator); |
| 102 | + if (!entry) { |
| 103 | + ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 104 | + goto exit; |
| 105 | + } |
| 106 | + if (NULL == utils_mutex_init(&(entry->value.mmap_lock))) { |
| 107 | + LOG_ERR("Failed to initialize mutex for the IPC cache entry"); |
| 108 | + umf_ba_global_free(entry); |
| 109 | + ret = UMF_RESULT_ERROR_UNKNOWN; |
| 110 | + goto exit; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + entry->key = *key; |
| 115 | + entry->ref_count = 0; |
| 116 | + entry->handle_id = handle_id; |
| 117 | + entry->value.mmaped_size = 0; |
| 118 | + entry->value.mmaped_base_ptr = NULL; |
| 119 | + |
| 120 | + HASH_ADD(hh, cache->hash_table, key, sizeof(entry->key), entry); |
| 121 | + DL_PREPEND(cache->frequency_list, entry); |
| 122 | + cache->cur_size += 1; |
| 123 | + } |
| 124 | + |
| 125 | +exit: |
| 126 | + if (ret == UMF_RESULT_SUCCESS) { |
| 127 | + utils_atomic_increment(&entry->ref_count); |
| 128 | + *retEntry = &entry->value; |
| 129 | + } |
| 130 | + |
| 131 | + utils_mutex_unlock(&(cache->cache_lock)); |
| 132 | + |
| 133 | + if (evicted) { |
| 134 | + eviction_cb(key, &evicted_value); |
| 135 | + } |
| 136 | + |
| 137 | + return ret; |
| 138 | +} |
0 commit comments