redis源码笔记-内存管理zmalloc.c

redis的内存分配主要就是对malloc和free进行了一层简单的封装。具体的实现在zmalloc.h和zmalloc.c中。本文将对redis的内存管理相关几个比较重要的函数做逐一的介绍
参考:

  1. http://blog.csdn.net/guodongxiaren/article/details/44783767
  2. http://www.voidcn.com/article/p-kxxvjygo-bpm.html
  3. http://blog.ddup.us/2011/05/11/redis-internal-memory-allocation/
  4. http://blog.csdn.net/taozhi20084525/article/details/23621345
void *zmalloc(size_t size);
void *zcalloc(size_t size);
void *zrealloc(void *ptr, size_t size);
void zfree(void *ptr);
size_t zmalloc_used_memory(void);
void zmalloc_enable_thread_safeness(void);
float zmalloc_get_fragmentation_ratio(size_t rss);
size_t zmalloc_get_rss(void);

zmalloc

在zmalloc函数中,实际可能会每次多申请一个 PREFIX_SIZE的空间。从如下的代码中看出,如果定义了宏HAVE_MALLOC_SIZE,那么 PREFIX_SIZE的长度为0。其他的情况下,都会多分配至少8字节的长度的内存空间。

  • zmalloc.c

    #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

    ```
    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

    }

这么做的原因是因为: tcmalloc 和 Mac平台下的 malloc 函数族提供了计算已分配空间大小的函数(分别是tcmallocsize和mallocsize),所以就不需要单独分配一段空间记录大小了。而针对linux和sun平台则要记录分配空间大小。对于linux,使用sizeof(sizet)定长字段记录;对于sun os,使用sizeof(long long)定长字段记录。因此当宏HAVE_MALLOC_SIZE没有被定义的时候,就需要在多分配出的空间内记录下当前申请的内存空间的大小。
![image](https://pic4.zhimg.com/50/v2-439f7c9d9a4b3d9a9f4e0a2737ab9fd1_hd.jpg)

### update_zmalloc_stat_alloc

update_zmalloc_stat_alloc 是一个宏,因为sizeof(long) == 8 [64位系统中],所以其实第一个if的代码等价于if(_n&7) _n += 8 - (_n&7);  这段代码就是判断分配的内存空间的大小是不是8的倍数。如果内存大小不是8的倍数,就加上相应的偏移量使之变成8的倍数。_n&7 在功能上等价于 _n%8,不过位操作的效率显然更高。
#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)  
malloc函数本身能够保证分配的内存是8字节对齐的,如果要分配的内存不是8的倍数,那么malloc就会多分配一点,来凑成8的倍数。所以这段代码真正的作用是获得使用内存的精确大小。   
第二个if主要判断当前是否处于线程安全的情况下。如果处于线程安全的情况下,就使用update_zmalloc_stat_add宏来更改全局变量used_memory。否则的话就直接加上n。
#define update_zmalloc_stat_add(__n) do { \  
    pthread_mutex_lock(&used_memory_mutex); \  
    used_memory += (__n); \  
    pthread_mutex_unlock(&used_memory_mutex); \  
} while(0)  

###  zmalloc_size
在zmalloc.h的代码中,有一段如下定义的代码:

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


可以看如果使用了jemalloc tcmalloc 或者apple系统下,都提供了检测内存块大小的函数,因此 zmalloc_size就使用相应的库函数。如果默认使用libc的话则 zmalloc_size函数有以下的定义:

/* 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;
      }

      扫描二维码关注公众号,回复: 86633 查看本文章

      endif

      ## zfree
      有分配就有内存回收,zfree 函数就是实现内存回收的功能。

      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

    }
    ```
    上面的代码可以看出,根据用的库不相同,回收的时候也采用了不同的方法。

#else
    realptr = (char*)ptr-PREFIX_SIZE;
    oldsize = *((size_t*)realptr);
    update_zmalloc_stat_free(oldsize+PREFIX_SIZE);
    free(realptr);
#endif

可以发现如果使用的libc库,则需要将ptr指针向前偏移8个字节的长度,回退到最初malloc返回的地址,然后通过类型转换再取指针所指向的值。通过zmalloc()函数的分析,可知这里存储着我们最初需要分配的内存大小(zmalloc中的size),这里赋值给oldsize。update_zmalloc_stat_free()也是一个宏函数,和zmalloc中update_zmalloc_stat_alloc()大致相同,唯一不同之处是前者在给变量used_memory减去分配的空间,而后者是加上该空间大小。
最后free(realptr),清除空间。

zcalloc

zcalloc 函数与zmalloc函数的功能基本相同,但有2点不同的是:

  1. 分配的空间大小是 size * nmemb。;
  2. calloc()会对分配的空间做初始化工作(初始化为0),而malloc()不会。

    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
    }

zrealloc

zrealloc 函数用来修改内存大小。具体的流程基本是分配新的内存大小,然后把老的内存数据拷贝过去,之后释放原有的内存。

    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  
    }  

zmalloc_used_memory

zmalloc_used_memory 函数用来获取当前使用的内存总量,其中__sync_add_and_fetch就是宏update_zmalloc_stat_add。关于do while(0)的用法可以参见http://blog.csdn.net/luoweifu/article/details/38563161

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;
}

zmalloc_get_rss

这个函数可以获取当前进程实际所驻留在内存中的空间大小,即不包括被交换(swap)出去的空间。该函数大致的操作就是在当前进程的 /proc/

/* 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 = '\0';

    rss = strtoll(p,NULL,10);
    rss *= page;
    return rss;
}
#elif defined(HAVE_TASKINFO)
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/task.h>
#include <mach/mach_init.h>

size_t zmalloc_get_rss(void) {
    task_t task = MACH_PORT_NULL;
    struct task_basic_info t_info;
    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;

    if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS)
        return 0;
    task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);

    return t_info.resident_size;
}
#else
size_t zmalloc_get_rss(void) {
    /* If we can't get the RSS in an OS-specific way for this system just
     * return the memory usage we estimated in zmalloc()..
     *
     * Fragmentation will appear to be always 1 (no fragmentation)
     * of course... */
    return zmalloc_used_memory();
}
#endif

zmalloc_get_fragmentation_ratio

这个函数可以来提供内存碎片率的指标,直接用驻留在物理内存中的内存/除以分配的总物理内存,得到一个所谓的碎片率, 实际留在物理内存中的除以总分配的。

/* Fragmentation = RSS / allocated-bytes */
float zmalloc_get_fragmentation_ratio(size_t rss) {
    return (float)rss/zmalloc_used_memory();
}

猜你喜欢

转载自www.cnblogs.com/bush2582/p/8969000.html
今日推荐