Skip to content

Commit f6e7d60

Browse files
committed
WIP. memory leak in jemalloc when do reload
1 parent 9e62670 commit f6e7d60

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

src/bio.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,19 @@ void bioRestore(pthread_t *threads) {
9898
memcpy(bio_threads, threads, sizeof(bio_threads));
9999

100100
for (j = 0; j < BIO_NUM_OPS; j++) {
101-
pthread_mutex_init(&bio_mutex[j],NULL);
102-
pthread_cond_init(&bio_newjob_cond[j],NULL);
103-
pthread_cond_init(&bio_step_cond[j],NULL);
104101
bio_jobs[j] = listCreate();
105102
bio_pending[j] = 0;
106103
}
107104
}
108105

109-
static bool bioEmpty() {
106+
static unsigned bioEmpty() {
110107
int i;
111108
for (i = 0; i < BIO_NUM_OPS;i++) {
112109
if (bio_pending[i] > 0) {
113-
return false;
110+
return 0;
114111
}
115112
}
116-
return true;
113+
return 1;
117114
}
118115

119116
void bioSave(pthread_t *threads) {

src/redis-server-wrap.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ int main(int argc, char ** argv)
1919
RELOAD reload = NULL;
2020
int ret;
2121

22-
ctx.reload = false;
22+
ctx.reload = 0;
2323

2424
strncpy(ctx.hashseed, "1234567890123456", 16);
25+
ctx.reloadTimes = 0;
2526

2627
for(;;) {
2728
printf("load dynamic library\n");
@@ -50,7 +51,8 @@ int main(int argc, char ** argv)
5051
fprintf(stderr, "dlclose failed\n");
5152
exit(EXIT_FAILURE);
5253
}
53-
ctx.reload = true;
54+
ctx.reload = 1;
55+
ctx.reloadTimes++;
5456
}
5557
exit(EXIT_SUCCESS);
5658
}

src/server.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include <sys/utsname.h>
5656
#include <locale.h>
5757
#include <sys/socket.h>
58+
#include <mcheck.h>
5859

5960
/* Our shared "common" objects */
6061

@@ -1525,6 +1526,26 @@ void initServerConfig(void) {
15251526
server.watchdog_period = 0;
15261527
}
15271528

1529+
void reloadServerCommands() {
1530+
/* release old dict */
1531+
dictRelease(server.commands);
1532+
dictRelease(server.orig_commands);
1533+
1534+
/* re-add command to table */
1535+
server.commands = dictCreate(&commandTableDictType,NULL);
1536+
server.orig_commands = dictCreate(&commandTableDictType,NULL);
1537+
populateCommandTable();
1538+
server.delCommand = lookupCommandByCString("del");
1539+
server.multiCommand = lookupCommandByCString("multi");
1540+
server.lpushCommand = lookupCommandByCString("lpush");
1541+
server.lpopCommand = lookupCommandByCString("lpop");
1542+
server.rpopCommand = lookupCommandByCString("rpop");
1543+
server.sremCommand = lookupCommandByCString("srem");
1544+
server.execCommand = lookupCommandByCString("exec");
1545+
server.expireCommand = lookupCommandByCString("expire");
1546+
server.pexpireCommand = lookupCommandByCString("pexpire");
1547+
}
1548+
15281549
extern char **environ;
15291550

15301551
/* Restart the server, executing the same executable that started this
@@ -3975,6 +3996,7 @@ int serverRun(int argc, char **argv, struct wrapperContext *ctx) {
39753996
serverRestore(&ctx->server, &ctx->shared);
39763997
bioRestore(ctx->bioThreads);
39773998
evictionPoolRestore(ctx->evictionpool);
3999+
zmalloc_used_memory_restore(ctx->usedmemory);
39784000
}
39794001

39804002
#ifdef REDIS_TEST
@@ -4022,6 +4044,8 @@ int serverRun(int argc, char **argv, struct wrapperContext *ctx) {
40224044
for (j = 0; j < argc; j++) server.exec_argv[j] = zstrdup(argv[j]);
40234045
} else {
40244046
moduleReloadModulesSystem();
4047+
/* command adress may be changed, so reload it */
4048+
reloadServerCommands();
40254049
}
40264050

