在nginx的啓動過程中,ngx_init_cycle這個函數做了大部分的初始化工作,而初始化的變量都保存在ngx_cycle_t這個結構體中,爲了深入瞭解這個函數都做了那些初始化工作,就化時間研究了一下,並寫下來以便以後參考。
1ngx_cycle_t
關於這個結構體前面已經簡單介紹過,這裏不再贅述。
2ngx_init_cycle
ngx_cycle_t * ngx_init_cycle(ngx_cycle_t *old_cycle);
先看一下這個old_cycle參數從哪裏來的:
ngx_cycle_t *cycle, init_cycle;
init_cycle.pool = ngx_create_pool(1024, log);//創建內存池
if (init_cycle.pool == NULL) {
return 1;
}
......
......
if (ngx_process_options(&init_cycle) != NGX_OK) {
return 1;
}
cycle = ngx_init_cycle(&init_cycle);
可見在調用ngx_int_cycle之前,這個old_cycle已經保存了一些參數,這些參數包括:prefix,conf_prefix,conf_file, conf_para。主要在ngx_process_options這個函數中初始化old_cycle,這個函數比較簡單,看一下代碼:
static ngx_int_t
ngx_process_options(ngx_cycle_t *cycle)
{
u_char *p;
size_t len;
if (ngx_prefix) {
len = ngx_strlen(ngx_prefix);
p = ngx_prefix;
if (!ngx_path_separator(*p)) {
p = ngx_pnalloc(cycle->pool, len + 1);
if (p == NULL) {
return NGX_ERROR;
}
ngx_memcpy(p, ngx_prefix, len);
p[len++] = '/';
}
cycle->conf_prefix.len = len;
cycle->conf_prefix.data = p;
cycle->prefix.len = len;
cycle->prefix.data = p;
} else {
#ifndef NGX_PREFIX
p = ngx_pnalloc(cycle->pool, NGX_MAX_PATH);
if (p == NULL) {
return NGX_ERROR;
}
if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {//得到當前的工作目錄
ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
return NGX_ERROR;
}
len = ngx_strlen(p);
p[len++] = '/';
cycle->conf_prefix.len = len;
cycle->conf_prefix.data = p;//配置目錄
cycle->prefix.len = len;
cycle->prefix.data = p;//安裝目錄
#else
#ifdef NGX_CONF_PREFIX
ngx_str_set(&cycle->conf_prefix, NGX_CONF_PREFIX);//配置文件相對於安裝的目錄
#else
ngx_str_set(&cycle->conf_prefix, NGX_PREFIX);
#endif
ngx_str_set(&cycle->prefix, NGX_PREFIX);
#endif
}
if (ngx_conf_file) {
cycle->conf_file.len = ngx_strlen(ngx_conf_file);
cycle->conf_file.data = ngx_conf_file;
} else {
ngx_str_set(&cycle->conf_file, NGX_CONF_PATH);
}
if (ngx_conf_full_name(cycle, &cycle->conf_file, 0) != NGX_OK) {
return NGX_ERROR;
}
for (p = cycle->conf_file.data + cycle->conf_file.len - 1;
p > cycle->conf_file.data;
p--)
{
if (ngx_path_separator(*p)) {
cycle->conf_prefix.len = p - ngx_cycle->conf_file.data + 1;
cycle->conf_prefix.data = ngx_cycle->conf_file.data;
break;
}
}
if (ngx_conf_params) {
cycle->conf_param.len = ngx_strlen(ngx_conf_params);
cycle->conf_param.data = ngx_conf_params;
}
if (ngx_test_config) {
cycle->log->log_level = NGX_LOG_INFO;
}
return NGX_OK;
}
現在開始進入關鍵:ngx_init_cycle,按照執行步驟來進行
2.1調用ngx_timezone_update()、ngx_timeofday() 、ngx_time_update(0,0)做時間校準
ngx_timezone_update();
/* force localtime update with a new timezone */
tp = ngx_timeofday();
tp->sec = 0;
ngx_time_update();
2.2創建一個新的ngx_cycle_t變量cycle,並且初始化其大部分的成員字段,有一些是從傳入的old_cycle直接拷貝過來的,這些字段包括:log,conf_prefix,prefix,conf_file,conf_param,初始化pathes數組、初始化open_files鏈表、初始化shared_memory鏈表、初始化listening數組等等。
cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));//返回的新的cycle,以後這個cycle就是全局的
if (cycle == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
cycle->pool = pool;//全局cycle的內存池
cycle->log = log;//全局log
cycle->new_log.log_level = NGX_LOG_ERR;
cycle->old_cycle = old_cycle;//old_cycle
cycle->conf_prefix.len = old_cycle->conf_prefix.len;//在nginx.c的ngx_process_options中獲得
cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);
if (cycle->conf_prefix.data == NULL) {//配置文件路徑爲NULL
ngx_destroy_pool(pool);
return NULL;
}
cycle->prefix.len = old_cycle->prefix.len;//在nginx.c的ngx_process_options中獲得
cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);
if (cycle->prefix.data == NULL) {//安裝目錄爲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) {//配置文件相對於安裝目錄的路徑爲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->pathes.nelts ? old_cycle->pathes.nelts : 10;//Nginx的操作目錄個數
cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));//數組申請內存
if (cycle->pathes.elts == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
cycle->pathes.nelts = 0;
cycle->pathes.size = sizeof(ngx_path_t *);
cycle->pathes.nalloc = n;
cycle->pathes.pool = pool;
/*計算open_files的個數*/
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;
}
//初始化open_files鏈表,
if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
!= NGX_OK)
{
ngx_destroy_pool(pool);
return NULL;
}
/*根據old_cycle的shared_memory單鏈表來初始化cycle的單鏈表*/
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;
}
//ngx_list_init主要是初始鏈表的elts,併爲其申請內存
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;//默認listening的大小爲10
/*初始化listening數組*/
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_connection_t
ngx_queue_init(&cycle->reusable_connections_queue);
//初始化conf_ctx,ngx_max_module是nginx中總的模塊數
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
hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
cycle->hostname.len = ngx_strlen(hostname);
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);
2.3調用所有NGX_CORE_MODULE的create_conf構建配置結構體,並保存在conf_ctx數組裏面
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
continue;
}
module = ngx_modules[i]->ctx;
if (module->create_conf) {
rv = module->create_conf(cycle);
if (rv == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
//返回的配置結構指針放到conf_ctx數組中,偏移位置爲ngx_module_t.index
cycle->conf_ctx[ngx_modules[i]->index] = rv;
}
}
senv = environ;
/*初始化臨時變量ngx_conf_t conf*/
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;//
2.4從命令行和配置文件中把所有配置更新到cycle的conf_ctx中:首先調用ngx_conf_param把命令行中的指令(-g directives)轉換爲配置結構並把指針加入到cycle.conf_ctx中;接着調用ngx_conf_parse(..,filename)把配置文件中的指令轉換爲配置結構並把指針加入到cycle.conf_ctx中。
if (ngx_conf_param(&conf) != NGX_CONF_OK) {//src/core/ngx_conf_file.c
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);
}
2.5調用所有核心模塊的init函數初始化所有核心模塊的配置結構體
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
continue;
}
module = ngx_modules[i]->ctx;
if (module->init_conf) {//調用模塊的init_conf初始化所有核心模塊的配置結構體
if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
== NGX_CONF_ERROR)
{
environ = senv;
ngx_destroy_cycle_pools(&conf);
return NULL;
}
}
}
if (ngx_process == NGX_PROCESS_SIGNALLER) {
return cycle;
}
2.6獲得核心模塊ngx_core_dodule的配置結構,然後調用ngx_create_pidfile創建pid文件
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
if (ngx_test_config) {
//創建進程pid文件
if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
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配置結構體
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);
}
}
2.7調用ngx_test_lockfile(filename,log),ngx_create_pathes(cycle,user),接着打開errorlog文件並賦值給cycle->new_log.file
if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
goto failed;
}
if (ngx_create_pathes(cycle, ccf->user) != NGX_OK) {
goto failed;
}
if (cycle->new_log.file == NULL) {
cycle->new_log.file = ngx_conf_open_file(cycle, &error_log);
if (cycle->new_log.file == NULL) {
goto failed;
}
}
2.8打開新文件,在第2步的時候提到cycle->open_files這個鏈表是空的,只是給它預先分配了空間,並沒有數據,這裏之所以可能會有文件被打開,估計是前面讀配置文件的時候,調用各個鉤子的過程中,填充了這個鏈表,把ngx_open_file_t結構變量填充進來(結構體中包含要打開文件的路徑信息),接着修改一下cycle的成員:cycle->log = &cycle->new_log;pool->log = &cycle->new_log;
2.9創建共享內存,和open_files類似,在第2步的時候cycle->share_memory也初始化爲一個空的鏈表,也是預分配了空間,如果此時鏈表中已經被填充了ngx_shm_zone_t結構變量(其中包含需要共享內存的尺寸和標識等信息),那麼這裏就會分配共享內存,並且調用合適的初始化鉤子初始化分配的共享內存,每塊共享內存都會有name標識,這裏也會做一些排重,已經分配的就不會再去分配,從對open_files和share_memory的處理過程可以看出,nginx在資源管理上是集中分配的,請求資源的時候分配說明性的結構變量,然後在恰當的時機纔去真正分配資源。
2.10處理listening sockets,cycle->listening是ngx_listening_t結構的數組,把cycle->listening於old_cycle->listening進行比較,設置cycle->listening的一些狀態信息,接着調用ngx_open_listening_sockets(cycle)啓動cycle->listening中的所有監聽socket,循環調用socket,bind,listen完成服務端監聽socket的啓動。接着調用ngx_configure_listening_sockets(cycle)配置監聽socket,會根據ngx_listening_t中的狀態信息設置socket的讀寫緩存和TCP_DEFER_ACCEPT。
if (old_cycle->listening.nelts) {//如果old_cycle中已經有套接字句柄
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) {//標識位爲1表示跳過設置ngx_lisenting_t中的套接字,
continue; //爲0表示正常初始化套接字
}
/********************************************************************************************************/
//有個問題:nls[n].sockaddr是什麼時候初始化的
/********************************************************************************************************/
if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK)
{
nls[n].fd = ls[i].fd;
nls[n].previous = &ls[i];
ls[i].remain = 1;//不關閉已經打開的套接字
if (ls[n].backlog != nls[i].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[n].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
break;
}
}
if (nls[n].fd == -1) {
nls[n].open = 1;
}
}
} 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
}
}
/*
這裏有個問題:cycle->listening中的每個成員ngx_listening_t的sockaddr在哪裏初始化的
因爲ngx_open_listening_sockets函數會用這個變量來創建新的套接字
*/
//啓動cycle->listening中的所有監聽socket,循環調用socket,bind,listen完成服務端監聽socket的啓動
if (ngx_open_listening_sockets(cycle) != NGX_OK) {
goto failed;
}
if (!ngx_test_config) {//接着調用ngx_configure_listening_sockets(cycle)配置監聽socket
ngx_configure_listening_sockets(cycle);
這個階段還有個問題沒解決(見註釋)???????????????。
2.11調用所有模塊的init_module方法,只有ngx_event_core_module定義了init_module。
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_module) {
if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
/* fatal */
exit(1);
}
}
}
2.12關閉或者刪除一些殘留在old_cycle中的資源,首先釋放不用的共享內存,接着關閉不使用的監聽socket,再關閉不使用的打開文件,最後把old_cycle放入ngx_old_cycles中,這是一個ngx_cycle_t *的數組,最後設定一個定時器,定期回調ngx_cleaner_event清理ngx_old_cycles,這裏設置了30000ms清理一次。
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事件
ngx_cleaner_event.handler = ngx_clean_old_cycles;//注意這個handler
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;
//將ngx_cleaner_event事件添加到定時器,定時清理old_cycle
if (!ngx_cleaner_event.timer_set) {
ngx_add_timer(&ngx_cleaner_event, 30000);//30000毫秒
ngx_cleaner_event.timer_set = 1;
}
http://blog.csdn.net/xiaoliangsky/article/details/39996563
下面附上一張圖:
圖片來源http://blog.csdn.net/lengzijian/article/details/7575813#plain
參考資料:
http://blog.csdn.net/lengzijian/article/details/7575813#plain