When the value of a Redis key is swap to a file, the value of the key to the Redisobject will be changed to Vmpointer,vmpointer to save the value on the disk file. This includes the offset of the start page and the number of consecutive pages.
[HTML]View Plaincopy
- 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 was only allocated if VM is active, otherwise the
- * Object allocation function would just allocate
- * sizeof (REDISOBJCT) minus sizeof (REDISOBJECTVM), so using
- * Redis without VM active is not having any overhead. */
- } robj;
[HTML]View Plaincopy
- 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 was stored on disk */
- off_t usedpages; /* Number of pages used on disk */
- } Vmpointer;
The logic to import the key's value into memory is as follows:
[HTML]View Plaincopy
- 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", Strerr or (errno));
- _exit (1);
- }
- if (server.vm_enabled) Pthread_mutex_unlock (&server.io_swapfile_mutex);
- return o;
- }
Dissected Redis VMS