菜鳥nginx源碼剖析數據結構篇(九) 內存池ngx_pool_thtml
Author:Echo Chen(陳斌)nginx
Email:chenb19870707@gmail.com數據結構
Blog:Blog.csdn.net/chen19870707app
Date:Nov 11th, 2014函數
今天是一年一度的光棍節,尚未女友的程序猿童鞋不妨new一個出來,內存管理一直是C/C++中最棘手的部分,遠不止new/delete、malloc/free這麼簡單。隨着代碼量的遞增,程序結構複雜度的提升。今天咱們就一塊兒研究一下以精巧著稱的nginx的內存池。性能
頭文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_palloc.h學習
源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_palloc.cui
先來學習一下nginx內存池的幾個主要數據結構:
ngx_pool_data_t(內存池數據塊結構)spa
1: typedef struct {
2: u_char *last;
3: u_char *end;
4: ngx_pool_t *next;
5: ngx_uint_t failed;
6: } ngx_pool_data_t;
ngx_pool_s(內存池頭部結構).net
1: struct ngx_pool_s {
2: ngx_pool_data_t d;
3: size_t max;
4: ngx_pool_t *current;
5: ngx_chain_t *chain;
6: ngx_pool_large_t *large;
7: ngx_pool_cleanup_t *cleanup;
8: ngx_log_t *log;
9: };
由ngx_pool_data_t和ngx_pool_t組成的nginx內存池結構以下圖所示:
在分析內存池方法前,須要對幾個主要的內存相關函數做一下介紹:
ngx_alloc:(只是對malloc進行了簡單的封裝)
1: void *
2: ngx_alloc(size_t size, ngx_log_t *log)
3: {
4: void *p;
5:
6: p = malloc(size);
7: if (p == NULL) {
8: ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
9: "malloc(%uz) failed", size);
10: }
11:
12: ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);
13:
14: return p;
15: }
ngx_calloc:(調用malloc並初始化爲0)
1: void *
2: ngx_calloc(size_t size, ngx_log_t *log)
3: {
4: void *p;
5:
6: p = ngx_alloc(size, log);
7:
8: if (p) {
9: ngx_memzero(p, size);
10: }
11:
12: return p;
13: }
ngx_memzero:
1: #define ngx_memzero(buf, n) (void) memset(buf, 0, n)
ngx_free :
1: #define ngx_free free
ngx_memalign:
1: void *
2: ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
3: {
4: void *p;
5: int err;
6:
7: err = posix_memalign(&p, alignment, size);
8:
9: if (err) {
10: ngx_log_error(NGX_LOG_EMERG, log, err,
11: "posix_memalign(%uz, %uz) failed", alignment, size);
12: p = NULL;
13: }
14:
15: ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 0,
16: "posix_memalign: %p:%uz @%uz", p, size, alignment);
17:
18: return p;
19: }
這裏alignment主要是針對部分unix平臺須要動態的對齊,對POSIX 1003.1d提供的posix_memalign( )進行封裝,在大多數狀況下,編譯器和C庫透明地幫你處理對齊問題。nginx中經過宏NGX_HAVE_POSIX_MEMALIGN來控制;調用 posix_memalign( )成功時會返回 size字節的動態內存,而且這塊內存的地址是 alignment的倍數。參數 alignment必須是2的冪,仍是 void指針的大小的倍數。返回的內存塊的地址放在了 memptr裏面,函數返回值是 0.
建立內存池 | ngx_pool_t * ngx_create_pool(size_t size, ngx_log_t *log); |
銷燬內存池 | 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); |
內存清除 | ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p); |
ngx_create_pool用於建立一個內存池,咱們建立時,傳入咱們的須要的初始大小:
1: ngx_pool_t *
2: ngx_create_pool(size_t size, ngx_log_t *log)
3: {
4: ngx_pool_t *p;
5:
6: //以16(NGX_POOL_ALIGNMENT)字節對齊分配size內存
7: p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
8: if (p == NULL) {
9: return NULL;
10: }
11:
12: //初始狀態:last指向ngx_pool_t結構體以後數據取起始位置
13: p->d.last = (u_char *) p + sizeof(ngx_pool_t);
14: //end指向分配的整個size大小的內存的末尾
15: p->d.end = (u_char *) p + size;
16:
17: p->d.next = NULL;
18: p->d.failed = 0;
19:
20: size = size - sizeof(ngx_pool_t);
21: //#define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1),內存池最大不超過4095,x86中頁的大小爲4K
22: p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
23:
24: p->current = p;
25: p->chain = NULL;
26: p->large = NULL;
27: p->cleanup = NULL;
28: p->log = log;
29:
30: return p;
31: }
nginx對內存的管理分爲大內存與小內存,當某一個申請的內存大於某一個值時,就須要從大內存中分配空間,不然從小內存中分配空間。
nginx中的內存池是在建立的時候就設定好了大小,在之後分配小塊內存的時候,若是內存不夠,則是從新建立一塊內存串到內存池中,而不是將原有的內存池進行擴張。當要分配大塊內存是,則是在內存池外面再分配空間進行管理的,稱爲大塊內存池。
1: void *
2: ngx_palloc(ngx_pool_t *pool, size_t size)
3: {
4: u_char *m;
5: ngx_pool_t *p;
6:
7: //若是申請的內存大小小於內存池的max值
8: if (size <= pool->max) {
9:
10: p = pool->current;
11:
12: do {
13: //對內存地址進行對齊處理
14: m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);
15:
16: //若是當前內存塊夠分配內存,則直接分配
17: if ((size_t) (p->d.end - m) >= size)
18: {
19: p->d.last = m + size;
20:
21: return m;
22: }
23:
24: //若是當前內存塊有效容量不夠分配,則移動到下一個內存塊進行分配
25: p = p->d.next;
26:
27: } while (p);
28:
29: //當前全部內存塊都沒有空閒了,開闢一塊新的內存,以下2詳細解釋
30: return ngx_palloc_block(pool, size);
31: }
32:
33: //分配大塊內存
34: return ngx_palloc_large(pool, size);
35: }
須要說明的幾點:
一、ngx_align_ptr,這是一個用來內存地址取整的宏,很是精巧,一句話就搞定了。做用不言而喻,取整能夠下降CPU讀取內存的次數,提升性能。由於這裏並無真正意義調用malloc等函數申請內存,而是移動指針標記而已,因此內存對齊的活,C編譯器幫不了你了,得本身動手。
1: #define ngx_align_ptr(p, a) \
2: (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
二、開闢一個新的內存塊 ngx_palloc_block(ngx_pool_t *pool, size_t size)
這個函數是用來分配新的內存塊,爲pool內存池開闢一個新的內存塊,並申請使用size大小的內存;
1: static void *
2: ngx_palloc_block(ngx_pool_t *pool, size_t size)
3: {
4: u_char *m;
5: size_t psize;
6: ngx_pool_t *p, *new;
7:
8: //計算內存池第一個內存塊的大小
9: psize = (size_t) (pool->d.end - (u_char *) pool);
10:
11: //分配和第一個內存塊一樣大小的內存塊
12: m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
13: if (m == NULL) {
14: return NULL;
15: }
16:
17: new = (ngx_pool_t *) m;
18:
19: //設置新內存塊的end
20: new->d.end = m + psize;
21: new->d.next = NULL;
22: new->d.failed = 0;
23:
24: //將指針m移動到d後面的一個位置,做爲起始位置
25: m += sizeof(ngx_pool_data_t);
26: //對m指針按4字節對齊處理
27: m = ngx_align_ptr(m, NGX_ALIGNMENT);
28: //設置新內存塊的last,即申請使用size大小的內存
29: new->d.last = m + size;
30:
31: //這裏的循環用來找最後一個鏈表節點,這裏failed用來控制循環的長度,若是分配失敗次數達到5次,就忽略,不須要每次都從頭找起
32: for (p = pool->current; p->d.next; p = p->d.next) {
33: if (p->d.failed++ > 4) {
34: pool->current = p->d.next;
35: }
36: }
37:
38: p->d.next = new;
39:
40: return m;
41: }
三、分配大塊內存 ngx_palloc_large(ngx_pool_t *pool, size_t size)
在ngx_palloc中首先會判斷申請的內存大小是否超過內存塊的最大限值,若是超過,則直接調用ngx_palloc_large,進入大內存塊的分配流程;
1: static void *
2: ngx_palloc_large(ngx_pool_t *pool, size_t size)
3: {
4: void *p;
5: ngx_uint_t n;
6: ngx_pool_large_t *large;
7:
8: // 直接在系統堆中分配一塊大小爲size的空間
9: p = ngx_alloc(size, pool->log);
10: if (p == NULL) {
11: return NULL;
12: }
13:
14: n = 0;
15:
16: // 查找到一個空的large區,若是有,則將剛纔分配的空間交由它管理
17: for (large = pool->large; large; large = large->next) {
18: if (large->alloc == NULL) {
19: large->alloc = p;
20: return p;
21: }
22: //爲了提升效率, 若是在三次內沒有找到空的large結構體,則建立一個
23: if (n++ > 3) {
24: break;
25: }
26: }
27:
28:
29: large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
30: if (large == NULL) {
31: ngx_free(p);
32: return NULL;
33: }
34:
35: //將large連接到內存池
36: large->alloc = p;
37: large->next = pool->large;
38: pool->large = large;
39:
40: return p;
41: }
整個內存池分配以下圖:
1: void
2: ngx_reset_pool(ngx_pool_t *pool)
3: {
4: ngx_pool_t *p;
5: ngx_pool_large_t *l;
6:
7: //釋放大塊內存
8: for (l = pool->large; l; l = l->next) {
9: if (l->alloc) {
10: ngx_free(l->alloc);
11: }
12: }
13:
14: // 重置全部小塊內存區
15: for (p = pool; p; p = p->d.next) {
16: p->d.last = (u_char *) p + sizeof(ngx_pool_t);
17: p->d.failed = 0;
18: }
19:
20: pool->current = pool;
21: pool->chain = NULL;
22: pool->large = NULL;
23: }
1: ngx_int_t
2: ngx_pfree(ngx_pool_t *pool, void *p)
3: {
4: ngx_pool_large_t *l;
5:
6: //只檢查是不是大內存塊,若是是大內存塊則釋放
7: for (l = pool->large; l; l = l->next) {
8: if (p == l->alloc) {
9: ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
10: "free: %p", l->alloc);
11: ngx_free(l->alloc);
12: l->alloc = NULL;
13:
14: return NGX_OK;
15: }
16: }
17:
18: return NGX_DECLINED;
19: }
因此說Nginx內存池中大內存塊和小內存塊的分配與釋放是不同的。咱們在使用內存池時,可使用ngx_palloc進行分配,使用ngx_pfree釋放。而對於大內存,這樣作是沒有問題的,而對於小內存就不同了,分配的小內存,不會進行釋放。由於大內存塊的分配只對前3個內存塊進行檢查,不然就直接分配內存,因此大內存塊的釋放必須及時。
Nginx內存池支持經過回調函數,對外部資源的清理。ngx_pool_cleanup_t是回調函數結構體,它在內存池中以鏈表形式保存,在內存池進行銷燬時,循環調用這些回調函數對數據進行清理。
1: typedef struct ngx_pool_cleanup_s ngx_pool_cleanup_t;
2:
3: struct ngx_pool_cleanup_s {
4: ngx_pool_cleanup_pt handler;
5: void *data;
6: ngx_pool_cleanup_t *next;
7: };
其中
next://指向下一個回調函數結構體;
若是咱們須要添加本身的回調函數,則須要調用ngx_pool_cleanup_add來獲得一個ngx_pool_cleanup_t,而後設置handler爲咱們的清理函數,並設置data爲咱們要清理的數據。這樣在ngx_destroy_pool中會循環調用handler清理數據;
1: ngx_pool_cleanup_t *
2: ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
3: {
4: ngx_pool_cleanup_t *c;
5:
6: //分配ngx_pool_cleanup_t
7: c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
8: if (c == NULL) {
9: return NULL;
10: }
11:
12: //給data分配內存
13: if (size) {
14: c->data = ngx_palloc(p, size);
15: if (c->data == NULL) {
16: return NULL;
17: }
18:
19: } else {
20: c->data = NULL;
21: }
22:
23: //將回掉函數鏈入內存池
24: c->handler = NULL;
25: c->next = p->cleanup;
26:
27: p->cleanup = c;
28:
29: ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c);
30:
31: return c;
32: }
好比:咱們能夠將一個開打的文件描述符做爲資源掛載到內存池上,同時提供一個關閉文件描述的函數註冊到handler上,那麼內存池在釋放的時候,就會調用咱們提供的關閉文件函數來處理文件描述符資源了。
1: void
2: ngx_destroy_pool(ngx_pool_t *pool)
3: {
4: ngx_pool_t *p, *n;
5: ngx_pool_large_t *l;
6: ngx_pool_cleanup_t *c;
7:
8: //依次調用外部析構回調函數
9: for (c = pool->cleanup; c; c = c->next) {
10: if (c->handler) {
11: ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
12: "run cleanup: %p", c);
13: c->handler(c->data);
14: }
15: }
16:
17: //釋放大塊內存
18: for (l = pool->large; l; l = l->next) {
19:
20: ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);
21:
22: if (l->alloc) {
23: ngx_free(l->alloc);
24: }
25: }
26: //釋放小塊內存
27: for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
28: ngx_free(p);
29:
30: if (n == NULL) {
31: break;
32: }
33: }
34: }
1.http://www.cnblogs.com/xiekeli/archive/2012/10/17/2727432.html
2.《深刻理解Nginx》
-Echo Chen
-