Memcached the initialization of source code analysis--slab

Source: Internet
Author: User
Tags cas memcached prev

The following is only my notes.

/** * Determines the chunk sizes and initializes the slab class descriptors * accordingly.*/ /** * Determine the size of the chunk, initialize the corresponding descriptor of the slabs class*/voidSlabs_init (Constsize_t limit,Const DoubleFactorConst BOOLPrealloc) {    /** * #define POWER_SMALLEST 1//power_smallest definition, indicating that slab contains at least one chunk * below this line I is the chunk counter, record the current slab has been divided by how much A chunk with an initial value of 0*/    inti = power_smallest-1; /** * The size below is chunk, in bytes. * Size of item occupies memory size with settings.chunk_size * settings.chunk_size default value is 48, initialized in MEMCACHED-1.4.22/MEMCACHED.C static     void Settings_init (void);     * The definition of settings is in Memcached-1.4.22/memcached.h. */unsignedintSize =sizeof(item) +settings.chunk_size; /** * Limit the amount of memory used is limit MB, default is 64MB, this default value is determined by settings.maxbytes. */Mem_limit=limit; /** * Whether to pre-allocate a large chunk in memory with a default size of 64MB, default Prealloc=false*/    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"); }    }    /** This line is very simple, the Slabclass object's sizeof (Slabclass) bytes are all initialized to 0*/memset (Slabclass,0,sizeof(Slabclass)); /** * next to the specific cutting slab place.     * #define POWER_LARGEST 200//is defined here is the slab of up to 200 types * Settings.item_size_max is the maximum number of bytes for item, default is 1MB. * Factor is the growth factor of chunk, which increases to factor times before.     The default is 1.25. */     while(++i < power_largest && size <= Settings.item_size_max/factor) {        /** * The following statement implements the function: Ensure that the items byte number is chunk_align_bytes integer times * #define Chunk_align_bytes 8//chunk_align_bytes Righteousness*/        /*Make sure items is always n-byte aligned*/        if(Size%chunk_align_bytes) Size+ = chunk_align_bytes-(size%chunk_align_bytes); /** * Defines the size of the chunk in the current slab. */slabclass[i].size=size; /** * Defines the number of chunk in the current slab. */Slabclass[i].perslab= Settings.item_size_max/slabclass[i].size; Size*=factor; if(Settings.verbose >1) {fprintf (stderr,"Slab class%3d:chunk size%9u perslab%7u\n", I, slabclass[i].size, Slabclass[i].perslab); }    }    /** * Record how many kinds of slabs there are*/power_largest=i; /** * Set the chunk value of the largest size in slabs*/slabclass[power_largest].size=Settings.item_size_max; /** * The number of chunk in the largest slab in chunk size*/Slabclass[power_largest].perslab=1; if(Settings.verbose >1) {fprintf (stderr,"Slab class%3d:chunk size%9u perslab%7u\n", I, slabclass[i].size, Slabclass[i].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); }    }    /** * Whether pre-allocated*/    if(Prealloc) {slabs_preallocate (power_largest); }}/** * POWERS-OF-N allocation Structures * code in MEMCACHED-1.4.22/SLABS.C*/typedefstruct{unsignedintSize/*Sizes of items*/unsignedintPerslab;/*How many items per slab*/    void*slots;/*List of item Ptrs*/unsignedintSl_curr;/*Total free items in list*/unsignedintslabs;/*How many slabs were allocated for this class*/    void**slab_list;/*array of slab pointers*/unsignedintList_size;/*size of prev array*/unsignedintKilling;/*index+1 of Dying slab, or zero if none*/size_t requested;/*The number of requested bytes*/} slabclass_t;Staticslabclass_t slabclass[max_number_of_slab_classes];/** * memcache, each piece of data is recorded in an item, the specific structure of item is as follows * Structure for storing items within memcached. * The following code is in the Memcached-1.4.22/mem Cached.h 343 lines to 368 rows*/typedefstruct_stritem {struct_stritem *Next; struct_stritem *prev; struct_stritem *h_next;/*Hash Chain Next*/rel_time_t time; /*Least recent Access*/rel_time_t Exptime; /*Expire Time*/    intNbytes;/*Size of data*/unsigned ShortRefCount;    uint8_t Nsuffix; /*length of Flags-and-length string*/uint8_t it_flags; /*item_* above*/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 if we do * the little shuffle to save space is not using CAS. */Union {uint64_t CAs; Charend;    } data[]; /*If It_flags & item_cas we have 8 bytes CAS*/    /*Then null-terminated key*/    /*Then "Flags length\r\n" (no terminating null)*/    /*Then the data with terminating \ r \ n (no terminating null; it ' s binary!)*/} item;

The work done by the Slabs_init function is simply to initialize the Slabclass array, to predefine the size of all the slab that might be used, and not to actually apply for memory space. Only when the write operation is actually performed will the appropriate size slab be chosen based on the size of the data, then the space is applied and the relevant data is stored. Therefore, when you use the stats Slabs command to view the slabs used in memcached, you will find that the number of slabs used is not sequential, which is the reason.

Such as:

When you use the stats slabs command to see which slabs are currently in use, the found numbers are 1, 4, and 9, which actually only have three data:

$str str_repeat (' I ', +); $memc->set (' just_a_test ',$str, ten); $str str_repeat (' I ', $); $memc->set (' just_a_test ',$str, ten); $str str_repeat (' I ', 5); $memc->set (' just_a_test ',$str, 10);

It can be seen that memcached really performs a write operation in order to select the appropriate size slab in the Slabclass array and then store it. The number shown in the figure is actually the subscript of the corresponding slab in the Slabclass array.

This figure also shows a problem: the same key is stored three times, each corresponding to the value of the size of different, not covering each other, but a piece of memory, which is memcached using slab to store a feature.

Memcached the initialization of source code analysis--slab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.