Skip to content

Refactoring of the memory tracker and IPC cache #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions src/ipc_cache.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 2024 Intel Corporation
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand All @@ -22,41 +22,41 @@
#pragma warning(disable : 4702)
#endif

struct ipc_handle_cache_entry_t;
struct ipc_opened_cache_entry_t;

typedef struct ipc_handle_cache_entry_t *hash_map_t;
typedef struct ipc_handle_cache_entry_t *lru_list_t;
typedef struct ipc_opened_cache_entry_t *hash_map_t;
typedef struct ipc_opened_cache_entry_t *lru_list_t;

typedef struct ipc_handle_cache_entry_t {
typedef struct ipc_opened_cache_entry_t {
UT_hash_handle hh;
struct ipc_handle_cache_entry_t *next, *prev;
ipc_mapped_handle_cache_key_t key;
struct ipc_opened_cache_entry_t *next, *prev;
ipc_opened_cache_key_t key;
uint64_t ref_count;
uint64_t handle_id;
hash_map_t
*hash_table; // pointer to the hash table to which the entry belongs
ipc_mapped_handle_cache_value_t value;
} ipc_handle_cache_entry_t;
ipc_opened_cache_value_t value;
} ipc_opened_cache_entry_t;

typedef struct ipc_mapped_handle_cache_global_t {
typedef struct ipc_opened_cache_global_t {
utils_mutex_t cache_lock;
umf_ba_pool_t *cache_allocator;
size_t max_size;
size_t cur_size;
lru_list_t lru_list;
} ipc_mapped_handle_cache_global_t;
} ipc_opened_cache_global_t;

typedef struct ipc_mapped_handle_cache_t {
ipc_mapped_handle_cache_global_t *global;
typedef struct ipc_opened_cache_t {
ipc_opened_cache_global_t *global;
hash_map_t hash_table;
ipc_mapped_handle_cache_eviction_cb_t eviction_cb;
} ipc_mapped_handle_cache_t;
ipc_opened_cache_eviction_cb_t eviction_cb;
} ipc_opened_cache_t;

ipc_mapped_handle_cache_global_t *IPC_MAPPED_CACHE_GLOBAL = NULL;
ipc_opened_cache_global_t *IPC_OPENED_CACHE_GLOBAL = NULL;

