1. 首页
  2. 技术知识

nginx之内存池的实现

目录

    一、简介二、数据结构

      2.1 内存池主要结构2.2 大内存链2.3 清理任务链

    三、内存结构图

      3.1 逻辑3.2 实际

    四、实现

      4.1 创建内存池4.2 从内存池中分配空间4.3 注册清理任务4.4 重置内存池4.5 销毁内存池4.6 大内存释放4.7 分配并清空数据4.8 回调文件清理

一、简介

最新稳定版本nginx1.20.2。

为了能高效、快速的分配内存,以及减少内存碎片等,nginx实现了自己的内存池基础组件。

主要实现文件ngx_palloc.h, ngx_palloc.c

二、数据结构


2.1 内存池主要结构

  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;
  7. struct ngx_pool_s {
  8.     ngx_pool_data_t       d;
  9.     size_t                max;
  10.     ngx_pool_t           *current;
  11.     ngx_chain_t          *chain;
  12.     ngx_pool_large_t     *large;
  13.     ngx_pool_cleanup_t   *cleanup;
  14.     ngx_log_t            *log;
  15. };

复制代码 内存池中第一个成员是一个结构体:

使用ngx_pool_data_t结构体来表示当前内存池信息。

last :下次开始分配的地址

end: 内存池的结束地址

next: 内存池链表,将多个内存池连接起来

max

整个内存池的最大大小

current

指向从当前内存池开始查找可用内存

chain

buffer使用的,这里不涉及

large

当需要的内存大于内存池最大大小时,需要通过malloc直接分配,然后形成链表进行组织

cleanup

清理工作的回调链表

log

日志句柄

2.2 大内存链

当需要分配的内存比内存池的最大大小都大时,内存池无法满足分配,所以直接从系统中分配,然后构成一个链表进行维护。

  1. typedef struct ngx_pool_large_s  ngx_pool_large_t;
  2. struct ngx_pool_large_s {
  3.     ngx_pool_large_t     *next;
  4.     void                 *alloc;
  5. };

复制代码
2.3 清理任务链

有一个回调任务的链表,当内存池销毁时,将依次遍历此链表,逐一回调handler进行清理工作。

  1. typedef void (*ngx_pool_cleanup_pt)(void *data);
  2. typedef struct ngx_pool_cleanup_s  ngx_pool_cleanup_t;
  3. struct ngx_pool_cleanup_s {
  4.     ngx_pool_cleanup_pt   handler;
  5.     void                 *data;
  6.     ngx_pool_cleanup_t   *next;
  7. };

复制代码
三、内存结构图


3.1 逻辑


3.2 实际

可以看出,很多节点都是从内存池中分配的,所以可以把精力都放在实际的数据上而不必在意其他细节上。

四、实现


4.1 创建内存池

  1. /*
  2. * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize – 1), i.e. 4095 on x86.
  3. * On Windows NT it decreases a number of locked pages in a kernel.
  4. */
  5. #define NGX_MAX_ALLOC_FROM_POOL  (ngx_pagesize – 1)
  6. #define NGX_DEFAULT_POOL_SIZE    (16 * 1024)

复制代码

  1. ngx_pool_t *
  2. ngx_create_pool(size_t size, ngx_log_t *log)
  3. {
  4.     ngx_pool_t  *p;
  5.     p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
  6.     if (p == NULL) {
  7.         return NULL;
  8.     }
  9.     p->d.last = (u_char *) p + sizeof(ngx_pool_t);
  10.     p->d.end = (u_char *) p + size;
  11.     p->d.next = NULL;
  12.     p->d.failed = 0;
  13.     size = size – sizeof(ngx_pool_t);
  14.     p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
  15.     p->current = p;
  16.     p->chain = NULL;
  17.     p->large = NULL;
  18.     p->cleanup = NULL;
  19.     p->log = log;
  20.     return p;
  21. }

复制代码 从代码中可以看到,内存池最大不超过pagesize的大小

4.2 从内存池中分配空间

分配函数分了内存对齐和内存不对齐,但这只控制了内存池中分配空间,不控制大内存分配。

(1)分配小空间

    内存对齐 ngx_palloc内存不对齐 ngx_pnalloc

  1. void *
  2. ngx_palloc(ngx_pool_t *pool, size_t size)
  3. {
  4. #if !(NGX_DEBUG_PALLOC)
  5.     if (size <= pool->max) {
  6.         return ngx_palloc_small(pool, size, 1);
  7.     }
  8. #endif
  9.     return ngx_palloc_large(pool, size);
  10. }

复制代码 当需要分配的空间小于max时,将使用小内存分配方式(即从内存池中分配空间),而ngx_pnalloc和ngx_palloc相比只是调用ngx_palloc_small时的最后一个参数为0。

