mirror of https://github.com/redis/redis.git
				
				
				
			Merge pull request #6346 from MeirShpilraien/expose_zmalloc_capabilities
Expose used memory to modules via redismodule api
This commit is contained in:
		
						commit
						fef1e63c70
					
				
							
								
								
									
										22
									
								
								src/module.c
								
								
								
								
							
							
						
						
									
										22
									
								
								src/module.c
								
								
								
								
							| 
						 | 
				
			
			@ -5995,6 +5995,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_GetUsedMemoryRatio(){
 | 
			
		||||
    float level;
 | 
			
		||||
    getMaxmemoryState(NULL, NULL, NULL, &level);
 | 
			
		||||
    return level;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* --------------------------------------------------------------------------
 | 
			
		||||
 * Scanning keyspace and hashes
 | 
			
		||||
 * -------------------------------------------------------------------------- */
 | 
			
		||||
| 
						 | 
				
			
			@ -7360,6 +7380,8 @@ void moduleRegisterCoreAPI(void) {
 | 
			
		|||
    REGISTER_API(BlockClientOnKeys);
 | 
			
		||||
    REGISTER_API(SignalKeyAsReady);
 | 
			
		||||
    REGISTER_API(GetBlockedClientReadyKey);
 | 
			
		||||
    REGISTER_API(GetUsedMemoryRatio);
 | 
			
		||||
    REGISTER_API(MallocSize);
 | 
			
		||||
    REGISTER_API(ScanCursorCreate);
 | 
			
		||||
    REGISTER_API(ScanCursorDestroy);
 | 
			
		||||
    REGISTER_API(ScanCursorRestart);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -649,6 +649,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_GetUsedMemoryRatio)();
 | 
			
		||||
size_t REDISMODULE_API_FUNC(RedisModule_MallocSize)(void* ptr);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define RedisModule_IsAOFClient(id) ((id) == UINT64_MAX)
 | 
			
		||||
| 
						 | 
				
			
			@ -871,6 +873,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(GetUsedMemoryRatio);
 | 
			
		||||
    REDISMODULE_GET_API(MallocSize);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (RedisModule_IsModuleNameBusy && RedisModule_IsModuleNameBusy(name)) return REDISMODULE_ERR;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue