Nginx 内存池刨析
实例源码刨析
#define BLOCK_SIZE 16 //每次分配内存块大小
#define MEM_POOL_SIZE (1024 * 4) //内存池每块大小
int i = 0, k = 0;
int use_free = 0;
ngx_pagesize = getpagesize();//获取系统的页大小
//printf("pagesize: %zu\n",ngx_pagesize);
char * ptr = NULL;
for(k = 0; k< 1024 * 500; k++)
{
ngx_pool_t * mem_pool = ngx_create_pool(MEM_POOL_SIZE);//创建池
for(i = 0; i < 1024 ; i++)
{
ptr = ngx_palloc(mem_pool,BLOCK_SIZE);
if(!ptr) fprintf(stderr,"ngx_palloc failed. \n");
else {
*ptr = '\0';
*(ptr + BLOCK_SIZE -1) = '\0';
}
}
ngx_destroy_pool(mem_pool);
}
一些函数和宏定义
#define NGX_ALIGNMENT sizeof(unsigned long) /* platform word 平台字节长*/
#define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1)) /* b对齐为a的倍数*/
#define ngx_align_ptr(p, a) \
(u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))/*用于将指针 p 对齐到指定的对齐字节数 a*/
#define ngx_memzero(buf, n) (void) memset(buf, 0, n)
ngx_create_pool
创建线程池
ngx_pool_t * ngx_create_pool(size_t size)
{
ngx_pool_t *p;
p = ngx_memalign(NGX_POOL_ALIGNMENT, size);
if (p == NULL) {
return NULL;
}
//它将 p 指针向后偏移 sizeof(ngx_pool_t) 字节,这样 p->d.last 就指向了内存块中除去 ngx_pool_t 结构体大小后的位置,即可用于分配的起始位置。
//将 p 指向的内存块的 last 成员设置为指向当前内存块后面的一段内存的起始地址。
//这段内存的大小为 sizeof(ngx_pool_t),即 ngx_pool_t 结构体的大小。
//这样做是为了在这块内存中分配其他数据时,可以从这段内存的末尾开始分配,确保内存块的内存布局是连续的。
p->d.last = (u_char *) p + sizeof(ngx_pool_t);//指向这块内存已使用区域的最后部分。
//指向未使用区域的尾部
p->d.end = (u_char *) p + size;
//下一块内存块的地址
p->d.next = NULL;
//内存分配的失败次数
p->d.failed = 0;
//这行代码确定了内存池中可用于分配的最大内存大小。为了确保内存分配不会超出预先设定的阈值,
size = size - sizeof(ngx_pool_t);
p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
// 当前正在使用的数据块的指针
p->current = p;
//p->chain = NULL;
p->large = NULL;
//p->cleanup = NULL;
//p->log = log;
return p;
}
ngx_memalign
void * ngx_memalign(size_t alignment, size_t size)
{
void *p;
int err;
err = posix_memalign(&p, alignment, size);//按照指定的对齐要求和大小分配内存块。
if (err) {
fprintf(stderr,"posix_memalign(%zu, %zu) failed", alignment, size);
p = NULL;
}
if(debug) fprintf(stderr,"posix_memalign: %p:%zu @%zu", p, size, alignment);
return p;
}
ngx_palloc分配内存
void *ngx_palloc(ngx_pool_t *pool, size_t size)
{
//判断是分配内存池的内存还是单独分配一块内存位于大储存块区。
#if !(NGX_DEBUG_PALLOC)
if (size <= pool->max) {
return ngx_palloc_small(pool, size, 1);
}
#endif
return ngx_palloc_large(pool, size);
}
static inline void *ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
{
u_char *m;
ngx_pool_t *p;
p = pool->current;
do {
m = p->d.last;
if (align) {
m = ngx_align_ptr(m, NGX_ALIGNMENT);
}
if ((size_t) (p->d.end - m) >= size) {
p->d.last = m + size;
return m;
}
p = p->d.next;
} while (p);
return ngx_palloc_block(pool, size);
}
- 定义一个指向内存池结构的指针p。
- 将当前内存池的指针赋值给p。
- 使用一个do-while循环来遍历内存池链表。
- 获取当前内存池的最后一个内存块的指针m。
- 如果align为真,则将m对齐到NGX_ALIGNMENT字节边界。
- 检查当前内存池中是否有足够的空间来分配size大小的内存块。如果有足够的空间,将内存块的指针m设置为分配内存块的起始地址,并将内存池的最后一个内存块的指针更新为m+size。然后返回m。
- 如果内存池中没有足够的空间,则将p指向下一个内存池,继续遍历内存池链表。
- 如果遍历完内存池链表仍然没有找到足够的空间,则调用ngx_palloc_block函数分配一个新的内存块,并将新分配的内存块的指针返回。
ngx_palloc_large
static void *ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_uint_t n;
ngx_pool_large_t *large;
p = ngx_alloc(size);
if (p == NULL) {
return NULL;
}
n = 0;
for (large = pool->large; large; large = large->next) {
if (large->alloc == NULL) {
large->alloc = p;
return p;
}
if (n++ > 3) {
break;
}
}
large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
ngx_free(p);
return NULL;
}
large->alloc = p;
large->next = pool->large;
pool->large = large;
return p;
}
- 首先,尝试使用
ngx_alloc
函数分配一块大小为size
的内存。如果分配成功,则将该内存块的指针赋值给p
。- 然后,遍历内存池中的
large
链表,查找一个alloc
字段为NULL
的节点。如果找到,则将分配的内存块的指针赋值给该节点的alloc
字段,并返回该内存块的指针。- 如果遍历了
large
链表3次仍然没有找到合适的节点,则跳出循环。- 分配一个新的
ngx_pool_large_t
结构体,将其alloc
字段赋值为分配的内存块的指针,并将该结构体添加到内存池的large
链表中。- 返回分配的内存块的指针。
函数
ngx_palloc_large
的用途是在Nginx的内存池中分配大块内存。在Nginx中,大块内存通常用于存储文件句柄、连接池等。注意事项:
- 在使用
ngx_palloc_large
分配内存后,需要确保在适当的时候释放内存,以避免内存泄漏。- 在使用
ngx_palloc_large
分配内存时,如果内存池中的large
链表已经很长,可能会导致内存分配效率降低。因此,在分配大块内存时,可以考虑使用其他内存分配机制,如ngx_palloc
函数。
void * ngx_alloc(size_t size)
{
void *p;
p = malloc(size);
if (p == NULL) {
fprintf(stderr,"malloc(%zu) failed", size);
}
if(debug) fprintf(stderr, "malloc: %p:%zu", p, size);
return p;
}
void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
void *p;
ngx_pool_large_t *large;
p = ngx_memalign(alignment, size);
if (p == NULL) {
return NULL;
}
large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
ngx_free(p);
return NULL;
}
large->alloc = p;
large->next = pool->large;
pool->large = large;
return p;
}
- 使用
ngx_memalign
函数尝试分配一块大小为size
且对齐为alignment
的内存。如果分配失败,直接返回NULL
。- 使用
ngx_palloc_small
函数在内存池中分配一个ngx_pool_large_t
结构体的空间,用于存储分配的内存块信息。如果分配失败,释放之前分配的内存,并返回NULL
。- 将分配的内存块的指针存入
ngx_pool_large_t
结构体的alloc
字段,并将该结构体添加到内存池的large
链表中。- 返回分配的内存块的指针。
void * ngx_pcalloc(ngx_pool_t *pool, size_t size)
{
void *p;
p = ngx_palloc(pool, size);
if (p) {
ngx_memzero(p, size);
}
return p;
}
- 首先,通过
ngx_palloc
函数尝试在内存池中分配一块大小为size
的内存。如果分配成功,则将返回的指针赋值给p
。- 如果
p
不为空,使用ngx_memzero
函数将p
指向的内存块初始化为全零。ngx_memzero
函数的作用是将一块内存块的所有字节设置为零,这在初始化内存时非常有用,可以确保内存中的所有字节都被初始化为零,从而避免未初始化的内存导致的错误。- 最后,返回
p
,即分配的内存块的指针。
源码部分
#include "mem_core.h"
#define BLOCK_SIZE 16 //每次分配内存块大小
#define MEM_POOL_SIZE (1024 * 4) //内存池每块大小
int main(int argc, char **argv)
{
int i = 0, k = 0;
int use_free = 0;
ngx_pagesize = getpagesize();
//printf("pagesize: %zu\n",ngx_pagesize);
if(argc >= 2){
use_free = 1;
printf("use malloc/free\n");
} else {
printf("use mempool.\n");
}
if(!use_free){
char * ptr = NULL;
for(k = 0; k< 1024 * 500; k++)
{
ngx_pool_t * mem_pool = ngx_create_pool(MEM_POOL_SIZE);
for(i = 0; i < 1024 ; i++)
{
ptr = ngx_palloc(mem_pool,BLOCK_SIZE);
if(!ptr) fprintf(stderr,"ngx_palloc failed. \n");
else {
*ptr = '\0';
*(ptr + BLOCK_SIZE -1) = '\0';
}
}
ngx_destroy_pool(mem_pool);
}
} else {
char * ptr[1024];
for(k = 0; k< 1024 * 500; k++){
for(i = 0; i < 1024 ; i++)
{
ptr[i] = malloc(BLOCK_SIZE);
if(!ptr[i]) fprintf(stderr,"malloc failed. reason:%s\n",strerror(errno));
else{
*ptr[i] = '\0';
*(ptr[i] + BLOCK_SIZE - 1) = '\0';
}
}
for(i = 0; i < 1024 ; i++){
if(ptr[i]) free(ptr[i]);
}
}
}
return 0;
}
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include "mem_core.h"
static int debug = 0;
ngx_uint_t ngx_pagesize;
ngx_uint_t ngx_pagesize_shift;
ngx_uint_t ngx_cacheline_size;
void *
ngx_alloc(size_t size)
{
void *p;
p = malloc(size);
if (p == NULL) {
fprintf(stderr,"malloc(%zu) failed", size);
}
if(debug) fprintf(stderr, "malloc: %p:%zu", p, size);
return p;
}
void *
ngx_calloc(size_t size)
{
void *p;
p = ngx_alloc(size);
if (p) {
ngx_memzero(p, size);
}
return p;
}
/*
#if (NGX_HAVE_POSIX_MEMALIGN)
void *
ngx_memalign(size_t alignment, size_t size)
{
void *p;
int err;
err = posix_memalign(&p, alignment, size);
if (err) {
fprintf(stderr,"posix_memalign(%zu, %zu) failed", alignment, size);
p = NULL;
}
if(debug) fprintf(stderr,"posix_memalign: %p:%zu @%zu", p, size, alignment);
return p;
}
#elif (NGX_HAVE_MEMALIGN)
void *
ngx_memalign(size_t alignment, size_t size)
{
void *p;
p = memalign(alignment, size);
if (p == NULL) {
fprintf(stderr,"memalign(%zu, %zu) failed", alignment, size);
}
if(debug) fprintf(stderr,"memalign: %p:%zu @%zu", p, size, alignment);
return p;
}
#endif
*/
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_ALLOC_H_INCLUDED_
#define _NGX_ALLOC_H_INCLUDED_
#include "mem_core.h"
void *ngx_alloc(size_t size);
void *ngx_calloc(size_t size);
#define ngx_free free
/*
* Linux has memalign() or posix_memalign()
* Solaris has memalign()
* FreeBSD 7.0 has posix_memalign(), besides, early version's malloc()
* aligns allocations bigger than page size at the page boundary
*/
/*#if (NGX_HAVE_POSIX_MEMALIGN || NGX_HAVE_MEMALIGN)
void *ngx_memalign(size_t alignment, size_t size);
#else
*/
#define ngx_memalign(alignment, size) ngx_alloc(size)
/*
#endif
*/
extern ngx_uint_t ngx_pagesize;
extern ngx_uint_t ngx_pagesize_shift;
extern ngx_uint_t ngx_cacheline_size;
#endif /* _NGX_ALLOC_H_INCLUDED_ */
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_CORE_H_INCLUDED_
#define _NGX_CORE_H_INCLUDED_
#define NGX_HAVE_POSIX_MEMALIGN 1
typedef struct ngx_pool_s ngx_pool_t;
#define NGX_OK 0
#define NGX_ERROR -1
#define NGX_AGAIN -2
#define NGX_BUSY -3
#define NGX_DONE -4
#define NGX_DECLINED -5
#define NGX_ABORT -6
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <sys/stat.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
typedef intptr_t ngx_int_t;
typedef uintptr_t ngx_uint_t;
#define NGX_ALIGNMENT sizeof(unsigned long) /* platform word */
#define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1))
#define ngx_align_ptr(p, a) \
(u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
#define ngx_memzero(buf, n) (void) memset(buf, 0, n)
#include "mem_alloc.h"
#include "mem_pool_palloc.h"
#endif /* _NGX_CORE_H_INCLUDED_ */
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include "mem_core.h"
static inline void *ngx_palloc_small(ngx_pool_t *pool, size_t size,
ngx_uint_t align);
static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);
ngx_pool_t *
ngx_create_pool(size_t size)
{
ngx_pool_t *p;
p = ngx_memalign(NGX_POOL_ALIGNMENT, size);
if (p == NULL) {
return NULL;
}
p->d.last = (u_char *) p + sizeof(ngx_pool_t);
p->d.end = (u_char *) p + size;
p->d.next = NULL;
p->d.failed = 0;
size = size - sizeof(ngx_pool_t);
p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
p->current = p;
//p->chain = NULL;
p->large = NULL;
//p->cleanup = NULL;
//p->log = log;
return p;
}
void
ngx_destroy_pool(ngx_pool_t *pool)
{
ngx_pool_t *p, *n;
ngx_pool_large_t *l;
//ngx_pool_cleanup_t *c;
/*for (c = pool->cleanup; c; c = c->next) {
if (c->handler) {
ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
"run cleanup: %p", c);
c->handler(c->data);
}
}*/
#if (NGX_DEBUG)
/*
* we could allocate the pool->log from this pool
* so we cannot use this log while free()ing the pool
*/
for (l = pool->large; l; l = l->next) {
fprintf(stderr,"free: %p", l->alloc);
}
for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
fprintf(stderr,"free: %p, unused: %zu", p, p->d.end - p->d.last);
if (n == NULL) {
break;
}
}
#endif
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
ngx_free(l->alloc);
}
}
for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
ngx_free(p);
if (n == NULL) {
break;
}
}
}
void
ngx_reset_pool(ngx_pool_t *pool)
{
ngx_pool_t *p;
ngx_pool_large_t *l;
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
ngx_free(l->alloc);
}
}
for (p = pool; p; p = p->d.next) {
p->d.last = (u_char *) p + sizeof(ngx_pool_t);
p->d.failed = 0;
}
pool->current = pool;
//pool->chain = NULL;
pool->large = NULL;
}
void *ngx_palloc(ngx_pool_t *pool, size_t size)
{
#if !(NGX_DEBUG_PALLOC)
if (size <= pool->max) {
return ngx_palloc_small(pool, size, 1);
}
#endif
return ngx_palloc_large(pool, size);
}
void *
ngx_pnalloc(ngx_pool_t *pool, size_t size)
{
#if !(NGX_DEBUG_PALLOC)
if (size <= pool->max) {
return ngx_palloc_small(pool, size, 0);
}
#endif
return ngx_palloc_large(pool, size);
}
static inline void *
ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
{
u_char *m;
ngx_pool_t *p;
p = pool->current;
do {
m = p->d.last;
if (align) {
m = ngx_align_ptr(m, NGX_ALIGNMENT);
}
if ((size_t) (p->d.end - m) >= size) {
p->d.last = m + size;
return m;
}
p = p->d.next;
} while (p);
return ngx_palloc_block(pool, size);
}
static void *
ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
u_char *m;
size_t psize;
ngx_pool_t *p, *new;
psize = (size_t) (pool->d.end - (u_char *) pool);
m = ngx_memalign(NGX_POOL_ALIGNMENT, psize);
if (m == NULL) {
return NULL;
}
new = (ngx_pool_t *) m;
new->d.end = m + psize;
new->d.next = NULL;
new->d.failed = 0;
m += sizeof(ngx_pool_data_t);
m = ngx_align_ptr(m, NGX_ALIGNMENT);
new->d.last = m + size;
for (p = pool->current; p->d.next; p = p->d.next) {
if (p->d.failed++ > 4) {
pool->current = p->d.next;
}
}
p->d.next = new;
return m;
}
static void *
ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_uint_t n;
ngx_pool_large_t *large;
p = ngx_alloc(size);
if (p == NULL) {
return NULL;
}
n = 0;
for (large = pool->large; large; large = large->next) {
if (large->alloc == NULL) {
large->alloc = p;
return p;
}
if (n++ > 3) {
break;
}
}
large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
ngx_free(p);
return NULL;
}
large->alloc = p;
large->next = pool->large;
pool->large = large;
return p;
}
void *
ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
void *p;
ngx_pool_large_t *large;
p = ngx_memalign(alignment, size);
if (p == NULL) {
return NULL;
}
large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
ngx_free(p);
return NULL;
}
large->alloc = p;
large->next = pool->large;
pool->large = large;
return p;
}
ngx_int_t
ngx_pfree(ngx_pool_t *pool, void *p)
{
ngx_pool_large_t *l;
for (l = pool->large; l; l = l->next) {
if (p == l->alloc) {
fprintf(stderr,"free: %p", l->alloc);
ngx_free(l->alloc);
l->alloc = NULL;
return NGX_OK;
}
}
return NGX_DECLINED;
}
void *
ngx_pcalloc(ngx_pool_t *pool, size_t size)
{
void *p;
p = ngx_palloc(pool, size);
if (p) {
ngx_memzero(p, size);
}
return p;
}
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_PALLOC_H_INCLUDED_
#define _NGX_PALLOC_H_INCLUDED_
#include "mem_core.h"
/*
* NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86.
* On Windows NT it decreases a number of locked pages in a kernel.
*/
#define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1)
#define NGX_DEFAULT_POOL_SIZE (16 * 1024)
#define NGX_POOL_ALIGNMENT 16
#define NGX_MIN_POOL_SIZE \
ngx_align((sizeof(ngx_pool_t) + 2 * sizeof(ngx_pool_large_t)), \
NGX_POOL_ALIGNMENT)
typedef struct ngx_pool_large_s ngx_pool_large_t;
struct ngx_pool_large_s {
ngx_pool_large_t *next; // 指向下一块大内存块的指针
void *alloc; // 大内存块的起始地址
};
typedef struct {
u_char *last; // 保存当前数据块中内存分配指针的当前位置。每次Nginx程序从内存池中申请内存时,
//从该指针保存的位置开始划分出请求的内存大小,并更新该指针到新的位置。
u_char *end; // 保存内存块的结束位置
ngx_pool_t *next; // 内存池由多块内存块组成,指向下一个数据块的位置。
ngx_uint_t failed; // 当前数据块内存不足引起分配失败的次数
} ngx_pool_data_t;
struct ngx_pool_s {
ngx_pool_data_t d; // 内存池当前的数据区指针的结构体
size_t max; // 当前数据块最大可分配的内存大小(Bytes)
ngx_pool_t *current; // 当前正在使用的数据块的指针
ngx_pool_large_t *large; // pool 中指向大数据块的指针(大数据快是指 size > max 的数据块)
};
void *ngx_alloc(size_t size);
void *ngx_calloc(size_t size);
ngx_pool_t *ngx_create_pool(size_t size);
void ngx_destroy_pool(ngx_pool_t *pool);
void ngx_reset_pool(ngx_pool_t *pool);
void *ngx_palloc(ngx_pool_t *pool, size_t size);
void *ngx_pnalloc(ngx_pool_t *pool, size_t size);
void *ngx_pcalloc(ngx_pool_t *pool, size_t size);
void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);
ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p);
#endif /* _NGX_PALLOC_H_INCLUDED_ */