This commit is contained in:
Alex Cope 2023-06-30 09:39:49 -07:00
parent c894255fea
commit d346601067
5 changed files with 43 additions and 13 deletions

View File

@ -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)

View File

@ -429,14 +429,14 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev
maxmemory = static_cast<size_t>(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<size_t>(g_pserver->cron_malloc_stats.sys_total * free_mem_ratio);
sys_free_mem_buffer = static_cast<long>(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<size_t>(mem_threshold);
maxmemory = ((long)maxmemory < mem_threshold) ? maxmemory : static_cast<size_t>(mem_threshold);
}
}

31
src/meminfo.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "server.h"
#include <fstream>
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<std::streamsize>::max(), '\n');
}
return 0; // nothing found
}
# else
return 0;
# endif
}
size_t getMemAvailable() {
return getMemKey("MemAvailable:");
}
size_t getMemTotal() {
return getMemKey("MemTotal:");
}

View File

@ -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<size_t>(sysinf.totalram);
g_pserver->cron_malloc_stats.sys_free = static_cast<size_t>(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. */

View File

@ -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);