nginx 核心初始化收尾工作

2015-01-04 11:45:31   最后更新: 2015-01-04 19:14:47   访问数量:560




至此,我们的 ngx_init_cycle 函数已经执行到了尾声,这也意味着,初始化工作即将结束,那么,让我们来一起看看都有哪些工作需要在最后处理吧

/* 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; }

 

共享内存空间已经在此前继承给了新的 cycle 结构,所以对于老的共享内存空间我们需要在这一过程中释放掉

/* 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); 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; }

 

对于在初始化过程中打开错误的socket连接和文件,我们当然需要在善后工作进展的同时关闭他们

ngx_destroy_pool(conf.temp_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; }

 

old_cycle 内存池中的所有数据已经全部继承到了新的 cycle 中,旧的内存池就可以释放掉了

同时,配置文件已经完成了解析,配置解析过程中建立的临时内存池也需要一并释放掉

 

至此,我们的核心初始化工作已经全面结束

让我们来对比一下 init_cycle 与 ngx_cycle 在这个过程前后的值,以便清楚这个过程究竟做了什么

 

{ conf_ctx = 0x0, pool = 0x80fe9f0, { d = { last = 0x80fea18 "", end = 0x80fedf0 "", next = 0x0, failed = 0 }, max = 984, current = 0x80fe9f0, chain = 0x0, large = 0x0, cleanup = 0x0, log = 0x80e3020 <ngx_log> { log_level = 6, file = 0x80e3000 <ngx_log_file>, connection = 0, handler = 0x0, data = 0x0, writer = 0x0, wdata = 0x0, action = 0x0, next = 0x0 } } log = 0x80e3020 <ngx_log>, new_log = { log_level = 0, file = 0x0, connection = 0, handler = 0x0, data = 0x0, writer = 0x0, wdata = 0x0, action = 0x0, next = 0x0 }, log_use_stderr = 0, files = 0x0, free_connections = 0x0, free_connection_n = 0, reusable_connections_queue = { prev = 0x0, next = 0x0 }, listening = { elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0 }, paths = { elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0 }, open_files = { last = 0x0, part = { elts = 0x0, nelts = 0, next = 0x0 }, size = 0, nalloc = 0, pool = 0x0 }, shared_memory = { last = 0x0, part = { elts = 0x0, nelts = 0, next = 0x0 }, size = 0, nalloc = 0, pool = 0x0 }, connection_n = 0, files_n = 0, connections = 0x0, read_events = 0x0, write_events = 0x0, old_cycle = 0x0, conf_file = { len = 21, data = 0x80ba105 "/etc/nginx/nginx.conf" }, conf_param = { len = 0, data = 0x0 }, conf_prefix = { len = 11, data = 0x80ba105 "/etc/nginx/nginx.conf" }, prefix = { len = 34, data = 0x80ba8e4 "/home/zeyu/Workspace/nginx-1.7.7//" }, lock_file = { len = 0, data = 0x0 }, hostname = { len = 0, data = 0x0 } }

 

{ conf_ctx = 0x0, pool = 0x80fe9f0, { d = { last = 0x8103710 "", end = 0xb7d28450 "", next = 0x0, failed = 0 }, max = 984, current = 0x80fe9f0, chain = 0x0, large = 0x0, cleanup = 0x0, log = 0x80e3020 <ngx_log> { log_level = 6, file = 0x80e3000 <ngx_log_file>, connection = 0, handler = 0x0, data = 0x0, writer = 0x0, wdata = 0x0, action = 0x0, next = 0x0 } } log = 0x80e3020 <ngx_log>, new_log = { log_level = 0, file = 0x0, connection = 0, handler = 0x0, data = 0x0, writer = 0x0, wdata = 0x0, action = 0x0, next = 0x0 }, log_use_stderr = 0, files = 0x0, free_connections = 0x0, free_connection_n = 0, reusable_connections_queue = { prev = 0x0, next = 0x0 }, listening = { elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0 }, paths = { elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0 }, open_files = { last = 0x0, part = { elts = 0x0, nelts = 0, next = 0x0 }, size = 0, nalloc = 0, pool = 0x0 }, shared_memory = { last = 0x0, part = { elts = 0x0, nelts = 0, next = 0x0 }, size = 0, nalloc = 0, pool = 0x0 }, connection_n = 0, files_n = 0, connections = 0x0, read_events = 0x0, write_events = 0x0, old_cycle = 0x0, conf_file = { len = 21, data = 0x80ba105 "/etc/nginx/nginx.conf" }, conf_param = { len = 0, data = 0x0 }, conf_prefix = { len = 11, data = 0x80ba105 "/etc/nginx/nginx.conf" }, prefix = { len = 34, data = 0x80ba8e4 "/home/zeyu/Workspace/nginx-1.7.7//" }, lock_file = { len = 0, data = 0x0 }, hostname = { len = 0, data = 0x0 } }

 

我们看到,除了内存池已经被释放以外,并没有太大的变化

{ conf_ctx = 0x80fff38, pool = 0x80ff700, { d = { last = 0x8103700 "", end = 0x8103700 "", next = 0x8108750, failed = 1 }, max = 4095, current = 0x80ff700, chain = 0x0, large = 0x810fea0, cleanup = 0x810e45c, log = 0x80ff734 { log_level = 4, file = 0x80ff898, { fd = 5, name = { len = 24, data = 0x80baca6 "/var/log/nginx/error.log" }, flush = 0x0, data = 0x0 } connection = 0, handler = 0x0, data = 0x0, writer = 0x0, wdata = 0x0, action = 0x0, next = 0x0 } } log = 0x80ff734, new_log = { log_level = 4, file = 0x80ff898, connection = 0, handler = 0x0, data = 0x0, writer = 0x0, wdata = 0x0, action = 0x0, next = 0x0 }, log_use_stderr = 0, files = 0x0, free_connections = 0x0, free_connection_n = 0, reusable_connections_queue = { prev = 0x80ff768, next = 0x80ff768 }, listening = { elts = 0x80ffa38, nelts = 1, size = 128, nalloc = 10, pool = 0x80ff700 }, paths = { elts = 0x80ff85c, nelts = 5, size = 4, nalloc = 10, pool = 0x80ff700 }, open_files = { last = 0x80ff79c, part = { elts = 0x80ff884, nelts = 2, next = 0x0 }, size = 20, nalloc = 20, pool = 0x80ff700 }, shared_memory = { last = 0x80ff7b8, part = { elts = 0x80ffa14, nelts = 0, next = 0x0 }, size = 36, nalloc = 1, pool = 0x80ff700 }, connection_n = 1024, files_n = 0, connections = 0x0, read_events = 0x0, write_events = 0x0, old_cycle = 0x0, conf_file = { len = 21, data = 0x80ff845 "/etc/nginx/nginx.conf" }, conf_param = { len = 0, data = 0x80ff85b "" }, conf_prefix = { len = 11, data = 0x80ff818 "/etc/nginx//home/zeyu/Workspace/nginx-1.7.7///etc/nginx/nginx.conf" }, prefix = { len = 34, data = 0x80ff823 "/home/zeyu/Workspace/nginx-1.7.7///etc/nginx/nginx.conf" }, lock_file = { len = 50, data = 0x810ffce "/home/zeyu/Workspace/nginx-1.7.7//logs/nginx.lock.accept" }, hostname = { len = 12, data = 0x8100008 "iz257wczihcz\001" } }

 

可以看到有以下变化:

  1. 有了为模块配置数组分配的内存区域
  2. 分配并初始化16KB内存池
  3. log 结构有了自己的 error.log 文件,并且优先级提高到4
  4. 监听了最初的 socketfd
  5. 拥有了5个关注的目录、2个已打开的文件
  6. 分配了一块未使用的共享内存

 






技术帖      network      龙潭书斋      服务器      nginx      server      init      cycle      ngx_cycle_t      ngx_cycle_s      ngx_cycle_init     


京ICP备15018585号