Skip to content

Commit

Permalink
Renames engineManager::engine_cache_memory to `engineManager::total…
Browse files Browse the repository at this point in the history
…_memory_overhead`

It also fixes a bug, where we would never decrement the memory overhead
of scripting engines that were unregistered.

Signed-off-by: Ricardo Dias <[email protected]>
  • Loading branch information
rjd15372 committed Jan 13, 2025
1 parent 8646134 commit 12c3667
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ unsigned long functionsMemoryOverhead(void) {
memory_overhead += dictMemUsage(curr_functions_lib_ctx->functions);
memory_overhead += sizeof(functionsLibCtx);
memory_overhead += curr_functions_lib_ctx->cache_memory;
memory_overhead += scriptingEngineManagerGetCacheMemory();
memory_overhead += scriptingEngineManagerGetTotalMemoryOverhead();

return memory_overhead;
}
Expand Down
23 changes: 15 additions & 8 deletions src/scripting_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ typedef struct scriptingEngine {


typedef struct engineManger {
dict *engines; /* engines dictionary */
size_t engine_cache_memory;
dict *engines; /* engines dictionary */
size_t total_memory_overhead; /* the sum of the memory overhead of all registered scripting engines */
} engineManager;


static engineManager engineMgr = {
.engines = NULL,
.engine_cache_memory = 0,
.total_memory_overhead = 0,
};

static uint64_t dictStrCaseHash(const void *key) {
Expand All @@ -55,8 +55,10 @@ int scriptingEngineManagerInit(void) {
return C_OK;
}

size_t scriptingEngineManagerGetCacheMemory(void) {
return engineMgr.engine_cache_memory;
/* Returns the amount of memory overhead consumed by all registered scripting
engines. */
size_t scriptingEngineManagerGetTotalMemoryOverhead(void) {
return engineMgr.total_memory_overhead;
}

size_t scriptingEngineManagerGetNumEngines(void) {
Expand Down Expand Up @@ -117,9 +119,9 @@ int scriptingEngineManagerRegister(const char *engine_name,
dictAdd(engineMgr.engines, engine_name_sds, e);

engineMemoryInfo mem_info = scriptingEngineCallGetMemoryInfo(e);
engineMgr.engine_cache_memory += zmalloc_size(e) +
sdsAllocSize(e->name) +
mem_info.engine_memory_overhead;
engineMgr.total_memory_overhead += zmalloc_size(e) +
sdsAllocSize(e->name) +
mem_info.engine_memory_overhead;

return C_OK;
}
Expand All @@ -139,6 +141,11 @@ int scriptingEngineManagerUnregister(const char *engine_name) {

functionsRemoveLibFromEngine(e);

engineMemoryInfo mem_info = scriptingEngineCallGetMemoryInfo(e);
engineMgr.total_memory_overhead -= zmalloc_size(e) +
sdsAllocSize(e->name) +
mem_info.engine_memory_overhead;

sdsfree(e->name);
freeClient(e->c);
if (e->module_ctx) {
Expand Down
2 changes: 1 addition & 1 deletion src/scripting_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef void (*engineIterCallback)(scriptingEngine *engine, void *context);
* Engine manager API functions.
*/
int scriptingEngineManagerInit(void);
size_t scriptingEngineManagerGetCacheMemory(void);
size_t scriptingEngineManagerGetTotalMemoryOverhead(void);
size_t scriptingEngineManagerGetNumEngines(void);
size_t scriptingEngineManagerGetMemoryUsage(void);
int scriptingEngineManagerRegister(const char *engine_name,
Expand Down

0 comments on commit 12c3667

Please sign in to comment.