Skip to content

Commit

Permalink
Use sdsAllocSize instead of sdsZmallocSize
Browse files Browse the repository at this point in the history
sdsAllocSize returns the correct size without consulting the
allocator. The only exception is SDS_TYPE_5, for which it has to
consult the allocator.

The commit deletes sdsZmallocSize and explicitly includes sds.h where
sdsAllocSize is called.

Signed-off-by: Vadym Khoptynets <[email protected]>
  • Loading branch information
poiuj committed Aug 19, 2024
1 parent b3467fc commit f7e9cec
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "cluster.h"
#include "threads_mngr.h"
#include "io_threads.h"
#include "sds.h"

#include <arpa/inet.h>
#include <signal.h>
Expand Down Expand Up @@ -673,7 +674,7 @@ void debugCommand(client *c) {
addReplyStatusFormat(c,
"key_sds_len:%lld, key_sds_avail:%lld, key_zmalloc: %lld, "
"val_sds_len:%lld, val_sds_avail:%lld, val_zmalloc: %lld",
(long long)sdslen(key), (long long)sdsavail(key), (long long)sdsZmallocSize(key),
(long long)sdslen(key), (long long)sdsavail(key), (long long)sdsAllocSize(key),
(long long)sdslen(val->ptr), (long long)sdsavail(val->ptr),
(long long)getStringObjectSdsUsedMemory(val));
}
Expand Down
5 changes: 3 additions & 2 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "monotonic.h"
#include "resp_parser.h"
#include "script_lua.h"
#include "sds.h"

#include <lua.h>
#include <lauxlib.h>
Expand Down Expand Up @@ -490,7 +491,7 @@ sds luaCreateFunction(client *c, robj *body, int evalsha) {
l->node = luaScriptsLRUAdd(c, sha, evalsha);
int retval = dictAdd(lctx.lua_scripts, sha, l);
serverAssertWithInfo(c ? c : lctx.lua_client, NULL, retval == DICT_OK);
lctx.lua_scripts_mem += sdsZmallocSize(sha) + getStringObjectSdsUsedMemory(body);
lctx.lua_scripts_mem += sdsAllocSize(sha) + getStringObjectSdsUsedMemory(body);
incrRefCount(body);
return sha;
}
Expand All @@ -516,7 +517,7 @@ void luaDeleteFunction(client *c, sds sha) {
/* We only delete `EVAL` scripts, which must exist in the LRU list. */
serverAssert(l->node);
listDelNode(lctx.lua_scripts_lru_list, l->node);
lctx.lua_scripts_mem -= sdsZmallocSize(sha) + getStringObjectSdsUsedMemory(l->body);
lctx.lua_scripts_mem -= sdsAllocSize(sha) + getStringObjectSdsUsedMemory(l->body);
dictFreeUnlinkedEntry(lctx.lua_scripts, de);
}

Expand Down
6 changes: 3 additions & 3 deletions src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ static dict *engines = NULL;
static functionsLibCtx *curr_functions_lib_ctx = NULL;

static size_t functionMallocSize(functionInfo *fi) {
return zmalloc_size(fi) + sdsZmallocSize(fi->name) + (fi->desc ? sdsZmallocSize(fi->desc) : 0) +
return zmalloc_size(fi) + sdsAllocSize(fi->name) + (fi->desc ? sdsAllocSize(fi->desc) : 0) +
fi->li->ei->engine->get_function_memory_overhead(fi->function);
}

static size_t libraryMallocSize(functionLibInfo *li) {
return zmalloc_size(li) + sdsZmallocSize(li->name) + sdsZmallocSize(li->code);
return zmalloc_size(li) + sdsAllocSize(li->name) + sdsAllocSize(li->code);
}