从pool->current指向的内存池开始遍历,寻找满足分配大小的空间,找到则返回首地址

  1. static ngx_inline void *
  2. ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
  3. {
  4.     u_char      *m;
  5.     ngx_pool_t  *p;
  6.     p = pool->current;
  7.     do {
  8.         m = p->d.last;
  9.         if (align) {
  10.             m = ngx_align_ptr(m, NGX_ALIGNMENT);
  11.         }
  12.         if ((size_t) (p->d.end – m) >= size) {
  13.             p->d.last = m + size;
  14.             return m;
  15.         }
  16.         p = p->d.next;
  17.     } while (p);
  18.     return ngx_palloc_block(pool, size);
  19. }

复制代码 当现有内存池中都无法满足分配条件时,创建新的内存池

  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.     psize = (size_t) (pool->d.end – (u_char *) pool);
  8.     m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
  9.     if (m == NULL) {
  10.         return NULL;
  11.     }
  12.     new = (ngx_pool_t *) m;
  13.     new->d.end = m + psize;
  14.     new->d.next = NULL;
  15.     new->d.failed = 0;
  16.     m += sizeof(ngx_pool_data_t);
  17.     m = ngx_align_ptr(m, NGX_ALIGNMENT);
  18.     new->d.last = m + size;
  19.     for (p = pool->current; p->d.next; p = p->d.next) {
  20.         if (p->d.failed++ > 4) {
  21.             pool->current = p->d.next;
  22.         }
  23.     }
  24.     p->d.next = new;
  25.     return m;
  26. }

复制代码 其中,创建好新的内存池后,又做了一次遍历,将failed计数加一,当大于4时,将跳过此内存池,下次就不从它开始查找。

即认为超过4次你都不能满足分配,以后都不能满足分配,不再用你了,减少遍历个数,加快成功分配效率

(2)分配大空间

  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.     p = ngx_alloc(size, pool->log);
  8.     if (p == NULL) {
  9.         return NULL;
  10.     }
  11.     n = 0;
  12.     for (large = pool->large; large; large = large->next) {
  13.         if (large->alloc == NULL) {
  14.             large->alloc = p;
  15.             return p;
  16.         }
  17.         if (n++ > 3) {
  18.             break;
  19.         }
  20.     }
  21.     large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
  22.     if (large == NULL) {
  23.         ngx_free(p);
  24.         return NULL;
  25.     }
  26.     large->alloc = p;
  27.     large->next = pool->large;
  28.     pool->large = large;
  29.     return p;
  30. }

复制代码 可以看出,为了避免分配空间,遍历large链查找可重用的节点,但是如果链表过大又可能太慢,所以只查找前三个,如果三个都没有找到,则直接分配(而且节点也是从内存池中分配的,所以后续清理时,不需要管节点,只需要释放申请的大内存本身)

内存对齐

  1. void *
  2. ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
  3. {
  4.     void              *p;
  5.     ngx_pool_large_t  *large;
  6.     p = ngx_memalign(alignment, size, pool->log);
  7.     if (p == NULL) {
  8.         return NULL;
  9.     }
  10.     large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
  11.     if (large == NULL) {
  12.         ngx_free(p);
  13.         return NULL;
  14.     }
  15.     large->alloc = p;
  16.     large->next = pool->large;
  17.     pool->large = large;
  18.     return p;
  19. }

复制代码
4.3 注册清理任务

  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.     c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
  6.     if (c == NULL) {
  7.         return NULL;
  8.     }
  9.     if (size) {
  10.         c->data = ngx_palloc(p, size);
  11.         if (c->data == NULL) {
  12.             return NULL;
  13.         }
  14.     } else {
  15.         c->data = NULL;
  16.     }
  17.     c->handler = NULL;
  18.     c->next = p->cleanup;
  19.     p->cleanup = c;
  20.     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, “add cleanup: %p”, c);
  21.     return c;
  22. }

复制代码 可以看出,这里只是分配了一个节点,并没有设置handler以及data数据,所以还得看具体的调用方进行设置,因为这里返回了分配的节点。

比如在函数ngx_create_temp_file中

  1. ngx_int_t
  2. ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
  3.     ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)
  4. {
  5.     …
  6.     cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
  7.     if (cln == NULL) {
  8.         return NGX_ERROR;
  9.     }
  10.        …
  11.         file->fd = ngx_open_tempfile(file->name.data, persistent, access);
  12.                                 …
  13.         if (file->fd != NGX_INVALID_FILE) {
  14.             cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;
  15.             clnf = cln->data;
  16.             clnf->fd = file->fd;
  17.             clnf->name = file->name.data;
  18.             clnf->log = pool->log;
  19.             return NGX_OK;
  20.         }
  21.                         …
  22. }

复制代码 生成临时文件,将fd以及文件名注册到清理任务中,后续文件不使用了则不需要特殊处理,内存内存池释放时将统一清理。

