Skip to content
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

Dram allocations #173

Draft
wants to merge 7 commits into
base: 6.0-memkind_alloc_by_size
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/adlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ void listRelease(list *list)
zfree(list);
}

/* Free the whole list from DRAM.
*
* This function can't fail. */
void listReleaseDRAM(list *list)
{
listEmpty(list);
zfree_dram(list);
}

/* Add a new node to the list, to head, containing the specified 'value'
* pointer as value.
*
Expand Down
1 change: 1 addition & 0 deletions src/adlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typedef struct list {
list *listCreate(void);
list *listCreateDRAM(void);
void listRelease(list *list);
void listReleaseDRAM(list *list);
void listEmpty(list *list);
list *listAddNodeHead(list *list, void *value);
list *listAddNodeTail(list *list, void *value);
Expand Down
8 changes: 4 additions & 4 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int dictExpand(dict *d, unsigned long size)
/* Allocate the new hash table and initialize all pointers to NULL */
n.size = realsize;
n.sizemask = realsize-1;
n.table = zcalloc(realsize*sizeof(dictEntry*));
n.table = zcalloc_dram(realsize*sizeof(dictEntry*));
n.used = 0;

/* Is this the first initialization? If so it's not really a rehashing
Expand Down Expand Up @@ -219,7 +219,7 @@ int dictRehash(dict *d, int n) {

/* Check if we already rehashed the whole table... */
if (d->ht[0].used == 0) {
zfree(d->ht[0].table);
zfree_dram(d->ht[0].table);
d->ht[0] = d->ht[1];
_dictReset(&d->ht[1]);
d->rehashidx = -1;
Expand Down Expand Up @@ -541,7 +541,7 @@ long long dictFingerprint(dict *d) {

dictIterator *dictGetIterator(dict *d)
{
dictIterator *iter = zmalloc(sizeof(*iter));
dictIterator *iter = zmalloc_dram(sizeof(*iter));

iter->d = d;
iter->table = 0;
Expand Down Expand Up @@ -602,7 +602,7 @@ void dictReleaseIterator(dictIterator *iter)
else
assert(iter->fingerprint == dictFingerprint(iter->d));
}
zfree(iter);
zfree_dram(iter);
}

/* Return a random entry from the hash table. Useful to
Expand Down
34 changes: 17 additions & 17 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void linkClient(client *c) {
}

client *createClient(connection *conn) {
client *c = zmalloc(sizeof(client));
client *c = zmalloc_dram(sizeof(client));

/* passing NULL as conn it is possible to create a non connected client.
* This is useful since all the commands needs to be executed
Expand All @@ -108,8 +108,8 @@ client *createClient(connection *conn) {
c->name = NULL;
c->bufpos = 0;
c->qb_pos = 0;
c->querybuf = sdsempty();
c->pending_querybuf = sdsempty();
c->querybuf = sdsdramempty();
c->pending_querybuf = sdsdramempty();
c->querybuf_peak = 0;
c->reqtype = 0;
c->argc = 0;
Expand All @@ -133,7 +133,7 @@ client *createClient(connection *conn) {
c->slave_listening_port = 0;
c->slave_ip[0] = '\0';
c->slave_capa = SLAVE_CAPA_NONE;
c->reply = listCreate();
c->reply = listCreateDRAM();
c->reply_bytes = 0;
c->obuf_soft_limit_reached_time = 0;
listSetFreeMethod(c->reply,freeClientReplyValue);
Expand All @@ -148,9 +148,9 @@ client *createClient(connection *conn) {
c->bpop.numreplicas = 0;
c->bpop.reploffset = 0;
c->woff = 0;
c->watched_keys = listCreate();
c->watched_keys = listCreateDRAM();
c->pubsub_channels = dictCreate(&objectKeyPointerValueDictType,NULL);
c->pubsub_patterns = listCreate();
c->pubsub_patterns = listCreateDRAM();
c->peerid = NULL;
c->client_list_node = NULL;
c->client_tracking_redirection = 0;
Expand Down Expand Up @@ -1092,16 +1092,16 @@ void freeClient(client *c) {

/* UNWATCH all the keys */
unwatchAllKeys(c);
listRelease(c->watched_keys);
listReleaseDRAM(c->watched_keys);

/* Unsubscribe from all the pubsub channels */
pubsubUnsubscribeAllChannels(c,0);
pubsubUnsubscribeAllPatterns(c,0);
dictRelease(c->pubsub_channels);
listRelease(c->pubsub_patterns);
listReleaseDRAM(c->pubsub_patterns);

/* Free data structures. */
listRelease(c->reply);
listReleaseDRAM(c->reply);
freeClientArgv(c);

/* Unlink the client: this will close the socket, remove the I/O
Expand Down Expand Up @@ -1148,10 +1148,10 @@ void freeClient(client *c) {
/* Release other dynamically allocated client structure fields,
* and finally release the client structure itself. */
if (c->name) decrRefCount(c->name);
zfree(c->argv);
zfree_dram(c->argv);
freeClientMultiState(c);
sdsfree(c->peerid);
zfree(c);
zfree_dram(c);
}

