1、Memcached源码解析 启动参数 m指定最大使用内存大小 默认64MB t线程数 默认4 l绑定地址 默认 所有都允许 无论内外网或者本机更换IP 有安全隐患 若设置为127 0 0 1就只能本机访问 m最大内存使用 单位MB 默认64MB M内存耗尽时返回错误 而不是删除项 c最大同时连接数 默认是1024 f块大小增长因子 默认是1 25 n最小分配空间 key value flags默认是48 k锁定所有内存页 注意你可以锁定的内存上限 p指定端口号 默认11211 UUDP监听端口 默认 11211 0时关闭 LTrytouselargememorypages 启动 setting
2、s init 初始化setting从启动命令中读取设置hash init 根据hash type 选择相应hash函数 默认是jenkinshashinit sasl todomain base event init initializemainthreadlibeventinstancestats init 统计数据初始化assoc init settings hashpower init 初始化hashtable 默认hashpower为16conn init connection初始化slabs init settings maxbytes settings factor preallo
3、cate thread init settings num threads main base init lru crawler 数据结构 slabcalss 1 slabclass 2 size96B perslab10922 slab list slabs list size slots sl curr slab list slab1 slab2 slab3 slab4 slab5 slabn chunk1 chunk2 chunk n 3 chunk n 2 chunk n 1 chunkn chunk1 chunk2 chunk3 chunk4 chunk5 chunk 空闲链表的头
4、null hashtable h next slabcalss 1 slabclass 2 size96B perslab10922 slab list slabs list size slots sl curr slab1 slab2 slab3 slab4 slab5 slabn chunk1 chunk2 chunk3 chunk4 chunk5 chunk6 chunk7 chunk8 chunk n 4 chunk n 3 chunk n 2 chunk n 1 chunkn 空闲链表的头 hashtable heads 1 tails 1 橙色线代表空闲链表的双向链接蓝色线代表LR
5、U链表的双向链接 heads 1 记录了LRU链表头tails 1 记录了LRU链表尾部 64B slab结构 slabclass t typedefstruct unsignedintsize sizesofitems unsignedintperslab howmanyitemsperslab void slots listofitemptrs unsignedintsl curr totalfreeitemsinlist unsignedintslabs howmanyslabswereallocatedforthisclass void slab list arrayofslabpoi
6、nters unsignedintlist size sizeofprevarray unsignedintkilling index 1ofdyingslab orzeroifnone size trequested Thenumberofrequestedbytes slabclass t slab相关的默认参数 definePOWER SMALLEST1 definePOWER LARGEST200 defineCHUNK ALIGN BYTES8 defineMAX NUMBER OF SLAB CLASSES POWER LARGEST 1 slab init limit 使用的内存
7、大小 factor 增长因子 prealloc 是否预先分配voidslabs init constsize tlimit constdoublefactor constboolprealloc 1 若是预先分配 先malloc一块大内存2 计算每个slabclass的size大小 最小的size为sizeof item settings chunk size 48B 然后根据factor 默认1 25 增长 直到item size max 1MB 3 调用slabs preallocate为每个slabclasspre allocatea1MBslab 然后将这1MB内存根据相应的slabc
8、lass大小 分为size大小的slab 并加到freelist中 libevent hash表 初始化assoc init primary hashtable2 16插入assoc inisert item it constuint32 thv 查找assoc find constchar key constsize tnkey constuint32 thv 删除assoc delete constchar key constsize tnkey constuint32 thv expanding且未扩容到当前bulket时访问old 其它访问primary hash表扩容 在insert
9、的时候 发现hash items hashsize hashpower 3 2 assoc start expand void 唤醒assoc maintenance thread switch item lock type ITEM LOCK GLOBAL 锁粒度变大mutex lock item typedefstruct stritem struct stritem next struct stritem prev struct stritem h next hashchainnext rel time ttime leastrecentaccess rel time texptime
10、expiretime intnbytes sizeofdata unsignedshortrefcount uint8 tnsuffix lengthofflags and lengthstring uint8 tit flags ITEM above uint8 tslabs clsid whichslabclasswe rein uint8 tnkey keylength w terminatingnullandpadding thisoddtypepreventstype punningissueswhenwedo thelittleshuffletosavespacewhennotus
11、ingCAS union uint64 tcas charend data ifit flags set过程 drive machine conn c 1conn read2conn parse cmdtry read command c process command conn c char command process update command item alloc key nkey flags realtime exptime vlen 将key value存到相应的chunk里面去3conn nreadcomplete nread c complete nread binary c complete update bin c store item it c cmd c do store item item comm c hv 和hashtable关联起来 item alloc 制作头部item make header 由这里可以推出item的存储格式 itemstruct key 1 suffix value r n根据获得的大小 寻找匹配的slabclass获得cache锁mutex lock 从相应的lru链表末尾开始尝试 检查5次是否有过期的数据 get过程