Skip to content
Browse files

Precision of getClientOutputBufferMemoryUsage() greatily improved, se…

…e issue #327 for more information.
  • Loading branch information...
1 parent 35a4761 commit 8b1829009b3c0710b3a8b889b64cd0bb8c3f46f1 @antirez committed
Showing with 35 additions and 8 deletions.
  1. +17 −8 src/networking.c
  2. +14 −0 src/zmalloc.c
  3. +4 −0 src/zmalloc.h
View
25 src/networking.c
@@ -131,7 +131,7 @@ void _addReplyObjectToList(redisClient *c, robj *o) {
listAddNodeTail(c->reply,o);
}
}
- c->reply_bytes += sdslen(o->ptr);
+ c->reply_bytes += zmalloc_size(o->ptr);
}
/* This method takes responsibility over the sds. When it is no longer
@@ -144,7 +144,7 @@ void _addReplySdsToList(redisClient *c, sds s) {
return;
}
- c->reply_bytes += sdslen(s);
+ c->reply_bytes += zmalloc_size(s);
if (listLength(c->reply) == 0) {
listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
} else {
@@ -169,7 +169,10 @@ void _addReplyStringToList(redisClient *c, char *s, size_t len) {
if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
if (listLength(c->reply) == 0) {
- listAddNodeTail(c->reply,createStringObject(s,len));
+ robj *o = createStringObject(s,len);
+
+ listAddNodeTail(c->reply,o);
+ c->reply_bytes += zmalloc_size(o->ptr);
} else {
tail = listNodeValue(listLast(c->reply));
@@ -177,13 +180,17 @@ void _addReplyStringToList(redisClient *c, char *s, size_t len) {
if (tail->ptr != NULL &&
sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
{
+ c->reply_bytes -= zmalloc_size(tail->ptr);
tail = dupLastObjectIfNeeded(c->reply);
tail->ptr = sdscatlen(tail->ptr,s,len);
+ c->reply_bytes += zmalloc_size(tail->ptr);
} else {
- listAddNodeTail(c->reply,createStringObject(s,len));
+ robj *o = createStringObject(s,len);
+
+ listAddNodeTail(c->reply,o);
+ c->reply_bytes += zmalloc_size(o->ptr);
}
}
- c->reply_bytes += len;
}
/* -----------------------------------------------------------------------------
@@ -295,7 +302,7 @@ void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
len = listNodeValue(ln);
len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
- c->reply_bytes += sdslen(len->ptr);
+ c->reply_bytes += zmalloc_size(len->ptr);
if (ln->next != NULL) {
next = listNodeValue(ln->next);
@@ -572,6 +579,7 @@ void freeClient(redisClient *c) {
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
redisClient *c = privdata;
int nwritten = 0, totwritten = 0, objlen;
+ size_t objmem;
robj *o;
REDIS_NOTUSED(el);
REDIS_NOTUSED(mask);
@@ -597,6 +605,7 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
} else {
o = listNodeValue(listFirst(c->reply));
objlen = sdslen(o->ptr);
+ objmem = zmalloc_size(o->ptr);
if (objlen == 0) {
listDelNode(c->reply,listFirst(c->reply));
@@ -617,7 +626,7 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
if (c->sentlen == objlen) {
listDelNode(c->reply,listFirst(c->reply));
c->sentlen = 0;
- c->reply_bytes -= objlen;
+ c->reply_bytes -= objmem;
}
}
/* Note that we avoid to send more than REDIS_MAX_WRITE_PER_EVENT
@@ -1087,7 +1096,7 @@ void rewriteClientCommandVector(redisClient *c, int argc, ...) {
* the caller wishes. The main usage of this function currently is
* enforcing the client output length limits. */
unsigned long getClientOutputBufferMemoryUsage(redisClient *c) {
- unsigned long list_item_size = sizeof(listNode);
+ unsigned long list_item_size = sizeof(listNode)+sizeof(robj);
return c->reply_bytes + (list_item_size*listLength(c->reply));
}
View
14 src/zmalloc.c
@@ -150,6 +150,20 @@ void *zrealloc(void *ptr, size_t size) {
#endif
}
+/* Provide zmalloc_size() for systems where this function is not provided by
+ * malloc itself, given that in that case we store an header with this
+ * information as the first bytes of every allocation. */
+#ifndef HAVE_MALLOC_SIZE
+size_t zmalloc_size(void *ptr) {
+ void *realptr = (char*)ptr-PREFIX_SIZE;
+ size_t size = *((size_t*)realptr);
+ /* Assume at least that all the allocations are padded at sizeof(long) by
+ * the underlying allocator. */
+ if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1));
+ return size+PREFIX_SIZE;
+}
+#endif
+
void zfree(void *ptr) {
#ifndef HAVE_MALLOC_SIZE
void *realptr;
View
4 src/zmalloc.h
@@ -76,4 +76,8 @@ void zmalloc_enable_thread_safeness(void);
float zmalloc_get_fragmentation_ratio(void);
size_t zmalloc_get_rss(void);
+#ifndef HAVE_MALLOC_SIZE
+size_t zmalloc_size(void *ptr);
+#endif
+
#endif /* __ZMALLOC_H */

0 comments on commit 8b18290

Please sign in to comment.
Something went wrong with that request. Please try again.