40274051

@@ -4041,7 +4065,7 @@ int serverRun(int argc, char **argv, struct wrapperContext *ctx) {
40414065
else if (strstr(argv[0],"redis-check-aof") != NULL)
40424066
redis_check_aof_main(argc,argv);
40434067

4044-
if (argc >= 2) {
4068+
if (!ctx->reload && argc >= 2) {
40454069
j = 1; /* First option to parse in argv[] */
40464070
sds options = sdsempty();
40474071
char *configfile = NULL;
@@ -4125,15 +4149,14 @@ int serverRun(int argc, char **argv, struct wrapperContext *ctx) {
41254149

41264150
server.supervised = redisIsSupervised(server.supervised_mode);
41274151
int background = server.daemonize && !server.supervised;
4128-
if (background) daemonize();
4129-
41304152
if (!ctx->reload) {
4153+
if (background) daemonize();
41314154
initServer();
4155+
redisSetProcTitle(argv[0]);
41324156
} else {
41334157
reloadServer();
41344158
}
41354159
if (background || server.pidfile) createPidFile();
4136-
redisSetProcTitle(argv[0]);
41374160
redisAsciiArt();
41384161
checkTcpBacklogSettings();
41394162

@@ -4144,7 +4167,7 @@ int serverRun(int argc, char **argv, struct wrapperContext *ctx) {
41444167
linuxMemoryWarnings();
41454168
#endif
41464169
moduleLoadFromQueue();
4147-
if (ctx->reload == 0) {
4170+
if (!ctx->reload) {
41484171
loadDataFromDisk();
41494172
}
41504173
if (server.cluster_enabled) {
@@ -4172,10 +4195,18 @@ int serverRun(int argc, char **argv, struct wrapperContext *ctx) {
41724195
aeSetAfterSleepProc(server.el,afterSleep);
41734196
aeMain(server.el);
41744197

4198+
if (ctx->reloadTimes == 0) {
4199+
mtrace();
4200+
}
4201+
if (ctx->reloadTimes == 200) {
4202+
muntrace();
4203+
}
4204+
41754205
/* save server ctx to s */
41764206
serverSave(&ctx->server, &ctx->shared);
41774207
bioSave(ctx->bioThreads);
41784208
evictionPoolSave(&ctx->evictionpool);
4209+
zmalloc_used_memory_save(&ctx->usedmemory);
41794210

41804211
return 0;
41814212
}

src/server.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,10 @@ struct wrapperContext {
12891289
struct sharedObjectsStruct shared;
12901290
pthread_t bioThreads[BIO_NUM_OPS];
12911291
void *evictionpool;
1292-
bool reload;
1292+
unsigned reload:1;
12931293
char hashseed[16];
1294+
unsigned int reloadTimes;
1295+
size_t usedmemory;
12941296
};
12951297
/*-----------------------------------------------------------------------------
12961298
* Extern declarations

src/zmalloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ size_t zmalloc_used_memory(void) {
215215
return um;
216216
}
217217

218+
void zmalloc_used_memory_save(size_t *m) {
219+
size_t um = zmalloc_used_memory();
220+
memcpy(m, &um, sizeof(size_t));
221+
}
222+
223+
void zmalloc_used_memory_restore(size_t m) {
224+
used_memory = m;
225+
}
226+
218227
void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) {
219228
zmalloc_oom_handler = oom_handler;
220229
}

src/zmalloc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ size_t zmalloc_get_smap_bytes_by_field(char *field, long pid);
8686
size_t zmalloc_get_memory_size(void);
8787
void zlibc_free(void *ptr);
8888

89+
void zmalloc_used_memory_restore(size_t m);
90+
void zmalloc_used_memory_save(size_t *m);
91+
8992
#ifdef HAVE_DEFRAG
9093
void zfree_no_tcache(void *ptr);
9194
void *zmalloc_no_tcache(size_t size);

0 commit comments

Comments
 (0)