Nginx源码分析:核心数据结构ngx_cycle_t与内存池概述
发布日期:2021-07-25 13:04:56 浏览次数:11 分类:技术文章

本文共 34469 字,大约阅读时间需要 114 分钟。

nginx源码分析

nginx-1.11.1参考书籍《深入理解nginx模块开发与架构解析》

核心数据结构与内存池概述

在Nginx中的核心数据结构就是ngx_cycle_t结构,在初始化的过程中就首先初始化了ngx_cycle_t结构,无论是master或者worker进程都初始化了该结构,本文就会分析该结构中的相关作用与成员;本文也会对Nginx的内存管理进行相关的分析,Nginx的内存管理相对简单,设计的出发点就是避免出现内存碎片、减少向操作系统申请内存的次数、统一管理提高效率。

Nginx内存池

Nginx的内存池操作相对简单不复杂,在Nginx初始化的时候就开始了申请一大块的内存,然后管理这一大块内存的使用,相关的操作方法都在ngx_palloc.c文件中,接下来我们直接分析内存池的相关方法。

创建内存池ngx_create_pool
ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log){    ngx_pool_t  *p;    p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);                // 申请16*size的内存块空间    if (p == NULL) {        return NULL;                                                // 如果申请失败则返回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;                                                       // 内存池}

通过上述代码,基本上就是初始化了内存池的相关数据,查看ngx_pool_t的数据结构,

struct ngx_pool_s {    ngx_pool_data_t       d;                // 内存的指向    size_t                max;              // 最大的数据块    ngx_pool_t           *current;          // 当前可用的位置    ngx_chain_t          *chain;                ngx_pool_large_t     *large;            // 申请的大块内存,通过一个单链表连接    ngx_pool_cleanup_t   *cleanup;          // 待释放的内存    ngx_log_t            *log;              // 输出日志};
内存池的释放ngx_destroy_pool

内存池的释放步骤如下:

voidngx_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) {                                           // 判断是否有处理handler            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,                           "run cleanup: %p", c);            c->handler(c->data);                                    // 有处理handler就调用处理        }    }#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) {        ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);    }    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {        ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,                       "free: %p, unused: %uz", 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;        }    }}

该函数的执行流程就是将pool申请的相关的大内存与相关注册待处理的内存池都依次释放,由于pool可能会出现内存不够用的情况,所以就使用了单向链表,依次保存当前可用的内存池,再释放的时候依次释放。

重置内存池

重置内存池基本思路就是先释放掉申请的大块内存,然后将pool的数据直接使用;

voidngx_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) {                          // 依次重置pool的其实可用位置        p->d.last = (u_char *) p + sizeof(ngx_pool_t);        p->d.failed = 0;    }    pool->current = pool;                                       // 设置当前可用内存池    pool->chain = NULL;                                         // 置空    pool->large = NULL;}
内存的申请

Nginx获取了内存池之后,就可以在申请对象的时候就使用此时我们就查看常用的申请内存的函数,

void *ngx_palloc(ngx_pool_t *pool, size_t size)                       // Nginx使用过程中申请内存{#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);}

申请内存的时候,会判断是否是申请大内存,如果是大内存则直接申请,小内存则从Pool中申请;

小内存的申请流程如下;

static ngx_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);            // 在last后面获取最近的对齐的内存地址        }        if ((size_t) (p->d.end - m) >= size) {              // 如果结尾的地址减去开始的地址大于等于size            p->d.last = m + size;                           // 记录last  往后移动            return m;                                       // 返回找到的位置        }        p = p->d.next;                                      // 如果没有找到合适的,获取下一个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, pool->log);     // 获取指定内存大小的内存    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);                           // 移动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;                      // 如果失败超过四次 则重新设置当前的pool为下一个        }       }    p->d.next = new;                                        // 生成下一个新的池    return m;}

主要就是先检查当前的内存池是否有可用满足需求的内存大小,如果有则获取地址返回,否则就重新申请一个pool来分配内存,新申请到的pool就加入到内存池链表中。

