美文网首页
[Memcached] Slab Allocation的MC项占

[Memcached] Slab Allocation的MC项占

作者: 大头8086 | 来源:发表于2017-07-06 20:37 被阅读56次

    引子

    从Slab Allocation原理知道,当MC选择一个Slab class的Chunk存放item数据,必须计算item的空间大小以选择合适的Slab class。按照很多人理解,item是不是只包含缓存对象的value?假设不是,那如何精确计算item的空间大小呢?从这个问题延伸,Chunk是不是只存放缓存对象的value?

    源码分析

    先看图1的item结构体,包含item、key、suffix、data等内容,注意key/data存放的数据不只是我们认为的key/value,下面将从源码层面计算item的长度。

    图1 item结构体

    摘录函数item_make_header()计算item长度的代码如下:

    sizeof(item) + nkey + *nsuffix + nbytes;//计算总大小  
    

    从下面一行代码,可知nkey = key.length + 1。

    size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 
    

    从函数do_item_alloc()函数注释知nbytes是要存储的value长度+2,因为在value的结尾处还要加上"\r\n",即 nbytes = value.length + 2。
    从下面一行代码,可知nsuffix后缀长度为:

    *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2);  
    

    所以ntotal=sizeof(item)+key.length+1+(" %d %d\r\n", flags, nbytes - 2).length+value.lenght+2,其中32位机器item结构是32字节,64位机器item结构是48字节。
    从下面一行代码,如果开启CAS功能,ntotal += sizeof(uint64_t),uint64_t是8字节。

     if (settings.use_cas) {//开启了CAS功能  
        ntotal += sizeof(uint64_t);  
    }  
    
    static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes,  
                         char *suffix, uint8_t *nsuffix) {  
        /* suffix is defined at 40 chars elsewhere.. */  
        *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2);  
        return sizeof(item) + nkey + *nsuffix + nbytes;//计算总大小  
    }  
      
    //key、flags、exptime三个参数是用户在使用set、add命令存储一条数据时输入的参数。  
    //nkey是key字符串的长度。nbytes则是用户要存储的data长度+2,因为在data的结尾处还要加上"\r\n"  
    //cur_hv则是根据键值key计算得到的哈希值。  
    item *do_item_alloc(char *key, const size_t nkey, const int flags,  
                        const rel_time_t exptime, const int nbytes,  
                        const uint32_t cur_hv) {  
        uint8_t nsuffix;  
        item *it = NULL;  
        char suffix[40];  
        //要存储这个item需要的总空间。要注意第一个参数是nkey+1,所以上面的那些宏计算时  
        //使用了(item)->nkey + 1  
        size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix);  
        if (settings.use_cas) {//开启了CAS功能  
            ntotal += sizeof(uint64_t);  
        }  
      
        //根据大小判断从属于哪个slab  
        unsigned int id = slabs_clsid(ntotal);  
        if (id == 0)//0表示不属于任何一个slab  
            return 0;  
          
        ...  
      
        it = slabs_alloc(ntotal, id);//从slab分配器中申请内存  
        it->refcount = 1;  
        it->it_flags = settings.use_cas ? ITEM_CAS : 0;  
        it->nkey = nkey;  
        it->nbytes = nbytes;  
        memcpy(ITEM_key(it), key, nkey);//这里只拷贝nkey个字节,最后一个字节空着  
        it->exptime = exptime;  
        memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);  
        it->nsuffix = nsuffix;  
      
        return it;  
    }  
    

    实践出真知

    图2查询key=emp4900001的value字节长度,key占用10字节,value占用93字节。从上面的公式计算item.length = 48 + 8 + 10 + 1 + (" 1 93\r\n").length + 93 + 2 = 169 Bytes。

    图2 查询key的value的字节长度

    通过图3的mem_requested和used_chunks计算item.length=59066176/349504=169 Bytes,证明从源码分析item长度正确。

    图 3 stats slabs命令结果

    相关文章

      网友评论

          本文标题:[Memcached] Slab Allocation的MC项占

          本文链接:https://www.haomeiwen.com/subject/wikkhxtx.html