getStringObjectSdsUsedMemory() function added.

Now that EMBSTR encoding exists we calculate the amount of memory used
by the SDS part of a Redis String object in two different ways:

1) For raw string object, the size of the allocation is considered.
2) For embstr objects, the length of the string itself is used.

The new function takes care of this logic.
This commit is contained in:
antirez 2013-07-23 11:50:17 +02:00
parent ddccd0ed58
commit ec7f480e11

View File

@ -41,6 +41,17 @@ size_t zmalloc_size_sds(sds s) {
return zmalloc_size(s-sizeof(struct sdshdr)); return zmalloc_size(s-sizeof(struct sdshdr));
} }
/* Return the amount of memory used by the sds string at object->ptr
* for a string object. */
size_t getStringObjectSdsUsedMemory(robj *o) {
redisAssertWithInfo(NULL,o,o->type == REDIS_STRING);
switch(o->encoding) {
case REDIS_ENCODING_RAW: return zmalloc_size_sds(o->ptr);
case REDIS_ENCODING_EMBSTR: return sdslen(o->ptr);
default: return 0; /* Just integer encoding for now. */
}
}
void *dupClientReplyValue(void *o) { void *dupClientReplyValue(void *o) {
incrRefCount((robj*)o); incrRefCount((robj*)o);
return o; return o;
@ -184,9 +195,7 @@ void _addReplyObjectToList(redisClient *c, robj *o) {
if (listLength(c->reply) == 0) { if (listLength(c->reply) == 0) {
incrRefCount(o); incrRefCount(o);
listAddNodeTail(c->reply,o); listAddNodeTail(c->reply,o);
c->reply_bytes += (o->encoding == REDIS_ENCODING_RAW) ? c->reply_bytes += getStringObjectSdsUsedMemory(o);
zmalloc_size_sds(o->ptr) :
sdslen(o->ptr);
} else { } else {
tail = listNodeValue(listLast(c->reply)); tail = listNodeValue(listLast(c->reply));
@ -202,9 +211,7 @@ void _addReplyObjectToList(redisClient *c, robj *o) {
} else { } else {
incrRefCount(o); incrRefCount(o);
listAddNodeTail(c->reply,o); listAddNodeTail(c->reply,o);
c->reply_bytes += (o->encoding == REDIS_ENCODING_RAW) ? c->reply_bytes += getStringObjectSdsUsedMemory(o);
zmalloc_size_sds(o->ptr) :
sdslen(o->ptr);
} }
} }
asyncCloseClientOnOutputBufferLimitReached(c); asyncCloseClientOnOutputBufferLimitReached(c);
@ -252,9 +259,7 @@ void _addReplyStringToList(redisClient *c, char *s, size_t len) {
robj *o = createStringObject(s,len); robj *o = createStringObject(s,len);
listAddNodeTail(c->reply,o); listAddNodeTail(c->reply,o);
c->reply_bytes += (o->encoding == REDIS_ENCODING_RAW) ? c->reply_bytes += getStringObjectSdsUsedMemory(o);
zmalloc_size_sds(o->ptr) :
sdslen(o->ptr);
} else { } else {
tail = listNodeValue(listLast(c->reply)); tail = listNodeValue(listLast(c->reply));
@ -270,9 +275,7 @@ void _addReplyStringToList(redisClient *c, char *s, size_t len) {
robj *o = createStringObject(s,len); robj *o = createStringObject(s,len);
listAddNodeTail(c->reply,o); listAddNodeTail(c->reply,o);
c->reply_bytes += (o->encoding == REDIS_ENCODING_RAW) ? c->reply_bytes += getStringObjectSdsUsedMemory(o);
zmalloc_size_sds(o->ptr) :
sdslen(o->ptr);
} }
} }
asyncCloseClientOnOutputBufferLimitReached(c); asyncCloseClientOnOutputBufferLimitReached(c);
@ -413,10 +416,7 @@ void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
/* Only glue when the next node is non-NULL (an sds in this case) */ /* Only glue when the next node is non-NULL (an sds in this case) */
if (next->ptr != NULL) { if (next->ptr != NULL) {
c->reply_bytes -= zmalloc_size_sds(len->ptr); c->reply_bytes -= zmalloc_size_sds(len->ptr);
if (next->encoding == REDIS_ENCODING_RAW) c->reply_bytes -= getStringObjectSdsUsedMemory(next);
c->reply_bytes -= zmalloc_size_sds(next->ptr);
else
c->reply_bytes -= sdslen(next->ptr);
len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr)); len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
c->reply_bytes += zmalloc_size_sds(len->ptr); c->reply_bytes += zmalloc_size_sds(len->ptr);
listDelNode(c->reply,ln->next); listDelNode(c->reply,ln->next);
@ -778,9 +778,7 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
} else { } else {
o = listNodeValue(listFirst(c->reply)); o = listNodeValue(listFirst(c->reply));
objlen = sdslen(o->ptr); objlen = sdslen(o->ptr);
objmem = (o->encoding == REDIS_ENCODING_RAW) ? objmem = getStringObjectSdsUsedMemory(o);
zmalloc_size_sds(o->ptr) :
sdslen(o->ptr);
if (objlen == 0) { if (objlen == 0) {
listDelNode(c->reply,listFirst(c->reply)); listDelNode(c->reply,listFirst(c->reply));