要想了解redis底层的内存管理是如何进行的,直接看源码绝对是一个很好的选择
下面是我添加了详细注释的源码,需要注意的是,为了便于源码分析,我把redis为了弥补平台差异的那部分代码删了,只需要知道有这个东西便好
下面我会贴上两份源码:一份是我自己的,有删减添加了注释的,一部分是原生的,可以做个参考对照
redis内存管理部分的源码在zmalloc.h文件和zmalloc.c文件
推荐文章:
https://www.cnblogs.com/likui360/p/5272443.html
https://www.cnblogs.com/likui360/p/5272975.html
http://wiki.jikexueyuan.com/project/redis/memory-data-management.html
我的源码
zmalloc.h:
#ifndef __ZMALLOC_H #define __ZMALLOC_H #define __xstr(s) __str(s) #define __str(s) #s #ifndef ZMALLOC_LIB #define ZMALLOC_LIB "libc" #endif /* CPU一次性能读取数据的二进制位数称为字长,也就是我们通常所说的32位系统(字长4个字节)、64位系统(字长8个字节)的由来。 所谓的8字节对齐,就是指变量的起始地址是8的倍数。 比如程序运行时(CPU)在读取long型数据的时候,只需要一个总线周期,时间更短, 如果不是8字节对齐的则需要两个总线周期才能读完数据。 本文中我提到的8字节对齐是针对64位系统而言的,如果是32位系统那么就是4字节对齐。 实际上Redis源码中的字节对齐是软编码,而非硬编码。 里面多用sizeof(long)或sizeof(size_t)来表示。 size_t(gcc中其值为long unsigned int)和long的长度是一样的, long的长度就是计算机的字长。 这样在未来的系统中如果字长(long的大小)不是8个字节了,该段代码依然能保证相应代码可用。 */ /** Redis内存模型: |--------|:代表8个字节大小 | 头部 | 实际内存 | |--------|--------|--------|--------|--------| ^ | ptr:实际返回的地址 上面的内存模型说明了两个问题: 1.内存对齐,都是8个字节的,可以提高cpu响应速度 2.返回的实际地址不包括头部 ps:从左到右内存地址是增加的,默认大端模式 */ //============ API ==================// //封装malloc,申请内存 void *zmalloc(size_t size); //封装calloc,不再支持按块成倍申请 void *zcalloc(size_t size); //封装realloc,内存扩展 void *zrealloc(void *ptr, size_t size); //封装free,内存释放,释放时会更新已经使用的内存值,如果在多线程下没有开启安全模式,可能会出现并发错误 void zfree(void *ptr); //复制一个字符串,为字符串在堆分配内存 char *zstrdup(const char *s); //获取已经使用的内存大小 size_t zmalloc_used_memory(void); //设置内存管理为多线程安全模式,设置之后在更新使用内存大小时会使用mutex进行互斥操作 void zmalloc_enable_thread_safeness(void); //设置内存异常时的回调函数 void zmalloc_set_oom_handler(void (*oom_handler)(size_t)); //内存碎片率,驻留在物理内存中的内存/总分配的物理内存 float zmalloc_get_fragmentation_ratio(size_t rss); //获取进程可使用的所有内存大小 size_t zmalloc_get_rss(void); //获得private_dirty字段 size_t zmalloc_get_private_dirty(void); //用这个释放内存时,不会更新使用内存变量的值。 void zlibc_free(void *ptr); //获取内内存块总体大小 size_t zmalloc_size(void *ptr); #endif /* __ZMALLOC_H */ /* 1.malloc函数和calloc函数的区别 void *malloc(unsigned int size) 在内存中分配一个大小为size的连续空间 void* calloc(unsigned int num,unsigned int size)在内存中分配num个size大小的内存空间 调用malloc后内存中的数据是随机的垃圾数据 调用calloc后内存中的数据是0,因为calloc会自动清零 2.redis为了方便管理内存,在分配一块内存之后,会将内存大小size插入内存头部,size所占内存大小固定 3.Redis允许使用四种内存管理策略:jemalloc,tcmalloc,苹果系统自带的malloc,其他系统自带的malloc 当存在前面三种策略的时候,就选择前面三种,第四种是没有选择的选择 jemalloc是freebsd操作系统自带的内存分配策略,具有速度快,多线程优化的特点 tcmalloc是谷歌开发的,里面集成了很多内存分配的测试工具 这二者的性能不分伯仲 4.Redis很多对象都是共享的,可以节约内存 5.memory aware【内存感知】 Redis能随时高性能的感知所使用的内存总量,实时的获取Redis所使用的内存的大小,从而随时感知内存 实现方式: 每次分配/释放内存时都更新一个全局的内存使用值 6.内存对齐,比如5位补齐为8位,CPU访问对齐内存仅仅需要一次,访问非对齐内存需要2次,内存对齐可以提高cpu的访问速率 */
zmalloc.c:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include "zmalloc.h" //记录已经使用的内存大小 static size_t used_memory = 0; //线程安全开关 static int zmalloc_thread_safe = 0; //互斥锁,如果开启了线程安全,而编译器又不支持原子操作函数,则需要互斥锁来完成代码互斥操作 pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER; //头部长度 #define PREFIX_SIZE (sizeof(size_t)) //以线程安全的方式 增加 内存已使用 变量 #define update_zmalloc_stat_add(__n) do { pthread_mutex_lock(&used_memory_mutex); used_memory += (__n); pthread_mutex_unlock(&used_memory_mutex); } while(0) //以线程安全的方式 减小 内存已使用 变量 #define update_zmalloc_stat_sub(__n) do { pthread_mutex_lock(&used_memory_mutex); used_memory -= (__n); pthread_mutex_unlock(&used_memory_mutex); } while(0) //增加 已经使用的内存大小,函数内确定是否以线程安全的模式运行 #define update_zmalloc_stat_alloc(__n) do { size_t _n = (__n); if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1));/*手动内存补齐*/ if (zmalloc_thread_safe) { update_zmalloc_stat_add(_n); } else { used_memory += _n; } } while(0) //减小 已经使用的内存大小,函数内确定是否以线程安全的模式运行 #define update_zmalloc_stat_free(__n) do { size_t _n = (__n); if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); /*手动内存补齐*/ if (zmalloc_thread_safe) { update_zmalloc_stat_sub(_n); } else { used_memory -= _n; } } while(0) //释放内存,不会更改 已使用内存变量 void zlibc_free(void *ptr) { free(ptr); } //打印错误信息并且终止程序 OOM:out of memory static void zmalloc_default_oom(size_t size) { fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes ", size); fflush(stderr); abort(); } //设置OMM时的回调函数 static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom; //申请size大小的连续内存空间,返回一个指向该空间的指针 void *zmalloc(size_t size) { //申请内存空间,预留了PREFIX_SIZE这样一小段空间 void *ptr = malloc(size+PREFIX_SIZE); //oom out of memory if (!ptr) zmalloc_oom_handler(size); // 如果分配不成功,那么说明内存用尽 //把内存大小存储在头部 *((size_t*)ptr) = size; //更新已经使用的内存大小 update_zmalloc_stat_alloc(size+PREFIX_SIZE); //返回指向该内存空间实际地址的指针,该指针不指向存放内存大小的内存头部 return (char*)ptr+PREFIX_SIZE; } //内存申请 void *zcalloc(size_t size) { //不再支持按倍数分配内存 void *ptr = calloc(1, size+PREFIX_SIZE); //内存分配失败,调用回调函数 if (!ptr) zmalloc_oom_handler(size); *((size_t*)ptr) = size; //更改已经使用的内存大小 update_zmalloc_stat_alloc(size+PREFIX_SIZE); //返回指向该内存块的指针 return (char*)ptr+PREFIX_SIZE; } //指定的内存扩展为size大小,返回扩展后的内存块指针 void *zrealloc(void *ptr, size_t size) { void *realptr; size_t oldsize; void *newptr; //原内存为空,则直接申请 if (ptr == NULL) return zmalloc(size); //实际内存地址指针(包含头部) realptr = (char*)ptr-PREFIX_SIZE; //实际内存大小(包含头部) oldsize = *((size_t*)realptr); //其实zrealloc底层还是封装了realloc,将原内存扩展为size+PREFIX_SIZE大小 newptr = realloc(realptr,size+PREFIX_SIZE); //内存扩展失败,进行回调 if (!newptr) zmalloc_oom_handler(size); //新内存块大小记录在内存头部 *((size_t*)newptr) = size; //修改已使用的内存大小 update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(size); //返回指针指向扩展后的内存块(不包含头部) return (char*)newptr+PREFIX_SIZE; } //返回指定内存块的大小(不包括头部) size_t zmalloc_size(void *ptr) { //指向实际内存块(包含头部) void *realptr = (char*)ptr-PREFIX_SIZE; //实际内存块大小 size_t size = *((size_t*)realptr); //内存对齐,比如5位补齐为8位,CPU访问对齐内存仅仅需要一次,访问非对齐内存需要2次,内存对齐可以提高cpu的访问速率 if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); //返回内存大小(不包括头部) return size+PREFIX_SIZE; } //释放内存,会更改 已使用的内存变量 void zfree(void *ptr) { /*释放内存之前要先获得包含头部内存的实际内存地址,才能避免头部内存的内存泄漏*/ void *realptr; size_t oldsize; //空内存 直接返回 if (ptr == NULL) return; //指向实际内存(包含内存头部) realptr = (char*)ptr-PREFIX_SIZE; //实际内存大小 oldsize = *((size_t*)realptr); //更改已使用的内存变量 update_zmalloc_stat_free(oldsize+PREFIX_SIZE); //释放 free(realptr); } //复制字符串 char *zstrdup(const char *s) { size_t l = strlen(s)+1; char *p = zmalloc(l); memcpy(p,s,l); return p; } //获得已使用的内存空间 size_t zmalloc_used_memory(void) { size_t um; //线程不安全的情况 加锁 if (zmalloc_thread_safe) { pthread_mutex_lock(&used_memory_mutex); um = used_memory; pthread_mutex_unlock(&used_memory_mutex); } else { um = used_memory; } return um; } //启动线程安全的开关,保证线程安全 void zmalloc_enable_thread_safeness(void) { zmalloc_thread_safe = 1; } //设置OMM的回调函数 void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) { zmalloc_oom_handler = oom_handler; } //下面几个函数深究起来意义不大 //获取进程可使用的所有内存大小 size_t zmalloc_get_rss(void) { //很多东西我注释掉了..... return zmalloc_used_memory(); } //内存碎片率 float zmalloc_get_fragmentation_ratio(size_t rss) { return (float)rss/zmalloc_used_memory(); } //获得private_dirty字段 size_t zmalloc_get_private_dirty(void) { return 0; }
原生源码:
zmalloc.h:
/* zmalloc - total amount of allocated memory aware version of malloc() * * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef __ZMALLOC_H #define __ZMALLOC_H /* Double expansion needed for stringification of macro values. */ #define __xstr(s) __str(s) #define __str(s) #s #if defined(USE_TCMALLOC) #define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR)) #include <google/tcmalloc.h> #if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1) #define HAVE_MALLOC_SIZE 1 #define zmalloc_size(p) tc_malloc_size(p) #else #error "Newer version of tcmalloc required" #endif #elif defined(USE_JEMALLOC) #define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX)) #include <jemalloc/jemalloc.h> #if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2) #define HAVE_MALLOC_SIZE 1 #define zmalloc_size(p) je_malloc_usable_size(p) #else #error "Newer version of jemalloc required" #endif #elif defined(__APPLE__) #include <malloc/malloc.h> #define HAVE_MALLOC_SIZE 1 #define zmalloc_size(p) malloc_size(p) #endif #ifndef ZMALLOC_LIB #define ZMALLOC_LIB "libc" #endif void *zmalloc(size_t size); void *zcalloc(size_t size); void *zrealloc(void *ptr, size_t size); void zfree(void *ptr); char *zstrdup(const char *s); size_t zmalloc_used_memory(void); void zmalloc_enable_thread_safeness(void); void zmalloc_set_oom_handler(void (*oom_handler)(size_t)); float zmalloc_get_fragmentation_ratio(size_t rss); size_t zmalloc_get_rss(void); size_t zmalloc_get_private_dirty(void); void zlibc_free(void *ptr); #ifndef HAVE_MALLOC_SIZE size_t zmalloc_size(void *ptr); #endif #endif /* __ZMALLOC_H */
zmalloc.c:
/* zmalloc - total amount of allocated memory aware version of malloc() * * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> /* This function provide us access to the original libc free(). This is useful * for instance to free results obtained by backtrace_symbols(). We need * to define this function before including zmalloc.h that may shadow the * free implementation if we use jemalloc or another non standard allocator. */ void zlibc_free(void *ptr) { free(ptr); } #include <string.h> #include <pthread.h> #include "config.h" #include "zmalloc.h" #ifdef HAVE_MALLOC_SIZE #define PREFIX_SIZE (0) #else #if defined(__sun) || defined(__sparc) || defined(__sparc__) #define PREFIX_SIZE (sizeof(long long)) #else #define PREFIX_SIZE (sizeof(size_t)) #endif #endif /* Explicitly override malloc/free etc when using tcmalloc. */ #if defined(USE_TCMALLOC) #define malloc(size) tc_malloc(size) #define calloc(count,size) tc_calloc(count,size) #define realloc(ptr,size) tc_realloc(ptr,size) #define free(ptr) tc_free(ptr) #elif defined(USE_JEMALLOC) #define malloc(size) je_malloc(size) #define calloc(count,size) je_calloc(count,size) #define realloc(ptr,size) je_realloc(ptr,size) #define free(ptr) je_free(ptr) #endif #ifdef HAVE_ATOMIC #define update_zmalloc_stat_add(__n) __sync_add_and_fetch(&used_memory, (__n)) #define update_zmalloc_stat_sub(__n) __sync_sub_and_fetch(&used_memory, (__n)) #else #define update_zmalloc_stat_add(__n) do { pthread_mutex_lock(&used_memory_mutex); used_memory += (__n); pthread_mutex_unlock(&used_memory_mutex); } while(0) #define update_zmalloc_stat_sub(__n) do { pthread_mutex_lock(&used_memory_mutex); used_memory -= (__n); pthread_mutex_unlock(&used_memory_mutex); } while(0) #endif #define update_zmalloc_stat_alloc(__n) do { size_t _n = (__n); if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); if (zmalloc_thread_safe) { update_zmalloc_stat_add(_n); } else { used_memory += _n; } } while(0) #define update_zmalloc_stat_free(__n) do { size_t _n = (__n); if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); if (zmalloc_thread_safe) { update_zmalloc_stat_sub(_n); } else { used_memory -= _n; } } while(0) static size_t used_memory = 0; static int zmalloc_thread_safe = 0; pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER; static void zmalloc_default_oom(size_t size) { fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes ", size); fflush(stderr); abort(); } static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom; void *zmalloc(size_t size) { void *ptr = malloc(size+PREFIX_SIZE); if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr)); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE); return (char*)ptr+PREFIX_SIZE; #endif } void *zcalloc(size_t size) { void *ptr = calloc(1, size+PREFIX_SIZE); if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_alloc(zmalloc_size(ptr)); return ptr; #else *((size_t*)ptr) = size; update_zmalloc_stat_alloc(size+PREFIX_SIZE); return (char*)ptr+PREFIX_SIZE; #endif } void *zrealloc(void *ptr, size_t size) { #ifndef HAVE_MALLOC_SIZE void *realptr; #endif size_t oldsize; void *newptr; if (ptr == NULL) return zmalloc(size); #ifdef HAVE_MALLOC_SIZE oldsize = zmalloc_size(ptr); newptr = realloc(ptr,size); if (!newptr) zmalloc_oom_handler(size); update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(zmalloc_size(newptr)); return newptr; #else realptr = (char*)ptr-PREFIX_SIZE; oldsize = *((size_t*)realptr); newptr = realloc(realptr,size+PREFIX_SIZE); if (!newptr) zmalloc_oom_handler(size); *((size_t*)newptr) = size; update_zmalloc_stat_free(oldsize); update_zmalloc_stat_alloc(size); return (char*)newptr+PREFIX_SIZE; #endif } /* Provide zmalloc_size() for systems where this function is not provided by * malloc itself, given that in that case we store a header with this * information as the first bytes of every allocation. */ #ifndef HAVE_MALLOC_SIZE size_t zmalloc_size(void *ptr) { void *realptr = (char*)ptr-PREFIX_SIZE; size_t size = *((size_t*)realptr); /* Assume at least that all the allocations are padded at sizeof(long) by * the underlying allocator. */ if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); return size+PREFIX_SIZE; } #endif void zfree(void *ptr) { #ifndef HAVE_MALLOC_SIZE void *realptr; size_t oldsize; #endif if (ptr == NULL) return; #ifdef HAVE_MALLOC_SIZE update_zmalloc_stat_free(zmalloc_size(ptr)); free(ptr); #else realptr = (char*)ptr-PREFIX_SIZE; oldsize = *((size_t*)realptr); update_zmalloc_stat_free(oldsize+PREFIX_SIZE); free(realptr); #endif } char *zstrdup(const char *s) { size_t l = strlen(s)+1; char *p = zmalloc(l); memcpy(p,s,l); return p; } size_t zmalloc_used_memory(void) { size_t um; if (zmalloc_thread_safe) { #ifdef HAVE_ATOMIC um = __sync_add_and_fetch(&used_memory, 0); #else pthread_mutex_lock(&used_memory_mutex); um = used_memory; pthread_mutex_unlock(&used_memory_mutex); #endif } else { um = used_memory; } return um; } void zmalloc_enable_thread_safeness(void) { zmalloc_thread_safe = 1; } void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) { zmalloc_oom_handler = oom_handler; } /* Get the RSS information in an OS-specific way. * * WARNING: the function zmalloc_get_rss() is not designed to be fast * and may not be called in the busy loops where Redis tries to release * memory expiring or swapping out objects. * * For this kind of "fast RSS reporting" usages use instead the * function RedisEstimateRSS() that is a much faster (and less precise) * version of the function. */ #if defined(HAVE_PROC_STAT) #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> size_t zmalloc_get_rss(void) { int page = sysconf(_SC_PAGESIZE); size_t rss; char buf[4096]; char filename[256]; int fd, count; char *p, *x; snprintf(filename,256,"/proc/%d/stat",getpid()); if ((fd = open(filename,O_RDONLY)) == -1) return 0; if (read(fd,buf,4096) <= 0) { close(fd); return 0; } close(fd); p = buf; count = 23; /* RSS is the 24th field in /proc/<pid>/stat */ while(p && count--) { p = strchr(p,' '); if (p) p++; } if (!p) return 0; x = strchr(p,' '); if (!x) return 0; *x = '