Skip to content

Commit 83f96a2

Browse files
committed
Refactor IPC cache implementation
1 parent b7b9d0a commit 83f96a2

File tree

3 files changed

+59
-62
lines changed

3 files changed

+59
-62
lines changed

src/ipc_cache.c

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,41 @@
2222
#pragma warning(disable : 4702)
2323
#endif
2424

25-
struct ipc_handle_cache_entry_t;
25+
struct ipc_opened_cache_entry_t;
2626

27-
typedef struct ipc_handle_cache_entry_t *hash_map_t;
28-
typedef struct ipc_handle_cache_entry_t *lru_list_t;
27+
typedef struct ipc_opened_cache_entry_t *hash_map_t;
28+
typedef struct ipc_opened_cache_entry_t *lru_list_t;
2929

30-
typedef struct ipc_handle_cache_entry_t {
30+
typedef struct ipc_opened_cache_entry_t {
3131
UT_hash_handle hh;
32-
struct ipc_handle_cache_entry_t *next, *prev;
33-
ipc_mapped_handle_cache_key_t key;
32+
struct ipc_opened_cache_entry_t *next, *prev;
33+
ipc_opened_cache_key_t key;
3434
uint64_t ref_count;
3535
uint64_t handle_id;
3636
hash_map_t
3737
*hash_table; // pointer to the hash table to which the entry belongs
38-
ipc_mapped_handle_cache_value_t value;
39-
} ipc_handle_cache_entry_t;
38+
ipc_opened_cache_value_t value;
39+
} ipc_opened_cache_entry_t;
4040

41-
typedef struct ipc_mapped_handle_cache_global_t {
41+
typedef struct ipc_opened_cache_global_t {
4242
utils_mutex_t cache_lock;
4343
umf_ba_pool_t *cache_allocator;
4444
size_t max_size;
4545
size_t cur_size;
4646
lru_list_t lru_list;
47-
} ipc_mapped_handle_cache_global_t;
47+
} ipc_opened_cache_global_t;
4848

49-
typedef struct ipc_mapped_handle_cache_t {
50-
ipc_mapped_handle_cache_global_t *global;
49+
typedef struct ipc_opened_cache_t {
50+
ipc_opened_cache_global_t *global;
5151
hash_map_t hash_table;
52-
ipc_mapped_handle_cache_eviction_cb_t eviction_cb;
53-
} ipc_mapped_handle_cache_t;
52+
ipc_opened_cache_eviction_cb_t eviction_cb;
53+
} ipc_opened_cache_t;
5454

55-
ipc_mapped_handle_cache_global_t *IPC_MAPPED_CACHE_GLOBAL = NULL;
55+
ipc_opened_cache_global_t *IPC_OPENED_CACHE_GLOBAL = NULL;
5656

