From cfb421fe3aa57d4d3cd712d72387889a876c34d5 Mon Sep 17 00:00:00 2001 From: "zhaozhao.zz" Date: Thu, 29 Nov 2018 01:01:47 +0800 Subject: [PATCH] MEMORY command: make USAGE more accurate In MEMORY USAGE command, we count the key argv[2] into usage, but the argument in command may contains free spaces because of sdsMakeRoomFor. But the key in db never contains free spaces because we use sdsdup when dbAdd, so using the real key to count the usage is more accurate. --- src/object.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/object.c b/src/object.c index 87b32f428..48ffa42b9 100644 --- a/src/object.c +++ b/src/object.c @@ -1285,8 +1285,6 @@ NULL * * Usage: MEMORY usage */ void memoryCommand(client *c) { - robj *o; - if (!strcasecmp(c->argv[1]->ptr,"help") && c->argc == 2) { const char *help[] = { "DOCTOR - Return memory problems reports.", @@ -1298,6 +1296,7 @@ NULL }; addReplyHelp(c, help); } else if (!strcasecmp(c->argv[1]->ptr,"usage") && c->argc >= 3) { + dictEntry *de; long long samples = OBJ_COMPUTE_SIZE_DEF_SAMPLES; for (int j = 3; j < c->argc; j++) { if (!strcasecmp(c->argv[j]->ptr,"samples") && @@ -1316,10 +1315,12 @@ NULL return; } } - if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) - == NULL) return; - size_t usage = objectComputeSize(o,samples); - usage += sdsAllocSize(c->argv[2]->ptr); + if ((de = dictFind(c->db->dict,c->argv[2]->ptr)) == NULL) { + addReply(c, shared.nullbulk); + return; + } + size_t usage = objectComputeSize(dictGetVal(de),samples); + usage += sdsAllocSize(dictGetKey(de)); usage += sizeof(dictEntry); addReplyLongLong(c,usage); } else if (!strcasecmp(c->argv[1]->ptr,"stats") && c->argc == 2) {