memcache的内存分配和重用机制
众所周知,memcache采用slab allocator管理内存,启动由-m指定可用的最大内存值(默认64M),默认是使用时再分配,-k可提前分配内存并锁定;每个slab包含若干大小为1M的内存页,这些内存又被分割成多个chunk,每个chunk存储一个item;
在mc启动初始化时,每个slab都预分配一个1M的内存页,由slabs_preallocate 完成(也可将相应代码注释掉关闭预分配功能);
chunk的增长因子由-f指定,默认1.25,起始大小为48字节;
图片来源
http://blog.csdn.net/lcli2009/article/details/22095251
http://blog.csdn.net/lcli2009/article/details/21985793
item
为实际存储数据的结构体,由两部分组成,属性信息和数据部分,数据部分包括cas,key和真实的value信息。
typedef struct _stritem { struct _stritem *next;//item在slab中存储时,是以双链表的形式存储的,next即后向指针 struct _stritem *prev;//prev为前向指针 struct _stritem *h_next;//Hash桶中元素的链接指针 rel_time_t time; //最近访问时间 rel_time_t exptime;//过期时间 int nbytes;//数据大小 unsigned shortrefcount;//引用次数 uint8_t nsuffix; uint8_t it_flags; uint8_t slabs_clsid;//标记item属于哪个slabclass下 uint8_t nkey; //key的长度 union { uint64_t cas; char end; } data[];//真实的数据信息 } item;
slab
何时分配新的slab? 1 bigger item; 2 no free chunk;
If the new item is bigger than the size of any existing blocks, then a new slab is created, divided up into blocks of a suitable size. If an existing slab with the right block sizealready exists, but there are no free blocks, a new slab is created.
内存页一旦被分配给slab,在memcache生命周期内不再更改,在内存总量确定的情形下,其它slab可能出现饿死现象;
为此1.4.11引入slab automove
Thealgorithm is slow and conservative. If a slab class is seen as having the highest eviction count 3 times 10 seconds apart, it will take a pagefrom a slab class which has had zero evictions in the last 30 seconds and move the memory.
若某个slab在10秒内出现3次eviction,则从过去30秒内没有出现eviction的slab里挪取一个内存页使用;
分为手工分配和自动分配:
手工:-o slab_reassign;在mc运行时输入echo "slabs reassign 1 4" | nc localhost 11211
自动: -o slab_reassign, slab_automove;mc每10秒进行一次重分配,手工暂停echo"slabs automove 0" | nc localhost 11211
typedef struct {
unsigned int size; /* 每个item大小, sizes of items */
unsigned int perslab; /* 每个page中包含多少个item , how many items per slab */
void **slots; /* 空闲的item指针, list of item ptrs */
unsigned int sl_total; /* 以分配空闲的item 个数, size of previous array */
unsigned int sl_curr; /* 当前空闲的item位置(也就是实际空闲item个数),从后往前的, first free slot */
void *end_page_ptr; /* 指向最后一个页面中空闲的item开始位置, pointer to next free item at end of page, or 0 */
unsigned int end_page_free; /* 最后一个页面,item个数, number of items remaining at end of last alloced page */
unsigned int slabs; /* 实际使用slab(page)个数 how many slabs were allocated for this class */
void **slab_list; /* 所有page的指针, array of slab pointers */
unsigned int list_size; /* 已经分配page指针个数,size of prev array */
unsigned int killing; /* index+1 of dying slab, or zero if none */
size_t requested; /* 所有被使用了的内存的大小, The number of requested bytes */
} slabclass_t;
图片来源 http://blog.csdn.net/benbendy1984/article/details/6216453
LRU和expired item
memcache并不会监视和清理过期数据,而是在客户端get时检查,称为lazy expiration。
item被检测到超时并不会被删除,而是放入slab->slots头部;
do_item_get --
--判断该item是否过期
do_item_unlink(it, hv);//将item从hashtable和LRU链中移除
do_item_remove(it);//删除item
do_item_remove
item_free(it);//释放item
slabs_free(it, ntotal, clsid);//slabclass结构执行释放
do_slabs_free(ptr, size, id);//执行释放
以下是do_slabs_free的代码,将expired item放入slab->slots的头部
it = (item *)ptr;
it->it_flags |= ITEM_SLABBED;//修改item的状态标识,修改为空闲
it->prev = 0;//断开数据链表
it->next = p->slots;
if (it->next) it->next->prev = it;
p->slots = it;
问:expired item何时被重用?
1 slab在新加item时会先查看LRU队尾;
2 如果队尾的item恰巧超时则重用,否则执行slabs_alloc;这一过程循环5次,若还没有找到可用item,则再次调用slabs_alloc;
3 slabs_alloc依次尝试a slab->slot即expired item链表;b slab->end_page_ptr 最后一个页面的空闲item; c 分配新的内存页
也就是说,只有LRU最后的5个元素状态为expired时,才有机会直接重用LRU,否则会依次尝试expired item list和slab的最后一个内存页的free item;
以下是代码实现
当客户端执行add操作,即往slab添加item时,调用do_item_alloc;
do_item_alloc
mutex_lock(&cache_lock);//执行LRU锁 存储时,会尝试从LRU中选择合适的空间的空间
int tries = 5;//如果LRU中尝试5次还没合适的空间,则执行申请空间的操作
search = tails;//第id个LRU表的尾部
/* We walk up *only* for locked items. Never searching for expired
for (; tries > 0 && search != NULL; tries--, search=search->prev) {
uint32_t hv = hash(ITEM_key(search), search->nkey, 0);//获取分段锁
if ((search->exptime != 0 && search->exptime < current_time)|| (search->time sl_curr != 0 || do_slabs_newslab(id) != 0)) {
/* We don't have more memory available */
ret = NULL;
} else if (p->sl_curr != 0) {
/* return off our freelist */
//从空闲(回收)地方分配
ret = p->slots[--p->sl_curr];
} else {
/* if we recently allocated a whole page, return from that */
assert(p->end_page_ptr != NULL);
ret = p->end_page_ptr;
if (--p->end_page_free != 0) {
p->end_page_ptr = ((caddr_t)p->end_page_ptr) + p->size;
} else {
p->end_page_ptr = 0;
}
}
if (ret) {
p->requested += size;
MEMCACHED_SLABS_ALLOCATE(size, id, p->size, ret);
} else {
MEMCACHED_SLABS_ALLOCATE_FAILED(size, id);
}
return ret;
}
do_slabs_newslab--即为该slab新分配一个数据页;
int len = p->size * p->perslab;
memory_allocate((size_t)len))
页:
[1]