forked from qodo-benchmark/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix MEMORY USAGE command #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomerqodo
wants to merge
2
commits into
coderabbit_full_base_fix_memory_usage_command_pr5
Choose a base branch
from
coderabbit_full_head_fix_memory_usage_command_pr5
base: coderabbit_full_base_fix_memory_usage_command_pr5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1202,48 +1202,51 @@ size_t streamRadixTreeMemoryUsage(rax *rax) { | |
| return size; | ||
| } | ||
|
|
||
| /* Returns the size in bytes consumed by the key's value in RAM. | ||
| /* Returns the size in bytes consumed by the object header, key and value in RAM. | ||
| * Note that the returned value is just an approximation, especially in the | ||
| * case of aggregated data types where only "sample_size" elements | ||
| * are checked and averaged to estimate the total size. */ | ||
| #define OBJ_COMPUTE_SIZE_DEF_SAMPLES 5 /* Default sample size. */ | ||
| size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | ||
| size_t kvobjComputeSize(robj *key, kvobj *o, size_t sample_size, int dbid) { | ||
| dict *d; | ||
| dictIterator di; | ||
| struct dictEntry *de; | ||
| size_t asize = 0, elesize = 0, elecount = 0, samples = 0; | ||
| size_t elesize = 0, elecount = 0, samples = 0; | ||
|
|
||
| /* All kv-objects has at least kvobj header and embedded key */ | ||
| size_t asize = malloc_usable_size((void *)o); | ||
|
|
||
| if (o->type == OBJ_STRING) { | ||
| if(o->encoding == OBJ_ENCODING_INT) { | ||
| asize = sizeof(*o); | ||
| /* Value already counted (reuse the "ptr" in header to store int) */ | ||
| } else if(o->encoding == OBJ_ENCODING_RAW) { | ||
| asize = sdsZmallocSize(o->ptr)+sizeof(*o); | ||
| asize += sdsZmallocSize(o->ptr); | ||
| } else if(o->encoding == OBJ_ENCODING_EMBSTR) { | ||
| asize = zmalloc_size((void *)o); | ||
| /* Value already counted (Value embedded in the object as well) */ | ||
| } else { | ||
| serverPanic("Unknown string encoding"); | ||
| } | ||
| } else if (o->type == OBJ_LIST) { | ||
| if (o->encoding == OBJ_ENCODING_QUICKLIST) { | ||
| quicklist *ql = o->ptr; | ||
| quicklistNode *node = ql->head; | ||
| asize = sizeof(*o)+sizeof(quicklist); | ||
| asize += sizeof(quicklist); | ||
| do { | ||
| elesize += sizeof(quicklistNode)+zmalloc_size(node->entry); | ||
| elecount += node->count; | ||
| samples++; | ||
| } while ((node = node->next) && samples < sample_size); | ||
| asize += (double)elesize/elecount*ql->count; | ||
| asize += (double)elesize/samples*ql->count; | ||
|
Comment on lines
1231
to
+1239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quicklist size scaling should use node count, not element count. 🔧 Proposed fix- asize += (double)elesize/samples*ql->count;
+ asize += (double)elesize/samples*ql->len;🤖 Prompt for AI Agents |
||
| } else if (o->encoding == OBJ_ENCODING_LISTPACK) { | ||
| asize = sizeof(*o)+zmalloc_size(o->ptr); | ||
| asize += zmalloc_size(o->ptr); | ||
| } else { | ||
| serverPanic("Unknown list encoding"); | ||
| } | ||
| } else if (o->type == OBJ_SET) { | ||
| if (o->encoding == OBJ_ENCODING_HT) { | ||
| d = o->ptr; | ||
| dictInitIterator(&di, d); | ||
| asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictBuckets(d)); | ||
| asize += sizeof(dict) + (sizeof(struct dictEntry*) * dictBuckets(d)); | ||
| while((de = dictNext(&di)) != NULL && samples < sample_size) { | ||
| sds ele = dictGetKey(de); | ||
| elesize += dictEntryMemUsage(0) + sdsZmallocSize(ele); | ||
|
|
@@ -1252,20 +1255,20 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | |
| dictResetIterator(&di); | ||
| if (samples) asize += (double)elesize/samples*dictSize(d); | ||
| } else if (o->encoding == OBJ_ENCODING_INTSET) { | ||
| asize = sizeof(*o)+zmalloc_size(o->ptr); | ||
| asize += zmalloc_size(o->ptr); | ||
| } else if (o->encoding == OBJ_ENCODING_LISTPACK) { | ||
| asize = sizeof(*o)+zmalloc_size(o->ptr); | ||
| asize += zmalloc_size(o->ptr); | ||
| } else { | ||
| serverPanic("Unknown set encoding"); | ||
| } | ||
| } else if (o->type == OBJ_ZSET) { | ||
| if (o->encoding == OBJ_ENCODING_LISTPACK) { | ||
| asize = sizeof(*o)+zmalloc_size(o->ptr); | ||
| asize += zmalloc_size(o->ptr); | ||
| } else if (o->encoding == OBJ_ENCODING_SKIPLIST) { | ||
| d = ((zset*)o->ptr)->dict; | ||
| zskiplist *zsl = ((zset*)o->ptr)->zsl; | ||
| zskiplistNode *znode = zsl->header->level[0].forward; | ||
| asize = sizeof(*o)+sizeof(zset)+sizeof(zskiplist)+sizeof(dict)+ | ||
| asize += sizeof(zset) + sizeof(zskiplist) + sizeof(dict) + | ||
| (sizeof(struct dictEntry*)*dictBuckets(d))+ | ||
| zmalloc_size(zsl->header); | ||
| while(znode != NULL && samples < sample_size) { | ||
|
|
@@ -1280,14 +1283,14 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | |
| } | ||
| } else if (o->type == OBJ_HASH) { | ||
| if (o->encoding == OBJ_ENCODING_LISTPACK) { | ||
| asize = sizeof(*o)+zmalloc_size(o->ptr); | ||
| asize += zmalloc_size(o->ptr); | ||
| } else if (o->encoding == OBJ_ENCODING_LISTPACK_EX) { | ||
| listpackEx *lpt = o->ptr; | ||
| asize = sizeof(*o) + zmalloc_size(lpt) + zmalloc_size(lpt->lp); | ||
| asize += zmalloc_size(lpt) + zmalloc_size(lpt->lp); | ||
| } else if (o->encoding == OBJ_ENCODING_HT) { | ||
| d = o->ptr; | ||
| dictInitIterator(&di, d); | ||
| asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictBuckets(d)); | ||
| asize += sizeof(dict) + (sizeof(struct dictEntry*) * dictBuckets(d)); | ||
| while((de = dictNext(&di)) != NULL && samples < sample_size) { | ||
| hfield ele = dictGetKey(de); | ||
| sds ele2 = dictGetVal(de); | ||
|
|
@@ -1302,7 +1305,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | |
| } | ||
| } else if (o->type == OBJ_STREAM) { | ||
| stream *s = o->ptr; | ||
| asize = sizeof(*o)+sizeof(*s); | ||
| asize += sizeof(*s); | ||
| asize += streamRadixTreeMemoryUsage(s->rax); | ||
|
|
||
| /* Now we have to add the listpacks. The last listpack is often non | ||
|
|
@@ -1312,7 +1315,8 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | |
| raxIterator ri; | ||
| raxStart(&ri,s->rax); | ||
| raxSeek(&ri,"^",NULL,0); | ||
| size_t lpsize = 0, samples = 0; | ||
| size_t lpsize = 0; | ||
| size_t samples = 0; | ||
| while(samples < sample_size && raxNext(&ri)) { | ||
| unsigned char *lp = ri.data; | ||
| /* Use the allocated size, since we overprovision the node initially. */ | ||
|
|
@@ -1323,7 +1327,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | |
| asize += lpsize; | ||
| } else { | ||
| if (samples) lpsize /= samples; /* Compute the average. */ | ||
| asize += lpsize * (s->rax->numele-1); | ||
| asize += lpsize * s->rax->numele; | ||
| /* No need to check if seek succeeded, we enter this branch only | ||
| * if there are a few elements in the radix tree. */ | ||
| raxSeek(&ri,"$",NULL,0); | ||
|
|
@@ -1364,7 +1368,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { | |
| raxStop(&ri); | ||
| } | ||
| } else if (o->type == OBJ_MODULE) { | ||
| asize = moduleGetMemUsage(key, o, sample_size, dbid); | ||
| asize += moduleGetMemUsage(key, o, sample_size, dbid); | ||
| } else { | ||
| serverPanic("Unknown object type"); | ||
| } | ||
|
|
@@ -1780,7 +1784,7 @@ NULL | |
| addReplyNull(c); | ||
| return; | ||
| } | ||
| size_t usage = objectComputeSize(c->argv[2], (robj *)kv, samples, c->db->id); | ||
| size_t usage = kvobjComputeSize(c->argv[2], kv, samples, c->db->id); | ||
| addReplyLongLong(c,usage); | ||
| } else if (!strcasecmp(c->argv[1]->ptr,"stats") && c->argc == 2) { | ||
| struct redisMemOverhead *mh = getMemoryOverheadData(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build break: replace
malloc_usable_sizewith Redis allocator wrapper.Line 1217 fails the build (
implicit declaration of function 'malloc_usable_size'). Use the zmalloc wrapper to keep portability.🔧 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: External Server Tests
[error] 1217-1217: Implicit declaration of function 'malloc_usable_size'; did you mean 'zmalloc_usable_size'? [-Werror=implicit-function-declaration]
🤖 Prompt for AI Agents