Skip to content

Commit bd005de

Browse files
author
michalbiesek
committed
Move logic of adjust threshold to zmalloc module
1 parent 4b72f11 commit bd005de

File tree

9 files changed

+90
-114
lines changed

9 files changed

+90
-114
lines changed

redis.conf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,8 +1880,5 @@ dynamic-threshold-min 24
18801880
# Maximum value of dynamic threshold
18811881
dynamic-threshold-max 10000
18821882

1883-
# DRAM/PMEM ratio period measured in miliseconds
1884-
memory-ratio-check-period 100
1885-
18861883
# Keep hashtable structure always on DRAM
18871884
hashtable-on-dram yes

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ endif
225225

226226
REDIS_SERVER_NAME=redis-server
227227
REDIS_SENTINEL_NAME=redis-sentinel
228-
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 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 lolwut.o lolwut5.o lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o setcpuaffinity.o pmem.o
228+
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 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 lolwut.o lolwut5.o lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o setcpuaffinity.o
229229
REDIS_CLI_NAME=redis-cli
230230
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o ae.o crcspeed.o crc64.o siphash.o crc16.o
231231
REDIS_BENCHMARK_NAME=redis-benchmark

src/config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,6 @@ standardConfig configs[] = {
22292229
createIntConfig("hz", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.config_hz, CONFIG_DEFAULT_HZ, INTEGER_CONFIG, NULL, updateHZ),
22302230
createIntConfig("min-replicas-to-write", "min-slaves-to-write", MODIFIABLE_CONFIG, 0, INT_MAX, server.repl_min_slaves_to_write, 0, INTEGER_CONFIG, NULL, updateGoodSlaves),
22312231
createIntConfig("min-replicas-max-lag", "min-slaves-max-lag", MODIFIABLE_CONFIG, 0, INT_MAX, server.repl_min_slaves_max_lag, 10, INTEGER_CONFIG, NULL, updateGoodSlaves),
2232-
createIntConfig("memory-ratio-check-period", NULL, IMMUTABLE_CONFIG, 1, INT_MAX, server.ratio_check_period, 100, INTEGER_CONFIG, NULL, NULL),
22332232

22342233
/* Unsigned int configs */
22352234
createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, server.maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients),

src/pmem.c

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/server.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,9 +1697,6 @@ void databasesCron(void) {
16971697
}
16981698
}
16991699

1700-
/* Adjust PMEM threshold. */
1701-
adjustPmemThresholdCycle();
1702-
17031700
/* Defrag keys gradually. */
17041701
activeDefragCycle();
17051702

@@ -2738,6 +2735,28 @@ void resetServerStats(void) {
27382735
server.aof_delayed_fsync = 0;
27392736
}
27402737

