diff options
author | antirez <antirez@gmail.com> | 2012-04-21 19:20:03 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2012-04-21 20:34:45 +0200 |
commit | d3701d27141b8e400ccdf5fbf22c504d112fab63 (patch) | |
tree | a6a0e876e7db17d47fb3a8125c1b93d7dfef3bb1 /src | |
parent | fd72fe261dc8ac1f6450dfb6197391bb530ac5a0 (diff) | |
download | redis-d3701d27141b8e400ccdf5fbf22c504d112fab63.tar.gz |
Limit memory used by big SLOWLOG entries.
Two limits are added:
1) Up to SLOWLOG_ENTRY_MAX_ARGV arguments are logged.
2) Up to SLOWLOG_ENTRY_MAX_STRING bytes per argument are logged.
3) slowlog-max-len is set to 128 by default (was 1024).
The number of remaining arguments / bytes is logged in the entry
so that the user can understand better the nature of the logged command.
Diffstat (limited to 'src')
-rw-r--r-- | src/redis.h | 2 | ||||
-rw-r--r-- | src/slowlog.c | 35 | ||||
-rw-r--r-- | src/slowlog.h | 3 |
3 files changed, 33 insertions, 7 deletions
diff --git a/src/redis.h b/src/redis.h index 42a9b5811..3b9d4e78f 100644 --- a/src/redis.h +++ b/src/redis.h @@ -52,7 +52,7 @@ #define REDIS_AOF_REWRITE_MIN_SIZE (1024*1024) #define REDIS_AOF_REWRITE_ITEMS_PER_CMD 64 #define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000 -#define REDIS_SLOWLOG_MAX_LEN 64 +#define REDIS_SLOWLOG_MAX_LEN 128 #define REDIS_MAX_CLIENTS 10000 #define REDIS_REPL_TIMEOUT 60 diff --git a/src/slowlog.c b/src/slowlog.c index cfd66dc63..53c44a017 100644 --- a/src/slowlog.c +++ b/src/slowlog.c @@ -16,13 +16,36 @@ * this function. */ slowlogEntry *slowlogCreateEntry(robj **argv, int argc, long long duration) { slowlogEntry *se = zmalloc(sizeof(*se)); - int j; + int j, slargc = argc; + + if (slargc > SLOWLOG_ENTRY_MAX_ARGC) slargc = SLOWLOG_ENTRY_MAX_ARGC; + se->argc = slargc; + se->argv = zmalloc(sizeof(robj*)*slargc); + for (j = 0; j < slargc; j++) { + /* Logging too many arguments is a useless memory waste, so we stop + * at SLOWLOG_ENTRY_MAX_ARGC, but use the last argument to specify + * how many remaining arguments there were in the original command. */ + if (slargc != argc && j == slargc-1) { + se->argv[j] = createObject(REDIS_STRING, + sdscatprintf(sdsempty(),"... (%d more arguments)", + argc-slargc+1)); + } else { + /* Trim too long strings as well... */ + if (argv[j]->type == REDIS_STRING && + argv[j]->encoding == REDIS_ENCODING_RAW && + sdslen(argv[j]->ptr) > SLOWLOG_ENTRY_MAX_STRING) + { + sds s = sdsnewlen(argv[j]->ptr, SLOWLOG_ENTRY_MAX_STRING); - se->argc = argc; - se->argv = zmalloc(sizeof(robj*)*argc); - for (j = 0; j < argc; j++) { - se->argv[j] = argv[j]; - incrRefCount(argv[j]); + s = sdscatprintf(s,"... (%lu more bytes)", + (unsigned long) + sdslen(argv[j]->ptr) - SLOWLOG_ENTRY_MAX_STRING); + se->argv[j] = createObject(REDIS_STRING,s); + } else { + se->argv[j] = argv[j]; + incrRefCount(argv[j]); + } + } } se->time = time(NULL); se->duration = duration; diff --git a/src/slowlog.h b/src/slowlog.h index bad770db4..bcc961cc9 100644 --- a/src/slowlog.h +++ b/src/slowlog.h @@ -1,3 +1,6 @@ +#define SLOWLOG_ENTRY_MAX_ARGC 32 +#define SLOWLOG_ENTRY_MAX_STRING 128 + /* This structure defines an entry inside the slow log list */ typedef struct slowlogEntry { robj **argv; |