申请大内存的流程如下;

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, pool->log);                             // 获取内存    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_pmemalign直接获取大块内存;

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, pool->log);               // 获取内存    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_cycle_t结构。

ngx_cycle_t数据结构

ngx_cycle_t结构体中包括了Nginx相关的配置信息,Nginx模块的特性都会被保存到该结构中首先先看一下该类型;

struct ngx_cycle_s {    void                  ****conf_ctx;                         // 配置文件一个多维数组指针指向    ngx_pool_t               *pool;                             // 内存池    ngx_log_t                *log;                              // 日志    ngx_log_t                 new_log;    ngx_uint_t                log_use_stderr;  /* unsigned  log_use_stderr:1; */    ngx_connection_t        **files;                            // 所有的连接数    ngx_connection_t         *free_connections;                 // 可用连接数    ngx_uint_t                free_connection_n;                // 可用连接数量    ngx_module_t            **modules;                          // 加载的模块    ngx_uint_t                modules_n;                        // 模块数量    ngx_uint_t                modules_used;    /* unsigned  modules_used:1; */    ngx_queue_t               reusable_connections_queue;       // 重用连接队列    ngx_array_t               listening;                        // 监听数组    ngx_array_t               paths;                            // 路劲    ngx_array_t               config_dump;    ngx_list_t                open_files;    ngx_list_t                shared_memory;                    // 共享内存    ngx_uint_t                connection_n;                     // 连接数    ngx_uint_t                files_n;                          // 文件数    ngx_connection_t         *connections;                      // 连接数    ngx_event_t              *read_events;                      // 读事件    ngx_event_t              *write_events;                     // 写事件    ngx_cycle_t              *old_cycle;                        // 就cycle    ngx_str_t                 conf_file;                        // 配置文件路劲    ngx_str_t                 conf_param;                       // 配置参数    ngx_str_t                 conf_prefix;                      // 配置所在路劲    ngx_str_t                 prefix;    ngx_str_t                 lock_file;                        // 文件锁    ngx_str_t                 hostname;                         // hostname};

通过ngx_init_cycled函数的执行过程来概述一下该结构的初始化过程;

ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle){    void                *rv;    char               **senv, **env;    ngx_uint_t           i, n;    ngx_log_t           *log;    ngx_time_t          *tp;    ngx_conf_t           conf;    ngx_pool_t          *pool;    ngx_cycle_t         *cycle, **old;    ngx_shm_zone_t      *shm_zone, *oshm_zone;    ngx_list_part_t     *part, *opart;    ngx_open_file_t     *file;    ngx_listening_t     *ls, *nls;    ngx_core_conf_t     *ccf, *old_ccf;    ngx_core_module_t   *module;    char                 hostname[NGX_MAXHOSTNAMELEN];    ngx_timezone_update();                                          // 更新时区    /* force localtime update with a new timezone */    tp = ngx_timeofday();                                               tp->sec = 0;    ngx_time_update();                                              // 更新时间    log = old_cycle->log;                                           // 获取日志    pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);               // 创建内存池    if (pool == NULL) {        return NULL;                                                // 如果创建失败则返回    }    pool->log = log;                                                // 重新设置日志    cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));                 // 申请cycle内存    if (cycle == NULL) {        ngx_destroy_pool(pool);                                     // 如果申请失败则释放pool        return NULL;    }    cycle->pool = pool;                                             // 设置内存池    cycle->log = log;    cycle->old_cycle = old_cycle;                                   // 设置日志和旧cycle    cycle->conf_prefix.len = old_cycle->conf_prefix.len;            // 获取文件的路径长度    cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);   // 获取文件路径数据    if (cycle->conf_prefix.data == NULL) {                          // 如果失败则释放pool        ngx_destroy_pool(pool);        return NULL;    }    cycle->prefix.len = old_cycle->prefix.len;                              cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);    if (cycle->prefix.data == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    cycle->conf_file.len = old_cycle->conf_file.len;                 // 获取配置文件信息    cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);    if (cycle->conf_file.data == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,                old_cycle->conf_file.len + 1);    cycle->conf_param.len = old_cycle->conf_param.len;                   // 获取文件参数等    cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);    if (cycle->conf_param.data == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;           // 路劲信息初始化    cycle->paths.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));    if (cycle->paths.elts == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    cycle->paths.nelts = 0;    cycle->paths.size = sizeof(ngx_path_t *);    cycle->paths.nalloc = n;    cycle->paths.pool = pool;    if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))        != NGX_OK)    {        ngx_destroy_pool(pool);        return NULL;    }    if (old_cycle->open_files.part.nelts) {                                // 初始化文件打开句柄        n = old_cycle->open_files.part.nelts;        for (part = old_cycle->open_files.part.next; part; part = part->next) {            n += part->nelts;        }    } else {        n = 20;    }    if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))        != NGX_OK)    {        ngx_destroy_pool(pool);        return NULL;    }    if (old_cycle->shared_memory.part.nelts) {                              // 初始化共享内存链表        n = old_cycle->shared_memory.part.nelts;        for (part = old_cycle->shared_memory.part.next; part; part = part->next)        {            n += part->nelts;        }    } else {        n = 1;    }    if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))        != NGX_OK)    {        ngx_destroy_pool(pool);        return NULL;    }    n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;       // 初始化监听数组    cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));    if (cycle->listening.elts == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    cycle->listening.nelts = 0;    cycle->listening.size = sizeof(ngx_listening_t);    cycle->listening.nalloc = n;    cycle->listening.pool = pool;    ngx_queue_init(&cycle->reusable_connections_queue);                     // 初始化可用链接双向链表    cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));   // 获取配置文件上下文内存    if (cycle->conf_ctx == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {                  // 获取主机名称        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");        ngx_destroy_pool(pool);        return NULL;    }    /* on Linux gethostname() silently truncates name that does not fit */    hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';    cycle->hostname.len = ngx_strlen(hostname);                             // 设置host信息    cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);    if (cycle->hostname.data == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);    if (ngx_cycle_modules(cycle) != NGX_OK) {                           // 初始化模块相关信息        ngx_destroy_pool(pool);        return NULL;    }    for (i = 0; cycle->modules[i]; i++) {                               // 遍历模块        if (cycle->modules[i]->type != NGX_CORE_MODULE) {            continue;        }        module = cycle->modules[i]->ctx;                                // 获取上下文        if (module->create_conf) {                                      // 如果module有create_conf方法            rv = module->create_conf(cycle);                            // 则创建配置信息            if (rv == NULL) {                ngx_destroy_pool(pool);                return NULL;            }            cycle->conf_ctx[cycle->modules[i]->index] = rv;             // 保存配置信息上下文        }    }    senv = environ;    ngx_memzero(&conf, sizeof(ngx_conf_t));    /* STUB: init array ? */    conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));          // 创建配置数组    if (conf.args == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);         // 创建临时内存池    if (conf.temp_pool == NULL) {        ngx_destroy_pool(pool);        return NULL;    }    conf.ctx = cycle->conf_ctx;                                         // 保存上下文信息    conf.cycle = cycle;    conf.pool = pool;    conf.log = log;    conf.module_type = NGX_CORE_MODULE;    conf.cmd_type = NGX_MAIN_CONF;#if 0    log->log_level = NGX_LOG_DEBUG_ALL;#endif    if (ngx_conf_param(&conf) != NGX_CONF_OK) {                         // 初始化传入参数        environ = senv;        ngx_destroy_cycle_pools(&conf);        return NULL;    }    if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {      // 解析传入配置        environ = senv;        ngx_destroy_cycle_pools(&conf);        return NULL;    }    if (ngx_test_config && !ngx_quiet_mode) {        ngx_log_stderr(0, "the configuration file %s syntax is ok",                       cycle->conf_file.data);    }    for (i = 0; cycle->modules[i]; i++) {                               // 遍历加载的modules        if (cycle->modules[i]->type != NGX_CORE_MODULE) {            continue;        }        module = cycle->modules[i]->ctx;                                // 获取上下文        if (module->init_conf) {                                        // 初始化相关配置            if (module->init_conf(cycle,                                  cycle->conf_ctx[cycle->modules[i]->index])                == NGX_CONF_ERROR)            {                environ = senv;                ngx_destroy_cycle_pools(&conf);                return NULL;            }        }    }    if (ngx_process == NGX_PROCESS_SIGNALLER) {                         return cycle;    }    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);       // 获取配置信息    if (ngx_test_config) {        if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {                         // 测试创建PID            goto failed;        }    } else if (!ngx_is_init_cycle(old_cycle)) {                             /*         * we do not create the pid file in the first ngx_init_cycle() call         * because we need to write the demonized process pid         */        old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,                                                   ngx_core_module);        if (ccf->pid.len != old_ccf->pid.len            || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0)        {            /* new pid file name */            if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {                goto failed;            }            ngx_delete_pidfile(old_cycle);        }    }    if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {      // 测试文件锁        goto failed;    }    if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {                 // 创建路径        goto failed;    }    if (ngx_log_open_default(cycle) != NGX_OK) {                        // 测试打开文件        goto failed;    }    /* open the new files */    part = &cycle->open_files.part;    file = part->elts;    for (i = 0; /* void */ ; i++) {                                     // 遍历打开配置和日志文件        if (i >= part->nelts) {            if (part->next == NULL) {                break;            }            part = part->next;            file = part->elts;            i = 0;        }        if (file[i].name.len == 0) {            continue;        }        file[i].fd = ngx_open_file(file[i].name.data,                                   NGX_FILE_APPEND,                                   NGX_FILE_CREATE_OR_OPEN,                                   NGX_FILE_DEFAULT_ACCESS);        ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,                       "log: %p %d \"%s\"",                       &file[i], file[i].fd, file[i].name.data);        if (file[i].fd == NGX_INVALID_FILE) {            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,                          ngx_open_file_n " \"%s\" failed",                          file[i].name.data);            goto failed;        }#if !(NGX_WIN32)        if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,                          "fcntl(FD_CLOEXEC) \"%s\" failed",                          file[i].name.data);            goto failed;        }#endif    }    cycle->log = &cycle->new_log;    pool->log = &cycle->new_log;    /* create shared memory */    part = &cycle->shared_memory.part;     shm_zone = part->elts;    for (i = 0; /* void */ ; i++) {        if (i >= part->nelts) {            if (part->next == NULL) {                break;            }            part = part->next;            shm_zone = part->elts;            i = 0;        }        if (shm_zone[i].shm.size == 0) {            ngx_log_error(NGX_LOG_EMERG, log, 0,                          "zero size shared memory zone \"%V\"",                          &shm_zone[i].shm.name);            goto failed;        }        shm_zone[i].shm.log = cycle->log;        opart = &old_cycle->shared_memory.part;        oshm_zone = opart->elts;        for (n = 0; /* void */ ; n++) {            if (n >= opart->nelts) {                if (opart->next == NULL) {                    break;                }                opart = opart->next;                oshm_zone = opart->elts;                n = 0;            }            if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {                continue;            }            if (ngx_strncmp(shm_zone[i].shm.name.data,                            oshm_zone[n].shm.name.data,                            shm_zone[i].shm.name.len)                != 0)            {                continue;            }            if (shm_zone[i].tag == oshm_zone[n].tag                && shm_zone[i].shm.size == oshm_zone[n].shm.size                && !shm_zone[i].noreuse)            {                shm_zone[i].shm.addr = oshm_zone[n].shm.addr;#if (NGX_WIN32)                shm_zone[i].shm.handle = oshm_zone[n].shm.handle;#endif                if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)                    != NGX_OK)                {                    goto failed;                }                goto shm_zone_found;            }            ngx_shm_free(&oshm_zone[n].shm);            break;        }        if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {            goto failed;        }        if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {            goto failed;        }        if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {            goto failed;        }    shm_zone_found:        continue;    }    /* handle the listening sockets */    if (old_cycle->listening.nelts) {                               // 处理旧的连接信息        ls = old_cycle->listening.elts;        for (i = 0; i < old_cycle->listening.nelts; i++) {            ls[i].remain = 0;        }        nls = cycle->listening.elts;        for (n = 0; n < cycle->listening.nelts; n++) {            for (i = 0; i < old_cycle->listening.nelts; i++) {                if (ls[i].ignore) {                    continue;                }                if (ls[i].remain) {                    continue;                }                if (ls[i].type != nls[n].type) {                    continue;                }                if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen,                                     ls[i].sockaddr, ls[i].socklen, 1)                      == NGX_OK)                {                    nls[n].fd = ls[i].fd;                    nls[n].previous = &ls[i];                    ls[i].remain = 1;                    if (ls[i].backlog != nls[n].backlog) {                        nls[n].listen = 1;                    }#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)                    /*                     * FreeBSD, except the most recent versions,                     * could not remove accept filter                     */                    nls[n].deferred_accept = ls[i].deferred_accept;                    if (ls[i].accept_filter && nls[n].accept_filter) {                        if (ngx_strcmp(ls[i].accept_filter,                                       nls[n].accept_filter)                            != 0)                        {                            nls[n].delete_deferred = 1;                            nls[n].add_deferred = 1;                        }                    } else if (ls[i].accept_filter) {                        nls[n].delete_deferred = 1;                    } else if (nls[n].accept_filter) {                        nls[n].add_deferred = 1;                    }#endif#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)                    if (ls[i].deferred_accept && !nls[n].deferred_accept) {                        nls[n].delete_deferred = 1;                    } else if (ls[i].deferred_accept != nls[n].deferred_accept)                    {                        nls[n].add_deferred = 1;                    }#endif#if (NGX_HAVE_REUSEPORT)                    if (nls[n].reuseport && !ls[i].reuseport) {                        nls[n].add_reuseport = 1;                    }#endif                    break;                }            }            if (nls[n].fd == (ngx_socket_t) -1) {                nls[n].open = 1;#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)                if (nls[n].accept_filter) {                    nls[n].add_deferred = 1;                }#endif#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)                if (nls[n].deferred_accept) {                    nls[n].add_deferred = 1;                }#endif            }        }    } else {        ls = cycle->listening.elts;                                 // 当前连接请求        for (i = 0; i < cycle->listening.nelts; i++) {              // 遍历            ls[i].open = 1;                                         // 设置为打开#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)            if (ls[i].accept_filter) {                ls[i].add_deferred = 1;            }#endif#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)            if (ls[i].deferred_accept) {                ls[i].add_deferred = 1;            }#endif        }    }    if (ngx_open_listening_sockets(cycle) != NGX_OK) {              // 打开并监听        goto failed;    }    if (!ngx_test_config) {        ngx_configure_listening_sockets(cycle);    }    /* commit the new cycle configuration */    if (!ngx_use_stderr) {        (void) ngx_log_redirect_stderr(cycle);    }    pool->log = cycle->log;    if (ngx_init_modules(cycle) != NGX_OK) {                        // 初始化modules        /* fatal */        exit(1);    }    /* close and delete stuff that lefts from an old cycle */    /* free the unnecessary shared memory */    opart = &old_cycle->shared_memory.part;    oshm_zone = opart->elts;    for (i = 0; /* void */ ; i++) {        if (i >= opart->nelts) {            if (opart->next == NULL) {                goto old_shm_zone_done;            }            opart = opart->next;            oshm_zone = opart->elts;            i = 0;        }        part = &cycle->shared_memory.part;        shm_zone = part->elts;        for (n = 0; /* void */ ; n++) {            if (n >= part->nelts) {                if (part->next == NULL) {                    break;                }                part = part->next;                shm_zone = part->elts;                n = 0;            }            if (oshm_zone[i].shm.name.len == shm_zone[n].shm.name.len                && ngx_strncmp(oshm_zone[i].shm.name.data,                               shm_zone[n].shm.name.data,                               oshm_zone[i].shm.name.len)                == 0)            {                goto live_shm_zone;            }        }        ngx_shm_free(&oshm_zone[i].shm);    live_shm_zone:        continue;    }old_shm_zone_done:    /* close the unnecessary listening sockets */    ls = old_cycle->listening.elts;    for (i = 0; i < old_cycle->listening.nelts; i++) {        if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {            continue;        }        if (ngx_close_socket(ls[i].fd) == -1) {            ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,                          ngx_close_socket_n " listening socket on %V failed",                          &ls[i].addr_text);        }#if (NGX_HAVE_UNIX_DOMAIN)        if (ls[i].sockaddr->sa_family == AF_UNIX) {            u_char  *name;            name = ls[i].addr_text.data + sizeof("unix:") - 1;            ngx_log_error(NGX_LOG_WARN, cycle->log, 0,                          "deleting socket %s", name);            if (ngx_delete_file(name) == NGX_FILE_ERROR) {                ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,                              ngx_delete_file_n " %s failed", name);            }        }#endif    }    /* close the unnecessary open files */    part = &old_cycle->open_files.part;                     // 关闭不需要的打开文件    file = part->elts;    for (i = 0; /* void */ ; i++) {        if (i >= part->nelts) {            if (part->next == NULL) {                break;            }            part = part->next;            file = part->elts;            i = 0;        }        if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {            continue;        }        if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,                          ngx_close_file_n " \"%s\" failed",                          file[i].name.data);        }    }    ngx_destroy_pool(conf.temp_pool);                       // 释放临时pool    if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {        /*         * perl_destruct() frees environ, if it is not the same as it was at         * perl_construct() time, therefore we save the previous cycle         * environment before ngx_conf_parse() where it will be changed.         */        env = environ;        environ = senv;        ngx_destroy_pool(old_cycle->pool);        cycle->old_cycle = NULL;        environ = env;        return cycle;    }    if (ngx_temp_pool == NULL) {        ngx_temp_pool = ngx_create_pool(128, cycle->log);        if (ngx_temp_pool == NULL) {            ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,                          "could not create ngx_temp_pool");            exit(1);        }        n = 10;        ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,                                          n * sizeof(ngx_cycle_t *));        if (ngx_old_cycles.elts == NULL) {            exit(1);        }        ngx_old_cycles.nelts = 0;        ngx_old_cycles.size = sizeof(ngx_cycle_t *);        ngx_old_cycles.nalloc = n;        ngx_old_cycles.pool = ngx_temp_pool;        ngx_cleaner_event.handler = ngx_clean_old_cycles;        ngx_cleaner_event.log = cycle->log;        ngx_cleaner_event.data = &dumb;        dumb.fd = (ngx_socket_t) -1;    }    ngx_temp_pool->log = cycle->log;    old = ngx_array_push(&ngx_old_cycles);    if (old == NULL) {        exit(1);    }    *old = old_cycle;    if (!ngx_cleaner_event.timer_set) {        ngx_add_timer(&ngx_cleaner_event, 30000);        ngx_cleaner_event.timer_set = 1;    }    return cycle;failed:    if (!ngx_is_init_cycle(old_cycle)) {        old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,                                                   ngx_core_module);        if (old_ccf->environment) {            environ = old_ccf->environment;        }    }    /* rollback the new cycle configuration */    part = &cycle->open_files.part;    file = part->elts;    for (i = 0; /* void */ ; i++) {        if (i >= part->nelts) {            if (part->next == NULL) {                break;            }            part = part->next;            file = part->elts;            i = 0;        }        if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {            continue;        }        if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,                          ngx_close_file_n " \"%s\" failed",                          file[i].name.data);        }    }    if (ngx_test_config) {        ngx_destroy_cycle_pools(&conf);        return NULL;    }    ls = cycle->listening.elts;    for (i = 0; i < cycle->listening.nelts; i++) {        if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {            continue;        }        if (ngx_close_socket(ls[i].fd) == -1) {            ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,                          ngx_close_socket_n " %V failed",                          &ls[i].addr_text);        }    }    ngx_destroy_cycle_pools(&conf);    return NULL;}

该函数的执行流程相对比较复杂,目前可归结为如下的流程;

  1. 更新当前时间
  2. 创建内存池
  3. 获取配置文件的路径
  4. 获取Nginx的路径
  5. 获取初始参数
  6. 路径信息初始化
  7. 初始化打开的监听句柄
  8. 获取相关主机信息
  9. 初始化加载的module
  10. 保存配置文件上下文并解析参数
  11. 测试文件锁和打开配置与日志文件
  12. 初始化监听请求
  13. 调用初始化module的初始化方法
  14. 关闭不需要的共享内存与不需要的文件句柄

基本的执行流程大致如上所述,Nginx的ngx_cycle_t的大致初始化流程如上所述。由于本人才疏学浅,如有错误请批评指正。

总结

本文主要大概探索了Nginx的内存池相关的操作过程,内存池的操作相对简单,都是常用的对内存的申请、释放,使用了比较多的链表来记录申请的和释放的内存区域;Nginx中的比较核心的cycle的数据结构,基本上保存了你虚拟执行的相关配置,内存、链接信息等,本文只是简单分析了初始化过程,等到碰到相关操作时再详细去描述各个成员的执行过程。由于本人才疏学浅,如有错误请批评指正。

转载地址:https://blog.csdn.net/qq_33339479/article/details/89213201 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Nginx源码分析:惊群处理与负载均衡
下一篇:Nginx源码分析:master/worker工作流程概述

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月17日 14时21分01秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

修改表格字体颜色_Excel技巧:Excel如何修改字体颜色 2019-04-21
native react 变颜色 点击_React Native主动更改StackNavigator标头颜色 2019-04-21
prism项目搭建 wpf_WPF MVVM使用prism4.1搭建 2019-04-21
python发微信红包群_用Python实现微信自动化抢红包,再也不用担心抢不到红包了... 2019-04-21
python中func自定义函数_Python函数之自定义函数&作用域&闭包 2019-04-21
wget连接指定端口_端口通不通 telnet wget ssh 2019-04-21
eureka 调用服务_Spring Cloud微服务架构从入门到会用(二)—服务注册中心Eureka... 2019-04-21
easyexcel 工具类_问了个在阿里的同学,他们常用的15款开发者工具! 2019-04-21
mysql统计结果大于0时返回true_mysql表查询练习 2019-04-21
c语言对结构体排序中间变量,求助:c语言怎么实现结构体的排序? 总是弄不对啊... 2019-04-21
c语言宏定义只能在最前面吗,C语言宏定义注意事项 2019-04-21
android悬浮窗服务卡死,Android 悬浮窗兼容问题谈 2019-04-21
表格相关的html语言,HTML标记语言——表格标记 2019-04-21
web聊天界面html,PC端Web聊天界面+代码分享(HTML+CSS) 2019-04-21
cmake qt 添加路径 项目_CMake配置Qt工程 2019-04-21
使用python开发的软件协议_WEB开发——Python WSGI协议详解 2019-04-21
冰点下载器手机版apk_冰点文库下载器 2019-04-21
python信号采集代码_13行代码实现:Python实时视频采集(附源码) 2019-04-21
h5引入json_纯js直接引入json文件 2019-04-21
python格式化字符串总结_Python字符串处理方法总结 2019-04-21