From 089ee5979d1b3d36dad57cecaf444b0dfc00db31 Mon Sep 17 00:00:00 2001 From: "meir@redislabs.com" Date: Wed, 28 Aug 2019 18:08:07 +0300 Subject: [PATCH 1/4] expose used memory via redismodule api The exposed functions: 1. RedisModule_GetUsedMemoryPercentage - return the used memory 2. RedisModue_MallocSize - return for a given pointer, the amount of memory allocated for this pointer --- src/module.c | 22 ++++++++++++++++++++++ src/redismodule.h | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/src/module.c b/src/module.c index ad34e7b64..75c98f12d 100644 --- a/src/module.c +++ b/src/module.c @@ -5891,6 +5891,26 @@ int RM_CommandFilterArgDelete(RedisModuleCommandFilterCtx *fctx, int pos) return REDISMODULE_OK; } +/* + * For a given pointer, return The amount of memory + * allocated for this pointer. + */ +size_t RM_MallocSize(void* ptr){ + return zmalloc_size(ptr); +} + +/* + * Return the a number between 0 to 1 indicating + * the amount of memory currently used. + * 0 - no memory limit + * 1 and above, memory limit reached. + */ +float RM_GetUsedMemoryPercentage(){ + float level; + getMaxmemoryState(NULL, NULL, NULL, &level); + return level; +} + /* -------------------------------------------------------------------------- * Module fork API * -------------------------------------------------------------------------- */ @@ -6971,4 +6991,6 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(BlockClientOnKeys); REGISTER_API(SignalKeyAsReady); REGISTER_API(GetBlockedClientReadyKey); + REGISTER_API(GetUsedMemoryPercentage); + REGISTER_API(MallocSize); } diff --git a/src/redismodule.h b/src/redismodule.h index 728c7f584..259ea2532 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -633,6 +633,8 @@ int REDISMODULE_API_FUNC(RedisModule_CommandFilterArgDelete)(RedisModuleCommandF int REDISMODULE_API_FUNC(RedisModule_Fork)(RedisModuleForkDoneHandler cb, void *user_data); int REDISMODULE_API_FUNC(RedisModule_ExitFromChild)(int retcode); int REDISMODULE_API_FUNC(RedisModule_KillForkChild)(int child_pid); +float REDISMODULE_API_FUNC(RedisModule_GetUsedMemoryPercentage)(); +size_t REDISMODULE_API_FUNC(RedisModule_MallocSize)(void* ptr); #endif #define RedisModule_IsAOFClient(id) ((id) == UINT64_MAX) @@ -842,6 +844,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int REDISMODULE_GET_API(Fork); REDISMODULE_GET_API(ExitFromChild); REDISMODULE_GET_API(KillForkChild); + REDISMODULE_GET_API(GetUsedMemoryPercentage); + REDISMODULE_GET_API(MallocSize); #endif if (RedisModule_IsModuleNameBusy && RedisModule_IsModuleNameBusy(name)) return REDISMODULE_ERR; From aded138a591fea32ebf0444dd2d217fc3bde7f2d Mon Sep 17 00:00:00 2001 From: "meir@redislabs.com" Date: Wed, 6 Nov 2019 11:25:21 +0200 Subject: [PATCH 2/4] return value between 0 to 100 instead of 0 to 1. --- src/module.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/module.c b/src/module.c index 75c98f12d..1671934b8 100644 --- a/src/module.c +++ b/src/module.c @@ -5903,12 +5903,12 @@ size_t RM_MallocSize(void* ptr){ * Return the a number between 0 to 1 indicating * the amount of memory currently used. * 0 - no memory limit - * 1 and above, memory limit reached. + * 100 and above, memory limit reached. */ float RM_GetUsedMemoryPercentage(){ float level; getMaxmemoryState(NULL, NULL, NULL, &level); - return level; + return level * 100; } /* -------------------------------------------------------------------------- From c032dc45d1945db248af32f024ce457e9c94e083 Mon Sep 17 00:00:00 2001 From: "meir@redislabs.com" Date: Wed, 6 Nov 2019 12:17:09 +0200 Subject: [PATCH 3/4] changed GetUsedMemoryPresentage -> GetUsedMemoryRatio and return value between 0 and 1. --- src/module.c | 6 +++--- src/redismodule.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/module.c b/src/module.c index 1671934b8..4dbc57ad6 100644 --- a/src/module.c +++ b/src/module.c @@ -5905,10 +5905,10 @@ size_t RM_MallocSize(void* ptr){ * 0 - no memory limit * 100 and above, memory limit reached. */ -float RM_GetUsedMemoryPercentage(){ +float RM_GetUsedMemoryRatio(){ float level; getMaxmemoryState(NULL, NULL, NULL, &level); - return level * 100; + return level; } /* -------------------------------------------------------------------------- @@ -6991,6 +6991,6 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(BlockClientOnKeys); REGISTER_API(SignalKeyAsReady); REGISTER_API(GetBlockedClientReadyKey); - REGISTER_API(GetUsedMemoryPercentage); + REGISTER_API(GetUsedMemoryRatio); REGISTER_API(MallocSize); } diff --git a/src/redismodule.h b/src/redismodule.h index 259ea2532..c09e03541 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -633,7 +633,7 @@ int REDISMODULE_API_FUNC(RedisModule_CommandFilterArgDelete)(RedisModuleCommandF int REDISMODULE_API_FUNC(RedisModule_Fork)(RedisModuleForkDoneHandler cb, void *user_data); int REDISMODULE_API_FUNC(RedisModule_ExitFromChild)(int retcode); int REDISMODULE_API_FUNC(RedisModule_KillForkChild)(int child_pid); -float REDISMODULE_API_FUNC(RedisModule_GetUsedMemoryPercentage)(); +float REDISMODULE_API_FUNC(RedisModule_GetUsedMemoryRatio)(); size_t REDISMODULE_API_FUNC(RedisModule_MallocSize)(void* ptr); #endif @@ -844,7 +844,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int REDISMODULE_GET_API(Fork); REDISMODULE_GET_API(ExitFromChild); REDISMODULE_GET_API(KillForkChild); - REDISMODULE_GET_API(GetUsedMemoryPercentage); + REDISMODULE_GET_API(GetUsedMemoryRatio); REDISMODULE_GET_API(MallocSize); #endif From e45239e3501edfa7012955a76536dff98c87e2a5 Mon Sep 17 00:00:00 2001 From: "meir@redislabs.com" Date: Wed, 6 Nov 2019 12:26:03 +0200 Subject: [PATCH 4/4] fix documentation --- src/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module.c b/src/module.c index 4dbc57ad6..947cc3083 100644 --- a/src/module.c +++ b/src/module.c @@ -5903,7 +5903,7 @@ size_t RM_MallocSize(void* ptr){ * Return the a number between 0 to 1 indicating * the amount of memory currently used. * 0 - no memory limit - * 100 and above, memory limit reached. + * 1 and above, memory limit reached. */ float RM_GetUsedMemoryRatio(){ float level;