Nginx缓冲区
Nginx 缓冲区设计
- 内存同样使用内存池进行分配
-
buffer
通过链表连接起来,不用了就回收到内存池的chain
-
buffer
结构体通过位域进行标识,节省内存空间
Nginx 缓冲区结构体组成
struct ngx_buf_s {
u_char *pos;
u_char *last;
off_t file_pos;
off_t file_last;
u_char *start; /* start of buffer */
u_char *end; /* end of buffer */
ngx_buf_tag_t tag;
ngx_file_t *file;
ngx_buf_t *shadow;
/* the buf's content could be changed */
unsigned temporary:1;
/*
* the buf's content is in a memory cache or in a read only memory
* and must not be changed
*/
unsigned memory:1;
/* the buf's content is mmap()ed and must not be changed */
unsigned mmap:1;
unsigned recycled:1;
unsigned in_file:1;
unsigned flush:1;
unsigned sync:1;
unsigned last_buf:1;
unsigned last_in_chain:1;
unsigned last_shadow:1;
unsigned temp_file:1;
/* STUB */ int num;
};
- nginx buffer 不仅可以作为内存缓冲,还可以作为磁盘缓冲用于处理文件,可以通过位域标识判断是否是内存缓冲
#define ngx_buf_in_memory(b) ((b)->temporary || (b)->memory || (b)->mmap)
#define ngx_buf_in_memory_only(b) (ngx_buf_in_memory(b) && !(b)->in_file)
-
pos
表示当前处理到的位置,last
则是需要处理到的位置,因此获取当前缓冲区需要处理的数据大小可以通过如下方式计算
#define ngx_buf_size(b) \
(ngx_buf_in_memory(b) ? (off_t) ((b)->last - (b)->pos): \
((b)->file_last - (b)->file_pos))
1. 创建临时缓冲
- 创建指定大小的内存缓冲区,标志为可写
- 注意
buffer
结构体需要初始化,初始时pos
和last
都指向start
ngx_buf_t *
ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
{
ngx_buf_t *b;
b = ngx_calloc_buf(pool);
if (b == NULL) {
return NULL;
}
b->start = ngx_palloc(pool, size);
if (b->start == NULL) {
return NULL;
}
/*
* set by ngx_calloc_buf():
*
* b->file_pos = 0;
* b->file_last = 0;
* b->file = NULL;
* b->shadow = NULL;
* b->tag = 0;
* and flags
*/
b->pos = b->start;
b->last = b->start;
b->end = b->last + size;
b->temporary = 1;
return b;
}
2. 获取一个缓冲链
- 缓冲链就是将多个
buffer
以链表的形式连接起来,形成更大的缓冲区,结构如下
struct ngx_chain_s {
ngx_buf_t *buf;
ngx_chain_t *next;
};
- 获取时首先尝试从内存池拿,如果没有就分配一个新的,注意这里只是单纯获取缓冲链,并没有设置
buffer
ngx_chain_t *
ngx_alloc_chain_link(ngx_pool_t *pool)
{
ngx_chain_t *cl;
cl = pool->chain;
if (cl) {
pool->chain = cl->next;
return cl;
}
cl = ngx_palloc(pool, sizeof(ngx_chain_t));
if (cl == NULL) {
return NULL;
}
return cl;
}
3. 获取带缓冲区的缓冲链
- 获取分配了
bufs->num
个大小为bufs->size
的buffer
的缓冲链 - 先分配
bufs->num * bufs->size
大小的内存块,然后再分配buffer
结构体,设置指针初始位置 - 最后获取
chain
(优先获取内存池中空闲的)
ngx_chain_t *
ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs)
{
u_char *p;
ngx_int_t i;
ngx_buf_t *b;
ngx_chain_t *chain, *cl, **ll;
p = ngx_palloc(pool, bufs->num * bufs->size);
if (p == NULL) {
return NULL;
}
ll = &chain;
for (i = 0; i < bufs->num; i++) {
b = ngx_calloc_buf(pool);
if (b == NULL) {
return NULL;
}
/*
* set by ngx_calloc_buf():
*
* b->file_pos = 0;
* b->file_last = 0;
* b->file = NULL;
* b->shadow = NULL;
* b->tag = 0;
* and flags
*
*/
b->pos = p;
b->last = p;
b->temporary = 1;
b->start = p;
p += bufs->size;
b->end = p;
cl = ngx_alloc_chain_link(pool);
if (cl == NULL) {
return NULL;
}
cl->buf = b;
*ll = cl;
ll = &cl->next;
}
*ll = NULL;
return chain;
}
4. 拷贝一条缓冲链到另一条的末尾
- 注意拷贝的只是
chain
,即chain
指向的buffer
没有拷贝,属于浅拷贝
ngx_int_t
ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in)
{
ngx_chain_t *cl, **ll;
ll = chain;
for (cl = *chain; cl; cl = cl->next) {
ll = &cl->next;
}
while (in) {
cl = ngx_alloc_chain_link(pool);
if (cl == NULL) {
*ll = NULL;
return NGX_ERROR;
}
cl->buf = in->buf;
*ll = cl;
ll = &cl->next;
in = in->next;
}
*ll = NULL;
return NGX_OK;
}
5. 尝试从空闲缓冲链中获取一个带缓冲区的缓冲链
- 如果
free
缓冲链不为空,则取第一个chain
返回即可 - 如果为空,则先获取一个缓冲链,然后为其分配一个
buffer
结构体
ngx_chain_t *
ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free)
{
ngx_chain_t *cl;
if (*free) {
cl = *free;
*free = cl->next;
cl->next = NULL;
return cl;
}
cl = ngx_alloc_chain_link(p);
if (cl == NULL) {
return NULL;
}
cl->buf = ngx_calloc_buf(p);
if (cl->buf == NULL) {
return NULL;
}
cl->next = NULL;
return cl;
}
6. 更新缓冲链
- 如果提供的
out
非空,将out
缓冲链连接到busy
的尾部 - 将
busy
缓冲链中与指定tag
匹配且没有待处理数据的buffer
放到给定的free
缓冲链中,如果不匹配则由内存池回收 - 若当前
buffer
存在未处理的内容,则停止更新
void
ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free, ngx_chain_t **busy,
ngx_chain_t **out, ngx_buf_tag_t tag)
{
ngx_chain_t *cl;
if (*out) {
if (*busy == NULL) {
*busy = *out;
} else {
for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
cl->next = *out;
}
*out = NULL;
}
while (*busy) {
cl = *busy;
if (cl->buf->tag != tag) {
*busy = cl->next;
ngx_free_chain(p, cl);
continue;
}
if (ngx_buf_size(cl->buf) != 0) {
break;
}
cl->buf->pos = cl->buf->start;
cl->buf->last = cl->buf->start;
*busy = cl->next;
cl->next = *free;
*free = cl;
}
}
7. 获取文件缓冲链待处理缓冲区合并的大小
- 遍历文件缓冲链,计算当前缓冲区的大小
- 累加缓冲区大小,直到遍历完链表或者总和即将超过给定
limit
时结束 - 移动
in
指针的位置到下一个未处理的节点
off_t
ngx_chain_coalesce_file(ngx_chain_t **in, off_t limit)
{
off_t total, size, aligned, fprev;
ngx_fd_t fd;
ngx_chain_t *cl;
total = 0;
cl = *in;
fd = cl->buf->file->fd;
do {
size = cl->buf->file_last - cl->buf->file_pos;
if (size > limit - total) {
size = limit - total;
aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
& ~((off_t) ngx_pagesize - 1);
if (aligned <= cl->buf->file_last) {
size = aligned - cl->buf->file_pos;
}
total += size;
break;
}
total += size;
fprev = cl->buf->file_pos + size;
cl = cl->next;
} while (cl
&& cl->buf->in_file
&& total < limit
&& fd == cl->buf->file->fd
&& fprev == cl->buf->file_pos);
*in = cl;
return total;
}
8. 根据发送字节数更新缓冲链
- 遍历缓冲链,计算当前缓冲链的待处理缓冲区大小,大于
sent
则移动指针,然后继续遍历 - 小于等于
sent
则直接将pos
指向last
表示这段缓冲区没有待处理的数据了 - 注意需要判断一下当前缓冲区是表示的内存还是文件
- 最后返回
in
指针,其指向下一个待更新的缓冲链节点
ngx_chain_t *
ngx_chain_update_sent(ngx_chain_t *in, off_t sent)
{
off_t size;
for ( /* void */ ; in; in = in->next) {
if (ngx_buf_special(in->buf)) {
continue;
}
if (sent == 0) {
break;
}
size = ngx_buf_size(in->buf);
if (sent >= size) {
sent -= size;
if (ngx_buf_in_memory(in->buf)) {
in->buf->pos = in->buf->last;
}
if (in->buf->in_file) {
in->buf->file_pos = in->buf->file_last;
}
continue;
}
if (ngx_buf_in_memory(in->buf)) {
in->buf->pos += (size_t) sent;
}
if (in->buf->in_file) {
in->buf->file_pos += sent;
}
break;
}
return in;
}
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.