xref: /f-stack/app/redis-5.0.5/src/object.c (revision 572c4311)
1*572c4311Sfengbojiang /* Redis Object implementation.
2*572c4311Sfengbojiang  *
3*572c4311Sfengbojiang  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4*572c4311Sfengbojiang  * All rights reserved.
5*572c4311Sfengbojiang  *
6*572c4311Sfengbojiang  * Redistribution and use in source and binary forms, with or without
7*572c4311Sfengbojiang  * modification, are permitted provided that the following conditions are met:
8*572c4311Sfengbojiang  *
9*572c4311Sfengbojiang  *   * Redistributions of source code must retain the above copyright notice,
10*572c4311Sfengbojiang  *     this list of conditions and the following disclaimer.
11*572c4311Sfengbojiang  *   * Redistributions in binary form must reproduce the above copyright
12*572c4311Sfengbojiang  *     notice, this list of conditions and the following disclaimer in the
13*572c4311Sfengbojiang  *     documentation and/or other materials provided with the distribution.
14*572c4311Sfengbojiang  *   * Neither the name of Redis nor the names of its contributors may be used
15*572c4311Sfengbojiang  *     to endorse or promote products derived from this software without
16*572c4311Sfengbojiang  *     specific prior written permission.
17*572c4311Sfengbojiang  *
18*572c4311Sfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*572c4311Sfengbojiang  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*572c4311Sfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*572c4311Sfengbojiang  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22*572c4311Sfengbojiang  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*572c4311Sfengbojiang  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*572c4311Sfengbojiang  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*572c4311Sfengbojiang  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*572c4311Sfengbojiang  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*572c4311Sfengbojiang  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*572c4311Sfengbojiang  * POSSIBILITY OF SUCH DAMAGE.
29*572c4311Sfengbojiang  */
30*572c4311Sfengbojiang 
31*572c4311Sfengbojiang #include "server.h"
32*572c4311Sfengbojiang #include <math.h>
33*572c4311Sfengbojiang #include <ctype.h>
34*572c4311Sfengbojiang 
35*572c4311Sfengbojiang #ifdef __CYGWIN__
36*572c4311Sfengbojiang #define strtold(a,b) ((long double)strtod((a),(b)))
37*572c4311Sfengbojiang #endif
38*572c4311Sfengbojiang 
39*572c4311Sfengbojiang /* ===================== Creation and parsing of objects ==================== */
40*572c4311Sfengbojiang 
createObject(int type,void * ptr)41*572c4311Sfengbojiang robj *createObject(int type, void *ptr) {
42*572c4311Sfengbojiang     robj *o = zmalloc(sizeof(*o));
43*572c4311Sfengbojiang     o->type = type;
44*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_RAW;
45*572c4311Sfengbojiang     o->ptr = ptr;
46*572c4311Sfengbojiang     o->refcount = 1;
47*572c4311Sfengbojiang 
48*572c4311Sfengbojiang     /* Set the LRU to the current lruclock (minutes resolution), or
49*572c4311Sfengbojiang      * alternatively the LFU counter. */
50*572c4311Sfengbojiang     if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
51*572c4311Sfengbojiang         o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL;
52*572c4311Sfengbojiang     } else {
53*572c4311Sfengbojiang         o->lru = LRU_CLOCK();
54*572c4311Sfengbojiang     }
55*572c4311Sfengbojiang     return o;
56*572c4311Sfengbojiang }
57*572c4311Sfengbojiang 
58*572c4311Sfengbojiang /* Set a special refcount in the object to make it "shared":
59*572c4311Sfengbojiang  * incrRefCount and decrRefCount() will test for this special refcount
60*572c4311Sfengbojiang  * and will not touch the object. This way it is free to access shared
61*572c4311Sfengbojiang  * objects such as small integers from different threads without any
62*572c4311Sfengbojiang  * mutex.
63*572c4311Sfengbojiang  *
64*572c4311Sfengbojiang  * A common patter to create shared objects:
65*572c4311Sfengbojiang  *
66*572c4311Sfengbojiang  * robj *myobject = makeObjectShared(createObject(...));
67*572c4311Sfengbojiang  *
68*572c4311Sfengbojiang  */
makeObjectShared(robj * o)69*572c4311Sfengbojiang robj *makeObjectShared(robj *o) {
70*572c4311Sfengbojiang     serverAssert(o->refcount == 1);
71*572c4311Sfengbojiang     o->refcount = OBJ_SHARED_REFCOUNT;
72*572c4311Sfengbojiang     return o;
73*572c4311Sfengbojiang }
74*572c4311Sfengbojiang 
75*572c4311Sfengbojiang /* Create a string object with encoding OBJ_ENCODING_RAW, that is a plain
76*572c4311Sfengbojiang  * string object where o->ptr points to a proper sds string. */
createRawStringObject(const char * ptr,size_t len)77*572c4311Sfengbojiang robj *createRawStringObject(const char *ptr, size_t len) {
78*572c4311Sfengbojiang     return createObject(OBJ_STRING, sdsnewlen(ptr,len));
79*572c4311Sfengbojiang }
80*572c4311Sfengbojiang 
81*572c4311Sfengbojiang /* Create a string object with encoding OBJ_ENCODING_EMBSTR, that is
82*572c4311Sfengbojiang  * an object where the sds string is actually an unmodifiable string
83*572c4311Sfengbojiang  * allocated in the same chunk as the object itself. */
createEmbeddedStringObject(const char * ptr,size_t len)84*572c4311Sfengbojiang robj *createEmbeddedStringObject(const char *ptr, size_t len) {
85*572c4311Sfengbojiang     robj *o = zmalloc(sizeof(robj)+sizeof(struct sdshdr8)+len+1);
86*572c4311Sfengbojiang     struct sdshdr8 *sh = (void*)(o+1);
87*572c4311Sfengbojiang 
88*572c4311Sfengbojiang     o->type = OBJ_STRING;
89*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_EMBSTR;
90*572c4311Sfengbojiang     o->ptr = sh+1;
91*572c4311Sfengbojiang     o->refcount = 1;
92*572c4311Sfengbojiang     if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
93*572c4311Sfengbojiang         o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL;
94*572c4311Sfengbojiang     } else {
95*572c4311Sfengbojiang         o->lru = LRU_CLOCK();
96*572c4311Sfengbojiang     }
97*572c4311Sfengbojiang 
98*572c4311Sfengbojiang     sh->len = len;
99*572c4311Sfengbojiang     sh->alloc = len;
100*572c4311Sfengbojiang     sh->flags = SDS_TYPE_8;
101*572c4311Sfengbojiang     if (ptr == SDS_NOINIT)
102*572c4311Sfengbojiang         sh->buf[len] = '\0';
103*572c4311Sfengbojiang     else if (ptr) {
104*572c4311Sfengbojiang         memcpy(sh->buf,ptr,len);
105*572c4311Sfengbojiang         sh->buf[len] = '\0';
106*572c4311Sfengbojiang     } else {
107*572c4311Sfengbojiang         memset(sh->buf,0,len+1);
108*572c4311Sfengbojiang     }
109*572c4311Sfengbojiang     return o;
110*572c4311Sfengbojiang }
111*572c4311Sfengbojiang 
112*572c4311Sfengbojiang /* Create a string object with EMBSTR encoding if it is smaller than
113*572c4311Sfengbojiang  * OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is
114*572c4311Sfengbojiang  * used.
115*572c4311Sfengbojiang  *
116*572c4311Sfengbojiang  * The current limit of 44 is chosen so that the biggest string object
117*572c4311Sfengbojiang  * we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */
118*572c4311Sfengbojiang #define OBJ_ENCODING_EMBSTR_SIZE_LIMIT 44
createStringObject(const char * ptr,size_t len)119*572c4311Sfengbojiang robj *createStringObject(const char *ptr, size_t len) {
120*572c4311Sfengbojiang     if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT)
121*572c4311Sfengbojiang         return createEmbeddedStringObject(ptr,len);
122*572c4311Sfengbojiang     else
123*572c4311Sfengbojiang         return createRawStringObject(ptr,len);
124*572c4311Sfengbojiang }
125*572c4311Sfengbojiang 
126*572c4311Sfengbojiang /* Create a string object from a long long value. When possible returns a
127*572c4311Sfengbojiang  * shared integer object, or at least an integer encoded one.
128*572c4311Sfengbojiang  *
129*572c4311Sfengbojiang  * If valueobj is non zero, the function avoids returning a a shared
130*572c4311Sfengbojiang  * integer, because the object is going to be used as value in the Redis key
131*572c4311Sfengbojiang  * space (for instance when the INCR command is used), so we want LFU/LRU
132*572c4311Sfengbojiang  * values specific for each key. */
createStringObjectFromLongLongWithOptions(long long value,int valueobj)133*572c4311Sfengbojiang robj *createStringObjectFromLongLongWithOptions(long long value, int valueobj) {
134*572c4311Sfengbojiang     robj *o;
135*572c4311Sfengbojiang 
136*572c4311Sfengbojiang     if (server.maxmemory == 0 ||
137*572c4311Sfengbojiang         !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS))
138*572c4311Sfengbojiang     {
139*572c4311Sfengbojiang         /* If the maxmemory policy permits, we can still return shared integers
140*572c4311Sfengbojiang          * even if valueobj is true. */
141*572c4311Sfengbojiang         valueobj = 0;
142*572c4311Sfengbojiang     }
143*572c4311Sfengbojiang 
144*572c4311Sfengbojiang     if (value >= 0 && value < OBJ_SHARED_INTEGERS && valueobj == 0) {
145*572c4311Sfengbojiang         incrRefCount(shared.integers[value]);
146*572c4311Sfengbojiang         o = shared.integers[value];
147*572c4311Sfengbojiang     } else {
148*572c4311Sfengbojiang         if (value >= LONG_MIN && value <= LONG_MAX) {
149*572c4311Sfengbojiang             o = createObject(OBJ_STRING, NULL);
150*572c4311Sfengbojiang             o->encoding = OBJ_ENCODING_INT;
151*572c4311Sfengbojiang             o->ptr = (void*)((long)value);
152*572c4311Sfengbojiang         } else {
153*572c4311Sfengbojiang             o = createObject(OBJ_STRING,sdsfromlonglong(value));
154*572c4311Sfengbojiang         }
155*572c4311Sfengbojiang     }
156*572c4311Sfengbojiang     return o;
157*572c4311Sfengbojiang }
158*572c4311Sfengbojiang 
159*572c4311Sfengbojiang /* Wrapper for createStringObjectFromLongLongWithOptions() always demanding
160*572c4311Sfengbojiang  * to create a shared object if possible. */
createStringObjectFromLongLong(long long value)161*572c4311Sfengbojiang robj *createStringObjectFromLongLong(long long value) {
162*572c4311Sfengbojiang     return createStringObjectFromLongLongWithOptions(value,0);
163*572c4311Sfengbojiang }
164*572c4311Sfengbojiang 
165*572c4311Sfengbojiang /* Wrapper for createStringObjectFromLongLongWithOptions() avoiding a shared
166*572c4311Sfengbojiang  * object when LFU/LRU info are needed, that is, when the object is used
167*572c4311Sfengbojiang  * as a value in the key space, and Redis is configured to evict based on
168*572c4311Sfengbojiang  * LFU/LRU. */
createStringObjectFromLongLongForValue(long long value)169*572c4311Sfengbojiang robj *createStringObjectFromLongLongForValue(long long value) {
170*572c4311Sfengbojiang     return createStringObjectFromLongLongWithOptions(value,1);
171*572c4311Sfengbojiang }
172*572c4311Sfengbojiang 
173*572c4311Sfengbojiang /* Create a string object from a long double. If humanfriendly is non-zero
174*572c4311Sfengbojiang  * it does not use exponential format and trims trailing zeroes at the end,
175*572c4311Sfengbojiang  * however this results in loss of precision. Otherwise exp format is used
176*572c4311Sfengbojiang  * and the output of snprintf() is not modified.
177*572c4311Sfengbojiang  *
178*572c4311Sfengbojiang  * The 'humanfriendly' option is used for INCRBYFLOAT and HINCRBYFLOAT. */
createStringObjectFromLongDouble(long double value,int humanfriendly)179*572c4311Sfengbojiang robj *createStringObjectFromLongDouble(long double value, int humanfriendly) {
180*572c4311Sfengbojiang     char buf[MAX_LONG_DOUBLE_CHARS];
181*572c4311Sfengbojiang     int len = ld2string(buf,sizeof(buf),value,humanfriendly);
182*572c4311Sfengbojiang     return createStringObject(buf,len);
183*572c4311Sfengbojiang }
184*572c4311Sfengbojiang 
185*572c4311Sfengbojiang /* Duplicate a string object, with the guarantee that the returned object
186*572c4311Sfengbojiang  * has the same encoding as the original one.
187*572c4311Sfengbojiang  *
188*572c4311Sfengbojiang  * This function also guarantees that duplicating a small integer object
189*572c4311Sfengbojiang  * (or a string object that contains a representation of a small integer)
190*572c4311Sfengbojiang  * will always result in a fresh object that is unshared (refcount == 1).
191*572c4311Sfengbojiang  *
192*572c4311Sfengbojiang  * The resulting object always has refcount set to 1. */
dupStringObject(const robj * o)193*572c4311Sfengbojiang robj *dupStringObject(const robj *o) {
194*572c4311Sfengbojiang     robj *d;
195*572c4311Sfengbojiang 
196*572c4311Sfengbojiang     serverAssert(o->type == OBJ_STRING);
197*572c4311Sfengbojiang 
198*572c4311Sfengbojiang     switch(o->encoding) {
199*572c4311Sfengbojiang     case OBJ_ENCODING_RAW:
200*572c4311Sfengbojiang         return createRawStringObject(o->ptr,sdslen(o->ptr));
201*572c4311Sfengbojiang     case OBJ_ENCODING_EMBSTR:
202*572c4311Sfengbojiang         return createEmbeddedStringObject(o->ptr,sdslen(o->ptr));
203*572c4311Sfengbojiang     case OBJ_ENCODING_INT:
204*572c4311Sfengbojiang         d = createObject(OBJ_STRING, NULL);
205*572c4311Sfengbojiang         d->encoding = OBJ_ENCODING_INT;
206*572c4311Sfengbojiang         d->ptr = o->ptr;
207*572c4311Sfengbojiang         return d;
208*572c4311Sfengbojiang     default:
209*572c4311Sfengbojiang         serverPanic("Wrong encoding.");
210*572c4311Sfengbojiang         break;
211*572c4311Sfengbojiang     }
212*572c4311Sfengbojiang }
213*572c4311Sfengbojiang 
createQuicklistObject(void)214*572c4311Sfengbojiang robj *createQuicklistObject(void) {
215*572c4311Sfengbojiang     quicklist *l = quicklistCreate();
216*572c4311Sfengbojiang     robj *o = createObject(OBJ_LIST,l);
217*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_QUICKLIST;
218*572c4311Sfengbojiang     return o;
219*572c4311Sfengbojiang }
220*572c4311Sfengbojiang 
createZiplistObject(void)221*572c4311Sfengbojiang robj *createZiplistObject(void) {
222*572c4311Sfengbojiang     unsigned char *zl = ziplistNew();
223*572c4311Sfengbojiang     robj *o = createObject(OBJ_LIST,zl);
224*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_ZIPLIST;
225*572c4311Sfengbojiang     return o;
226*572c4311Sfengbojiang }
227*572c4311Sfengbojiang 
createSetObject(void)228*572c4311Sfengbojiang robj *createSetObject(void) {
229*572c4311Sfengbojiang     dict *d = dictCreate(&setDictType,NULL);
230*572c4311Sfengbojiang     robj *o = createObject(OBJ_SET,d);
231*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_HT;
232*572c4311Sfengbojiang     return o;
233*572c4311Sfengbojiang }
234*572c4311Sfengbojiang 
createIntsetObject(void)235*572c4311Sfengbojiang robj *createIntsetObject(void) {
236*572c4311Sfengbojiang     intset *is = intsetNew();
237*572c4311Sfengbojiang     robj *o = createObject(OBJ_SET,is);
238*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_INTSET;
239*572c4311Sfengbojiang     return o;
240*572c4311Sfengbojiang }
241*572c4311Sfengbojiang 
createHashObject(void)242*572c4311Sfengbojiang robj *createHashObject(void) {
243*572c4311Sfengbojiang     unsigned char *zl = ziplistNew();
244*572c4311Sfengbojiang     robj *o = createObject(OBJ_HASH, zl);
245*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_ZIPLIST;
246*572c4311Sfengbojiang     return o;
247*572c4311Sfengbojiang }
248*572c4311Sfengbojiang 
createZsetObject(void)249*572c4311Sfengbojiang robj *createZsetObject(void) {
250*572c4311Sfengbojiang     zset *zs = zmalloc(sizeof(*zs));
251*572c4311Sfengbojiang     robj *o;
252*572c4311Sfengbojiang 
253*572c4311Sfengbojiang     zs->dict = dictCreate(&zsetDictType,NULL);
254*572c4311Sfengbojiang     zs->zsl = zslCreate();
255*572c4311Sfengbojiang     o = createObject(OBJ_ZSET,zs);
256*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_SKIPLIST;
257*572c4311Sfengbojiang     return o;
258*572c4311Sfengbojiang }
259*572c4311Sfengbojiang 
createZsetZiplistObject(void)260*572c4311Sfengbojiang robj *createZsetZiplistObject(void) {
261*572c4311Sfengbojiang     unsigned char *zl = ziplistNew();
262*572c4311Sfengbojiang     robj *o = createObject(OBJ_ZSET,zl);
263*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_ZIPLIST;
264*572c4311Sfengbojiang     return o;
265*572c4311Sfengbojiang }
266*572c4311Sfengbojiang 
createStreamObject(void)267*572c4311Sfengbojiang robj *createStreamObject(void) {
268*572c4311Sfengbojiang     stream *s = streamNew();
269*572c4311Sfengbojiang     robj *o = createObject(OBJ_STREAM,s);
270*572c4311Sfengbojiang     o->encoding = OBJ_ENCODING_STREAM;
271*572c4311Sfengbojiang     return o;
272*572c4311Sfengbojiang }
273*572c4311Sfengbojiang 
createModuleObject(moduleType * mt,void * value)274*572c4311Sfengbojiang robj *createModuleObject(moduleType *mt, void *value) {
275*572c4311Sfengbojiang     moduleValue *mv = zmalloc(sizeof(*mv));
276*572c4311Sfengbojiang     mv->type = mt;
277*572c4311Sfengbojiang     mv->value = value;
278*572c4311Sfengbojiang     return createObject(OBJ_MODULE,mv);
279*572c4311Sfengbojiang }
280*572c4311Sfengbojiang 
freeStringObject(robj * o)281*572c4311Sfengbojiang void freeStringObject(robj *o) {
282*572c4311Sfengbojiang     if (o->encoding == OBJ_ENCODING_RAW) {
283*572c4311Sfengbojiang         sdsfree(o->ptr);
284*572c4311Sfengbojiang     }
285*572c4311Sfengbojiang }
286*572c4311Sfengbojiang 
freeListObject(robj * o)287*572c4311Sfengbojiang void freeListObject(robj *o) {
288*572c4311Sfengbojiang     if (o->encoding == OBJ_ENCODING_QUICKLIST) {
289*572c4311Sfengbojiang         quicklistRelease(o->ptr);
290*572c4311Sfengbojiang     } else {
291*572c4311Sfengbojiang         serverPanic("Unknown list encoding type");
292*572c4311Sfengbojiang     }
293*572c4311Sfengbojiang }
294*572c4311Sfengbojiang 
freeSetObject(robj * o)295*572c4311Sfengbojiang void freeSetObject(robj *o) {
296*572c4311Sfengbojiang     switch (o->encoding) {
297*572c4311Sfengbojiang     case OBJ_ENCODING_HT:
298*572c4311Sfengbojiang         dictRelease((dict*) o->ptr);
299*572c4311Sfengbojiang         break;
300*572c4311Sfengbojiang     case OBJ_ENCODING_INTSET:
301*572c4311Sfengbojiang         zfree(o->ptr);
302*572c4311Sfengbojiang         break;
303*572c4311Sfengbojiang     default:
304*572c4311Sfengbojiang         serverPanic("Unknown set encoding type");
305*572c4311Sfengbojiang     }
306*572c4311Sfengbojiang }
307*572c4311Sfengbojiang 
freeZsetObject(robj * o)308*572c4311Sfengbojiang void freeZsetObject(robj *o) {
309*572c4311Sfengbojiang     zset *zs;
310*572c4311Sfengbojiang     switch (o->encoding) {
311*572c4311Sfengbojiang     case OBJ_ENCODING_SKIPLIST:
312*572c4311Sfengbojiang         zs = o->ptr;
313*572c4311Sfengbojiang         dictRelease(zs->dict);
314*572c4311Sfengbojiang         zslFree(zs->zsl);
315*572c4311Sfengbojiang         zfree(zs);
316*572c4311Sfengbojiang         break;
317*572c4311Sfengbojiang     case OBJ_ENCODING_ZIPLIST:
318*572c4311Sfengbojiang         zfree(o->ptr);
319*572c4311Sfengbojiang         break;
320*572c4311Sfengbojiang     default:
321*572c4311Sfengbojiang         serverPanic("Unknown sorted set encoding");
322*572c4311Sfengbojiang     }
323*572c4311Sfengbojiang }
324*572c4311Sfengbojiang 
freeHashObject(robj * o)325*572c4311Sfengbojiang void freeHashObject(robj *o) {
326*572c4311Sfengbojiang     switch (o->encoding) {
327*572c4311Sfengbojiang     case OBJ_ENCODING_HT:
328*572c4311Sfengbojiang         dictRelease((dict*) o->ptr);
329*572c4311Sfengbojiang         break;
330*572c4311Sfengbojiang     case OBJ_ENCODING_ZIPLIST:
331*572c4311Sfengbojiang         zfree(o->ptr);
332*572c4311Sfengbojiang         break;
333*572c4311Sfengbojiang     default:
334*572c4311Sfengbojiang         serverPanic("Unknown hash encoding type");
335*572c4311Sfengbojiang         break;
336*572c4311Sfengbojiang     }
337*572c4311Sfengbojiang }
338*572c4311Sfengbojiang 
freeModuleObject(robj * o)339*572c4311Sfengbojiang void freeModuleObject(robj *o) {
340*572c4311Sfengbojiang     moduleValue *mv = o->ptr;
341*572c4311Sfengbojiang     mv->type->free(mv->value);
342*572c4311Sfengbojiang     zfree(mv);
343*572c4311Sfengbojiang }
344*572c4311Sfengbojiang 
freeStreamObject(robj * o)345*572c4311Sfengbojiang void freeStreamObject(robj *o) {
346*572c4311Sfengbojiang     freeStream(o->ptr);
347*572c4311Sfengbojiang }
348*572c4311Sfengbojiang 
incrRefCount(robj * o)349*572c4311Sfengbojiang void incrRefCount(robj *o) {
350*572c4311Sfengbojiang     if (o->refcount != OBJ_SHARED_REFCOUNT) o->refcount++;
351*572c4311Sfengbojiang }
352*572c4311Sfengbojiang 
decrRefCount(robj * o)353*572c4311Sfengbojiang void decrRefCount(robj *o) {
354*572c4311Sfengbojiang     if (o->refcount == 1) {
355*572c4311Sfengbojiang         switch(o->type) {
356*572c4311Sfengbojiang         case OBJ_STRING: freeStringObject(o); break;
357*572c4311Sfengbojiang         case OBJ_LIST: freeListObject(o); break;
358*572c4311Sfengbojiang         case OBJ_SET: freeSetObject(o); break;
359*572c4311Sfengbojiang         case OBJ_ZSET: freeZsetObject(o); break;
360*572c4311Sfengbojiang         case OBJ_HASH: freeHashObject(o); break;
361*572c4311Sfengbojiang         case OBJ_MODULE: freeModuleObject(o); break;
362*572c4311Sfengbojiang         case OBJ_STREAM: freeStreamObject(o); break;
363*572c4311Sfengbojiang         default: serverPanic("Unknown object type"); break;
364*572c4311Sfengbojiang         }
365*572c4311Sfengbojiang         zfree(o);
366*572c4311Sfengbojiang     } else {
367*572c4311Sfengbojiang         if (o->refcount <= 0) serverPanic("decrRefCount against refcount <= 0");
368*572c4311Sfengbojiang         if (o->refcount != OBJ_SHARED_REFCOUNT) o->refcount--;
369*572c4311Sfengbojiang     }
370*572c4311Sfengbojiang }
371*572c4311Sfengbojiang 
372*572c4311Sfengbojiang /* This variant of decrRefCount() gets its argument as void, and is useful
373*572c4311Sfengbojiang  * as free method in data structures that expect a 'void free_object(void*)'
374*572c4311Sfengbojiang  * prototype for the free method. */
decrRefCountVoid(void * o)375*572c4311Sfengbojiang void decrRefCountVoid(void *o) {
376*572c4311Sfengbojiang     decrRefCount(o);
377*572c4311Sfengbojiang }
378*572c4311Sfengbojiang 
379*572c4311Sfengbojiang /* This function set the ref count to zero without freeing the object.
380*572c4311Sfengbojiang  * It is useful in order to pass a new object to functions incrementing
381*572c4311Sfengbojiang  * the ref count of the received object. Example:
382*572c4311Sfengbojiang  *
383*572c4311Sfengbojiang  *    functionThatWillIncrementRefCount(resetRefCount(CreateObject(...)));
384*572c4311Sfengbojiang  *
385*572c4311Sfengbojiang  * Otherwise you need to resort to the less elegant pattern:
386*572c4311Sfengbojiang  *
387*572c4311Sfengbojiang  *    *obj = createObject(...);
388*572c4311Sfengbojiang  *    functionThatWillIncrementRefCount(obj);
389*572c4311Sfengbojiang  *    decrRefCount(obj);
390*572c4311Sfengbojiang  */
resetRefCount(robj * obj)391*572c4311Sfengbojiang robj *resetRefCount(robj *obj) {
392*572c4311Sfengbojiang     obj->refcount = 0;
393*572c4311Sfengbojiang     return obj;
394*572c4311Sfengbojiang }
395*572c4311Sfengbojiang 
checkType(client * c,robj * o,int type)396*572c4311Sfengbojiang int checkType(client *c, robj *o, int type) {
397*572c4311Sfengbojiang     if (o->type != type) {
398*572c4311Sfengbojiang         addReply(c,shared.wrongtypeerr);
399*572c4311Sfengbojiang         return 1;
400*572c4311Sfengbojiang     }
401*572c4311Sfengbojiang     return 0;
402*572c4311Sfengbojiang }
403*572c4311Sfengbojiang 
isSdsRepresentableAsLongLong(sds s,long long * llval)404*572c4311Sfengbojiang int isSdsRepresentableAsLongLong(sds s, long long *llval) {
405*572c4311Sfengbojiang     return string2ll(s,sdslen(s),llval) ? C_OK : C_ERR;
406*572c4311Sfengbojiang }
407*572c4311Sfengbojiang 
isObjectRepresentableAsLongLong(robj * o,long long * llval)408*572c4311Sfengbojiang int isObjectRepresentableAsLongLong(robj *o, long long *llval) {
409*572c4311Sfengbojiang     serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
410*572c4311Sfengbojiang     if (o->encoding == OBJ_ENCODING_INT) {
411*572c4311Sfengbojiang         if (llval) *llval = (long) o->ptr;
412*572c4311Sfengbojiang         return C_OK;
413*572c4311Sfengbojiang     } else {
414*572c4311Sfengbojiang         return isSdsRepresentableAsLongLong(o->ptr,llval);
415*572c4311Sfengbojiang     }
416*572c4311Sfengbojiang }
417*572c4311Sfengbojiang 
418*572c4311Sfengbojiang /* Optimize the SDS string inside the string object to require little space,
419*572c4311Sfengbojiang  * in case there is more than 10% of free space at the end of the SDS
420*572c4311Sfengbojiang  * string. This happens because SDS strings tend to overallocate to avoid
421*572c4311Sfengbojiang  * wasting too much time in allocations when appending to the string. */
trimStringObjectIfNeeded(robj * o)422*572c4311Sfengbojiang void trimStringObjectIfNeeded(robj *o) {
423*572c4311Sfengbojiang     if (o->encoding == OBJ_ENCODING_RAW &&
424*572c4311Sfengbojiang         sdsavail(o->ptr) > sdslen(o->ptr)/10)
425*572c4311Sfengbojiang     {
426*572c4311Sfengbojiang         o->ptr = sdsRemoveFreeSpace(o->ptr);
427*572c4311Sfengbojiang     }
428*572c4311Sfengbojiang }
429*572c4311Sfengbojiang 
430*572c4311Sfengbojiang /* Try to encode a string object in order to save space */
tryObjectEncoding(robj * o)431*572c4311Sfengbojiang robj *tryObjectEncoding(robj *o) {
432*572c4311Sfengbojiang     long value;
433*572c4311Sfengbojiang     sds s = o->ptr;
434*572c4311Sfengbojiang     size_t len;
435*572c4311Sfengbojiang 
436*572c4311Sfengbojiang     /* Make sure this is a string object, the only type we encode
437*572c4311Sfengbojiang      * in this function. Other types use encoded memory efficient
438*572c4311Sfengbojiang      * representations but are handled by the commands implementing
439*572c4311Sfengbojiang      * the type. */
440*572c4311Sfengbojiang     serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
441*572c4311Sfengbojiang 
442*572c4311Sfengbojiang     /* We try some specialized encoding only for objects that are
443*572c4311Sfengbojiang      * RAW or EMBSTR encoded, in other words objects that are still
444*572c4311Sfengbojiang      * in represented by an actually array of chars. */
445*572c4311Sfengbojiang     if (!sdsEncodedObject(o)) return o;
446*572c4311Sfengbojiang 
447*572c4311Sfengbojiang     /* It's not safe to encode shared objects: shared objects can be shared
448*572c4311Sfengbojiang      * everywhere in the "object space" of Redis and may end in places where
449*572c4311Sfengbojiang      * they are not handled. We handle them only as values in the keyspace. */
450*572c4311Sfengbojiang      if (o->refcount > 1) return o;
451*572c4311Sfengbojiang 
452*572c4311Sfengbojiang     /* Check if we can represent this string as a long integer.
453*572c4311Sfengbojiang      * Note that we are sure that a string larger than 20 chars is not
454*572c4311Sfengbojiang      * representable as a 32 nor 64 bit integer. */
455*572c4311Sfengbojiang     len = sdslen(s);
456*572c4311Sfengbojiang     if (len <= 20 && string2l(s,len,&value)) {
457*572c4311Sfengbojiang         /* This object is encodable as a long. Try to use a shared object.
458*572c4311Sfengbojiang          * Note that we avoid using shared integers when maxmemory is used
459*572c4311Sfengbojiang          * because every object needs to have a private LRU field for the LRU
460*572c4311Sfengbojiang          * algorithm to work well. */
461*572c4311Sfengbojiang         if ((server.maxmemory == 0 ||
462*572c4311Sfengbojiang             !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS)) &&
463*572c4311Sfengbojiang             value >= 0 &&
464*572c4311Sfengbojiang             value < OBJ_SHARED_INTEGERS)
465*572c4311Sfengbojiang         {
466*572c4311Sfengbojiang             decrRefCount(o);
467*572c4311Sfengbojiang             incrRefCount(shared.integers[value]);
468*572c4311Sfengbojiang             return shared.integers[value];
469*572c4311Sfengbojiang         } else {
470*572c4311Sfengbojiang             if (o->encoding == OBJ_ENCODING_RAW) sdsfree(o->ptr);
471*572c4311Sfengbojiang             o->encoding = OBJ_ENCODING_INT;
472*572c4311Sfengbojiang             o->ptr = (void*) value;
473*572c4311Sfengbojiang             return o;
474*572c4311Sfengbojiang         }
475*572c4311Sfengbojiang     }
476*572c4311Sfengbojiang 
477*572c4311Sfengbojiang     /* If the string is small and is still RAW encoded,
478*572c4311Sfengbojiang      * try the EMBSTR encoding which is more efficient.
479*572c4311Sfengbojiang      * In this representation the object and the SDS string are allocated
480*572c4311Sfengbojiang      * in the same chunk of memory to save space and cache misses. */
481*572c4311Sfengbojiang     if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT) {
482*572c4311Sfengbojiang         robj *emb;
483*572c4311Sfengbojiang 
484*572c4311Sfengbojiang         if (o->encoding == OBJ_ENCODING_EMBSTR) return o;
485*572c4311Sfengbojiang         emb = createEmbeddedStringObject(s,sdslen(s));
486*572c4311Sfengbojiang         decrRefCount(o);
487*572c4311Sfengbojiang         return emb;
488*572c4311Sfengbojiang     }
489*572c4311Sfengbojiang 
490*572c4311Sfengbojiang     /* We can't encode the object...
491*572c4311Sfengbojiang      *
492*572c4311Sfengbojiang      * Do the last try, and at least optimize the SDS string inside
493*572c4311Sfengbojiang      * the string object to require little space, in case there
494*572c4311Sfengbojiang      * is more than 10% of free space at the end of the SDS string.
495*572c4311Sfengbojiang      *
496*572c4311Sfengbojiang      * We do that only for relatively large strings as this branch
497*572c4311Sfengbojiang      * is only entered if the length of the string is greater than
498*572c4311Sfengbojiang      * OBJ_ENCODING_EMBSTR_SIZE_LIMIT. */
499*572c4311Sfengbojiang     trimStringObjectIfNeeded(o);
500*572c4311Sfengbojiang 
501*572c4311Sfengbojiang     /* Return the original object. */
502*572c4311Sfengbojiang     return o;
503*572c4311Sfengbojiang }
504*572c4311Sfengbojiang 
505*572c4311Sfengbojiang /* Get a decoded version of an encoded object (returned as a new object).
506*572c4311Sfengbojiang  * If the object is already raw-encoded just increment the ref count. */
getDecodedObject(robj * o)507*572c4311Sfengbojiang robj *getDecodedObject(robj *o) {
508*572c4311Sfengbojiang     robj *dec;
509*572c4311Sfengbojiang 
510*572c4311Sfengbojiang     if (sdsEncodedObject(o)) {
511*572c4311Sfengbojiang         incrRefCount(o);
512*572c4311Sfengbojiang         return o;
513*572c4311Sfengbojiang     }
514*572c4311Sfengbojiang     if (o->type == OBJ_STRING && o->encoding == OBJ_ENCODING_INT) {
515*572c4311Sfengbojiang         char buf[32];
516*572c4311Sfengbojiang 
517*572c4311Sfengbojiang         ll2string(buf,32,(long)o->ptr);
518*572c4311Sfengbojiang         dec = createStringObject(buf,strlen(buf));
519*572c4311Sfengbojiang         return dec;
520*572c4311Sfengbojiang     } else {
521*572c4311Sfengbojiang         serverPanic("Unknown encoding type");
522*572c4311Sfengbojiang     }
523*572c4311Sfengbojiang }
524*572c4311Sfengbojiang 
525*572c4311Sfengbojiang /* Compare two string objects via strcmp() or strcoll() depending on flags.
526*572c4311Sfengbojiang  * Note that the objects may be integer-encoded. In such a case we
527*572c4311Sfengbojiang  * use ll2string() to get a string representation of the numbers on the stack
528*572c4311Sfengbojiang  * and compare the strings, it's much faster than calling getDecodedObject().
529*572c4311Sfengbojiang  *
530*572c4311Sfengbojiang  * Important note: when REDIS_COMPARE_BINARY is used a binary-safe comparison
531*572c4311Sfengbojiang  * is used. */
532*572c4311Sfengbojiang 
533*572c4311Sfengbojiang #define REDIS_COMPARE_BINARY (1<<0)
534*572c4311Sfengbojiang #define REDIS_COMPARE_COLL (1<<1)
535*572c4311Sfengbojiang 
compareStringObjectsWithFlags(robj * a,robj * b,int flags)536*572c4311Sfengbojiang int compareStringObjectsWithFlags(robj *a, robj *b, int flags) {
537*572c4311Sfengbojiang     serverAssertWithInfo(NULL,a,a->type == OBJ_STRING && b->type == OBJ_STRING);
538*572c4311Sfengbojiang     char bufa[128], bufb[128], *astr, *bstr;
539*572c4311Sfengbojiang     size_t alen, blen, minlen;
540*572c4311Sfengbojiang 
541*572c4311Sfengbojiang     if (a == b) return 0;
542*572c4311Sfengbojiang     if (sdsEncodedObject(a)) {
543*572c4311Sfengbojiang         astr = a->ptr;
544*572c4311Sfengbojiang         alen = sdslen(astr);
545*572c4311Sfengbojiang     } else {
546*572c4311Sfengbojiang         alen = ll2string(bufa,sizeof(bufa),(long) a->ptr);
547*572c4311Sfengbojiang         astr = bufa;
548*572c4311Sfengbojiang     }
549*572c4311Sfengbojiang     if (sdsEncodedObject(b)) {
550*572c4311Sfengbojiang         bstr = b->ptr;
551*572c4311Sfengbojiang         blen = sdslen(bstr);
552*572c4311Sfengbojiang     } else {
553*572c4311Sfengbojiang         blen = ll2string(bufb,sizeof(bufb),(long) b->ptr);
554*572c4311Sfengbojiang         bstr = bufb;
555*572c4311Sfengbojiang     }
556*572c4311Sfengbojiang     if (flags & REDIS_COMPARE_COLL) {
557*572c4311Sfengbojiang         return strcoll(astr,bstr);
558*572c4311Sfengbojiang     } else {
559*572c4311Sfengbojiang         int cmp;
560*572c4311Sfengbojiang 
561*572c4311Sfengbojiang         minlen = (alen < blen) ? alen : blen;
562*572c4311Sfengbojiang         cmp = memcmp(astr,bstr,minlen);
563*572c4311Sfengbojiang         if (cmp == 0) return alen-blen;
564*572c4311Sfengbojiang         return cmp;
565*572c4311Sfengbojiang     }
566*572c4311Sfengbojiang }
567*572c4311Sfengbojiang 
568*572c4311Sfengbojiang /* Wrapper for compareStringObjectsWithFlags() using binary comparison. */
compareStringObjects(robj * a,robj * b)569*572c4311Sfengbojiang int compareStringObjects(robj *a, robj *b) {
570*572c4311Sfengbojiang     return compareStringObjectsWithFlags(a,b,REDIS_COMPARE_BINARY);
571*572c4311Sfengbojiang }
572*572c4311Sfengbojiang 
573*572c4311Sfengbojiang /* Wrapper for compareStringObjectsWithFlags() using collation. */
collateStringObjects(robj * a,robj * b)574*572c4311Sfengbojiang int collateStringObjects(robj *a, robj *b) {
575*572c4311Sfengbojiang     return compareStringObjectsWithFlags(a,b,REDIS_COMPARE_COLL);
576*572c4311Sfengbojiang }
577*572c4311Sfengbojiang 
578*572c4311Sfengbojiang /* Equal string objects return 1 if the two objects are the same from the
579*572c4311Sfengbojiang  * point of view of a string comparison, otherwise 0 is returned. Note that
580*572c4311Sfengbojiang  * this function is faster then checking for (compareStringObject(a,b) == 0)
581*572c4311Sfengbojiang  * because it can perform some more optimization. */
equalStringObjects(robj * a,robj * b)582*572c4311Sfengbojiang int equalStringObjects(robj *a, robj *b) {
583*572c4311Sfengbojiang     if (a->encoding == OBJ_ENCODING_INT &&
584*572c4311Sfengbojiang         b->encoding == OBJ_ENCODING_INT){
585*572c4311Sfengbojiang         /* If both strings are integer encoded just check if the stored
586*572c4311Sfengbojiang          * long is the same. */
587*572c4311Sfengbojiang         return a->ptr == b->ptr;
588*572c4311Sfengbojiang     } else {
589*572c4311Sfengbojiang         return compareStringObjects(a,b) == 0;
590*572c4311Sfengbojiang     }
591*572c4311Sfengbojiang }
592*572c4311Sfengbojiang 
stringObjectLen(robj * o)593*572c4311Sfengbojiang size_t stringObjectLen(robj *o) {
594*572c4311Sfengbojiang     serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
595*572c4311Sfengbojiang     if (sdsEncodedObject(o)) {
596*572c4311Sfengbojiang         return sdslen(o->ptr);
597*572c4311Sfengbojiang     } else {
598*572c4311Sfengbojiang         return sdigits10((long)o->ptr);
599*572c4311Sfengbojiang     }
600*572c4311Sfengbojiang }
601*572c4311Sfengbojiang 
getDoubleFromObject(const robj * o,double * target)602*572c4311Sfengbojiang int getDoubleFromObject(const robj *o, double *target) {
603*572c4311Sfengbojiang     double value;
604*572c4311Sfengbojiang     char *eptr;
605*572c4311Sfengbojiang 
606*572c4311Sfengbojiang     if (o == NULL) {
607*572c4311Sfengbojiang         value = 0;
608*572c4311Sfengbojiang     } else {
609*572c4311Sfengbojiang         serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
610*572c4311Sfengbojiang         if (sdsEncodedObject(o)) {
611*572c4311Sfengbojiang             errno = 0;
612*572c4311Sfengbojiang             value = strtod(o->ptr, &eptr);
613*572c4311Sfengbojiang             if (sdslen(o->ptr) == 0 ||
614*572c4311Sfengbojiang                 isspace(((const char*)o->ptr)[0]) ||
615*572c4311Sfengbojiang                 (size_t)(eptr-(char*)o->ptr) != sdslen(o->ptr) ||
616*572c4311Sfengbojiang                 (errno == ERANGE &&
617*572c4311Sfengbojiang                     (value == HUGE_VAL || value == -HUGE_VAL || value == 0)) ||
618*572c4311Sfengbojiang                 isnan(value))
619*572c4311Sfengbojiang                 return C_ERR;
620*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_INT) {
621*572c4311Sfengbojiang             value = (long)o->ptr;
622*572c4311Sfengbojiang         } else {
623*572c4311Sfengbojiang             serverPanic("Unknown string encoding");
624*572c4311Sfengbojiang         }
625*572c4311Sfengbojiang     }
626*572c4311Sfengbojiang     *target = value;
627*572c4311Sfengbojiang     return C_OK;
628*572c4311Sfengbojiang }
629*572c4311Sfengbojiang 
getDoubleFromObjectOrReply(client * c,robj * o,double * target,const char * msg)630*572c4311Sfengbojiang int getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg) {
631*572c4311Sfengbojiang     double value;
632*572c4311Sfengbojiang     if (getDoubleFromObject(o, &value) != C_OK) {
633*572c4311Sfengbojiang         if (msg != NULL) {
634*572c4311Sfengbojiang             addReplyError(c,(char*)msg);
635*572c4311Sfengbojiang         } else {
636*572c4311Sfengbojiang             addReplyError(c,"value is not a valid float");
637*572c4311Sfengbojiang         }
638*572c4311Sfengbojiang         return C_ERR;
639*572c4311Sfengbojiang     }
640*572c4311Sfengbojiang     *target = value;
641*572c4311Sfengbojiang     return C_OK;
642*572c4311Sfengbojiang }
643*572c4311Sfengbojiang 
getLongDoubleFromObject(robj * o,long double * target)644*572c4311Sfengbojiang int getLongDoubleFromObject(robj *o, long double *target) {
645*572c4311Sfengbojiang     long double value;
646*572c4311Sfengbojiang     char *eptr;
647*572c4311Sfengbojiang 
648*572c4311Sfengbojiang     if (o == NULL) {
649*572c4311Sfengbojiang         value = 0;
650*572c4311Sfengbojiang     } else {
651*572c4311Sfengbojiang         serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
652*572c4311Sfengbojiang         if (sdsEncodedObject(o)) {
653*572c4311Sfengbojiang             errno = 0;
654*572c4311Sfengbojiang             value = strtold(o->ptr, &eptr);
655*572c4311Sfengbojiang             if (sdslen(o->ptr) == 0 ||
656*572c4311Sfengbojiang                 isspace(((const char*)o->ptr)[0]) ||
657*572c4311Sfengbojiang                 (size_t)(eptr-(char*)o->ptr) != sdslen(o->ptr) ||
658*572c4311Sfengbojiang                 (errno == ERANGE &&
659*572c4311Sfengbojiang                     (value == HUGE_VAL || value == -HUGE_VAL || value == 0)) ||
660*572c4311Sfengbojiang                 isnan(value))
661*572c4311Sfengbojiang                 return C_ERR;
662*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_INT) {
663*572c4311Sfengbojiang             value = (long)o->ptr;
664*572c4311Sfengbojiang         } else {
665*572c4311Sfengbojiang             serverPanic("Unknown string encoding");
666*572c4311Sfengbojiang         }
667*572c4311Sfengbojiang     }
668*572c4311Sfengbojiang     *target = value;
669*572c4311Sfengbojiang     return C_OK;
670*572c4311Sfengbojiang }
671*572c4311Sfengbojiang 
getLongDoubleFromObjectOrReply(client * c,robj * o,long double * target,const char * msg)672*572c4311Sfengbojiang int getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg) {
673*572c4311Sfengbojiang     long double value;
674*572c4311Sfengbojiang     if (getLongDoubleFromObject(o, &value) != C_OK) {
675*572c4311Sfengbojiang         if (msg != NULL) {
676*572c4311Sfengbojiang             addReplyError(c,(char*)msg);
677*572c4311Sfengbojiang         } else {
678*572c4311Sfengbojiang             addReplyError(c,"value is not a valid float");
679*572c4311Sfengbojiang         }
680*572c4311Sfengbojiang         return C_ERR;
681*572c4311Sfengbojiang     }
682*572c4311Sfengbojiang     *target = value;
683*572c4311Sfengbojiang     return C_OK;
684*572c4311Sfengbojiang }
685*572c4311Sfengbojiang 
getLongLongFromObject(robj * o,long long * target)686*572c4311Sfengbojiang int getLongLongFromObject(robj *o, long long *target) {
687*572c4311Sfengbojiang     long long value;
688*572c4311Sfengbojiang 
689*572c4311Sfengbojiang     if (o == NULL) {
690*572c4311Sfengbojiang         value = 0;
691*572c4311Sfengbojiang     } else {
692*572c4311Sfengbojiang         serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
693*572c4311Sfengbojiang         if (sdsEncodedObject(o)) {
694*572c4311Sfengbojiang             if (string2ll(o->ptr,sdslen(o->ptr),&value) == 0) return C_ERR;
695*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_INT) {
696*572c4311Sfengbojiang             value = (long)o->ptr;
697*572c4311Sfengbojiang         } else {
698*572c4311Sfengbojiang             serverPanic("Unknown string encoding");
699*572c4311Sfengbojiang         }
700*572c4311Sfengbojiang     }
701*572c4311Sfengbojiang     if (target) *target = value;
702*572c4311Sfengbojiang     return C_OK;
703*572c4311Sfengbojiang }
704*572c4311Sfengbojiang 
getLongLongFromObjectOrReply(client * c,robj * o,long long * target,const char * msg)705*572c4311Sfengbojiang int getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg) {
706*572c4311Sfengbojiang     long long value;
707*572c4311Sfengbojiang     if (getLongLongFromObject(o, &value) != C_OK) {
708*572c4311Sfengbojiang         if (msg != NULL) {
709*572c4311Sfengbojiang             addReplyError(c,(char*)msg);
710*572c4311Sfengbojiang         } else {
711*572c4311Sfengbojiang             addReplyError(c,"value is not an integer or out of range");
712*572c4311Sfengbojiang         }
713*572c4311Sfengbojiang         return C_ERR;
714*572c4311Sfengbojiang     }
715*572c4311Sfengbojiang     *target = value;
716*572c4311Sfengbojiang     return C_OK;
717*572c4311Sfengbojiang }
718*572c4311Sfengbojiang 
getLongFromObjectOrReply(client * c,robj * o,long * target,const char * msg)719*572c4311Sfengbojiang int getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg) {
720*572c4311Sfengbojiang     long long value;
721*572c4311Sfengbojiang 
722*572c4311Sfengbojiang     if (getLongLongFromObjectOrReply(c, o, &value, msg) != C_OK) return C_ERR;
723*572c4311Sfengbojiang     if (value < LONG_MIN || value > LONG_MAX) {
724*572c4311Sfengbojiang         if (msg != NULL) {
725*572c4311Sfengbojiang             addReplyError(c,(char*)msg);
726*572c4311Sfengbojiang         } else {
727*572c4311Sfengbojiang             addReplyError(c,"value is out of range");
728*572c4311Sfengbojiang         }
729*572c4311Sfengbojiang         return C_ERR;
730*572c4311Sfengbojiang     }
731*572c4311Sfengbojiang     *target = value;
732*572c4311Sfengbojiang     return C_OK;
733*572c4311Sfengbojiang }
734*572c4311Sfengbojiang 
strEncoding(int encoding)735*572c4311Sfengbojiang char *strEncoding(int encoding) {
736*572c4311Sfengbojiang     switch(encoding) {
737*572c4311Sfengbojiang     case OBJ_ENCODING_RAW: return "raw";
738*572c4311Sfengbojiang     case OBJ_ENCODING_INT: return "int";
739*572c4311Sfengbojiang     case OBJ_ENCODING_HT: return "hashtable";
740*572c4311Sfengbojiang     case OBJ_ENCODING_QUICKLIST: return "quicklist";
741*572c4311Sfengbojiang     case OBJ_ENCODING_ZIPLIST: return "ziplist";
742*572c4311Sfengbojiang     case OBJ_ENCODING_INTSET: return "intset";
743*572c4311Sfengbojiang     case OBJ_ENCODING_SKIPLIST: return "skiplist";
744*572c4311Sfengbojiang     case OBJ_ENCODING_EMBSTR: return "embstr";
745*572c4311Sfengbojiang     default: return "unknown";
746*572c4311Sfengbojiang     }
747*572c4311Sfengbojiang }
748*572c4311Sfengbojiang 
749*572c4311Sfengbojiang /* =========================== Memory introspection ========================= */
750*572c4311Sfengbojiang 
751*572c4311Sfengbojiang 
752*572c4311Sfengbojiang /* This is an helper function with the goal of estimating the memory
753*572c4311Sfengbojiang  * size of a radix tree that is used to store Stream IDs.
754*572c4311Sfengbojiang  *
755*572c4311Sfengbojiang  * Note: to guess the size of the radix tree is not trivial, so we
756*572c4311Sfengbojiang  * approximate it considering 16 bytes of data overhead for each
757*572c4311Sfengbojiang  * key (the ID), and then adding the number of bare nodes, plus some
758*572c4311Sfengbojiang  * overhead due by the data and child pointers. This secret recipe
759*572c4311Sfengbojiang  * was obtained by checking the average radix tree created by real
760*572c4311Sfengbojiang  * workloads, and then adjusting the constants to get numbers that
761*572c4311Sfengbojiang  * more or less match the real memory usage.
762*572c4311Sfengbojiang  *
763*572c4311Sfengbojiang  * Actually the number of nodes and keys may be different depending
764*572c4311Sfengbojiang  * on the insertion speed and thus the ability of the radix tree
765*572c4311Sfengbojiang  * to compress prefixes. */
streamRadixTreeMemoryUsage(rax * rax)766*572c4311Sfengbojiang size_t streamRadixTreeMemoryUsage(rax *rax) {
767*572c4311Sfengbojiang     size_t size;
768*572c4311Sfengbojiang     size = rax->numele * sizeof(streamID);
769*572c4311Sfengbojiang     size += rax->numnodes * sizeof(raxNode);
770*572c4311Sfengbojiang     /* Add a fixed overhead due to the aux data pointer, children, ... */
771*572c4311Sfengbojiang     size += rax->numnodes * sizeof(long)*30;
772*572c4311Sfengbojiang     return size;
773*572c4311Sfengbojiang }
774*572c4311Sfengbojiang 
775*572c4311Sfengbojiang /* Returns the size in bytes consumed by the key's value in RAM.
776*572c4311Sfengbojiang  * Note that the returned value is just an approximation, especially in the
777*572c4311Sfengbojiang  * case of aggregated data types where only "sample_size" elements
778*572c4311Sfengbojiang  * are checked and averaged to estimate the total size. */
779*572c4311Sfengbojiang #define OBJ_COMPUTE_SIZE_DEF_SAMPLES 5 /* Default sample size. */
objectComputeSize(robj * o,size_t sample_size)780*572c4311Sfengbojiang size_t objectComputeSize(robj *o, size_t sample_size) {
781*572c4311Sfengbojiang     sds ele, ele2;
782*572c4311Sfengbojiang     dict *d;
783*572c4311Sfengbojiang     dictIterator *di;
784*572c4311Sfengbojiang     struct dictEntry *de;
785*572c4311Sfengbojiang     size_t asize = 0, elesize = 0, samples = 0;
786*572c4311Sfengbojiang 
787*572c4311Sfengbojiang     if (o->type == OBJ_STRING) {
788*572c4311Sfengbojiang         if(o->encoding == OBJ_ENCODING_INT) {
789*572c4311Sfengbojiang             asize = sizeof(*o);
790*572c4311Sfengbojiang         } else if(o->encoding == OBJ_ENCODING_RAW) {
791*572c4311Sfengbojiang             asize = sdsAllocSize(o->ptr)+sizeof(*o);
792*572c4311Sfengbojiang         } else if(o->encoding == OBJ_ENCODING_EMBSTR) {
793*572c4311Sfengbojiang             asize = sdslen(o->ptr)+2+sizeof(*o);
794*572c4311Sfengbojiang         } else {
795*572c4311Sfengbojiang             serverPanic("Unknown string encoding");
796*572c4311Sfengbojiang         }
797*572c4311Sfengbojiang     } else if (o->type == OBJ_LIST) {
798*572c4311Sfengbojiang         if (o->encoding == OBJ_ENCODING_QUICKLIST) {
799*572c4311Sfengbojiang             quicklist *ql = o->ptr;
800*572c4311Sfengbojiang             quicklistNode *node = ql->head;
801*572c4311Sfengbojiang             asize = sizeof(*o)+sizeof(quicklist);
802*572c4311Sfengbojiang             do {
803*572c4311Sfengbojiang                 elesize += sizeof(quicklistNode)+ziplistBlobLen(node->zl);
804*572c4311Sfengbojiang                 samples++;
805*572c4311Sfengbojiang             } while ((node = node->next) && samples < sample_size);
806*572c4311Sfengbojiang             asize += (double)elesize/samples*ql->len;
807*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_ZIPLIST) {
808*572c4311Sfengbojiang             asize = sizeof(*o)+ziplistBlobLen(o->ptr);
809*572c4311Sfengbojiang         } else {
810*572c4311Sfengbojiang             serverPanic("Unknown list encoding");
811*572c4311Sfengbojiang         }
812*572c4311Sfengbojiang     } else if (o->type == OBJ_SET) {
813*572c4311Sfengbojiang         if (o->encoding == OBJ_ENCODING_HT) {
814*572c4311Sfengbojiang             d = o->ptr;
815*572c4311Sfengbojiang             di = dictGetIterator(d);
816*572c4311Sfengbojiang             asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
817*572c4311Sfengbojiang             while((de = dictNext(di)) != NULL && samples < sample_size) {
818*572c4311Sfengbojiang                 ele = dictGetKey(de);
819*572c4311Sfengbojiang                 elesize += sizeof(struct dictEntry) + sdsAllocSize(ele);
820*572c4311Sfengbojiang                 samples++;
821*572c4311Sfengbojiang             }
822*572c4311Sfengbojiang             dictReleaseIterator(di);
823*572c4311Sfengbojiang             if (samples) asize += (double)elesize/samples*dictSize(d);
824*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_INTSET) {
825*572c4311Sfengbojiang             intset *is = o->ptr;
826*572c4311Sfengbojiang             asize = sizeof(*o)+sizeof(*is)+is->encoding*is->length;
827*572c4311Sfengbojiang         } else {
828*572c4311Sfengbojiang             serverPanic("Unknown set encoding");
829*572c4311Sfengbojiang         }
830*572c4311Sfengbojiang     } else if (o->type == OBJ_ZSET) {
831*572c4311Sfengbojiang         if (o->encoding == OBJ_ENCODING_ZIPLIST) {
832*572c4311Sfengbojiang             asize = sizeof(*o)+(ziplistBlobLen(o->ptr));
833*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {
834*572c4311Sfengbojiang             d = ((zset*)o->ptr)->dict;
835*572c4311Sfengbojiang             zskiplist *zsl = ((zset*)o->ptr)->zsl;
836*572c4311Sfengbojiang             zskiplistNode *znode = zsl->header->level[0].forward;
837*572c4311Sfengbojiang             asize = sizeof(*o)+sizeof(zset)+(sizeof(struct dictEntry*)*dictSlots(d));
838*572c4311Sfengbojiang             while(znode != NULL && samples < sample_size) {
839*572c4311Sfengbojiang                 elesize += sdsAllocSize(znode->ele);
840*572c4311Sfengbojiang                 elesize += sizeof(struct dictEntry) + zmalloc_size(znode);
841*572c4311Sfengbojiang                 samples++;
842*572c4311Sfengbojiang                 znode = znode->level[0].forward;
843*572c4311Sfengbojiang             }
844*572c4311Sfengbojiang             if (samples) asize += (double)elesize/samples*dictSize(d);
845*572c4311Sfengbojiang         } else {
846*572c4311Sfengbojiang             serverPanic("Unknown sorted set encoding");
847*572c4311Sfengbojiang         }
848*572c4311Sfengbojiang     } else if (o->type == OBJ_HASH) {
849*572c4311Sfengbojiang         if (o->encoding == OBJ_ENCODING_ZIPLIST) {
850*572c4311Sfengbojiang             asize = sizeof(*o)+(ziplistBlobLen(o->ptr));
851*572c4311Sfengbojiang         } else if (o->encoding == OBJ_ENCODING_HT) {
852*572c4311Sfengbojiang             d = o->ptr;
853*572c4311Sfengbojiang             di = dictGetIterator(d);
854*572c4311Sfengbojiang             asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
855*572c4311Sfengbojiang             while((de = dictNext(di)) != NULL && samples < sample_size) {
856*572c4311Sfengbojiang                 ele = dictGetKey(de);
857*572c4311Sfengbojiang                 ele2 = dictGetVal(de);
858*572c4311Sfengbojiang                 elesize += sdsAllocSize(ele) + sdsAllocSize(ele2);
859*572c4311Sfengbojiang                 elesize += sizeof(struct dictEntry);
860*572c4311Sfengbojiang                 samples++;
861*572c4311Sfengbojiang             }
862*572c4311Sfengbojiang             dictReleaseIterator(di);
863*572c4311Sfengbojiang             if (samples) asize += (double)elesize/samples*dictSize(d);
864*572c4311Sfengbojiang         } else {
865*572c4311Sfengbojiang             serverPanic("Unknown hash encoding");
866*572c4311Sfengbojiang         }
867*572c4311Sfengbojiang     } else if (o->type == OBJ_STREAM) {
868*572c4311Sfengbojiang         stream *s = o->ptr;
869*572c4311Sfengbojiang         asize = sizeof(*o);
870*572c4311Sfengbojiang         asize += streamRadixTreeMemoryUsage(s->rax);
871*572c4311Sfengbojiang 
872*572c4311Sfengbojiang         /* Now we have to add the listpacks. The last listpack is often non
873*572c4311Sfengbojiang          * complete, so we estimate the size of the first N listpacks, and
874*572c4311Sfengbojiang          * use the average to compute the size of the first N-1 listpacks, and
875*572c4311Sfengbojiang          * finally add the real size of the last node. */
876*572c4311Sfengbojiang         raxIterator ri;
877*572c4311Sfengbojiang         raxStart(&ri,s->rax);
878*572c4311Sfengbojiang         raxSeek(&ri,"^",NULL,0);
879*572c4311Sfengbojiang         size_t lpsize = 0, samples = 0;
880*572c4311Sfengbojiang         while(samples < sample_size && raxNext(&ri)) {
881*572c4311Sfengbojiang             unsigned char *lp = ri.data;
882*572c4311Sfengbojiang             lpsize += lpBytes(lp);
883*572c4311Sfengbojiang             samples++;
884*572c4311Sfengbojiang         }
885*572c4311Sfengbojiang         if (s->rax->numele <= samples) {
886*572c4311Sfengbojiang             asize += lpsize;
887*572c4311Sfengbojiang         } else {
888*572c4311Sfengbojiang             if (samples) lpsize /= samples; /* Compute the average. */
889*572c4311Sfengbojiang             asize += lpsize * (s->rax->numele-1);
890*572c4311Sfengbojiang             /* No need to check if seek succeeded, we enter this branch only
891*572c4311Sfengbojiang              * if there are a few elements in the radix tree. */
892*572c4311Sfengbojiang             raxSeek(&ri,"$",NULL,0);
893*572c4311Sfengbojiang             raxNext(&ri);
894*572c4311Sfengbojiang             asize += lpBytes(ri.data);
895*572c4311Sfengbojiang         }
896*572c4311Sfengbojiang         raxStop(&ri);
897*572c4311Sfengbojiang 
898*572c4311Sfengbojiang         /* Consumer groups also have a non trivial memory overhead if there
899*572c4311Sfengbojiang          * are many consumers and many groups, let's count at least the
900*572c4311Sfengbojiang          * overhead of the pending entries in the groups and consumers
901*572c4311Sfengbojiang          * PELs. */
902*572c4311Sfengbojiang         if (s->cgroups) {
903*572c4311Sfengbojiang             raxStart(&ri,s->cgroups);
904*572c4311Sfengbojiang             raxSeek(&ri,"^",NULL,0);
905*572c4311Sfengbojiang             while(raxNext(&ri)) {
906*572c4311Sfengbojiang                 streamCG *cg = ri.data;
907*572c4311Sfengbojiang                 asize += sizeof(*cg);
908*572c4311Sfengbojiang                 asize += streamRadixTreeMemoryUsage(cg->pel);
909*572c4311Sfengbojiang                 asize += sizeof(streamNACK)*raxSize(cg->pel);
910*572c4311Sfengbojiang 
911*572c4311Sfengbojiang                 /* For each consumer we also need to add the basic data
912*572c4311Sfengbojiang                  * structures and the PEL memory usage. */
913*572c4311Sfengbojiang                 raxIterator cri;
914*572c4311Sfengbojiang                 raxStart(&cri,cg->consumers);
915*572c4311Sfengbojiang                 raxSeek(&cri,"^",NULL,0);
916*572c4311Sfengbojiang                 while(raxNext(&cri)) {
917*572c4311Sfengbojiang                     streamConsumer *consumer = cri.data;
918*572c4311Sfengbojiang                     asize += sizeof(*consumer);
919*572c4311Sfengbojiang                     asize += sdslen(consumer->name);
920*572c4311Sfengbojiang                     asize += streamRadixTreeMemoryUsage(consumer->pel);
921*572c4311Sfengbojiang                     /* Don't count NACKs again, they are shared with the
922*572c4311Sfengbojiang                      * consumer group PEL. */
923*572c4311Sfengbojiang                 }
924*572c4311Sfengbojiang                 raxStop(&cri);
925*572c4311Sfengbojiang             }
926*572c4311Sfengbojiang             raxStop(&ri);
927*572c4311Sfengbojiang         }
928*572c4311Sfengbojiang     } else if (o->type == OBJ_MODULE) {
929*572c4311Sfengbojiang         moduleValue *mv = o->ptr;
930*572c4311Sfengbojiang         moduleType *mt = mv->type;
931*572c4311Sfengbojiang         if (mt->mem_usage != NULL) {
932*572c4311Sfengbojiang             asize = mt->mem_usage(mv->value);
933*572c4311Sfengbojiang         } else {
934*572c4311Sfengbojiang             asize = 0;
935*572c4311Sfengbojiang         }
936*572c4311Sfengbojiang     } else {
937*572c4311Sfengbojiang         serverPanic("Unknown object type");
938*572c4311Sfengbojiang     }
939*572c4311Sfengbojiang     return asize;
940*572c4311Sfengbojiang }
941*572c4311Sfengbojiang 
942*572c4311Sfengbojiang /* Release data obtained with getMemoryOverheadData(). */
freeMemoryOverheadData(struct redisMemOverhead * mh)943*572c4311Sfengbojiang void freeMemoryOverheadData(struct redisMemOverhead *mh) {
944*572c4311Sfengbojiang     zfree(mh->db);
945*572c4311Sfengbojiang     zfree(mh);
946*572c4311Sfengbojiang }
947*572c4311Sfengbojiang 
948*572c4311Sfengbojiang /* Return a struct redisMemOverhead filled with memory overhead
949*572c4311Sfengbojiang  * information used for the MEMORY OVERHEAD and INFO command. The returned
950*572c4311Sfengbojiang  * structure pointer should be freed calling freeMemoryOverheadData(). */
getMemoryOverheadData(void)951*572c4311Sfengbojiang struct redisMemOverhead *getMemoryOverheadData(void) {
952*572c4311Sfengbojiang     int j;
953*572c4311Sfengbojiang     size_t mem_total = 0;
954*572c4311Sfengbojiang     size_t mem = 0;
955*572c4311Sfengbojiang     size_t zmalloc_used = zmalloc_used_memory();
956*572c4311Sfengbojiang     struct redisMemOverhead *mh = zcalloc(sizeof(*mh));
957*572c4311Sfengbojiang 
958*572c4311Sfengbojiang     mh->total_allocated = zmalloc_used;
959*572c4311Sfengbojiang     mh->startup_allocated = server.initial_memory_usage;
960*572c4311Sfengbojiang     mh->peak_allocated = server.stat_peak_memory;
961*572c4311Sfengbojiang     mh->total_frag =
962*572c4311Sfengbojiang         (float)server.cron_malloc_stats.process_rss / server.cron_malloc_stats.zmalloc_used;
963*572c4311Sfengbojiang     mh->total_frag_bytes =
964*572c4311Sfengbojiang         server.cron_malloc_stats.process_rss - server.cron_malloc_stats.zmalloc_used;
965*572c4311Sfengbojiang     mh->allocator_frag =
966*572c4311Sfengbojiang         (float)server.cron_malloc_stats.allocator_active / server.cron_malloc_stats.allocator_allocated;
967*572c4311Sfengbojiang     mh->allocator_frag_bytes =
968*572c4311Sfengbojiang         server.cron_malloc_stats.allocator_active - server.cron_malloc_stats.allocator_allocated;
969*572c4311Sfengbojiang     mh->allocator_rss =
970*572c4311Sfengbojiang         (float)server.cron_malloc_stats.allocator_resident / server.cron_malloc_stats.allocator_active;
971*572c4311Sfengbojiang     mh->allocator_rss_bytes =
972*572c4311Sfengbojiang         server.cron_malloc_stats.allocator_resident - server.cron_malloc_stats.allocator_active;
973*572c4311Sfengbojiang     mh->rss_extra =
974*572c4311Sfengbojiang         (float)server.cron_malloc_stats.process_rss / server.cron_malloc_stats.allocator_resident;
975*572c4311Sfengbojiang     mh->rss_extra_bytes =
976*572c4311Sfengbojiang         server.cron_malloc_stats.process_rss - server.cron_malloc_stats.allocator_resident;
977*572c4311Sfengbojiang 
978*572c4311Sfengbojiang     mem_total += server.initial_memory_usage;
979*572c4311Sfengbojiang 
980*572c4311Sfengbojiang     mem = 0;
981*572c4311Sfengbojiang     if (server.repl_backlog)
982*572c4311Sfengbojiang         mem += zmalloc_size(server.repl_backlog);
983*572c4311Sfengbojiang     mh->repl_backlog = mem;
984*572c4311Sfengbojiang     mem_total += mem;
985*572c4311Sfengbojiang 
986*572c4311Sfengbojiang     mem = 0;
987*572c4311Sfengbojiang     if (listLength(server.slaves)) {
988*572c4311Sfengbojiang         listIter li;
989*572c4311Sfengbojiang         listNode *ln;
990*572c4311Sfengbojiang 
991*572c4311Sfengbojiang         listRewind(server.slaves,&li);
992*572c4311Sfengbojiang         while((ln = listNext(&li))) {
993*572c4311Sfengbojiang             client *c = listNodeValue(ln);
994*572c4311Sfengbojiang             mem += getClientOutputBufferMemoryUsage(c);
995*572c4311Sfengbojiang             mem += sdsAllocSize(c->querybuf);
996*572c4311Sfengbojiang             mem += sizeof(client);
997*572c4311Sfengbojiang         }
998*572c4311Sfengbojiang     }
999*572c4311Sfengbojiang     mh->clients_slaves = mem;
1000*572c4311Sfengbojiang     mem_total+=mem;
1001*572c4311Sfengbojiang 
1002*572c4311Sfengbojiang     mem = 0;
1003*572c4311Sfengbojiang     if (listLength(server.clients)) {
1004*572c4311Sfengbojiang         listIter li;
1005*572c4311Sfengbojiang         listNode *ln;
1006*572c4311Sfengbojiang 
1007*572c4311Sfengbojiang         listRewind(server.clients,&li);
1008*572c4311Sfengbojiang         while((ln = listNext(&li))) {
1009*572c4311Sfengbojiang             client *c = listNodeValue(ln);
1010*572c4311Sfengbojiang             if (c->flags & CLIENT_SLAVE && !(c->flags & CLIENT_MONITOR))
1011*572c4311Sfengbojiang                 continue;
1012*572c4311Sfengbojiang             mem += getClientOutputBufferMemoryUsage(c);
1013*572c4311Sfengbojiang             mem += sdsAllocSize(c->querybuf);
1014*572c4311Sfengbojiang             mem += sizeof(client);
1015*572c4311Sfengbojiang         }
1016*572c4311Sfengbojiang     }
1017*572c4311Sfengbojiang     mh->clients_normal = mem;
1018*572c4311Sfengbojiang     mem_total+=mem;
1019*572c4311Sfengbojiang 
1020*572c4311Sfengbojiang     mem = 0;
1021*572c4311Sfengbojiang     if (server.aof_state != AOF_OFF) {
1022*572c4311Sfengbojiang         mem += sdsalloc(server.aof_buf);
1023*572c4311Sfengbojiang         mem += aofRewriteBufferSize();
1024*572c4311Sfengbojiang     }
1025*572c4311Sfengbojiang     mh->aof_buffer = mem;
1026*572c4311Sfengbojiang     mem_total+=mem;
1027*572c4311Sfengbojiang 
1028*572c4311Sfengbojiang     mem = server.lua_scripts_mem;
1029*572c4311Sfengbojiang     mem += dictSize(server.lua_scripts) * sizeof(dictEntry) +
1030*572c4311Sfengbojiang         dictSlots(server.lua_scripts) * sizeof(dictEntry*);
1031*572c4311Sfengbojiang     mem += dictSize(server.repl_scriptcache_dict) * sizeof(dictEntry) +
1032*572c4311Sfengbojiang         dictSlots(server.repl_scriptcache_dict) * sizeof(dictEntry*);
1033*572c4311Sfengbojiang     if (listLength(server.repl_scriptcache_fifo) > 0) {
1034*572c4311Sfengbojiang         mem += listLength(server.repl_scriptcache_fifo) * (sizeof(listNode) +
1035*572c4311Sfengbojiang             sdsZmallocSize(listNodeValue(listFirst(server.repl_scriptcache_fifo))));
1036*572c4311Sfengbojiang     }
1037*572c4311Sfengbojiang     mh->lua_caches = mem;
1038*572c4311Sfengbojiang     mem_total+=mem;
1039*572c4311Sfengbojiang 
1040*572c4311Sfengbojiang     for (j = 0; j < server.dbnum; j++) {
1041*572c4311Sfengbojiang         redisDb *db = server.db+j;
1042*572c4311Sfengbojiang         long long keyscount = dictSize(db->dict);
1043*572c4311Sfengbojiang         if (keyscount==0) continue;
1044*572c4311Sfengbojiang 
1045*572c4311Sfengbojiang         mh->total_keys += keyscount;
1046*572c4311Sfengbojiang         mh->db = zrealloc(mh->db,sizeof(mh->db[0])*(mh->num_dbs+1));
1047*572c4311Sfengbojiang         mh->db[mh->num_dbs].dbid = j;
1048*572c4311Sfengbojiang 
1049*572c4311Sfengbojiang         mem = dictSize(db->dict) * sizeof(dictEntry) +
1050*572c4311Sfengbojiang               dictSlots(db->dict) * sizeof(dictEntry*) +
1051*572c4311Sfengbojiang               dictSize(db->dict) * sizeof(robj);
1052*572c4311Sfengbojiang         mh->db[mh->num_dbs].overhead_ht_main = mem;
1053*572c4311Sfengbojiang         mem_total+=mem;
1054*572c4311Sfengbojiang 
1055*572c4311Sfengbojiang         mem = dictSize(db->expires) * sizeof(dictEntry) +
1056*572c4311Sfengbojiang               dictSlots(db->expires) * sizeof(dictEntry*);
1057*572c4311Sfengbojiang         mh->db[mh->num_dbs].overhead_ht_expires = mem;
1058*572c4311Sfengbojiang         mem_total+=mem;
1059*572c4311Sfengbojiang 
1060*572c4311Sfengbojiang         mh->num_dbs++;
1061*572c4311Sfengbojiang     }
1062*572c4311Sfengbojiang 
1063*572c4311Sfengbojiang     mh->overhead_total = mem_total;
1064*572c4311Sfengbojiang     mh->dataset = zmalloc_used - mem_total;
1065*572c4311Sfengbojiang     mh->peak_perc = (float)zmalloc_used*100/mh->peak_allocated;
1066*572c4311Sfengbojiang 
1067*572c4311Sfengbojiang     /* Metrics computed after subtracting the startup memory from
1068*572c4311Sfengbojiang      * the total memory. */
1069*572c4311Sfengbojiang     size_t net_usage = 1;
1070*572c4311Sfengbojiang     if (zmalloc_used > mh->startup_allocated)
1071*572c4311Sfengbojiang         net_usage = zmalloc_used - mh->startup_allocated;
1072*572c4311Sfengbojiang     mh->dataset_perc = (float)mh->dataset*100/net_usage;
1073*572c4311Sfengbojiang     mh->bytes_per_key = mh->total_keys ? (net_usage / mh->total_keys) : 0;
1074*572c4311Sfengbojiang 
1075*572c4311Sfengbojiang     return mh;
1076*572c4311Sfengbojiang }
1077*572c4311Sfengbojiang 
1078*572c4311Sfengbojiang /* Helper for "MEMORY allocator-stats", used as a callback for the jemalloc
1079*572c4311Sfengbojiang  * stats output. */
inputCatSds(void * result,const char * str)1080*572c4311Sfengbojiang void inputCatSds(void *result, const char *str) {
1081*572c4311Sfengbojiang     /* result is actually a (sds *), so re-cast it here */
1082*572c4311Sfengbojiang     sds *info = (sds *)result;
1083*572c4311Sfengbojiang     *info = sdscat(*info, str);
1084*572c4311Sfengbojiang }
1085*572c4311Sfengbojiang 
1086*572c4311Sfengbojiang /* This implements MEMORY DOCTOR. An human readable analysis of the Redis
1087*572c4311Sfengbojiang  * memory condition. */
getMemoryDoctorReport(void)1088*572c4311Sfengbojiang sds getMemoryDoctorReport(void) {
1089*572c4311Sfengbojiang     int empty = 0;          /* Instance is empty or almost empty. */
1090*572c4311Sfengbojiang     int big_peak = 0;       /* Memory peak is much larger than used mem. */
1091*572c4311Sfengbojiang     int high_frag = 0;      /* High fragmentation. */
1092*572c4311Sfengbojiang     int high_alloc_frag = 0;/* High allocator fragmentation. */
1093*572c4311Sfengbojiang     int high_proc_rss = 0;  /* High process rss overhead. */
1094*572c4311Sfengbojiang     int high_alloc_rss = 0; /* High rss overhead. */
1095*572c4311Sfengbojiang     int big_slave_buf = 0;  /* Slave buffers are too big. */
1096*572c4311Sfengbojiang     int big_client_buf = 0; /* Client buffers are too big. */
1097*572c4311Sfengbojiang     int many_scripts = 0;   /* Script cache has too many scripts. */
1098*572c4311Sfengbojiang     int num_reports = 0;
1099*572c4311Sfengbojiang     struct redisMemOverhead *mh = getMemoryOverheadData();
1100*572c4311Sfengbojiang 
1101*572c4311Sfengbojiang     if (mh->total_allocated < (1024*1024*5)) {
1102*572c4311Sfengbojiang         empty = 1;
1103*572c4311Sfengbojiang         num_reports++;
1104*572c4311Sfengbojiang     } else {
1105*572c4311Sfengbojiang         /* Peak is > 150% of current used memory? */
1106*572c4311Sfengbojiang         if (((float)mh->peak_allocated / mh->total_allocated) > 1.5) {
1107*572c4311Sfengbojiang             big_peak = 1;
1108*572c4311Sfengbojiang             num_reports++;
1109*572c4311Sfengbojiang         }
1110*572c4311Sfengbojiang 
1111*572c4311Sfengbojiang         /* Fragmentation is higher than 1.4 and 10MB ?*/
1112*572c4311Sfengbojiang         if (mh->total_frag > 1.4 && mh->total_frag_bytes > 10<<20) {
1113*572c4311Sfengbojiang             high_frag = 1;
1114*572c4311Sfengbojiang             num_reports++;
1115*572c4311Sfengbojiang         }
1116*572c4311Sfengbojiang 
1117*572c4311Sfengbojiang         /* External fragmentation is higher than 1.1 and 10MB? */
1118*572c4311Sfengbojiang         if (mh->allocator_frag > 1.1 && mh->allocator_frag_bytes > 10<<20) {
1119*572c4311Sfengbojiang             high_alloc_frag = 1;
1120*572c4311Sfengbojiang             num_reports++;
1121*572c4311Sfengbojiang         }
1122*572c4311Sfengbojiang 
1123*572c4311Sfengbojiang         /* Allocator fss is higher than 1.1 and 10MB ? */
1124*572c4311Sfengbojiang         if (mh->allocator_rss > 1.1 && mh->allocator_rss_bytes > 10<<20) {
1125*572c4311Sfengbojiang             high_alloc_rss = 1;
1126*572c4311Sfengbojiang             num_reports++;
1127*572c4311Sfengbojiang         }
1128*572c4311Sfengbojiang 
1129*572c4311Sfengbojiang         /* Non-Allocator fss is higher than 1.1 and 10MB ? */
1130*572c4311Sfengbojiang         if (mh->rss_extra > 1.1 && mh->rss_extra_bytes > 10<<20) {
1131*572c4311Sfengbojiang             high_proc_rss = 1;
1132*572c4311Sfengbojiang             num_reports++;
1133*572c4311Sfengbojiang         }
1134*572c4311Sfengbojiang 
1135*572c4311Sfengbojiang         /* Clients using more than 200k each average? */
1136*572c4311Sfengbojiang         long numslaves = listLength(server.slaves);
1137*572c4311Sfengbojiang         long numclients = listLength(server.clients)-numslaves;
1138*572c4311Sfengbojiang         if (mh->clients_normal / numclients > (1024*200)) {
1139*572c4311Sfengbojiang             big_client_buf = 1;
1140*572c4311Sfengbojiang             num_reports++;
1141*572c4311Sfengbojiang         }
1142*572c4311Sfengbojiang 
1143*572c4311Sfengbojiang         /* Slaves using more than 10 MB each? */
1144*572c4311Sfengbojiang         if (numslaves > 0 && mh->clients_slaves / numslaves > (1024*1024*10)) {
1145*572c4311Sfengbojiang             big_slave_buf = 1;
1146*572c4311Sfengbojiang             num_reports++;
1147*572c4311Sfengbojiang         }
1148*572c4311Sfengbojiang 
1149*572c4311Sfengbojiang         /* Too many scripts are cached? */
1150*572c4311Sfengbojiang         if (dictSize(server.lua_scripts) > 1000) {
1151*572c4311Sfengbojiang             many_scripts = 1;
1152*572c4311Sfengbojiang             num_reports++;
1153*572c4311Sfengbojiang         }
1154*572c4311Sfengbojiang     }
1155*572c4311Sfengbojiang 
1156*572c4311Sfengbojiang     sds s;
1157*572c4311Sfengbojiang     if (num_reports == 0) {
1158*572c4311Sfengbojiang         s = sdsnew(
1159*572c4311Sfengbojiang         "Hi Sam, I can't find any memory issue in your instance. "
1160*572c4311Sfengbojiang         "I can only account for what occurs on this base.\n");
1161*572c4311Sfengbojiang     } else if (empty == 1) {
1162*572c4311Sfengbojiang         s = sdsnew(
1163*572c4311Sfengbojiang         "Hi Sam, this instance is empty or is using very little memory, "
1164*572c4311Sfengbojiang         "my issues detector can't be used in these conditions. "
1165*572c4311Sfengbojiang         "Please, leave for your mission on Earth and fill it with some data. "
1166*572c4311Sfengbojiang         "The new Sam and I will be back to our programming as soon as I "
1167*572c4311Sfengbojiang         "finished rebooting.\n");
1168*572c4311Sfengbojiang     } else {
1169*572c4311Sfengbojiang         s = sdsnew("Sam, I detected a few issues in this Redis instance memory implants:\n\n");
1170*572c4311Sfengbojiang         if (big_peak) {
1171*572c4311Sfengbojiang             s = sdscat(s," * Peak memory: In the past this instance used more than 150% the memory that is currently using. The allocator is normally not able to release memory after a peak, so you can expect to see a big fragmentation ratio, however this is actually harmless and is only due to the memory peak, and if the Redis instance Resident Set Size (RSS) is currently bigger than expected, the memory will be used as soon as you fill the Redis instance with more data. If the memory peak was only occasional and you want to try to reclaim memory, please try the MEMORY PURGE command, otherwise the only other option is to shutdown and restart the instance.\n\n");
1172*572c4311Sfengbojiang         }
1173*572c4311Sfengbojiang         if (high_frag) {
1174*572c4311Sfengbojiang             s = sdscatprintf(s," * High total RSS: This instance has a memory fragmentation and RSS overhead greater than 1.4 (this means that the Resident Set Size of the Redis process is much larger than the sum of the logical allocations Redis performed). This problem is usually due either to a large peak memory (check if there is a peak memory entry above in the report) or may result from a workload that causes the allocator to fragment memory a lot. If the problem is a large peak memory, then there is no issue. Otherwise, make sure you are using the Jemalloc allocator and not the default libc malloc. Note: The currently used allocator is \"%s\".\n\n", ZMALLOC_LIB);
1175*572c4311Sfengbojiang         }
1176*572c4311Sfengbojiang         if (high_alloc_frag) {
1177*572c4311Sfengbojiang             s = sdscatprintf(s," * High allocator fragmentation: This instance has an allocator external fragmentation greater than 1.1. This problem is usually due either to a large peak memory (check if there is a peak memory entry above in the report) or may result from a workload that causes the allocator to fragment memory a lot. You can try enabling 'activedefrag' config option.\n\n");
1178*572c4311Sfengbojiang         }
1179*572c4311Sfengbojiang         if (high_alloc_rss) {
1180*572c4311Sfengbojiang             s = sdscatprintf(s," * High allocator RSS overhead: This instance has an RSS memory overhead is greater than 1.1 (this means that the Resident Set Size of the allocator is much larger than the sum what the allocator actually holds). This problem is usually due to a large peak memory (check if there is a peak memory entry above in the report), you can try the MEMORY PURGE command to reclaim it.\n\n");
1181*572c4311Sfengbojiang         }
1182*572c4311Sfengbojiang         if (high_proc_rss) {
1183*572c4311Sfengbojiang             s = sdscatprintf(s," * High process RSS overhead: This instance has non-allocator RSS memory overhead is greater than 1.1 (this means that the Resident Set Size of the Redis process is much larger than the RSS the allocator holds). This problem may be due to Lua scripts or Modules.\n\n");
1184*572c4311Sfengbojiang         }
1185*572c4311Sfengbojiang         if (big_slave_buf) {
1186*572c4311Sfengbojiang             s = sdscat(s," * Big replica buffers: The replica output buffers in this instance are greater than 10MB for each replica (on average). This likely means that there is some replica instance that is struggling receiving data, either because it is too slow or because of networking issues. As a result, data piles on the master output buffers. Please try to identify what replica is not receiving data correctly and why. You can use the INFO output in order to check the replicas delays and the CLIENT LIST command to check the output buffers of each replica.\n\n");
1187*572c4311Sfengbojiang         }
1188*572c4311Sfengbojiang         if (big_client_buf) {
1189*572c4311Sfengbojiang             s = sdscat(s," * Big client buffers: The clients output buffers in this instance are greater than 200K per client (on average). This may result from different causes, like Pub/Sub clients subscribed to channels bot not receiving data fast enough, so that data piles on the Redis instance output buffer, or clients sending commands with large replies or very large sequences of commands in the same pipeline. Please use the CLIENT LIST command in order to investigate the issue if it causes problems in your instance, or to understand better why certain clients are using a big amount of memory.\n\n");
1190*572c4311Sfengbojiang         }
1191*572c4311Sfengbojiang         if (many_scripts) {
1192*572c4311Sfengbojiang             s = sdscat(s," * Many scripts: There seem to be many cached scripts in this instance (more than 1000). This may be because scripts are generated and `EVAL`ed, instead of being parameterized (with KEYS and ARGV), `SCRIPT LOAD`ed and `EVALSHA`ed. Unless `SCRIPT FLUSH` is called periodically, the scripts' caches may end up consuming most of your memory.\n\n");
1193*572c4311Sfengbojiang         }
1194*572c4311Sfengbojiang         s = sdscat(s,"I'm here to keep you safe, Sam. I want to help you.\n");
1195*572c4311Sfengbojiang     }
1196*572c4311Sfengbojiang     freeMemoryOverheadData(mh);
1197*572c4311Sfengbojiang     return s;
1198*572c4311Sfengbojiang }
1199*572c4311Sfengbojiang 
1200*572c4311Sfengbojiang /* Set the object LRU/LFU depending on server.maxmemory_policy.
1201*572c4311Sfengbojiang  * The lfu_freq arg is only relevant if policy is MAXMEMORY_FLAG_LFU.
1202*572c4311Sfengbojiang  * The lru_idle and lru_clock args are only relevant if policy
1203*572c4311Sfengbojiang  * is MAXMEMORY_FLAG_LRU.
1204*572c4311Sfengbojiang  * Either or both of them may be <0, in that case, nothing is set. */
objectSetLRUOrLFU(robj * val,long long lfu_freq,long long lru_idle,long long lru_clock)1205*572c4311Sfengbojiang void objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
1206*572c4311Sfengbojiang                        long long lru_clock) {
1207*572c4311Sfengbojiang     if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
1208*572c4311Sfengbojiang         if (lfu_freq >= 0) {
1209*572c4311Sfengbojiang             serverAssert(lfu_freq <= 255);
1210*572c4311Sfengbojiang             val->lru = (LFUGetTimeInMinutes()<<8) | lfu_freq;
1211*572c4311Sfengbojiang         }
1212*572c4311Sfengbojiang     } else if (lru_idle >= 0) {
1213*572c4311Sfengbojiang         /* Provided LRU idle time is in seconds. Scale
1214*572c4311Sfengbojiang          * according to the LRU clock resolution this Redis
1215*572c4311Sfengbojiang          * instance was compiled with (normally 1000 ms, so the
1216*572c4311Sfengbojiang          * below statement will expand to lru_idle*1000/1000. */
1217*572c4311Sfengbojiang         lru_idle = lru_idle*1000/LRU_CLOCK_RESOLUTION;
1218*572c4311Sfengbojiang         long lru_abs = lru_clock - lru_idle; /* Absolute access time. */
1219*572c4311Sfengbojiang         /* If the LRU field underflows (since LRU it is a wrapping
1220*572c4311Sfengbojiang          * clock), the best we can do is to provide a large enough LRU
1221*572c4311Sfengbojiang          * that is half-way in the circlular LRU clock we use: this way
1222*572c4311Sfengbojiang          * the computed idle time for this object will stay high for quite
1223*572c4311Sfengbojiang          * some time. */
1224*572c4311Sfengbojiang         if (lru_abs < 0)
1225*572c4311Sfengbojiang             lru_abs = (lru_clock+(LRU_CLOCK_MAX/2)) % LRU_CLOCK_MAX;
1226*572c4311Sfengbojiang         val->lru = lru_abs;
1227*572c4311Sfengbojiang     }
1228*572c4311Sfengbojiang }
1229*572c4311Sfengbojiang 
1230*572c4311Sfengbojiang /* ======================= The OBJECT and MEMORY commands =================== */
1231*572c4311Sfengbojiang 
1232*572c4311Sfengbojiang /* This is a helper function for the OBJECT command. We need to lookup keys
1233*572c4311Sfengbojiang  * without any modification of LRU or other parameters. */
objectCommandLookup(client * c,robj * key)1234*572c4311Sfengbojiang robj *objectCommandLookup(client *c, robj *key) {
1235*572c4311Sfengbojiang     dictEntry *de;
1236*572c4311Sfengbojiang 
1237*572c4311Sfengbojiang     if ((de = dictFind(c->db->dict,key->ptr)) == NULL) return NULL;
1238*572c4311Sfengbojiang     return (robj*) dictGetVal(de);
1239*572c4311Sfengbojiang }
1240*572c4311Sfengbojiang 
objectCommandLookupOrReply(client * c,robj * key,robj * reply)1241*572c4311Sfengbojiang robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply) {
1242*572c4311Sfengbojiang     robj *o = objectCommandLookup(c,key);
1243*572c4311Sfengbojiang 
1244*572c4311Sfengbojiang     if (!o) addReply(c, reply);
1245*572c4311Sfengbojiang     return o;
1246*572c4311Sfengbojiang }
1247*572c4311Sfengbojiang 
1248*572c4311Sfengbojiang /* Object command allows to inspect the internals of an Redis Object.
1249*572c4311Sfengbojiang  * Usage: OBJECT <refcount|encoding|idletime|freq> <key> */
objectCommand(client * c)1250*572c4311Sfengbojiang void objectCommand(client *c) {
1251*572c4311Sfengbojiang     robj *o;
1252*572c4311Sfengbojiang 
1253*572c4311Sfengbojiang     if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
1254*572c4311Sfengbojiang         const char *help[] = {
1255*572c4311Sfengbojiang "ENCODING <key> -- Return the kind of internal representation used in order to store the value associated with a key.",
1256*572c4311Sfengbojiang "FREQ <key> -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key.",
1257*572c4311Sfengbojiang "IDLETIME <key> -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key.",
1258*572c4311Sfengbojiang "REFCOUNT <key> -- Return the number of references of the value associated with the specified key.",
1259*572c4311Sfengbojiang NULL
1260*572c4311Sfengbojiang         };
1261*572c4311Sfengbojiang         addReplyHelp(c, help);
1262*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"refcount") && c->argc == 3) {
1263*572c4311Sfengbojiang         if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
1264*572c4311Sfengbojiang                 == NULL) return;
1265*572c4311Sfengbojiang         addReplyLongLong(c,o->refcount);
1266*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"encoding") && c->argc == 3) {
1267*572c4311Sfengbojiang         if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
1268*572c4311Sfengbojiang                 == NULL) return;
1269*572c4311Sfengbojiang         addReplyBulkCString(c,strEncoding(o->encoding));
1270*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"idletime") && c->argc == 3) {
1271*572c4311Sfengbojiang         if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
1272*572c4311Sfengbojiang                 == NULL) return;
1273*572c4311Sfengbojiang         if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
1274*572c4311Sfengbojiang             addReplyError(c,"An LFU maxmemory policy is selected, idle time not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.");
1275*572c4311Sfengbojiang             return;
1276*572c4311Sfengbojiang         }
1277*572c4311Sfengbojiang         addReplyLongLong(c,estimateObjectIdleTime(o)/1000);
1278*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"freq") && c->argc == 3) {
1279*572c4311Sfengbojiang         if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
1280*572c4311Sfengbojiang                 == NULL) return;
1281*572c4311Sfengbojiang         if (!(server.maxmemory_policy & MAXMEMORY_FLAG_LFU)) {
1282*572c4311Sfengbojiang             addReplyError(c,"An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.");
1283*572c4311Sfengbojiang             return;
1284*572c4311Sfengbojiang         }
1285*572c4311Sfengbojiang         /* LFUDecrAndReturn should be called
1286*572c4311Sfengbojiang          * in case of the key has not been accessed for a long time,
1287*572c4311Sfengbojiang          * because we update the access time only
1288*572c4311Sfengbojiang          * when the key is read or overwritten. */
1289*572c4311Sfengbojiang         addReplyLongLong(c,LFUDecrAndReturn(o));
1290*572c4311Sfengbojiang     } else {
1291*572c4311Sfengbojiang         addReplySubcommandSyntaxError(c);
1292*572c4311Sfengbojiang     }
1293*572c4311Sfengbojiang }
1294*572c4311Sfengbojiang 
1295*572c4311Sfengbojiang /* The memory command will eventually be a complete interface for the
1296*572c4311Sfengbojiang  * memory introspection capabilities of Redis.
1297*572c4311Sfengbojiang  *
1298*572c4311Sfengbojiang  * Usage: MEMORY usage <key> */
memoryCommand(client * c)1299*572c4311Sfengbojiang void memoryCommand(client *c) {
1300*572c4311Sfengbojiang     if (!strcasecmp(c->argv[1]->ptr,"help") && c->argc == 2) {
1301*572c4311Sfengbojiang         const char *help[] = {
1302*572c4311Sfengbojiang "DOCTOR - Return memory problems reports.",
1303*572c4311Sfengbojiang "MALLOC-STATS -- Return internal statistics report from the memory allocator.",
1304*572c4311Sfengbojiang "PURGE -- Attempt to purge dirty pages for reclamation by the allocator.",
1305*572c4311Sfengbojiang "STATS -- Return information about the memory usage of the server.",
1306*572c4311Sfengbojiang "USAGE <key> [SAMPLES <count>] -- Return memory in bytes used by <key> and its value. Nested values are sampled up to <count> times (default: 5).",
1307*572c4311Sfengbojiang NULL
1308*572c4311Sfengbojiang         };
1309*572c4311Sfengbojiang         addReplyHelp(c, help);
1310*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"usage") && c->argc >= 3) {
1311*572c4311Sfengbojiang         dictEntry *de;
1312*572c4311Sfengbojiang         long long samples = OBJ_COMPUTE_SIZE_DEF_SAMPLES;
1313*572c4311Sfengbojiang         for (int j = 3; j < c->argc; j++) {
1314*572c4311Sfengbojiang             if (!strcasecmp(c->argv[j]->ptr,"samples") &&
1315*572c4311Sfengbojiang                 j+1 < c->argc)
1316*572c4311Sfengbojiang             {
1317*572c4311Sfengbojiang                 if (getLongLongFromObjectOrReply(c,c->argv[j+1],&samples,NULL)
1318*572c4311Sfengbojiang                      == C_ERR) return;
1319*572c4311Sfengbojiang                 if (samples < 0) {
1320*572c4311Sfengbojiang                     addReply(c,shared.syntaxerr);
1321*572c4311Sfengbojiang                     return;
1322*572c4311Sfengbojiang                 }
1323*572c4311Sfengbojiang                 if (samples == 0) samples = LLONG_MAX;;
1324*572c4311Sfengbojiang                 j++; /* skip option argument. */
1325*572c4311Sfengbojiang             } else {
1326*572c4311Sfengbojiang                 addReply(c,shared.syntaxerr);
1327*572c4311Sfengbojiang                 return;
1328*572c4311Sfengbojiang             }
1329*572c4311Sfengbojiang         }
1330*572c4311Sfengbojiang         if ((de = dictFind(c->db->dict,c->argv[2]->ptr)) == NULL) {
1331*572c4311Sfengbojiang             addReply(c, shared.nullbulk);
1332*572c4311Sfengbojiang             return;
1333*572c4311Sfengbojiang         }
1334*572c4311Sfengbojiang         size_t usage = objectComputeSize(dictGetVal(de),samples);
1335*572c4311Sfengbojiang         usage += sdsAllocSize(dictGetKey(de));
1336*572c4311Sfengbojiang         usage += sizeof(dictEntry);
1337*572c4311Sfengbojiang         addReplyLongLong(c,usage);
1338*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"stats") && c->argc == 2) {
1339*572c4311Sfengbojiang         struct redisMemOverhead *mh = getMemoryOverheadData();
1340*572c4311Sfengbojiang 
1341*572c4311Sfengbojiang         addReplyMultiBulkLen(c,(25+mh->num_dbs)*2);
1342*572c4311Sfengbojiang 
1343*572c4311Sfengbojiang         addReplyBulkCString(c,"peak.allocated");
1344*572c4311Sfengbojiang         addReplyLongLong(c,mh->peak_allocated);
1345*572c4311Sfengbojiang 
1346*572c4311Sfengbojiang         addReplyBulkCString(c,"total.allocated");
1347*572c4311Sfengbojiang         addReplyLongLong(c,mh->total_allocated);
1348*572c4311Sfengbojiang 
1349*572c4311Sfengbojiang         addReplyBulkCString(c,"startup.allocated");
1350*572c4311Sfengbojiang         addReplyLongLong(c,mh->startup_allocated);
1351*572c4311Sfengbojiang 
1352*572c4311Sfengbojiang         addReplyBulkCString(c,"replication.backlog");
1353*572c4311Sfengbojiang         addReplyLongLong(c,mh->repl_backlog);
1354*572c4311Sfengbojiang 
1355*572c4311Sfengbojiang         addReplyBulkCString(c,"clients.slaves");
1356*572c4311Sfengbojiang         addReplyLongLong(c,mh->clients_slaves);
1357*572c4311Sfengbojiang 
1358*572c4311Sfengbojiang         addReplyBulkCString(c,"clients.normal");
1359*572c4311Sfengbojiang         addReplyLongLong(c,mh->clients_normal);
1360*572c4311Sfengbojiang 
1361*572c4311Sfengbojiang         addReplyBulkCString(c,"aof.buffer");
1362*572c4311Sfengbojiang         addReplyLongLong(c,mh->aof_buffer);
1363*572c4311Sfengbojiang 
1364*572c4311Sfengbojiang         addReplyBulkCString(c,"lua.caches");
1365*572c4311Sfengbojiang         addReplyLongLong(c,mh->lua_caches);
1366*572c4311Sfengbojiang 
1367*572c4311Sfengbojiang         for (size_t j = 0; j < mh->num_dbs; j++) {
1368*572c4311Sfengbojiang             char dbname[32];
1369*572c4311Sfengbojiang             snprintf(dbname,sizeof(dbname),"db.%zd",mh->db[j].dbid);
1370*572c4311Sfengbojiang             addReplyBulkCString(c,dbname);
1371*572c4311Sfengbojiang             addReplyMultiBulkLen(c,4);
1372*572c4311Sfengbojiang 
1373*572c4311Sfengbojiang             addReplyBulkCString(c,"overhead.hashtable.main");
1374*572c4311Sfengbojiang             addReplyLongLong(c,mh->db[j].overhead_ht_main);
1375*572c4311Sfengbojiang 
1376*572c4311Sfengbojiang             addReplyBulkCString(c,"overhead.hashtable.expires");
1377*572c4311Sfengbojiang             addReplyLongLong(c,mh->db[j].overhead_ht_expires);
1378*572c4311Sfengbojiang         }
1379*572c4311Sfengbojiang 
1380*572c4311Sfengbojiang         addReplyBulkCString(c,"overhead.total");
1381*572c4311Sfengbojiang         addReplyLongLong(c,mh->overhead_total);
1382*572c4311Sfengbojiang 
1383*572c4311Sfengbojiang         addReplyBulkCString(c,"keys.count");
1384*572c4311Sfengbojiang         addReplyLongLong(c,mh->total_keys);
1385*572c4311Sfengbojiang 
1386*572c4311Sfengbojiang         addReplyBulkCString(c,"keys.bytes-per-key");
1387*572c4311Sfengbojiang         addReplyLongLong(c,mh->bytes_per_key);
1388*572c4311Sfengbojiang 
1389*572c4311Sfengbojiang         addReplyBulkCString(c,"dataset.bytes");
1390*572c4311Sfengbojiang         addReplyLongLong(c,mh->dataset);
1391*572c4311Sfengbojiang 
1392*572c4311Sfengbojiang         addReplyBulkCString(c,"dataset.percentage");
1393*572c4311Sfengbojiang         addReplyDouble(c,mh->dataset_perc);
1394*572c4311Sfengbojiang 
1395*572c4311Sfengbojiang         addReplyBulkCString(c,"peak.percentage");
1396*572c4311Sfengbojiang         addReplyDouble(c,mh->peak_perc);
1397*572c4311Sfengbojiang 
1398*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator.allocated");
1399*572c4311Sfengbojiang         addReplyLongLong(c,server.cron_malloc_stats.allocator_allocated);
1400*572c4311Sfengbojiang 
1401*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator.active");
1402*572c4311Sfengbojiang         addReplyLongLong(c,server.cron_malloc_stats.allocator_active);
1403*572c4311Sfengbojiang 
1404*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator.resident");
1405*572c4311Sfengbojiang         addReplyLongLong(c,server.cron_malloc_stats.allocator_resident);
1406*572c4311Sfengbojiang 
1407*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator-fragmentation.ratio");
1408*572c4311Sfengbojiang         addReplyDouble(c,mh->allocator_frag);
1409*572c4311Sfengbojiang 
1410*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator-fragmentation.bytes");
1411*572c4311Sfengbojiang         addReplyLongLong(c,mh->allocator_frag_bytes);
1412*572c4311Sfengbojiang 
1413*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator-rss.ratio");
1414*572c4311Sfengbojiang         addReplyDouble(c,mh->allocator_rss);
1415*572c4311Sfengbojiang 
1416*572c4311Sfengbojiang         addReplyBulkCString(c,"allocator-rss.bytes");
1417*572c4311Sfengbojiang         addReplyLongLong(c,mh->allocator_rss_bytes);
1418*572c4311Sfengbojiang 
1419*572c4311Sfengbojiang         addReplyBulkCString(c,"rss-overhead.ratio");
1420*572c4311Sfengbojiang         addReplyDouble(c,mh->rss_extra);
1421*572c4311Sfengbojiang 
1422*572c4311Sfengbojiang         addReplyBulkCString(c,"rss-overhead.bytes");
1423*572c4311Sfengbojiang         addReplyLongLong(c,mh->rss_extra_bytes);
1424*572c4311Sfengbojiang 
1425*572c4311Sfengbojiang         addReplyBulkCString(c,"fragmentation"); /* this is the total RSS overhead, including fragmentation */
1426*572c4311Sfengbojiang         addReplyDouble(c,mh->total_frag); /* it is kept here for backwards compatibility */
1427*572c4311Sfengbojiang 
1428*572c4311Sfengbojiang         addReplyBulkCString(c,"fragmentation.bytes");
1429*572c4311Sfengbojiang         addReplyLongLong(c,mh->total_frag_bytes);
1430*572c4311Sfengbojiang 
1431*572c4311Sfengbojiang         freeMemoryOverheadData(mh);
1432*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"malloc-stats") && c->argc == 2) {
1433*572c4311Sfengbojiang #if defined(USE_JEMALLOC)
1434*572c4311Sfengbojiang         sds info = sdsempty();
1435*572c4311Sfengbojiang         je_malloc_stats_print(inputCatSds, &info, NULL);
1436*572c4311Sfengbojiang         addReplyBulkSds(c, info);
1437*572c4311Sfengbojiang #else
1438*572c4311Sfengbojiang         addReplyBulkCString(c,"Stats not supported for the current allocator");
1439*572c4311Sfengbojiang #endif
1440*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"doctor") && c->argc == 2) {
1441*572c4311Sfengbojiang         sds report = getMemoryDoctorReport();
1442*572c4311Sfengbojiang         addReplyBulkSds(c,report);
1443*572c4311Sfengbojiang     } else if (!strcasecmp(c->argv[1]->ptr,"purge") && c->argc == 2) {
1444*572c4311Sfengbojiang #if defined(USE_JEMALLOC)
1445*572c4311Sfengbojiang         char tmp[32];
1446*572c4311Sfengbojiang         unsigned narenas = 0;
1447*572c4311Sfengbojiang         size_t sz = sizeof(unsigned);
1448*572c4311Sfengbojiang         if (!je_mallctl("arenas.narenas", &narenas, &sz, NULL, 0)) {
1449*572c4311Sfengbojiang             sprintf(tmp, "arena.%d.purge", narenas);
1450*572c4311Sfengbojiang             if (!je_mallctl(tmp, NULL, 0, NULL, 0)) {
1451*572c4311Sfengbojiang                 addReply(c, shared.ok);
1452*572c4311Sfengbojiang                 return;
1453*572c4311Sfengbojiang             }
1454*572c4311Sfengbojiang         }
1455*572c4311Sfengbojiang         addReplyError(c, "Error purging dirty pages");
1456*572c4311Sfengbojiang #else
1457*572c4311Sfengbojiang         addReply(c, shared.ok);
1458*572c4311Sfengbojiang         /* Nothing to do for other allocators. */
1459*572c4311Sfengbojiang #endif
1460*572c4311Sfengbojiang     } else {
1461*572c4311Sfengbojiang         addReplyErrorFormat(c, "Unknown subcommand or wrong number of arguments for '%s'. Try MEMORY HELP", (char*)c->argv[1]->ptr);
1462*572c4311Sfengbojiang     }
1463*572c4311Sfengbojiang }
1464