nginx事件模块 -- 第四篇

微信公众号:郑尔多斯
关注可了解更多的Nginx知识。任何问题或建议,请公众号留言;
关注公众号,有趣有内涵的文章第一时间送达!

剧情回顾

上一篇文章中我们详细介绍了Nginx中事件模块的初始化过程。这里简单的回顾一下:Nginxworker进程启动之后,会首先进行worker的初始化,在初始化过程中将遍历调用所有模块的init_process函数。与Nginx事件机制相关的三个模块中,只有ngx_event_core_module才实现了对应的钩子函数ngx_event_process_init。上一篇文章我们已经比较详细的分析了这篇文章,但是还遗留了几个问题,比如,epoll是如何初始化的,时间定时器是如何初始化的。本篇文章就分析一下epoll是如何初始化的。

epoll初始化

首先观察ngx_event_process_init下面的代码:

for (m = 0; cycle->modules[m]; m++) {
        if (cycle->modules[m]->type != NGX_EVENT_MODULE) {
            continue;
        }

        if (cycle->modules[m]->ctx_index != ecf->use) {
            continue;
        }

        module = cycle->modules[m]->ctx;

        if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
            /* fatal */
            exit(2);
        }

        break;
    }

这部分代码就涵盖了epoll的初始化过程,它是如何实现的呢?

遍历所有的模块,找到我们选择的事件处理模块,本例中就是ngx_epoll,然后调用时间模块的action.init()方法。

下面我们看一下ngx_epoll的源码:

static ngx_event_module_t  ngx_epoll_module_ctx = {
    &epoll_name,
    ngx_epoll_create_conf,               /* create configuration */
    ngx_epoll_init_conf,                 /* init configuration */

    {
        ngx_epoll_add_event,             /* add an event */
        ngx_epoll_del_event,             /* delete an event */
        ngx_epoll_add_event,             /* enable an event */
        ngx_epoll_del_event,             /* disable an event */
        ngx_epoll_add_connection,        /* add an connection */
        ngx_epoll_del_connection,        /* delete an connection */
#if (NGX_HAVE_EVENTFD)
        ngx_epoll_notify,                /* trigger a notify */
#else
        NULL,                            /* trigger a notify */
#endif
        ngx_epoll_process_events,        /* process the events */
        ngx_epoll_init,                  /* init the events */
        ngx_epoll_done,                  /* done the events */
    }
};

从代码中可以看到,action.init()对应于ngx_epoll_init(),下面我们分析一下该函数

ngx_epoll_init

先看代码:

static ngx_int_t
ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
{
    ngx_epoll_conf_t  *epcf;

    epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module);

    if (ep == -1) {
        ep = epoll_create(cycle->connection_n / 2);

        if (ep == -1) {
            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                          "epoll_create() failed");
            return NGX_ERROR;
        }

#if (NGX_HAVE_EVENTFD)
        if (ngx_epoll_notify_init(cycle->log) != NGX_OK) {
            ngx_epoll_module_ctx.actions.notify = NULL;
        }
#endif

#if (NGX_HAVE_FILE_AIO)
        ngx_epoll_aio_init(cycle, epcf);
#endif

#if (NGX_HAVE_EPOLLRDHUP)
        ngx_epoll_test_rdhup(cycle);
#endif
    }

    if (nevents < epcf->events) {
        if (event_list) {
            ngx_free(event_list);
        }

        event_list = ngx_alloc(sizeof(struct epoll_event) * epcf->events,
                               cycle->log);
        if (event_list == NULL) {
            return NGX_ERROR;
        }
    }

    nevents = epcf->events;

    ngx_io = ngx_os_io;

    ngx_event_actions = ngx_epoll_module_ctx.actions;

#if (NGX_HAVE_CLEAR_EVENT)
    ngx_event_flags = NGX_USE_CLEAR_EVENT
#else
    ngx_event_flags = NGX_USE_LEVEL_EVENT
#endif
                      |NGX_USE_GREEDY_EVENT
                      |NGX_USE_EPOLL_EVENT;

    return NGX_OK;
}

这部分代码就是epoll初始化的代码,我们可以清晰的理一下思路:

  • 使用epoll_create()创建一个epoll实例
  • 根据配置文件中events的数量创建对应数量的epoll_event结构体
  • 设置一些全局变量的值,如nevents , ngx_io , ngx_event_actions
  • 这里有一个比较重要的变量就是 ngx_event_flags,这个变量在后面经常使用到,通过调试发现该变量的值为:NGX_USE_CLEAR_EVENT | NGX_USE_GREEDY_EVENT | NGX_USE_EPOLL_EVENT

喜欢本文的朋友们,欢迎长按下图关注订阅号郑尔多斯,更多精彩内容第一时间送达
郑尔多斯

猜你喜欢

转载自blog.csdn.net/weixin_43797048/article/details/85246795