diff --git a/src/config.c b/src/config.c index 9698bc7c9a..1de0925cc7 100644 --- a/src/config.c +++ b/src/config.c @@ -2509,7 +2509,7 @@ static int updateMaxmemoryReserved(const char **err) { server.maxmemory_reserved_scale = 60; } } - server.maxmemory_available = (unsigned long long)server.maxmemory / 100.0 * (100 - server.maxmemory_reserved_scale); + calculateMaxAvailableMemory(); return 1; } @@ -2524,8 +2524,7 @@ static int updateMaxmemory(const char **err) { "depending on the maxmemory-policy.", server.maxmemory, used); } - server.maxmemory_available = - (unsigned long long)server.maxmemory / 100.0 * (100 - server.maxmemory_reserved_scale); + calculateMaxAvailableMemory(); startEvictionTimeProc(); } return 1; diff --git a/src/evict.c b/src/evict.c index 32fbf1a947..42a32a4449 100644 --- a/src/evict.c +++ b/src/evict.c @@ -405,8 +405,9 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev if (server.maxmemory_reserved_scale) { if (mem_reported <= server.maxmemory_available && !level) return C_OK; - } else if (mem_reported <= server.maxmemory && !level) + } else if (mem_reported <= server.maxmemory && !level) { return C_OK; + } /* Remove the size of replicas output buffers and AOF buffer from the * count of used memory. */ @@ -416,28 +417,16 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev /* Compute the ratio of memory usage. */ if (level) { - if (server.maxmemory_reserved_scale) - *level = (float)mem_used / (float)server.maxmemory_available; - else - *level = (float)mem_used / (float)server.maxmemory; + *level = (float)mem_used / (float)server.maxmemory_available; } - if (server.maxmemory_reserved_scale) { - if (mem_reported <= server.maxmemory_available) return C_OK; - } else if (mem_reported <= server.maxmemory) - return C_OK; + if (mem_reported <= server.maxmemory_available) return C_OK; /* Check if we are still over the memory limit. */ - if (server.maxmemory_reserved_scale) { - if (mem_used <= server.maxmemory_available) return C_OK; - } else if (mem_used <= server.maxmemory) - return C_OK; + if (mem_used <= server.maxmemory_available) return C_OK; /* Compute how much memory we need to free. */ - if (server.maxmemory_reserved_scale) { - mem_tofree = mem_used - server.maxmemory_available; - } else - mem_tofree = mem_used - server.maxmemory; + mem_tofree = mem_used - server.maxmemory_available; if (logical) *logical = mem_used; if (tofree) *tofree = mem_tofree; diff --git a/src/server.c b/src/server.c index a0b59f598e..11459c4cd7 100644 --- a/src/server.c +++ b/src/server.c @@ -2622,8 +2622,7 @@ void initServer(void) { resetReplicationBuffer(); if (server.maxmemory) { - server.maxmemory_available = - (unsigned long long)server.maxmemory / 100.0 * (100 - server.maxmemory_reserved_scale); + calculateMaxAvailableMemory(); } /* Make sure the locale is set on startup based on the config file. */ diff --git a/src/server.h b/src/server.h index 9eeb1b0f96..c4d8c3cc36 100644 --- a/src/server.h +++ b/src/server.h @@ -3012,6 +3012,9 @@ void trimStringObjectIfNeeded(robj *o, int trim_small_values); static inline int canUseSharedObject(void) { return server.maxmemory == 0 || !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS); } +static inline void calculateMaxAvailableMemory(void) { + server.maxmemory_available = (unsigned long long)server.maxmemory / 100.0 * (100 - server.maxmemory_reserved_scale); +} #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR) /* Synchronous I/O with timeout */