2738+
/* Initialize the pmem threshold. */
2739+
static void pmemThresholdInit(void) {
2740+
switch(server.memory_alloc_policy) {
2741+
case MEM_POLICY_ONLY_DRAM:
2742+
zmalloc_set_threshold(UINT_MAX);
2743+
break;
2744+
case MEM_POLICY_ONLY_PMEM:
2745+
zmalloc_set_threshold(0U);
2746+
break;
2747+
case MEM_POLICY_THRESHOLD:
2748+
zmalloc_set_threshold(server.static_threshold);
2749+
break;
2750+
case MEM_POLICY_RATIO:
2751+
zmalloc_set_threshold(server.initial_dynamic_threshold);
2752+
zmalloc_set_ratio(server.target_pmem_dram_ratio);
2753+
zmalloc_set_threshold_range(server.dynamic_threshold_min, server.dynamic_threshold_max);
2754+
break;
2755+
default:
2756+
serverAssert(NULL);
2757+
}
2758+
}
2759+
27412760
void initServer(void) {
27422761
int j;
27432762

src/server.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,6 @@ struct redisServer {
13471347
unsigned int dynamic_threshold_max; /* Maximum value of dynamic threshold */
13481348
ratioDramPmemConfig dram_pmem_ratio; /* DRAM/Persistent Memory ratio */
13491349
double target_pmem_dram_ratio; /* Target PMEM/DRAM ratio */
1350-
int ratio_check_period; /* Period of checking ratio in Cron*/
13511350
int hashtable_on_dram; /* Keep hashtable always on DRAM */
13521351
/* Blocked clients */
13531352
unsigned int blocked_clients; /* # of clients executing a blocking cmd.*/
@@ -2215,10 +2214,6 @@ uint64_t dictSdsHash(const void *key);
22152214
int dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);
22162215
void dictSdsDestructor(void *privdata, void *val);
22172216

2218-
/* pmem.c - Handling Persistent Memory */
2219-
void pmemThresholdInit(void);
2220-
void adjustPmemThresholdCycle(void);
2221-
22222217
/* Git SHA1 */
22232218
char *redisGitSHA1(void);
22242219
char *redisGitDirty(void);

src/zmalloc.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
#include <limits.h>
32+
#include <math.h>
3233
#include <stdio.h>
3334
#include <stdlib.h>
3435
#include <stdint.h>
@@ -149,8 +150,61 @@ static void *zrealloc_pmem(void *ptr, size_t size) {
149150
static size_t pmem_threshold = UINT_MAX;
150151
static size_t used_memory = 0;
151152
static size_t used_pmem_memory = 0;
153+
static double pmem_dram_ratio = 0.0;
154+
static int use_ratio = 0;
155+
static unsigned int dynamic_min_threshold = 0;
156+
static unsigned int dynamic_max_threshold = 0;
157+
static int zmalloc_counter = 0;
158+
152159
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
153160

161+
#define THRESHOLD_STEP_NORMAL 0.05
162+
#define THRESHOLD_STEP_AGGRESIVE (THRESHOLD_STEP_NORMAL*5)
163+
#define THRESHOLD_UP(val, step) ((size_t)ceil((1+(step))*val))
164+
#define THRESHOLD_DOWN(val, step) ((size_t)floor((1-(step))*val))
165+
166+
static inline size_t absDiff(size_t a, size_t b) {
167+
return a > b ? (a - b) : (b - a);
168+
}
169+
170+
static void adjustPmemThresholdCycle(void) {
171+
if (use_ratio) {
172+
if (zmalloc_counter++ == 1000) {
173+
zmalloc_counter = 0;
174+
/* Difference between target ratio and current ratio in last checkpoint*/
175+
static double ratio_diff_checkpoint;
176+
/* PMEM and DRAM utilization in last checkpoint*/
177+
static size_t total_memory_checkpoint;
178+
size_t pmem_memory = zmalloc_used_pmem_memory();
179+
size_t dram_memory = zmalloc_used_memory();
180+
size_t total_memory_current = pmem_memory + dram_memory;
181+
// do not modify threshold when change in memory usage is too small
182+
if (absDiff(total_memory_checkpoint, total_memory_current) > 100) {
183+
double current_ratio = (double)pmem_memory/dram_memory;
184+
double current_ratio_diff = fabs(current_ratio - pmem_dram_ratio);
185+
if (current_ratio_diff > 0.02) {
186+
//current ratio is worse than moment before
187+
double variableMultipler = current_ratio/pmem_dram_ratio;
188+
double step = (current_ratio_diff < ratio_diff_checkpoint) ?
189+
variableMultipler*THRESHOLD_STEP_NORMAL : variableMultipler*THRESHOLD_STEP_AGGRESIVE;
190+
size_t threshold = zmalloc_get_threshold();
191+
if (pmem_dram_ratio < current_ratio) {
192+
size_t higher_threshold = THRESHOLD_UP(threshold,step);
193+
if (higher_threshold <= dynamic_max_threshold)
194+
zmalloc_set_threshold(higher_threshold);
195+
} else {
196+
size_t lower_threshold = THRESHOLD_DOWN(threshold,step);
197+
if (lower_threshold >= dynamic_min_threshold)
198+
zmalloc_set_threshold(lower_threshold);
199+
}
200+
}
201+
ratio_diff_checkpoint = current_ratio_diff;
202+
}
203+
total_memory_checkpoint = total_memory_current;
204+
}
205+
}
206+
}
207+
154208
static void zmalloc_default_oom(size_t size) {
155209
fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",
156210
size);
@@ -263,6 +317,7 @@ static void *zrealloc_pmem(void *ptr, size_t size) {
263317
#endif
264318

265319
void *zmalloc(size_t size) {
320+
adjustPmemThresholdCycle();
266321
return (size < pmem_threshold) ? zmalloc_dram(size) : zmalloc_pmem(size);
267322
}
268323

@@ -299,6 +354,7 @@ void *zcalloc_dram(size_t size) {
299354
}
300355

301356
void *zcalloc(size_t size) {
357+
adjustPmemThresholdCycle();
302358
return (size < pmem_threshold) ? zcalloc_dram(size) : zcalloc_pmem(size);
303359
}
304360

@@ -418,6 +474,15 @@ void zmalloc_set_threshold(size_t threshold) {
418474
pmem_threshold = threshold;
419475
}
420476

477+
void zmalloc_set_ratio(double ratio) {
478+
use_ratio = 1;
479+
pmem_dram_ratio = ratio;
480+
}
481+
void zmalloc_set_threshold_range(unsigned int min, unsigned int max) {
482+
dynamic_min_threshold = min;
483+
dynamic_max_threshold = max;
484+
}
485+
421486
/* Get the RSS information in an OS-specific way.
422487
*
423488
* WARNING: the function zmalloc_get_rss() is not designed to be fast

src/zmalloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ size_t zmalloc_get_smap_bytes_by_field(char *field, long pid);
104104
size_t zmalloc_get_memory_size(void);
105105
void zlibc_free(void *ptr);
106106
void zmalloc_set_threshold(size_t threshold);
107+
void zmalloc_set_ratio(double ratio);
108+
void zmalloc_set_threshold_range(unsigned int min, unsigned int max);
107109
size_t zmalloc_get_threshold(void);
108110
void *zmalloc_dram(size_t size);
109111
void *zcalloc_dram(size_t size);

tests/unit/introspection.tcl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ start_server {tags {"introspection"}} {
103103
initial-dynamic-threshold
104104
dynamic-threshold-min
105105
dynamic-threshold-max
106-
memory-ratio-check-period
107106
hashtable-on-dram
108107
}
109108

0 commit comments

Comments
 (0)