PREFIX_SIZE說明
在zmalloc函數中,實際可能會每次多申請一個 PREFIX_SIZE的空間。從如下的代碼中看出,如果定義了宏HAVE_MALLOC_SIZE,那麼 PREFIX_SIZE的長度為0。其他的情況下,都會多配置設定至少8位元組的長度的記憶體空間
#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
這麼做的原因是因為: tcmalloc 和 Mac平台下的 malloc 函數族提供了計算已配置設定空間大小的函數(分别是tcmallocsize和mallocsize),是以就不需要單獨配置設定一段空間記錄大小了。而針對linux和sun平台則要記錄配置設定空間大小。對于linux,使用sizeof(sizet)定長字段記錄;對于sun os,使用sizeof(long long)定長字段記錄。是以當宏HAVE_MALLOC_SIZE沒有被定義的時候,就需要在多配置設定出的空間内記錄下目前申請的記憶體空間的大小。
根據情況使用不同的malloc實作 自行google USE_TCMALLOC和USE_JEMALLOC實作
/* 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)
#define mallocx(size,flags) je_mallocx(size,flags)
#define dallocx(ptr,flags) je_dallocx(ptr,flags)
#endif
update_zmalloc_stat_alloc 和 update_zmalloc_stat_free
這個函數的作用是跟新全局變量used_memory:redis記憶體使用情況
malloc函數本身能夠保證配置設定的記憶體是8位元組對齊的,如果要配置設定的記憶體不是8的倍數,那麼malloc就會多配置設定一點,來湊成8的倍數。
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1));這行代碼是實作位元組對齊, sizeof(long) = 8(64位系統, 32位系統中為4),這段代碼就是判斷配置設定的記憶體空間的大小是不是8的倍數。如果記憶體大小不是8的倍數,就加上相應的偏移量使之變成8的倍數
_n&(sizeof(long)-1) : _n&7 在功能上等價于 _n%8,位操作效率高
atomicIncr: redis自己實作原子操作函數:其記憶體實作也是調用系統底層原子操作接口,自行去學習__atomic 與 __sync兩個版本的原子作業系統接口
#define update_zmalloc_stat_alloc(__n) do { \
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
atomicIncr(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)); \
atomicDecr(used_memory,__n); \
} while(0)
static size_t used_memory = 0;
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
zmalloc
void *zmalloc(size_t size) {
//配置設定size+PREFIX_SIZE長度的記憶體
void *ptr = malloc(size+PREFIX_SIZE);
//報錯
if (!ptr) zmalloc_oom_handler(size);
#ifdef HAVE_MALLOC_SIZE
//位元組對齊 原子 更新全局變量used_memory
update_zmalloc_stat_alloc(zmalloc_size(ptr));
return ptr;
#else
//如果未定義HAVE_MALLOC_SIZE,多配置設定的PREFIX_SIZE長度的記憶體存貯使用的記憶體大小
*((size_t*)ptr) = size;
update_zmalloc_stat_alloc(size+PREFIX_SIZE);
//向前移動指針ptr
return (char*)ptr+PREFIX_SIZE;
#endif
}
zmalloc_size
通過指針ptr求記憶體使用的大小
#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;
}
size_t zmalloc_usable(void *ptr) {
return zmalloc_size(ptr)-PREFIX_SIZE;
}
#endif
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
}
可以發現如果使用的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點不同的是:
- 配置設定的空間大小是 size * nmemb。;
- 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+PREFIX_SIZE);
update_zmalloc_stat_alloc(size+PREFIX_SIZE);
return (char*)newptr+PREFIX_SIZE;
#endif
}
zstrdup
redis的對memcpy的封裝
char *zstrdup(const char *s) {
size_t l = strlen(s)+1;
char *p = zmalloc(l);
memcpy(p,s,l);
return p;
}
zmalloc_get_rss
這個函數可以擷取目前程序實際所駐留在記憶體中的空間大小,即不包括被交換(swap)出去的空間。該函數大緻的操作就是在目前程序的 /proc//stat 【表示目前程序id】檔案中進行檢索。該檔案的第24個字段是RSS的資訊,它的機關是pages(記憶體頁的數目)。如果沒從作業系統的層面擷取駐留記憶體大小,那就隻能绌劣的傳回已經配置設定出去的記憶體大小。
/* 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