nginx源码|Nginx源码分析——worker进程源码与工作原理(二)

一、说明 前面很多内容,可以说大致了解了下nginx是进程是怎么启动的,进程之间是如何通信的,花一些时间或多或少也都还是能看得懂的,后面要去了解ngx_worker_process_cycle函数,worker进程所有的实现或者核心基本都是在这个函数里面,可能需要花蛮长的时间去消耗去理解。 二、方法主流程

static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) { // 我们的场景 worker=1,ngx_processes数组中的编号 ngx_int_t worker = (intptr_t) data; // 设置两个全局变量后续用到 ngx_process = NGX_PROCESS_WORKER; ngx_worker = worker; // 初始化worker进程,包括一些数据结构、socket、套接字等等 // TODO 需要展开去了解 ngx_worker_process_init(cycle, worker); // 设置进程名 ngx_setproctitle("worker process"); // 无条件循环保持进程 for ( ; ; ) {// nginx在quit的时候 ngx_exiting = 1 // worker进程在ngx_worker_process_exit函数里面进行退出exit(0) if (ngx_exiting) { if (ngx_event_no_timers_left() == NGX_OK) { ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting"); ngx_worker_process_exit(cycle); } }ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle"); // 这个函数就worker进程核心的模块,定义了事件监听跟定时器 // TODO ngx_process_events_and_timers(cycle); // nginx终结,退出进程 if (ngx_terminate) { ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting"); ngx_worker_process_exit(cycle); } // nginx退出 if (ngx_quit) { ngx_quit = 0; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "gracefully shutting down"); ngx_setproctitle("worker process is shutting down"); if (!ngx_exiting) { ngx_exiting = 1; ngx_set_shutdown_timer(cycle); ngx_close_listening_sockets(cycle); ngx_close_idle_connections(cycle); } }if (ngx_reopen) { ngx_reopen = 0; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs"); ngx_reopen_files(cycle, -1); } } }

三、worker进程初始化 ./src/os/unix/ngx_prorcess_cycle.c ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker) 对于这个函数的入参,cycle基本是核心的数据结构都会用到,worker为当前进程的编号,我们的场景worker值为0 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 获取ngx_core_module模块的配置指针。这个函数会在很多地方都看到,用来获取nginx.conf配置文件中的配置。
if (worker >= 0 && ccf->priority != 0) { if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "setpriority(%d) failed", ccf->priority); } }

这里主要看worker进程是否设置了优先级, worker_priority值越小优先级越高,reload下看看。
#usernobody; worker_processes1; worker_priority -10;

top -u nobody

nginx源码|Nginx源码分析——worker进程源码与工作原理(二)
文章图片
if (ccf->rlimit_nofile != NGX_CONF_UNSET) { rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile; rlmt.rlim_max = (rlim_t) ccf->rlimit_nofile; if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "setrlimit(RLIMIT_NOFILE, %i) failed", ccf->rlimit_nofile); } }

设置打开的最大的文件数。
if (ccf->rlimit_core != NGX_CONF_UNSET) { rlmt.rlim_cur = (rlim_t) ccf->rlimit_core; rlmt.rlim_max = (rlim_t) ccf->rlimit_core; if (setrlimit(RLIMIT_CORE, &rlmt) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "setrlimit(RLIMIT_CORE, %O) failed", ccf->rlimit_core); } }

最大打开文件的大小。
if (geteuid() == 0) { if (setgid(ccf->group) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "setgid(%d) failed", ccf->group); /* fatal */ exit(2); }if (initgroups(ccf->username, ccf->group) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "initgroups(%s, %d) failed", ccf->username, ccf->group); }#if (NGX_HAVE_PR_SET_KEEPCAPS && NGX_HAVE_CAPABILITIES) if (ccf->transparent && ccf->user) { if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "prctl(PR_SET_KEEPCAPS, 1) failed"); /* fatal */ exit(2); } } #endifif (setuid(ccf->user) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "setuid(%d) failed", ccf->user); /* fatal */ exit(2); }#if (NGX_HAVE_CAPABILITIES) if (ccf->transparent && ccf->user) { struct __user_cap_data_structdata; struct __user_cap_header_structheader; ngx_memzero(&header, sizeof(struct __user_cap_header_struct)); ngx_memzero(&data, sizeof(struct __user_cap_data_struct)); header.version = _LINUX_CAPABILITY_VERSION_1; data.effective = CAP_TO_MASK(CAP_NET_RAW); data.permitted = data.effective; if (syscall(SYS_capset, &header, &data) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "capset() failed"); /* fatal */ exit(2); } } #endif }