/* Schedule a client to free it at a safe time in the serverCron() function.
Expand Down Expand Up @@ -1460,8 +1460,8 @@ int processInlineBuffer(client *c) {

/* Setup argv array on client structure */
if (argc) {
if (c->argv) zfree(c->argv);
c->argv = zmalloc(sizeof(robj*)*argc);
if (c->argv) zfree_dram(c->argv);
c->argv = zmalloc_dram(sizeof(robj*)*argc);
}

/* Create redis objects for all arguments. */
Expand Down Expand Up @@ -1554,8 +1554,8 @@ int processMultibulkBuffer(client *c) {
c->multibulklen = ll;

/* Setup argv array on client structure */
if (c->argv) zfree(c->argv);
c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
if (c->argv) zfree_dram(c->argv);
c->argv = zmalloc_dram(sizeof(robj*)*c->multibulklen);
}

serverAssertWithInfo(c,NULL,c->multibulklen > 0);
Expand Down Expand Up @@ -2472,7 +2472,7 @@ void rewriteClientCommandVector(client *c, int argc, ...) {
* sure that if the same objects are reused in the new vector the
* refcount gets incremented before it gets decremented. */
for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]);
zfree(c->argv);
zfree_dram(c->argv);
/* Replace argv and argc with our new versions. */
c->argv = argv;
c->argc = argc;
Expand All @@ -2484,7 +2484,7 @@ void rewriteClientCommandVector(client *c, int argc, ...) {
/* Completely replace the client command vector with the provided one. */
void replaceClientCommandVector(client *c, int argc, robj **argv) {
freeClientArgv(c);
zfree(c->argv);
zfree_dram(c->argv);
c->argv = argv;
c->argc = argc;
c->cmd = lookupCommandOrOriginal(c->argv[0]->ptr);
Expand Down
22 changes: 20 additions & 2 deletions src/sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

const char *SDS_NOINIT = "SDS_NOINIT";

#define SDS_GENERAL_VARIANT 0
#define SDS_DRAM_VARIANT 1

static inline int sdsHdrSize(char type) {
switch(type&SDS_TYPE_MASK) {
case SDS_TYPE_5:
Expand Down Expand Up @@ -86,7 +89,7 @@ static inline char sdsReqType(size_t string_size) {
* You can print the string with printf() as there is an implicit \0 at the
* end of the string. However the string is binary safe and can contain
* \0 characters in the middle, as the length is stored in the sds header. */
sds sdsnewlen(const void *init, size_t initlen) {
static sds _sdsnewlen(const void *init, size_t initlen, int on_dram) {
void *sh;
sds s;
char type = sdsReqType(initlen);
Expand All @@ -96,7 +99,8 @@ sds sdsnewlen(const void *init, size_t initlen) {
int hdrlen = sdsHdrSize(type);
unsigned char *fp; /* flags pointer. */

sh = s_malloc(hdrlen+initlen+1);
sh = (on_dram == SDS_DRAM_VARIANT) ? s_dram_malloc(hdrlen+initlen+1)
: s_malloc(hdrlen+initlen+1);
if (init==SDS_NOINIT)
init = NULL;
else if (!init)
Expand Down Expand Up @@ -144,12 +148,26 @@ sds sdsnewlen(const void *init, size_t initlen) {
return s;
}

sds sdsnewlen(const void *init, size_t initlen) {
return _sdsnewlen(init, initlen, SDS_GENERAL_VARIANT);
}

static sds sdsdramnewlen(const void *init, size_t initlen) {
return _sdsnewlen(init, initlen, SDS_DRAM_VARIANT);
}

/* Create an empty (zero length) sds string. Even in this case the string
* always has an implicit null term. */
sds sdsempty(void) {
return sdsnewlen("",0);
}

/* Create an empty (zero length) sds string on DRAM. Even in this case the string
* always has an implicit null term. */
sds sdsdramempty(void) {
return sdsdramnewlen("",0);
}

/* Create a new sds string starting from a null terminated C string. */
sds sdsnew(const char *init) {
size_t initlen = (init == NULL) ? 0 : strlen(init);
Expand Down
1 change: 1 addition & 0 deletions src/sds.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ static inline void sdssetalloc(sds s, size_t newlen) {
sds sdsnewlen(const void *init, size_t initlen);
sds sdsnew(const char *init);
sds sdsempty(void);
sds sdsdramempty(void);
sds sdsdup(const sds s);
void sdsfree(sds s);
sds sdsgrowzero(sds s, size_t len);
Expand Down
1 change: 1 addition & 0 deletions src/sdsalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
#define s_malloc zmalloc
#define s_realloc zrealloc
#define s_free zfree
#define s_dram_malloc zmalloc_dram

#endif