[转载]memcache源码分析之slabs – 先贝夜话 – 博客园.
slab是memcache用来管理item的内容存储部分。
分配内存时,memcache把我们通过参数m设置的内存大小分配到每个slab中
1、slab默认最多为200个,但是由于item的最大为1MB,而且每个slab里面存储的item的尺寸是根据factor来确定的,所以能够分配的slab的个数小于200。
2、关于增长因子factor参数(配置时参数名为f),默认为1.25,即每个slab所能存储的item的大小是根据factor的大小来变化的。
3、每个slab中含有一个或多个trunk,trunk中存储的就是item,item的最大为1M,所以trunk最大为1M
4、每个slab中会有一个item空闲列表,当新的item需要存储时,首先会考虑空闲列表,从中取出一个位置用来存储。当空闲列表满时,系统会去自动扩充。
5、每个slab中有二个参数为end_page_ptr、end_page_free,前者指向当前空闲的trunk指针,后者当前 trunk指向空闲处,当4中的空闲列表为空时,如果end_page_ptr和end_page_free不为空,则会在此trunk中存储item。 如果没有多余的trunk可用,系统会自动扩充trunk。
采用这种方式管理内存的好处是最大程度的减少了内存碎片的产生,提高了存储和读取效率。
下面是一些源码注释
slabs.h
2 |
void stats_prefix_init( void ); |
3 |
void stats_prefix_clear( void ); |
4 |
void stats_prefix_record_get( const char *key, const size_t nkey, const bool is_hit); |
5 |
void stats_prefix_record_delete( const char *key, const size_t nkey); |
6 |
void stats_prefix_record_set( const char *key, const size_t nkey); |
8 |
char *stats_prefix_dump( int *length); |
slabs.c
010 |
#include "memcached.h" |
011 |
#include <sys/stat.h> |
012 |
#include <sys/socket.h> |
013 |
#include <sys/signal.h> |
014 |
#include <sys/resource.h> |
016 |
#include <netinet/in.h> |
028 |
unsigned int perslab; |
031 |
unsigned int sl_total; |
032 |
unsigned int sl_curr; |
035 |
unsigned int end_page_free; |
040 |
unsigned int list_size; |
042 |
unsigned int killing; |
046 |
static slabclass_t slabclass[MAX_NUMBER_OF_SLAB_CLASSES]; |
047 |
static size_t mem_limit = 0; |
048 |
static size_t mem_malloced = 0; |
049 |
static int power_largest; |
051 |
static void *mem_base = NULL; |
052 |
static void *mem_current = NULL; |
053 |
static size_t mem_avail = 0; |
058 |
static pthread_mutex_t slabs_lock = PTHREAD_MUTEX_INITIALIZER; |
063 |
static int do_slabs_newslab( const unsigned int id); |
064 |
static void *memory_allocate( size_t size); |
066 |
#ifndef DONT_PREALLOC_SLABS |
073 |
static void slabs_preallocate ( const unsigned int maxslabs); |
078 |
unsigned int slabs_clsid( const size_t size) { |
079 |
int res = POWER_SMALLEST; |
083 |
while (size > slabclass[res].size) |
084 |
if (res++ == power_largest) |
092 |
void slabs_init( const size_t limit, const double factor, const bool prealloc) { |
093 |
int i = POWER_SMALLEST - 1; |
094 |
unsigned int size = sizeof (item) + settings.chunk_size; |
100 |
mem_base = malloc (mem_limit); |
101 |
if (mem_base != NULL) { |
102 |
mem_current = mem_base; |
103 |
mem_avail = mem_limit; |
105 |
fprintf (stderr, "Warning: Failed to allocate requested memory in one large chunk.\nWill allocate in smaller chunks\n" ); |
109 |
memset (slabclass, 0, sizeof (slabclass)); |
111 |
while (++i < POWER_LARGEST && size <= settings.item_size_max / factor) { |
113 |
if (size % CHUNK_ALIGN_BYTES) |
114 |
size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES); |
116 |
slabclass[i].size = size; |
117 |
slabclass[i].perslab = settings.item_size_max / slabclass[i].size; |
119 |
if (settings.verbose > 1) { |
120 |
fprintf (stderr, "slab class %3d: chunk size %9u perslab %7u\n" ,i, slabclass[i].size, slabclass[i].perslab); |
125 |
slabclass[power_largest].size = settings.item_size_max; |
126 |
slabclass[power_largest].perslab = 1; |
127 |
if (settings.verbose > 1) { |
128 |
fprintf (stderr, "slab class %3d: chunk size %9u perslab %7u\n" ,i, slabclass[i].size, slabclass[i].perslab); |
133 |
char *t_initial_malloc = getenv ( "T_MEMD_INITIAL_MALLOC" ); |
134 |
if (t_initial_malloc) { |
135 |
mem_malloced = ( size_t ) atol (t_initial_malloc); |
140 |
#ifndef DONT_PREALLOC_SLABS |
142 |
char *pre_alloc = getenv ( "T_MEMD_SLABS_ALLOC" ); |
144 |
if (pre_alloc == NULL || atoi (pre_alloc) != 0) { |
145 |
slabs_preallocate(power_largest); |
153 |
#ifndef DONT_PREALLOC_SLABS |
154 |
static void slabs_preallocate ( const unsigned int maxslabs) { |
156 |
unsigned int prealloc = 0; |
164 |
for (i = POWER_SMALLEST; i <= POWER_LARGEST; i++) { |
165 |
if (++prealloc > maxslabs) |
175 |
static int grow_slab_list ( const unsigned int id) { |
176 |
slabclass_t *p = &slabclass[id]; |
177 |
if (p->slabs == p->list_size) { |
178 |
size_t new_size = (p->list_size != 0) ? p->list_size * 2 : 16; |
179 |
void *new_list = realloc (p->slab_list, new_size * sizeof ( void *)); |
180 |
if (new_list == 0) return 0; |
181 |
p->list_size = new_size; |
182 |
p->slab_list = new_list; |
190 |
static int do_slabs_newslab( const unsigned int id) { |
191 |
slabclass_t *p = &slabclass[id]; |
192 |
int len = p->size * p->perslab; |
195 |
if ((mem_limit && mem_malloced + len > mem_limit && p->slabs > 0) || (grow_slab_list(id) == 0) || ((ptr = memory_allocate(( size_t )len)) == 0)) { |
196 |
MEMCACHED_SLABS_SLABCLASS_ALLOCATE_FAILED(id); |
200 |
memset (ptr, 0, ( size_t )len); |
201 |
p->end_page_ptr = ptr; |
202 |
p->end_page_free = p->perslab; |
204 |
p->slab_list[p->slabs++] = ptr; |
206 |
MEMCACHED_SLABS_SLABCLASS_ALLOCATE(id); |
214 |
static void *do_slabs_alloc( const size_t size, unsigned int id) { |
218 |
if (id < POWER_SMALLEST || id > power_largest) { |
219 |
MEMCACHED_SLABS_ALLOCATE_FAILED(size, 0); |
224 |
assert (p->sl_curr == 0 || ((item *)p->slots[p->sl_curr - 1])->slabs_clsid == 0); |
226 |
#ifdef USE_SYSTEM_MALLOC |
227 |
if (mem_limit && mem_malloced + size > mem_limit) { |
228 |
MEMCACHED_SLABS_ALLOCATE_FAILED(size, id); |
231 |
mem_malloced += size; |
233 |
MEMCACHED_SLABS_ALLOCATE(size, id, 0, ret); |
239 |
if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) != 0)) { |
241 |
} else if (p->sl_curr != 0) { |
243 |
ret = p->slots[--p->sl_curr]; |
246 |
assert (p->end_page_ptr != NULL); |
247 |
ret = p->end_page_ptr; |
248 |
if (--p->end_page_free != 0) { |
249 |
p->end_page_ptr = ((caddr_t)p->end_page_ptr) + p->size; |
256 |
p->requested += size; |
257 |
MEMCACHED_SLABS_ALLOCATE(size, id, p->size, ret); |
259 |
MEMCACHED_SLABS_ALLOCATE_FAILED(size, id); |
267 |
static void do_slabs_free( void *ptr, const size_t size, unsigned int id) { |
270 |
assert (((item *)ptr)->slabs_clsid == 0); |
271 |
assert (id >= POWER_SMALLEST && id <= power_largest); |
272 |
if (id < POWER_SMALLEST || id > power_largest) |
275 |
MEMCACHED_SLABS_FREE(size, id, ptr); |
278 |
#ifdef USE_SYSTEM_MALLOC |
279 |
mem_malloced -= size; |
284 |
if (p->sl_curr == p->sl_total) { |
285 |
int new_size = (p->sl_total != 0) ? p->sl_total * 2 : 16; |
286 |
void **new_slots = realloc (p->slots, new_size * sizeof ( void *)); |
289 |
p->slots = new_slots; |
290 |
p->sl_total = new_size; |
292 |
p->slots[p->sl_curr++] = ptr; |
293 |
p->requested -= size; |
298 |
static int nz_strcmp( int nzlength, const char *nz, const char *z) { |
299 |
int zlength= strlen (z); |
300 |
return (zlength == nzlength) && ( strncmp (nz, z, zlength) == 0) ? 0 : -1; |
305 |
bool get_stats( const char *stat_type, int nkey, ADD_STAT add_stats, void *c) { |
308 |
if (add_stats != NULL) { |
312 |
APPEND_STAT( "bytes" , "%llu" , (unsigned long long )stats.curr_bytes); |
313 |
APPEND_STAT( "curr_items" , "%u" , stats.curr_items); |
314 |
APPEND_STAT( "total_items" , "%u" , stats.total_items); |
315 |
APPEND_STAT( "evictions" , "%llu" ,(unsigned long long )stats.evictions); |
316 |
APPEND_STAT( "reclaimed" , "%llu" ,(unsigned long long )stats.reclaimed); |
318 |
} else if (nz_strcmp(nkey, stat_type, "items" ) == 0) { |
319 |
item_stats(add_stats, c); |
320 |
} else if (nz_strcmp(nkey, stat_type, "slabs" ) == 0) { |
321 |
slabs_stats(add_stats, c); |
322 |
} else if (nz_strcmp(nkey, stat_type, "sizes" ) == 0) { |
323 |
item_stats_sizes(add_stats, c); |
336 |
static void do_slabs_stats(ADD_STAT add_stats, void *c) { |
339 |
struct thread_stats thread_stats; |
340 |
threadlocal_stats_aggregate(&thread_stats); |
343 |
for (i = POWER_SMALLEST; i <= power_largest; i++) { |
344 |
slabclass_t *p = &slabclass[i]; |
346 |
uint32_t perslab, slabs; |
348 |
perslab = p->perslab; |
350 |
char key_str[STAT_KEY_LEN]; |
351 |
char val_str[STAT_VAL_LEN]; |
352 |
int klen = 0, vlen = 0; |
354 |
APPEND_NUM_STAT(i, "chunk_size" , "%u" , p->size); |
355 |
APPEND_NUM_STAT(i, "chunks_per_page" , "%u" , perslab); |
356 |
APPEND_NUM_STAT(i, "total_pages" , "%u" , slabs); |
357 |
APPEND_NUM_STAT(i, "total_chunks" , "%u" , slabs * perslab); |
358 |
APPEND_NUM_STAT(i, "used_chunks" , "%u" ,slabs*perslab - p->sl_curr - p->end_page_free); |
359 |
APPEND_NUM_STAT(i, "free_chunks" , "%u" , p->sl_curr); |
360 |
APPEND_NUM_STAT(i, "free_chunks_end" , "%u" , p->end_page_free); |
361 |
APPEND_NUM_STAT(i, "mem_requested" , "%llu" ,(unsigned long long )p->requested); |
362 |
APPEND_NUM_STAT(i, "get_hits" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].get_hits); |
363 |
APPEND_NUM_STAT(i, "cmd_set" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].set_cmds); |
364 |
APPEND_NUM_STAT(i, "delete_hits" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].delete_hits); |
365 |
APPEND_NUM_STAT(i, "incr_hits" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].incr_hits); |
366 |
APPEND_NUM_STAT(i, "decr_hits" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].decr_hits); |
367 |
APPEND_NUM_STAT(i, "cas_hits" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].cas_hits); |
368 |
APPEND_NUM_STAT(i, "cas_badval" , "%llu" ,(unsigned long long )thread_stats.slab_stats[i].cas_badval); |
376 |
APPEND_STAT( "active_slabs" , "%d" , total); |
377 |
APPEND_STAT( "total_malloced" , "%llu" , (unsigned long long )mem_malloced); |
378 |
add_stats(NULL, 0, NULL, 0, c); |
383 |
static void *memory_allocate( size_t size) { |
386 |
if (mem_base == NULL) { |
392 |
if (size > mem_avail) { |
397 |
if (size % CHUNK_ALIGN_BYTES) { |
398 |
size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES); |
401 |
mem_current = (( char *)mem_current) + size; |
402 |
if (size < mem_avail) { |
414 |
void *slabs_alloc( size_t size, unsigned int id) { |
417 |
pthread_mutex_lock(&slabs_lock); |
418 |
ret = do_slabs_alloc(size, id); |
419 |
pthread_mutex_unlock(&slabs_lock); |
425 |
void slabs_free( void *ptr, size_t size, unsigned int id) { |
426 |
pthread_mutex_lock(&slabs_lock); |
427 |
do_slabs_free(ptr, size, id); |
428 |
pthread_mutex_unlock(&slabs_lock); |
433 |
void slabs_stats(ADD_STAT add_stats, void *c) { |
434 |
pthread_mutex_lock(&slabs_lock); |
435 |
do_slabs_stats(add_stats, c); |
436 |
pthread_mutex_unlock(&slabs_lock); |