道破Redis的VM

原创文章是freas_1990。转载请注明出处:http://blog.csdn.net/freas_1990/article/details/42052813

Redis唯一的那个key的value是swap文件上的时候,该key的value指向的RedisObject将会改变成VMPointer。VMPointer保存了该value在磁盘文件上的信息,包含起始页面的偏移和连续的页面数等。

typedef struct redisObject {
unsigned type:4;
unsigned storage:2; /* REDIS_VM_MEMORY or REDIS_VM_SWAPPING */
unsigned encoding:4;
unsigned lru:22; /* lru time (relative to server.lruclock) */
int refcount;
void *ptr;
/* VM fields are only allocated if VM is active, otherwise the
* object allocation function will just allocate
* sizeof(redisObjct) minus sizeof(redisObjectVM), so using
* Redis without VM active will not have any overhead. */
} robj;
typedef struct vmPointer {
unsigned type:4;
unsigned storage:2; /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
unsigned notused:26;
unsigned int vtype; /* type of the object stored in the swap file */
off_t page; /* the page at witch the object is stored on disk */
off_t usedpages; /* number of pages used on disk */
} vmpointer;

将该key的value导入内存的逻辑例如以下:

robj *vmReadObjectFromSwap(off_t page, int type) {
robj *o; if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
redisLog(REDIS_WARNING,
"Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
strerror(errno));
_exit(1);
}
o = rdbLoadObject(type,server.vm_fp);
if (o == NULL) {
redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno));
_exit(1);
}
if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
return o;
}

到这里,redis VM已经指出的精髓,明白了吧?

版权声明:本文博客原创文章。博客,未经同意,不得转载。

上一篇:剑指offer--面试题23


下一篇:UVA 10003 Cutting Sticks 区间DP+记忆化搜索