static void engineStatsDispose(dict *d, void *obj) {
Expand Down Expand Up @@ -417,7 +417,7 @@ int functionsRegisterEngine(const char *engine_name, engine *engine) {

dictAdd(engines, engine_name_sds, ei);

engine_cache_memory += zmalloc_size(ei) + sdsZmallocSize(ei->name) + zmalloc_size(engine) +
engine_cache_memory += zmalloc_size(ei) + sdsAllocSize(ei->name) + zmalloc_size(engine) +
engine->get_engine_memory_overhead(engine->engine_ctx);

return C_OK;
Expand Down
18 changes: 5 additions & 13 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "cluster.h"
#include "cluster_slot_stats.h"
#include "script.h"
#include "sds.h"
#include "fpconv_dtoa.h"
#include "fmtargs.h"
#include <strings.h>
Expand All @@ -50,23 +51,14 @@ __thread sds thread_shared_qb = NULL;

typedef enum { PARSE_OK = 0, PARSE_ERR = -1, PARSE_NEEDMORE = -2 } parseResult;

/* Return the size consumed from the allocator, for the specified SDS string,
* including internal fragmentation. This function is used in order to compute
* the client output buffer size. */
size_t sdsZmallocSize(sds s) {
void *sh = sdsAllocPtr(s);
return zmalloc_size(sh);
}

/* Return the amount of memory used by the sds string at object->ptr
* for a string object. This includes internal fragmentation. */
size_t getStringObjectSdsUsedMemory(robj *o) {
serverAssertWithInfo(NULL, o, o->type == OBJ_STRING);
switch (o->encoding) {
case OBJ_ENCODING_RAW: return sdsZmallocSize(o->ptr);
case OBJ_ENCODING_EMBSTR: return zmalloc_size(o) - sizeof(robj);
default: return 0; /* Just integer encoding for now. */
if (o->encoding != OBJ_ENCODING_INT) {
return sdsAllocSize(o->ptr);
}
return 0;
}

/* Return the length of a string object.
Expand Down Expand Up @@ -4260,7 +4252,7 @@ size_t getClientMemoryUsage(client *c, size_t *output_buffer_mem_usage) {
size_t mem = getClientOutputBufferMemoryUsage(c);

if (output_buffer_mem_usage != NULL) *output_buffer_mem_usage = mem;
mem += c->querybuf ? sdsZmallocSize(c->querybuf) : 0;
mem += c->querybuf ? sdsAllocSize(c->querybuf) : 0;
mem += zmalloc_size(c);
mem += c->buf_usable_size;
/* For efficiency (less work keeping track of the argv memory), it doesn't include the used memory
Expand Down
11 changes: 6 additions & 5 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "functions.h"
#include "intset.h" /* Compact integer set structure */
#include "zmalloc.h"
#include "sds.h"
#include <math.h>
#include <ctype.h>

Expand Down Expand Up @@ -990,7 +991,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) {
if (o->encoding == OBJ_ENCODING_INT) {
asize = sizeof(*o);
} else if (o->encoding == OBJ_ENCODING_RAW) {
asize = sdsZmallocSize(o->ptr) + sizeof(*o);
asize = sdsAllocSize(o->ptr) + sizeof(*o);
} else if (o->encoding == OBJ_ENCODING_EMBSTR) {
asize = zmalloc_size((void *)o);
} else {
Expand Down Expand Up @@ -1018,7 +1019,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) {
asize = sizeof(*o) + sizeof(dict) + (sizeof(struct dictEntry *) * dictBuckets(d));
while ((de = dictNext(di)) != NULL && samples < sample_size) {
ele = dictGetKey(de);
elesize += dictEntryMemUsage(de) + sdsZmallocSize(ele);
elesize += dictEntryMemUsage(de) + sdsAllocSize(ele);
samples++;
}
dictReleaseIterator(di);
Expand All @@ -1040,7 +1041,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) {
asize = sizeof(*o) + sizeof(zset) + sizeof(zskiplist) + sizeof(dict) +
(sizeof(struct dictEntry *) * dictBuckets(d)) + zmalloc_size(zsl->header);
while (znode != NULL && samples < sample_size) {
elesize += sdsZmallocSize(znode->ele);
elesize += sdsAllocSize(znode->ele);
elesize += dictEntryMemUsage(NULL) + zmalloc_size(znode);
samples++;
znode = znode->level[0].forward;
Expand All @@ -1059,7 +1060,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) {
while ((de = dictNext(di)) != NULL && samples < sample_size) {
ele = dictGetKey(de);
ele2 = dictGetVal(de);
elesize += sdsZmallocSize(ele) + sdsZmallocSize(ele2);
elesize += sdsAllocSize(ele) + sdsAllocSize(ele2);
elesize += dictEntryMemUsage(de);
samples++;
}
Expand Down Expand Up @@ -1203,7 +1204,7 @@ struct serverMemOverhead *getMemoryOverheadData(void) {

mem = 0;
if (server.aof_state != AOF_OFF) {
mem += sdsZmallocSize(server.aof_buf);
mem += sdsAllocSize(server.aof_buf);
}
mh->aof_buffer = mem;
mem_total += mem;
Expand Down
3 changes: 2 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "threads_mngr.h"
#include "fmtargs.h"
#include "io_threads.h"
#include "sds.h"

#include <time.h>
#include <signal.h>
Expand Down Expand Up @@ -812,7 +813,7 @@ size_t ClientsPeakMemInput[CLIENTS_PEAK_MEM_USAGE_SLOTS] = {0};
size_t ClientsPeakMemOutput[CLIENTS_PEAK_MEM_USAGE_SLOTS] = {0};

int clientsCronTrackExpansiveClients(client *c, int time_idx) {
size_t qb_size = c->querybuf ? sdsZmallocSize(c->querybuf) : 0;
size_t qb_size = c->querybuf ? sdsAllocSize(c->querybuf) : 0;
size_t argv_size = c->argv ? zmalloc_size(c->argv) : 0;
size_t in_usage = qb_size + c->argv_len_sum + argv_size;
size_t out_usage = getClientOutputBufferMemoryUsage(c);
Expand Down
1 change: 0 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2807,7 +2807,6 @@ void addReplyLoadedModules(client *c);
void copyReplicaOutputBuffer(client *dst, client *src);
void addListRangeReply(client *c, robj *o, long start, long end, int reverse);
void deferredAfterErrorReply(client *c, list *errors);
size_t sdsZmallocSize(sds s);
size_t getStringObjectSdsUsedMemory(robj *o);
void freeClientReplyValue(void *o);
void *dupClientReplyValue(void *o);
Expand Down

0 comments on commit f7e9cec

Please sign in to comment.