From d3466010678c4643fcc8e40ce85c2ffca1ba572c Mon Sep 17 00:00:00 2001 From: Alex Cope Date: Fri, 30 Jun 2023 09:39:49 -0700 Subject: [PATCH] llu --- src/Makefile | 2 +- src/evict.cpp | 4 ++-- src/meminfo.cpp | 31 +++++++++++++++++++++++++++++++ src/server.cpp | 14 +++++--------- src/server.h | 5 ++++- 5 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 src/meminfo.cpp diff --git a/src/Makefile b/src/Makefile index 01c24b0df..007a0879d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -384,7 +384,7 @@ endif REDIS_SERVER_NAME=keydb-server$(PROG_SUFFIX) REDIS_SENTINEL_NAME=keydb-sentinel$(PROG_SUFFIX) -REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o t_nhash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o motd_server.o timeout.o setcpuaffinity.o AsyncWorkQueue.o snapshot.o storage/teststorageprovider.o keydbutils.o StorageCache.o monotonic.o cli_common.o mt19937-64.o $(ASM_OBJ) $(STORAGE_OBJ) +REDIS_SERVER_OBJ=adlist.o meminfo.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o t_nhash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o motd_server.o timeout.o setcpuaffinity.o AsyncWorkQueue.o snapshot.o storage/teststorageprovider.o keydbutils.o StorageCache.o monotonic.o cli_common.o mt19937-64.o $(ASM_OBJ) $(STORAGE_OBJ) KEYDB_SERVER_OBJ=SnapshotPayloadParseState.o REDIS_CLI_NAME=keydb-cli$(PROG_SUFFIX) REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crcspeed.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o motd_client.o monotonic.o cli_common.o mt19937-64.o $(ASM_OBJ) diff --git a/src/evict.cpp b/src/evict.cpp index 73808c41a..bfc923ecc 100644 --- a/src/evict.cpp +++ b/src/evict.cpp @@ -429,14 +429,14 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev maxmemory = static_cast(maxmemory*1.2); /* If free system memory is below a certain threshold, force eviction */ - long sys_free_mem_buffer; + long sys_free_mem_buffer = 0; if (g_pserver->force_eviction_percent && g_pserver->cron_malloc_stats.sys_total) { float free_mem_ratio = (float)(100 - g_pserver->force_eviction_percent)/100; size_t min_free_mem = static_cast(g_pserver->cron_malloc_stats.sys_total * free_mem_ratio); sys_free_mem_buffer = static_cast(g_pserver->cron_malloc_stats.sys_free - min_free_mem); if (sys_free_mem_buffer < 0) { long mem_threshold = mem_reported + sys_free_mem_buffer; - maxmemory = (maxmemory < mem_threshold) ? maxmemory : static_cast(mem_threshold); + maxmemory = ((long)maxmemory < mem_threshold) ? maxmemory : static_cast(mem_threshold); } } diff --git a/src/meminfo.cpp b/src/meminfo.cpp new file mode 100644 index 000000000..a3ec46805 --- /dev/null +++ b/src/meminfo.cpp @@ -0,0 +1,31 @@ +#include "server.h" +#include + +static size_t getMemKey(std::string key) { +# ifdef __linux__ + std::string token; + std::ifstream f("/proc/meminfo"); + while (f >> token) { + if (token == key) { + size_t mem_val; + if (f >> mem_val) { + return mem_val; + } else { + return 0; + } + f.ignore(std::numeric_limits::max(), '\n'); + } + return 0; // nothing found + } +# else + return 0; +# endif +} + +size_t getMemAvailable() { + return getMemKey("MemAvailable:"); +} + +size_t getMemTotal() { + return getMemKey("MemTotal:"); +} \ No newline at end of file diff --git a/src/server.cpp b/src/server.cpp index da17da0e8..23ac4750b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2314,17 +2314,10 @@ void cronUpdateMemoryStats() { if (!g_pserver->cron_malloc_stats.allocator_allocated) g_pserver->cron_malloc_stats.allocator_allocated = g_pserver->cron_malloc_stats.zmalloc_used; - #ifdef __linux__ if (g_pserver->force_eviction_percent) { - struct sysinfo sysinf; - memset(&sysinf, 0, sizeof sysinf); - if (!sysinfo(&sysinf)) { - g_pserver->cron_malloc_stats.sys_total = static_cast(sysinf.totalram); - g_pserver->cron_malloc_stats.sys_free = static_cast(sysinf.freeram); - } + g_pserver->cron_malloc_stats.sys_available = getMemAvailable(); + serverLog(LL_WARNING, "Setting sys_available:%llu", g_pserver->cron_malloc_stats.sys_available); } - #endif - } } @@ -4044,6 +4037,8 @@ void initServer(void) { g_pserver->cron_malloc_stats.allocator_allocated = 0; g_pserver->cron_malloc_stats.allocator_active = 0; g_pserver->cron_malloc_stats.allocator_resident = 0; + g_pserver->cron_malloc_stats.sys_available = 0; + g_pserver->cron_malloc_stats.sys_total = g_pserver->force_eviction_percent ? getMemTotal() : 0; g_pserver->lastbgsave_status = C_OK; g_pserver->aof_last_write_status = C_OK; g_pserver->aof_last_write_errno = 0; @@ -4051,6 +4046,7 @@ void initServer(void) { g_pserver->mvcc_tstamp = 0; + /* Create the timer callback, this is our way to process many background * operations incrementally, like clients timeout, eviction of unaccessed * expired keys and so forth. */ diff --git a/src/server.h b/src/server.h index d04aeccad..475ef3ad3 100644 --- a/src/server.h +++ b/src/server.h @@ -2015,7 +2015,7 @@ struct malloc_stats { size_t allocator_active; size_t allocator_resident; size_t sys_total; - size_t sys_free; + size_t sys_available; }; typedef struct socketFds { @@ -3663,6 +3663,9 @@ unsigned long LFUDecrAndReturn(robj_roptr o); #define EVICT_FAIL 2 int performEvictions(bool fPreSnapshot); +/* meminfo.cpp -- get memory info from /proc/memoryinfo for linux distros */ +size_t getMemAvailable(); +size_t getMemTotal(); /* Keys hashing / comparison functions for dict.c hash tables. */ uint64_t dictSdsHash(const void *key);