5757
umf_result_t umfIpcCacheGlobalInit(void) {
5858
umf_result_t ret = UMF_RESULT_SUCCESS;
59-
ipc_mapped_handle_cache_global_t *cache_global =
59+
ipc_opened_cache_global_t *cache_global =
6060
umf_ba_global_alloc(sizeof(*cache_global));
6161
if (!cache_global) {
6262
LOG_ERR("Failed to allocate memory for the IPC cache global data");
@@ -71,7 +71,7 @@ umf_result_t umfIpcCacheGlobalInit(void) {
7171
}
7272

7373
cache_global->cache_allocator =
74-
umf_ba_create(sizeof(ipc_handle_cache_entry_t));
74+
umf_ba_create(sizeof(ipc_opened_cache_entry_t));
7575
if (!cache_global->cache_allocator) {
7676
LOG_ERR("Failed to create IPC cache allocator");
7777
ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
@@ -83,7 +83,7 @@ umf_result_t umfIpcCacheGlobalInit(void) {
8383
cache_global->cur_size = 0;
8484
cache_global->lru_list = NULL;
8585

86-
IPC_MAPPED_CACHE_GLOBAL = cache_global;
86+
IPC_OPENED_CACHE_GLOBAL = cache_global;
8787
goto err_exit;
8888

8989
err_mutex_destroy:
@@ -97,15 +97,15 @@ umf_result_t umfIpcCacheGlobalInit(void) {
9797
#ifndef NDEBUG
9898
static size_t getGlobalLruListSize(lru_list_t lru_list) {
9999
size_t size = 0;
100-
ipc_handle_cache_entry_t *tmp;
100+
ipc_opened_cache_entry_t *tmp;
101101
DL_COUNT(lru_list, tmp, size);
102102
return size;
103103
}
104104
#endif /* NDEBUG */
105105

106106
void umfIpcCacheGlobalTearDown(void) {
107-
ipc_mapped_handle_cache_global_t *cache_global = IPC_MAPPED_CACHE_GLOBAL;
108-
IPC_MAPPED_CACHE_GLOBAL = NULL;
107+
ipc_opened_cache_global_t *cache_global = IPC_OPENED_CACHE_GLOBAL;
108+
IPC_OPENED_CACHE_GLOBAL = NULL;
109109

110110
if (!cache_global) {
111111
return;
@@ -119,31 +119,31 @@ void umfIpcCacheGlobalTearDown(void) {
119119
umf_ba_global_free(cache_global);
120120
}
121121

122-
ipc_mapped_handle_cache_handle_t umfIpcHandleMappedCacheCreate(
123-
ipc_mapped_handle_cache_eviction_cb_t eviction_cb) {
122+
ipc_opened_cache_handle_t
123+
umfIpcOpenedCacheCreate(ipc_opened_cache_eviction_cb_t eviction_cb) {
124124
if (eviction_cb == NULL) {
125125
LOG_ERR("Eviction callback is NULL");
126126
return NULL;
127127
}
128128

129-
ipc_mapped_handle_cache_t *cache = umf_ba_global_alloc(sizeof(*cache));
129+
ipc_opened_cache_t *cache = umf_ba_global_alloc(sizeof(*cache));
130130

131131
if (!cache) {
132132
LOG_ERR("Failed to allocate memory for the IPC cache");
133133
return NULL;
134134
}
135135

136-
assert(IPC_MAPPED_CACHE_GLOBAL != NULL);
136+
assert(IPC_OPENED_CACHE_GLOBAL != NULL);
137137

138-
cache->global = IPC_MAPPED_CACHE_GLOBAL;
138+
cache->global = IPC_OPENED_CACHE_GLOBAL;
139139
cache->hash_table = NULL;
140140
cache->eviction_cb = eviction_cb;
141141

142142
return cache;
143143
}
144144

145-
void umfIpcHandleMappedCacheDestroy(ipc_mapped_handle_cache_handle_t cache) {
146-
ipc_handle_cache_entry_t *entry, *tmp;
145+
void umfIpcOpenedCacheDestroy(ipc_opened_cache_handle_t cache) {
146+
ipc_opened_cache_entry_t *entry, *tmp;
147147
HASH_ITER(hh, cache->hash_table, entry, tmp) {
148148
DL_DELETE(cache->global->lru_list, entry);
149149
HASH_DEL(cache->hash_table, entry);
@@ -157,15 +157,14 @@ void umfIpcHandleMappedCacheDestroy(ipc_mapped_handle_cache_handle_t cache) {
157157
umf_ba_global_free(cache);
158158
}
159159

160-
umf_result_t
161-
umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
162-
const ipc_mapped_handle_cache_key_t *key,
163-
uint64_t handle_id,
164-
ipc_mapped_handle_cache_value_t **retEntry) {
165-
ipc_handle_cache_entry_t *entry = NULL;
160+
umf_result_t umfIpcOpenedCacheGet(ipc_opened_cache_handle_t cache,
161+
const ipc_opened_cache_key_t *key,
162+
uint64_t handle_id,
163+
ipc_opened_cache_value_t **retEntry) {
164+
ipc_opened_cache_entry_t *entry = NULL;
166165
umf_result_t ret = UMF_RESULT_SUCCESS;
167166
bool evicted = false;
168-
ipc_mapped_handle_cache_value_t evicted_value;
167+
ipc_opened_cache_value_t evicted_value;
169168

170169
if (!cache || !key || !retEntry) {
171170
LOG_ERR("Some arguments are NULL, cache=%p, key=%p, retEntry=%p",

src/ipc_cache.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,37 @@
1414

1515
#include "utils_concurrency.h"
1616

17-
typedef struct ipc_mapped_handle_cache_key_t {
17+
typedef struct ipc_opened_cache_key_t {
1818
void *remote_base_ptr;
1919
umf_memory_provider_handle_t local_provider;
2020
int remote_pid;
21-
} ipc_mapped_handle_cache_key_t;
21+
} ipc_opened_cache_key_t;
2222

23-
typedef struct ipc_mapped_handle_cache_value_t {
23+
typedef struct ipc_opened_cache_value_t {
2424
void *mapped_base_ptr;
2525
size_t mapped_size;
2626
utils_mutex_t mmap_lock;
27-
} ipc_mapped_handle_cache_value_t;
27+
} ipc_opened_cache_value_t;
2828

29-
struct ipc_mapped_handle_cache_t;
29+
struct ipc_opened_cache_t;
3030

31-
typedef struct ipc_mapped_handle_cache_t *ipc_mapped_handle_cache_handle_t;
31+
typedef struct ipc_opened_cache_t *ipc_opened_cache_handle_t;
3232

3333
umf_result_t umfIpcCacheGlobalInit(void);
3434
void umfIpcCacheGlobalTearDown(void);
3535

3636
// define pointer to the eviction callback function
37-
typedef void (*ipc_mapped_handle_cache_eviction_cb_t)(
38-
const ipc_mapped_handle_cache_key_t *key,
39-
const ipc_mapped_handle_cache_value_t *value);
37+
typedef void (*ipc_opened_cache_eviction_cb_t)(
38+
const ipc_opened_cache_key_t *key, const ipc_opened_cache_value_t *value);
4039

41-
ipc_mapped_handle_cache_handle_t umfIpcHandleMappedCacheCreate(
42-
ipc_mapped_handle_cache_eviction_cb_t eviction_cb);
40+
ipc_opened_cache_handle_t
41+
umfIpcOpenedCacheCreate(ipc_opened_cache_eviction_cb_t eviction_cb);
4342

44-
void umfIpcHandleMappedCacheDestroy(ipc_mapped_handle_cache_handle_t cache);
43+
void umfIpcOpenedCacheDestroy(ipc_opened_cache_handle_t cache);
4544

46-
umf_result_t
47-
umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
48-
const ipc_mapped_handle_cache_key_t *key,
49-
uint64_t handle_id,
50-
ipc_mapped_handle_cache_value_t **retEntry);
45+
umf_result_t umfIpcOpenedCacheGet(ipc_opened_cache_handle_t cache,
46+
const ipc_opened_cache_key_t *key,
47+
uint64_t handle_id,
48+
ipc_opened_cache_value_t **retEntry);
5149

5250
#endif /* UMF_IPC_CACHE_H */

src/provider/provider_tracking.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ typedef struct umf_tracking_memory_provider_t {
160160
umf_memory_tracker_handle_t hTracker;
161161
umf_memory_pool_handle_t pool;
162162
critnib *ipcCache;
163-
ipc_mapped_handle_cache_handle_t hIpcMappedCache;
163+
ipc_opened_cache_handle_t hIpcMappedCache;
164164
} umf_tracking_memory_provider_t;
165165

166166
typedef struct umf_tracking_memory_provider_t umf_tracking_memory_provider_t;
@@ -477,7 +477,7 @@ static void trackingFinalize(void *provider) {
477477
umf_tracking_memory_provider_t *p =
478478
(umf_tracking_memory_provider_t *)provider;
479479

480-
umfIpcHandleMappedCacheDestroy(p->hIpcMappedCache);
480+
umfIpcOpenedCacheDestroy(p->hIpcMappedCache);
481481

482482
critnib_delete(p->ipcCache);
483483

@@ -629,8 +629,8 @@ static umf_result_t trackingPutIpcHandle(void *provider,
629629
}
630630

631631
static void
632-
ipcMappedCacheEvictionCallback(const ipc_mapped_handle_cache_key_t *key,
633-
const ipc_mapped_handle_cache_value_t *value) {
632+
ipcOpenedCacheEvictionCallback(const ipc_opened_cache_key_t *key,
633+
const ipc_opened_cache_value_t *value) {
634634
umf_tracking_memory_provider_t *p =
635635
(umf_tracking_memory_provider_t *)key->local_provider;
636636
// umfMemoryTrackerRemove should be called before umfMemoryProviderCloseIPCHandle
@@ -700,16 +700,16 @@ static umf_result_t trackingOpenIpcHandle(void *provider, void *providerIpcData,
700700

701701
umf_ipc_data_t *ipcUmfData = getIpcDataFromIpcHandle(providerIpcData);
702702

703-
// Compiler may add paddings to the ipc_mapped_handle_cache_key_t structure
703+
// Compiler may add paddings to the ipc_opened_cache_key_t structure
704704
// so we need to zero it out to avoid false cache miss.
705-
ipc_mapped_handle_cache_key_t key = {0};
705+
ipc_opened_cache_key_t key = {0};
706706
key.remote_base_ptr = ipcUmfData->base;
707707
key.local_provider = provider;
708708
key.remote_pid = ipcUmfData->pid;
709709

710-
ipc_mapped_handle_cache_value_t *cache_entry = NULL;
711-
ret = umfIpcHandleMappedCacheGet(p->hIpcMappedCache, &key,
712-
ipcUmfData->handle_id, &cache_entry);
710+
ipc_opened_cache_value_t *cache_entry = NULL;
711+
ret = umfIpcOpenedCacheGet(p->hIpcMappedCache, &key, ipcUmfData->handle_id,
712+
&cache_entry);
713713
if (ret != UMF_RESULT_SUCCESS) {
714714
LOG_ERR("failed to get cache entry");
715715
return ret;
@@ -798,7 +798,7 @@ umf_result_t umfTrackingMemoryProviderCreate(
798798
}
799799

800800
params.hIpcMappedCache =
801-
umfIpcHandleMappedCacheCreate(ipcMappedCacheEvictionCallback);
801+
umfIpcOpenedCacheCreate(ipcOpenedCacheEvictionCallback);
802802

803803
LOG_DEBUG("upstream=%p, tracker=%p, "
804804
"pool=%p, ipcCache=%p, hIpcMappedCache=%p",

0 commit comments

Comments
 (0)