4.4 重置内存池

    释放大内存重置内存中last重置failed计数

  1. void
  2. ngx_reset_pool(ngx_pool_t *pool)
  3. {
  4.     ngx_pool_t        *p;
  5.     ngx_pool_large_t  *l;
  6.     for (l = pool->large; l; l = l->next) {
  7.         if (l->alloc) {
  8.             ngx_free(l->alloc);
  9.         }
  10.     }
  11.     for (p = pool; p; p = p->d.next) {
  12.         p->d.last = (u_char *) p + sizeof(ngx_pool_t);
  13.         p->d.failed = 0;
  14.     }
  15.     pool->current = pool;
  16.     pool->chain = NULL;
  17.     pool->large = NULL;
  18. }

复制代码 这里有个现象:

在内存池中空间不足时,将调用ngx_palloc_block创建一个新的内存池,而last指向的是m += sizeof(ngx_pool_data_t);, 因此当前新分配的内存池将比第一个内存池可用大小多了(max,current,chain,large,cleanup,log)这几个字段大小(可能没有那么多,因为要对齐,可能对齐后就完全一样了),而现在重置时,p->d.last = (u_char *) p + sizeof(ngx_pool_t);每个内存池可用大小又变成一样的。

4.5 销毁内存池

    回调清理任务释放大内存释放内存池本身

  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.     for (c = pool->cleanup; c; c = c->next) {
  8.         if (c->handler) {
  9.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
  10.                            “run cleanup: %p”, c);
  11.             c->handler(c->data);
  12.         }
  13.     }
  14.     for (l = pool->large; l; l = l->next) {
  15.         if (l->alloc) {
  16.             ngx_free(l->alloc);
  17.         }
  18.     }
  19.     for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
  20.         ngx_free(p);
  21.         if (n == NULL) {
  22.             break;
  23.         }
  24.     }
  25. }

复制代码
4.6 大内存释放

通过遍历找到要释放的节点,将内存释放,并且将alloc设置成NULL,则有了节点重用的情况。

  1. ngx_int_t
  2. ngx_pfree(ngx_pool_t *pool, void *p)
  3. {
  4.     ngx_pool_large_t  *l;
  5.     for (l = pool->large; l; l = l->next) {
  6.         if (p == l->alloc) {
  7.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
  8.                            “free: %p”, l->alloc);
  9.             ngx_free(l->alloc);
  10.             l->alloc = NULL;
  11.             return NGX_OK;
  12.         }
  13.     }
  14.     return NGX_DECLINED;
  15. }

复制代码
4.7 分配并清空数据

  1. void *
  2. ngx_pcalloc(ngx_pool_t *pool, size_t size)
  3. {
  4.     void *p;
  5.     p = ngx_palloc(pool, size);
  6.     if (p) {
  7.         ngx_memzero(p, size);
  8.     }
  9.     return p;
  10. }

复制代码 正常分配的空间中都是X数据,所以当前函数在分配空间后,将分配的空间清零。

4.8 回调文件清理

(1) 手动关闭指定fd

遍历清理任务,找到ngx_pool_cleanup_file的handler,如果是要关闭的fd,则回调

  1. void
  2. ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd)
  3. {
  4.     ngx_pool_cleanup_t       *c;
  5.     ngx_pool_cleanup_file_t  *cf;
  6.     for (c = p->cleanup; c; c = c->next) {
  7.         if (c->handler == ngx_pool_cleanup_file) {
  8.             cf = c->data;
  9.             if (cf->fd == fd) {
  10.                 c->handler(cf);
  11.                 c->handler = NULL;
  12.                 return;
  13.             }
  14.         }
  15.     }
  16. }

复制代码 (2) 关闭fd

  1. void
  2. ngx_pool_cleanup_file(void *data)
  3. {
  4.     ngx_pool_cleanup_file_t  *c = data;
  5.     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, c->log, 0, “file cleanup: fd:%d”,
  6.                    c->fd);
  7.     if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
  8.         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
  9.                       ngx_close_file_n ” “%s” failed”, c->name);
  10.     }
  11. }

复制代码 (3) 删除文件并关闭fd

  1. void
  2. ngx_pool_delete_file(void *data)
  3. {
  4.     ngx_pool_cleanup_file_t  *c = data;
  5.     ngx_err_t  err;
  6.     ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, c->log, 0, “file cleanup: fd:%d %s”,
  7.                    c->fd, c->name);
  8.     if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
  9.         err = ngx_errno;
  10.         if (err != NGX_ENOENT) {
  11.             ngx_log_error(NGX_LOG_CRIT, c->log, err,
  12.                           ngx_delete_file_n ” “%s” failed”, c->name);
  13.         }
  14.     }
  15.     if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
  16.         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
  17.                       ngx_close_file_n ” “%s” failed”, c->name);
  18.     }
  19. }

复制代码 到此这篇关于nginx之内存池的实现的文章就介绍到这了,更多相关nginx 内存池内容请搜索软件技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件技术网!

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/108962.html

联系我们