您的位置:首页 > 理论基础 > 数据结构算法

memched1.0源码阅读(2)——基础数据结构

2016-05-30 20:13 513 查看
stats 这个结构表示memched的状态,例如读写的字节数、启动时间等等信息
// 状态
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: