码迷,mamicode.com
首页 > 其他好文 > 详细

点破Redis的VM

时间:2014-12-21 16:36:53      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

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


  1. typedef struct redisObject {  
  2.     unsigned type:4;  
  3.     unsigned storage:2;     /* REDIS_VM_MEMORY or REDIS_VM_SWAPPING */  
  4.     unsigned encoding:4;  
  5.     unsigned lru:22;        /* lru time (relative to server.lruclock) */  
  6.     int refcount;  
  7.     void *ptr;  
  8.     /* VM fields are only allocated if VM is active, otherwise the  
  9.      * object allocation function will just allocate  
  10.      * sizeof(redisObjct) minus sizeof(redisObjectVM), so using  
  11.      * Redis without VM active will not have any overhead. */  
  12. } robj;  


  1. typedef struct vmPointer {  
  2.     unsigned type:4;  
  3.     unsigned storage:2; /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */  
  4.     unsigned notused:26;  
  5.     unsigned int vtype; /* type of the object stored in the swap file */  
  6.     off_t page;         /* the page at witch the object is stored on disk */  
  7.     off_t usedpages;    /* number of pages used on disk */  
  8. } vmpointer;  


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

  1. robj *vmReadObjectFromSwap(off_t page, int type) {  
  2.     robj *o;  
  3.   
  4.     if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);  
  5.     if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {  
  6.         redisLog(REDIS_WARNING,  
  7.             "Unrecoverable VM problem in vmReadObjectFromSwap(): can‘t seek: %s",  
  8.             strerror(errno));  
  9.         _exit(1);  
  10.     }  
  11.     o = rdbLoadObject(type,server.vm_fp);  
  12.     if (o == NULL) {  
  13.         redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmReadObjectFromSwap(): can‘t load object from swap file: %s", strerror(errno));  
  14.         _exit(1);  
  15.     }  
  16.     if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);  
  17.     return o;  
  18. }  



点破Redis的VM

标签:

原文地址:http://www.cnblogs.com/yishujiayuan/p/4176677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!