memched1.0源码阅读(2)——基础数据结构
2016-05-30 20:13
513 查看
stats 这个结构表示memched的状态,例如读写的字节数、启动时间等等信息
settings 表示启动时的相关设置,例如监听的ip和端口等等
_stritem/item 表示memched存储的一个对象,它始终在内存中
conn 结构表示一个套接字会话,存储了一个套接字的相关信息
conn_states 枚举表示套接字对话的状态
slabclass_t 表示slabs内存分配器。它有一个属性叫做size,它表示了这个slab只分配大小为size的内存块,每一个slab都预先分配好一定量的内存块,然后放在slab中。所有的item对象的内存都从slab中分配内存,首先会根据item需要的内存的大小,计算出对应的slab的id(或者类别),然后找到对应的slab分配内存。
// 状态 struct stats { // 当前的对象的个数 unsigned int curr_items; // 总的对象的个数 unsigned int total_items; // 当前的占用的字节数量 unsigned long long curr_bytes; // 当前的连接数 unsigned int curr_conns; // 总的连接数 unsigned int total_conns; // 当前的套接字会话的数量 unsigned int conn_structs; // get命令的个数 unsigned int get_cmds; // set命令的个数 unsigned int set_cmds; // get命中的次数 unsigned int get_hits; // gei失败的次数(未命中) unsigned int get_misses; // 启动的时间 time_t started; /* when the process was started */ // 读取了多少字节 unsigned long long bytes_read; // 写入了多少字节 unsigned long long bytes_written; };
settings 表示启动时的相关设置,例如监听的ip和端口等等
// 设置 struct settings { // memched占用的最大字节数 unsigned long long maxbytes; // 对象的最大个数 int maxitems; // 连接的最大数量 int maxconns; // 监听的端口 int port; // 监听地址 struct in_addr interface; // 其他的一些命令 int verbose; };
_stritem/item 表示memched存储的一个对象,它始终在内存中
// 内存对象,所有的对象使用链表连接起来 typedef struct _stritem { struct _stritem *next; struct _stritem *prev; // 引用计数 int refcount; // 对象的标志 int it_flags; // key char *key; // 数据 void *data; // 当前item的类型(根据item占用内存的大小进行分类,从slabs中分配和回收内存的时候需要用到) unsigned int slabs_clsid; // 数据的大小 int nbytes; /* size of data */ // 总的大小(键加值占用的字节数) int ntotal; /* size of this struct + key + data */ // 标志 int flags; // 上一次被访问的时间 time_t time; /* least recent access */ // 已经在内存中多少时间了 time_t exptime; /* expire time */ // 数据 void * end[0]; } item;
conn 结构表示一个套接字会话,存储了一个套接字的相关信息
// 套接字会话 typedef struct { // 文件描述符 int sfd; // 状态 int state; // 事件 struct event event; // 事件标志 short ev_flags; // 正在触发的事件 short which; /* which events were just triggered */ // 读缓存 char *rbuf; // 读缓存的大小 int rsize; // 当前的有效字节数 int rbytes; // 写缓存 char *wbuf; // 当前的写位置 char *wcurr; int wsize; int wbytes; int write_and_go; /* which state to go into after finishing current write */ void *write_and_free; /* free this memory after finishing writing */ char *rcurr; int rlbytes; /* data for the nread state */ /* * item is used to hold an item structure created after reading the command * line of set/add/replace commands, but before we finished reading the actual * data. The data is read into item->data to avoid extra copying. */ // 当前处理的item void *item; /* for commands set/add/replace */ // 对item的操作(设置、新增、替换) int item_comm; /* which one is it: set/add/replace */ /* data for the swallow state */ // 被丢弃字节的数量(例如一些错误的信息需要丢弃) int sbytes; /* how many bytes to swallow */ /* data for the mwrite state */ // 被当前套接字处理的item数组 item **ilist; /* list of items to write out */ // 上述数组的大小 int isize; // 指向当前处理的item? item **icurr; // 剩余item的数量 int ileft; int ipart; /* 1 if we're writing a VALUE line, 2 if we're writing data */ char ibuf[256]; /* for VALUE lines */ char *iptr; int ibytes; } conn;
conn_states 枚举表示套接字对话的状态
// 套接字会话的状态 enum conn_states { // 正在监听 conn_listening, /* the socket which listens for connections */ // 正在读取 conn_read, /* reading in a command line */ // 正在写入 conn_write, /* writing out a simple response */ // 读取固定数量的字节 conn_nread, /* reading in a fixed number of bytes */ conn_swallow, /* swallowing unnecessary bytes w/o storing */ // 正在关闭 conn_closing, /* closing this connection */ // 顺序写 conn_mwrite /* writing out many items sequentially */ };
slabclass_t 表示slabs内存分配器。它有一个属性叫做size,它表示了这个slab只分配大小为size的内存块,每一个slab都预先分配好一定量的内存块,然后放在slab中。所有的item对象的内存都从slab中分配内存,首先会根据item需要的内存的大小,计算出对应的slab的id(或者类别),然后找到对应的slab分配内存。
// slabs对象 // memcached管理的对象叫做item,它只管管理这些对象是否可用,新增,删除等待,对于这些item占用的内存则由slabs管理 // slabs对于它分配的每一块内存都当作一个对象 typedef struct { // 这个对象的大小 unsigned int size; /* sizes of items */ // 有多少个item在slabs中 unsigned int perslab; /* how many items per slab */ // item对象的指针数组 void **slots; /* list of item ptrs */ unsigned int sl_total; /* size of previous array */ unsigned int sl_curr; /* first free slot */ // 当前类型的slabs分配了多少个对象 unsigned int slabs; /* how many slabs were allocated for this class */ } slabclass_t;
相关文章推荐
- JavaScript实现数据结构中的队列和堆栈
- 大话数据结构:线性表(1)
- 数据结构上机题目
- 边界表示的数据结构
- 数据结构-递归
- 【数据结构】哈希表的线性探测算法
- 【数据结构】处理哈希冲突的开链法(哈希桶)算法实现
- 【数据结构】位图BitMap、布隆过滤器的算法实现
- 【数据结构】两个队列实现一个栈
- 【数据结构】常用比较排序算法(包括:选择排序,堆排序,冒泡排序,选择排序,快速排序,归并排序)
- 【数据结构】大量数据(20万)的快速排序的递归与非递归算法、三数取中思想
- 【数据结构】非比较排序的算法实现(包括计数排序、计数排序)
- 【数据结构】使用栈Stack解决迷宫问题
- 【数据结构】(面试题)使用两个栈实现一个队列(详细介绍)
- 【数据结构】对称矩阵及对称矩阵的压缩存储
- 【数据结构】稀疏结构及稀疏矩阵的压缩存储,矩阵的(快速)转置
- 【数据结构】广义表的默认成员函数、深度、大小、打印
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- 【数据结构】二叉树(前、中、后)序遍历的递归与非递归算法
- 【数据结构】堆的实现(包括:默认成员函数,插元素push,删元素pop,访问根节点top,判空,大小)