From 4c3d9341fd9038a8dac0fed23dec171e17224905 Mon Sep 17 00:00:00 2001 From: Alex Cope Date: Tue, 27 Jun 2023 14:18:14 -0700 Subject: [PATCH] port changes to 6.3 --- src/evict.cpp | 20 +++++++++++++++++++- src/server.cpp | 10 ++++++++++ src/server.h | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/evict.cpp b/src/evict.cpp index 94bc132c1..aa6932e8b 100644 --- a/src/evict.cpp +++ b/src/evict.cpp @@ -421,10 +421,22 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev if (total) *total = mem_reported; size_t maxmemory = g_pserver->maxmemory; if (fPreSnapshot) - maxmemory = static_cast(maxmemory * 0.9); // derate memory by 10% since we won't be able to free during snapshot + maxmemory = static_cast(maxmemory*0.9); // derate memory by 10% since we won't be able to free during snapshot if (g_pserver->FRdbSaveInProgress()) maxmemory = static_cast(maxmemory*1.2); + /* If there is less than 10% free system memory, force eviction */ + bool mem_rss_max_exceeded; + if (g_pserver->cron_malloc_stats.sys_total) { + size_t mem_rss_max = static_cast(g_pserver->cron_malloc_stats.sys_total * 0.9); + mem_rss_max_exceeded = g_pserver->cron_malloc_stats.process_rss > mem_rss_max; + if (mem_rss_max_exceeded) { + /* This will always set maxmemory < mem_reported */ + float frag_ratio = (float)g_pserver->cron_malloc_stats.process_rss / (float)mem_reported; + maxmemory = static_cast((float)mem_rss_max / frag_ratio); + } + } + /* We may return ASAP if there is no need to compute the level. */ int return_ok_asap = !maxmemory || mem_reported <= maxmemory; if (return_ok_asap && !level) return C_OK; @@ -435,6 +447,12 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev size_t overhead = freeMemoryGetNotCountedMemory(); mem_used = (mem_used > overhead) ? mem_used-overhead : 0; + /* If we've exceeded max RSS memory, we want to force evictions no matter + * what so we also offset the overhead from maxmemory. */ + if (mem_rss_max_exceeded) { + maxmemory = (maxmemory > overhead) ? maxmemory-overhead : 0; + } + /* Compute the ratio of memory usage. */ if (level) { if (!maxmemory) { diff --git a/src/server.cpp b/src/server.cpp index 663e4e8fc..d1ef097c4 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -70,6 +70,7 @@ #ifdef __linux__ #include #include +#include #endif int g_fTestMode = false; @@ -2312,6 +2313,15 @@ void cronUpdateMemoryStats() { g_pserver->cron_malloc_stats.allocator_active = g_pserver->cron_malloc_stats.allocator_resident; if (!g_pserver->cron_malloc_stats.allocator_allocated) g_pserver->cron_malloc_stats.allocator_allocated = g_pserver->cron_malloc_stats.zmalloc_used; + + #ifdef __linux__ + struct sysinfo sysinf; + memset(&sysinf, 0, sizeof sysinf); + if (!sysinfo(&sysinf)) { + g_pserver->cron_malloc_stats.sys_total = static_cast(sysinf.totalram); + } + #endif + } } diff --git a/src/server.h b/src/server.h index 422cb9495..3a5ed977f 100644 --- a/src/server.h +++ b/src/server.h @@ -2014,6 +2014,7 @@ struct malloc_stats { size_t allocator_allocated; size_t allocator_active; size_t allocator_resident; + size_t sys_total; }; typedef struct socketFds {