umf_result_t umfIpcCacheGlobalInit(void) {
umf_result_t ret = UMF_RESULT_SUCCESS;
ipc_mapped_handle_cache_global_t *cache_global =
ipc_opened_cache_global_t *cache_global =
umf_ba_global_alloc(sizeof(*cache_global));
if (!cache_global) {
LOG_ERR("Failed to allocate memory for the IPC cache global data");
Expand All @@ -71,7 +71,7 @@ umf_result_t umfIpcCacheGlobalInit(void) {
}

cache_global->cache_allocator =
umf_ba_create(sizeof(ipc_handle_cache_entry_t));
umf_ba_create(sizeof(ipc_opened_cache_entry_t));
if (!cache_global->cache_allocator) {
LOG_ERR("Failed to create IPC cache allocator");
ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
Expand All @@ -83,7 +83,7 @@ umf_result_t umfIpcCacheGlobalInit(void) {
cache_global->cur_size = 0;
cache_global->lru_list = NULL;

IPC_MAPPED_CACHE_GLOBAL = cache_global;
IPC_OPENED_CACHE_GLOBAL = cache_global;
goto err_exit;

err_mutex_destroy:
Expand All @@ -97,15 +97,15 @@ umf_result_t umfIpcCacheGlobalInit(void) {
#ifndef NDEBUG
static size_t getGlobalLruListSize(lru_list_t lru_list) {
size_t size = 0;
ipc_handle_cache_entry_t *tmp;
ipc_opened_cache_entry_t *tmp;
DL_COUNT(lru_list, tmp, size);
return size;
}
#endif /* NDEBUG */

void umfIpcCacheGlobalTearDown(void) {
ipc_mapped_handle_cache_global_t *cache_global = IPC_MAPPED_CACHE_GLOBAL;
IPC_MAPPED_CACHE_GLOBAL = NULL;
ipc_opened_cache_global_t *cache_global = IPC_OPENED_CACHE_GLOBAL;
IPC_OPENED_CACHE_GLOBAL = NULL;

if (!cache_global) {
return;
Expand All @@ -119,31 +119,31 @@ void umfIpcCacheGlobalTearDown(void) {
umf_ba_global_free(cache_global);
}

ipc_mapped_handle_cache_handle_t umfIpcHandleMappedCacheCreate(
ipc_mapped_handle_cache_eviction_cb_t eviction_cb) {
ipc_opened_cache_handle_t
umfIpcOpenedCacheCreate(ipc_opened_cache_eviction_cb_t eviction_cb) {
if (eviction_cb == NULL) {
LOG_ERR("Eviction callback is NULL");
return NULL;
}

ipc_mapped_handle_cache_t *cache = umf_ba_global_alloc(sizeof(*cache));
ipc_opened_cache_t *cache = umf_ba_global_alloc(sizeof(*cache));

if (!cache) {
LOG_ERR("Failed to allocate memory for the IPC cache");
return NULL;
}

assert(IPC_MAPPED_CACHE_GLOBAL != NULL);
assert(IPC_OPENED_CACHE_GLOBAL != NULL);

cache->global = IPC_MAPPED_CACHE_GLOBAL;
cache->global = IPC_OPENED_CACHE_GLOBAL;
cache->hash_table = NULL;
cache->eviction_cb = eviction_cb;

return cache;
}

void umfIpcHandleMappedCacheDestroy(ipc_mapped_handle_cache_handle_t cache) {
ipc_handle_cache_entry_t *entry, *tmp;
void umfIpcOpenedCacheDestroy(ipc_opened_cache_handle_t cache) {
ipc_opened_cache_entry_t *entry, *tmp;
HASH_ITER(hh, cache->hash_table, entry, tmp) {
DL_DELETE(cache->global->lru_list, entry);
HASH_DEL(cache->hash_table, entry);
Expand All @@ -157,15 +157,14 @@ void umfIpcHandleMappedCacheDestroy(ipc_mapped_handle_cache_handle_t cache) {
umf_ba_global_free(cache);
}

umf_result_t
umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
const ipc_mapped_handle_cache_key_t *key,
uint64_t handle_id,
ipc_mapped_handle_cache_value_t **retEntry) {
ipc_handle_cache_entry_t *entry = NULL;
umf_result_t umfIpcOpenedCacheGet(ipc_opened_cache_handle_t cache,
const ipc_opened_cache_key_t *key,
uint64_t handle_id,
ipc_opened_cache_value_t **retEntry) {
ipc_opened_cache_entry_t *entry = NULL;
umf_result_t ret = UMF_RESULT_SUCCESS;
bool evicted = false;
ipc_mapped_handle_cache_value_t evicted_value;
ipc_opened_cache_value_t evicted_value;

if (!cache || !key || !retEntry) {
LOG_ERR("Some arguments are NULL, cache=%p, key=%p, retEntry=%p",
Expand Down
34 changes: 16 additions & 18 deletions src/ipc_cache.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 2024 Intel Corporation
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand All @@ -14,39 +14,37 @@

#include "utils_concurrency.h"

typedef struct ipc_mapped_handle_cache_key_t {
typedef struct ipc_opened_cache_key_t {
void *remote_base_ptr;
umf_memory_provider_handle_t local_provider;
int remote_pid;
} ipc_mapped_handle_cache_key_t;
} ipc_opened_cache_key_t;

typedef struct ipc_mapped_handle_cache_value_t {
typedef struct ipc_opened_cache_value_t {
void *mapped_base_ptr;
size_t mapped_size;
utils_mutex_t mmap_lock;
} ipc_mapped_handle_cache_value_t;
} ipc_opened_cache_value_t;

struct ipc_mapped_handle_cache_t;
struct ipc_opened_cache_t;

typedef struct ipc_mapped_handle_cache_t *ipc_mapped_handle_cache_handle_t;
typedef struct ipc_opened_cache_t *ipc_opened_cache_handle_t;

umf_result_t umfIpcCacheGlobalInit(void);
void umfIpcCacheGlobalTearDown(void);

// define pointer to the eviction callback function
typedef void (*ipc_mapped_handle_cache_eviction_cb_t)(
const ipc_mapped_handle_cache_key_t *key,
const ipc_mapped_handle_cache_value_t *value);
typedef void (*ipc_opened_cache_eviction_cb_t)(
const ipc_opened_cache_key_t *key, const ipc_opened_cache_value_t *value);

ipc_mapped_handle_cache_handle_t umfIpcHandleMappedCacheCreate(
ipc_mapped_handle_cache_eviction_cb_t eviction_cb);
ipc_opened_cache_handle_t
umfIpcOpenedCacheCreate(ipc_opened_cache_eviction_cb_t eviction_cb);

void umfIpcHandleMappedCacheDestroy(ipc_mapped_handle_cache_handle_t cache);
void umfIpcOpenedCacheDestroy(ipc_opened_cache_handle_t cache);

umf_result_t
umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
const ipc_mapped_handle_cache_key_t *key,
uint64_t handle_id,
ipc_mapped_handle_cache_value_t **retEntry);
umf_result_t umfIpcOpenedCacheGet(ipc_opened_cache_handle_t cache,
const ipc_opened_cache_key_t *key,
uint64_t handle_id,
ipc_opened_cache_value_t **retEntry);

#endif /* UMF_IPC_CACHE_H */
Loading
Loading