判断执行进程的是否是root用户,支持root用户配置group、username、user等等参数。
if (worker >= 0) { cpu_affinity = ngx_get_cpu_affinity(worker); if (cpu_affinity) { ngx_setaffinity(cpu_affinity, cycle->log); } }

设置进程与cpu的亲和性。主要是为了防止nginx在长时间运行过程中,被迁移到其他处理器上。既设置cpu_affinity可以指定cpu运行,这样可以降低nginx进行迁移的时候,对cpu产生的压力。
if (ccf->working_directory.len) { if (chdir((char *) ccf->working_directory.data) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "chdir(\"%s\") failed", ccf->working_directory.data); /* fatal */ exit(2); } }

指定进程的工作目录。
sigemptyset(&set); if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "sigprocmask() failed"); }

初始化信号。
tp = ngx_timeofday(); srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);

获取缓存时间,设置一个随机数种子,这个流程里面还没有看到哪里在用。。。
/* * disable deleting previous events for the listening sockets because * in the worker processes there are no events at all at this point */ ls = cycle->listening.elts; for (i = 0; i < cycle->listening.nelts; i++) { ls[i].previous = NULL; }

【nginx源码|Nginx源码分析——worker进程源码与工作原理(二)】
清空cycle->listening里面的已经关联好的套接字,在worker进程中它不在被使用。
for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->init_process) { if (cycle->modules[i]->init_process(cycle) == NGX_ERROR) { /* fatal */ exit(2); } } }

遍历所有modules模块,init_process方法不为空的时候执行。init_process方法初始化在,先看module的定义。
ngx_module_tngx_event_core_module = { NGX_MODULE_V1, &ngx_event_core_module_ctx,/* module context */ ngx_event_core_commands,/* module directives */ NGX_EVENT_MODULE,/* module type */ NULL,/* init master */ ngx_event_module_init,/* init module */ ngx_event_process_init,/* init process */ NULL,/* init thread */ NULL,/* exit thread */ NULL,/* exit process */ NULL,/* exit master */ NGX_MODULE_V1_PADDING };

执行的是ngx_event_process_init函数,这里初始化了很多事件后面再去展开去讲。//TODO 解析ngx_event_process_init
for (n = 0; n < ngx_last_process; n++) {if (ngx_processes[n].pid == -1) { continue; }if (n == ngx_process_slot) { continue; }if (ngx_processes[n].channel[1] == -1) { continue; }if (close(ngx_processes[n].channel[1]) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "close() channel failed"); } }

之前说过,在ngx_processes里面的channel是一对套接字,用于进程之间的通信,当前进程没有必要读其他进程的套接字,所有进行关闭close(ngx_processes[n].channel[1])。
if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "close() channel failed"); }

当前ngx_processes[ngx_process_slot].channel[0],用于对当前进程进行写入,因此使用不到,进行关闭。
if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT, ngx_channel_handler) == NGX_ERROR) { /* fatal */ exit(2); }

ngx_channel就是现在操作的进程的channel数组,绑定对应的读事件,并且绑定ngx_channel_handler,具体ngx_add_channel_event、ngx_channel_handler里面做什么。。。继续埋坑。//TODO ngx_add_channel_event、ngx_channel_handler 总结 本章看起来是在简单的陈述整体的流程,但是对于之前将的信息以及没有讲过信息都会比较整体的去应用到,所以整体在思路整理与说清楚前后逻辑这块想了比较就。

    推荐阅读