Skip to content

Commit 1ebcfb3

Browse files
committed
Implement size limit for the cache of opened IPC handles
1 parent 5a515c5 commit 1ebcfb3

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/ipc_cache.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ typedef struct ipc_mapped_handle_cache_t {
5454

5555
ipc_mapped_handle_cache_global_t *IPC_MAPPED_CACHE_GLOBAL = NULL;
5656

57+
// Returns value of the UMF_MAX_OPENED_IPC_HANDLES environment variable
58+
// or 0 if it is not set.
59+
static size_t umfIpcCacheGlobalInitMaxOpenedHandles(void) {
60+
const char *max_size_str = getenv("UMF_MAX_OPENED_IPC_HANDLES");
61+
if (max_size_str) {
62+
char *endptr;
63+
size_t max_size = strtoul(max_size_str, &endptr, 10);
64+
if (*endptr == '\0') {
65+
return max_size;
66+
}
67+
LOG_ERR("Invalid value of UMF_MAX_OPENED_IPC_HANDLES: %s",
68+
max_size_str);
69+
}
70+
return 0;
71+
}
72+
5773
umf_result_t umfIpcCacheGlobalInit(void) {
5874
umf_result_t ret = UMF_RESULT_SUCCESS;
5975
ipc_mapped_handle_cache_global_t *cache_global =
@@ -78,8 +94,7 @@ umf_result_t umfIpcCacheGlobalInit(void) {
7894
goto err_mutex_destroy;
7995
}
8096

81-
// TODO: make max_size configurable via environment variable
82-
cache_global->max_size = 0;
97+
cache_global->max_size = umfIpcCacheGlobalInitMaxOpenedHandles();
8398
cache_global->cur_size = 0;
8499
cache_global->lru_list = NULL;
85100

@@ -189,7 +204,16 @@ umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
189204
if (entry == NULL && cache->global->max_size != 0 &&
190205
cache->global->cur_size >= cache->global->max_size) {
191206
// If max_size is set and the cache is full, evict the least recently used entry.
192-
entry = cache->global->lru_list->prev;
207+
// we need to search for the least recently used entry with ref_count == 0
208+
// The utlist implementation of the doubly-linked list keeps a tail pointer in head->prev
209+
ipc_handle_cache_entry_t *candidate = cache->global->lru_list->prev;
210+
do {
211+
if (candidate->ref_count == 0) {
212+
entry = candidate;
213+
break;
214+
}
215+
candidate = candidate->prev;
216+
} while (candidate != cache->global->lru_list->prev);
193217
}
194218

195219
if (entry) { // we have eviction candidate
@@ -242,3 +266,20 @@ umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
242266

243267
return ret;
244268
}
269+
270+
umf_result_t
271+
umfIpcHandleMappedCacheRelease(ipc_mapped_handle_cache_value_t *cacheValue) {
272+
if (!cacheValue) {
273+
LOG_ERR("cacheValue is NULL");
274+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
275+
}
276+
277+
// get pointer to the entry
278+
ipc_handle_cache_entry_t *entry =
279+
(ipc_handle_cache_entry_t *)((char *)cacheValue -
280+
offsetof(ipc_handle_cache_entry_t, value));
281+
// decrement the ref count
282+
utils_atomic_decrement(&entry->ref_count);
283+
284+
return UMF_RESULT_SUCCESS;
285+
}

src/provider/provider_tracking.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ static umf_result_t trackingOpenIpcHandle(void *provider, void *providerIpcData,
709709

710710
void *mapped_ptr = NULL;
711711
utils_atomic_load_acquire(&(cache_entry->mapped_base_ptr), &mapped_ptr);
712-
if (mapped_ptr == NULL) {
712+
if (mapped_ptr == NULL) { // new cache entry
713713
utils_mutex_lock(&(cache_entry->mmap_lock));
714714
utils_atomic_load_acquire(&(cache_entry->mapped_base_ptr), &mapped_ptr);
715715
if (mapped_ptr == NULL) {
@@ -734,6 +734,10 @@ static umf_result_t trackingOpenIpcHandle(void *provider, void *providerIpcData,
734734

735735
static umf_result_t trackingCloseIpcHandle(void *provider, void *ptr,
736736
size_t size) {
737+
#if 0
738+
umf_tracking_memory_provider_t *p =
739+
(umf_tracking_memory_provider_t *)provider;
740+
#else
737741
(void)provider;
738742
(void)ptr;
739743
(void)size;
@@ -746,6 +750,7 @@ static umf_result_t trackingCloseIpcHandle(void *provider, void *ptr,
746750
// we need to introduce a reference counting mechanism.
747751
// The trackingOpenIpcHandle will increment the refcount for the corresponding entry.
748752
// The trackingCloseIpcHandle will decrement the refcount for the corresponding cache entry.
753+
#endif
749754
return UMF_RESULT_SUCCESS;
750755
}
751756

0 commit comments

Comments
 (0)