From bd70a5f5884ec46983ac32a1ae0b48f4d356a9d5 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Sun, 21 Nov 2010 16:27:47 +0100 Subject: [PATCH] Stop using /dev/null to find out the serialized object length Now the rdbSave* functions return the number of bytes written (or required to write) in serializing a Redis object, writing to /dev/null and using ftell (which doesn't work on FreeBSD) isn't needed anymore. --- src/debug.c | 2 +- src/rdb.c | 17 ++++++----------- src/redis.c | 5 ----- src/redis.h | 5 ++--- src/vm.c | 6 ++---- 5 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/debug.c b/src/debug.c index b364dd16..9e97868d 100644 --- a/src/debug.c +++ b/src/debug.c @@ -216,7 +216,7 @@ void debugCommand(redisClient *c) { "encoding:%s serializedlength:%lld " "lru:%d lru_seconds_idle:%lu", (void*)val, val->refcount, - strenc, (long long) rdbSavedObjectLen(val,NULL), + strenc, (long long) rdbSavedObjectLen(val), val->lru, estimateObjectIdleTime(val)); } else { vmpointer *vp = (vmpointer*) val; diff --git a/src/rdb.c b/src/rdb.c index 913938a3..5e69a324 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -389,20 +389,15 @@ int rdbSaveObject(FILE *fp, robj *o) { * the rdbSaveObject() function. Currently we use a trick to get * this length with very little changes to the code. In the future * we could switch to a faster solution. */ -off_t rdbSavedObjectLen(robj *o, FILE *fp) { - int nwritten; - if (fp == NULL) fp = server.devnull; - rewind(fp); - - /* Determining the saved length of an object should never return -1 */ - redisAssert((nwritten = rdbSaveObject(fp,o)) != -1); - return nwritten; +off_t rdbSavedObjectLen(robj *o) { + int len = rdbSaveObject(NULL,o); + redisAssert(len != -1); + return len; } /* Return the number of pages required to save this object in the swap file */ -off_t rdbSavedObjectPages(robj *o, FILE *fp) { - off_t bytes = rdbSavedObjectLen(o,fp); - +off_t rdbSavedObjectPages(robj *o) { + off_t bytes = rdbSavedObjectLen(o); return (bytes+(server.vm_page_size-1))/server.vm_page_size; } diff --git a/src/redis.c b/src/redis.c index cf5673a3..5b39c011 100644 --- a/src/redis.c +++ b/src/redis.c @@ -814,11 +814,6 @@ void initServer() { setupSigSegvAction(); server.mainthread = pthread_self(); - server.devnull = fopen("/dev/null","w"); - if (server.devnull == NULL) { - redisLog(REDIS_WARNING, "Can't open /dev/null: %s", server.neterr); - exit(1); - } server.clients = listCreate(); server.slaves = listCreate(); server.monitors = listCreate(); diff --git a/src/redis.h b/src/redis.h index 087002bf..11e4e557 100644 --- a/src/redis.h +++ b/src/redis.h @@ -481,7 +481,6 @@ struct redisServer { dict *pubsub_channels; /* Map channels to list of subscribed clients */ list *pubsub_patterns; /* A list of pubsub_patterns */ /* Misc */ - FILE *devnull; unsigned lruclock:22; /* clock incrementing every minute, for LRU */ unsigned lruclock_padding:10; }; @@ -743,8 +742,8 @@ int rdbSaveBackground(char *filename); void rdbRemoveTempFile(pid_t childpid); int rdbSave(char *filename); int rdbSaveObject(FILE *fp, robj *o); -off_t rdbSavedObjectPages(robj *o, FILE *fp); -off_t rdbSavedObjectLen(robj *o, FILE *fp); +off_t rdbSavedObjectLen(robj *o); +off_t rdbSavedObjectPages(robj *o); robj *rdbLoadObject(int type, FILE *fp); void backgroundSaveDoneHandler(int statloc); diff --git a/src/vm.c b/src/vm.c index c2e0d9ed..ac0d92e3 100644 --- a/src/vm.c +++ b/src/vm.c @@ -263,7 +263,7 @@ int vmWriteObjectOnSwap(robj *o, off_t page) { * If we can't find enough contiguous empty pages to swap the object on disk * NULL is returned. */ vmpointer *vmSwapObjectBlocking(robj *val) { - off_t pages = rdbSavedObjectPages(val,NULL); + off_t pages = rdbSavedObjectPages(val); off_t page; vmpointer *vp; @@ -821,9 +821,7 @@ void *IOThreadEntryPoint(void *arg) { vmpointer *vp = (vmpointer*)j->id; j->val = vmReadObjectFromSwap(j->page,vp->vtype); } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) { - FILE *fp = fopen("/dev/null","w+"); - j->pages = rdbSavedObjectPages(j->val,fp); - fclose(fp); + j->pages = rdbSavedObjectPages(j->val); } else if (j->type == REDIS_IOJOB_DO_SWAP) { if (vmWriteObjectOnSwap(j->val,j->page) == REDIS_ERR) j->canceled = 1; -- 2.45.2