//item的具体结构
typedef struct _stritem {
//记录下一个item的地址,主要用于LRU链和freelist链
struct _stritem *next;
//记录下一个item的地址,主要用于LRU链和freelist链
struct _stritem *prev;
//记录HashTable的下一个Item的地址
struct _stritem *h_next;
//最近访问的时间,只有set/add/replace等操作才会更新这个字段
//当执行flush命令的时候,需要用这个时间和执行flush命令的时间相比较,来判断是否失效
rel_time_t time; /* least recent access */
//缓存的过期时间。设置为0的时候,则永久有效。
//如果Memcached不能分配新的item的时候,设置为0的item也有可能被LRU淘汰
rel_time_t exptime; /* expire time */
//value数据大小
int nbytes; /* size of data */
//引用的次数。通过这个引用的次数,可以判断item是否被其它的线程在操作中。
//也可以通过refcount来判断当前的item是否可以被删除,只有refcount -1 = 0的时候才能被删除
unsigned short refcount;
uint8_t nsuffix; /* length of flags-and-length string */
uint8_t it_flags; /* ITEM_* above */
//slabs_class的ID。
uint8_t slabs_clsid;/* which slab class we're in */
uint8_t nkey; /* key length, w/terminating null and padding */
/* this odd type prevents type-punning issues when we do
* the little shuffle to save space when not using CAS. */
//数据存储结构
union {
uint64_t cas;
char end;
} data[];
/* if it_flags & ITEM_CAS we have 8 bytes CAS */
/* then null-terminated key */
/* then " flags length\r\n" (no terminating null) */
/* then data with terminating \r\n (no terminating null; it's binary!) */
} item; slabclass 划分数据空间
//slabclass的结构
typedef struct {
//当前的slabclass存储最大多大的item
unsigned int size;
//每一个slab上可以存储多少个item.每个slab大小为1M, 可以存储的item个数根据size决定。
unsigned int perslab;
//当前slabclass的(空闲item列表)freelist 的链表头部地址
//freelist的链表是通过item结构中的item->next和item->prev连建立链表结构关系
void *slots; /* list of item ptrs */
//当前总共剩余多少个空闲的item
//当sl_curr=0的时候,说明已经没有空闲的item,需要分配一个新的slab(每个1M,可以切割成N多个Item结构)
unsigned int sl_curr; /* total free items in list */
//总共分配多少个slabs
unsigned int slabs; /* how many slabs were allocated for this class */
//分配的slab链表
void **slab_list; /* array of slab pointers */
unsigned int list_size; /* size of prev array */
unsigned int killing; /* index+1 of dying slab, or zero if none */
//总共请求的总bytes
size_t requested; /* The number of requested bytes */
} slabclass_t;
//定义一个slabclass数组,用于存储最大200个的slabclass_t的结构。
static slabclass_t slabclass[MAX_NUMBER_OF_SLAB_CLASSES];
//slabclass初始化
void slabs_init(const size_t limit, const double factor, const bool prealloc) {
int i = POWER_SMALLEST - 1;
unsigned int size = sizeof(item) + settings.chunk_size;
mem_limit = limit;
//这边是否初始化的时候,就给每一个slabclass_t结构分配一个slab内存块
//默认都会分配
if (prealloc) {
/* Allocate everything in a big chunk with malloc */
mem_base = malloc(mem_limit);
if (mem_base != NULL) {
mem_current = mem_base;
mem_avail = mem_limit;
} else {
fprintf(stderr, "Warning: Failed to allocate requested memory in"
" one large chunk.\nWill allocate in smaller chunks\n");
}
}
memset(slabclass, 0, sizeof(slabclass));
//factor 默认等于1.25 ,也就是说前一个slabclass允许存储96byte大小的数据,
//则下一个slabclass可以存储120byte
while (++i < POWER_LARGEST && size <= settings.item_size_max / factor) {
/* Make sure items are always n-byte aligned */
if (size % CHUNK_ALIGN_BYTES)
size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES);
//每个slabclass存储最大多大的item
slabclass.size = size;
slabclass.perslab = settings.item_size_max / slabclass.size;
size *= factor;
if (settings.verbose > 1) {
fprintf(stderr, "slab class %3d: chunk size %9u perslab %7u\n",
i, slabclass.size, slabclass.perslab);
}
}
power_largest = i;
slabclass[power_largest].size = settings.item_size_max;
slabclass[power_largest].perslab = 1;
if (settings.verbose > 1) {
fprintf(stderr, "slab class %3d: chunk size %9u perslab %7u\n",
i, slabclass.size, slabclass.perslab);
}
/* for the test suite: faking of how much we've already malloc'd */
{
char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC");
if (t_initial_malloc) {
mem_malloced = (size_t)atol(t_initial_malloc);
}
}
if (prealloc) {
slabs_preallocate(power_largest);
}
}
//分配内存
static void slabs_preallocate (const unsigned int maxslabs) {
int i;
unsigned int prealloc = 0;
/* pre-allocate a 1MB slab in every size class so people don't get
confused by non-intuitive "SERVER_ERROR out of memory"
messages. this is the most common question on the mailing
list. if you really don't want this, you can rebuild without
these three lines. */
//给每一个slabclass_t结构分配一个默认的slab
for (i = POWER_SMALLEST; i <= POWER_LARGEST; i++) {
if (++prealloc > maxslabs)
return;
if (do_slabs_newslab(i) == 0) {
fprintf(stderr, "Error while preallocating slab memory!\n"
"If using -L or other prealloc options, max memory must be "
"at least %d megabytes.\n", power_largest);
exit(1);
}
}
}
//分配一个Item
static void *do_slabs_alloc(const size_t size, unsigned int id) {
slabclass_t *p;
void *ret = NULL;
item *it = NULL;
if (id < POWER_SMALLEST || id > power_largest) {
MEMCACHED_SLABS_ALLOCATE_FAILED(size, 0);
return NULL;
}
//获取slabclass
p = &slabclass[id];
assert(p->sl_curr == 0 || ((item *)p->slots)->slabs_clsid == 0);
/* fail unless we have space at the end of a recently allocated page,
we have something on our freelist, or we could allocate a new page */
//p->sl_curr 说明是否有空闲的item list
//如果没有空闲的item list,则取分配一个新的slab,如果分配失败,返回NULL
if (! (p->sl_curr != 0 || do_slabs_newslab(id) != 0)) {
/* We don't have more memory available */
ret = NULL;
//如果有free item lits,则从空闲的列表中取一个Item
} else if (p->sl_curr != 0) {
/* return off our freelist */
it = (item *)p->slots;
p->slots = it->next;
if (it->next) it->next->prev = 0;
p->sl_curr--;
ret = (void *)it;
}
if (ret) {
p->requested += size;
MEMCACHED_SLABS_ALLOCATE(size, id, p->size, ret);
} else {
MEMCACHED_SLABS_ALLOCATE_FAILED(size, id);
}
return ret;
}分配一个新的slab: