Merge of r4913: fixed CPU affinity on respawn of dead workers.
Worker processes are now made aware of their sequential number needed
to select CPU affinity mask. This replaces a workaround from r4865.
Maxim Dounin
8 years ago
19 | 19 | static ngx_uint_t ngx_reap_children(ngx_cycle_t *cycle); |
20 | 20 | static void ngx_master_process_exit(ngx_cycle_t *cycle); |
21 | 21 | static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); |
22 | static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority); | |
22 | static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker); | |
23 | 23 | static void ngx_worker_process_exit(ngx_cycle_t *cycle); |
24 | 24 | static void ngx_channel_handler(ngx_event_t *ev); |
25 | 25 | #if (NGX_THREADS) |
61 | 61 | #endif |
62 | 62 | |
63 | 63 | |
64 | uint64_t cpu_affinity; | |
65 | 64 | static u_char master_process[] = "master process"; |
66 | 65 | |
67 | 66 | |
359 | 358 | |
360 | 359 | for (i = 0; i < n; i++) { |
361 | 360 | |
362 | cpu_affinity = ngx_get_cpu_affinity(i); | |
363 | ||
364 | ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL, | |
365 | "worker process", type); | |
361 | ngx_spawn_process(cycle, ngx_worker_process_cycle, | |
362 | (void *) (intptr_t) i, "worker process", type); | |
366 | 363 | |
367 | 364 | ch.pid = ngx_processes[ngx_process_slot].pid; |
368 | 365 | ch.slot = ngx_process_slot; |
370 | 367 | |
371 | 368 | ngx_pass_open_channel(cycle, &ch); |
372 | 369 | } |
373 | ||
374 | cpu_affinity = 0; | |
375 | 370 | } |
376 | 371 | |
377 | 372 | |
725 | 720 | static void |
726 | 721 | ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) |
727 | 722 | { |
723 | ngx_int_t worker = (intptr_t) data; | |
724 | ||
728 | 725 | ngx_uint_t i; |
729 | 726 | ngx_connection_t *c; |
730 | 727 | |
731 | 728 | ngx_process = NGX_PROCESS_WORKER; |
732 | 729 | |
733 | ngx_worker_process_init(cycle, 1); | |
730 | ngx_worker_process_init(cycle, worker); | |
734 | 731 | |
735 | 732 | ngx_setproctitle("worker process"); |
736 | 733 | |
836 | 833 | |
837 | 834 | |
838 | 835 | static void |
839 | ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority) | |
836 | ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker) | |
840 | 837 | { |
841 | 838 | sigset_t set; |
839 | uint64_t cpu_affinity; | |
842 | 840 | ngx_int_t n; |
843 | 841 | ngx_uint_t i; |
844 | 842 | struct rlimit rlmt; |
852 | 850 | |
853 | 851 | ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
854 | 852 | |
855 | if (priority && ccf->priority != 0) { | |
853 | if (worker >= 0 && ccf->priority != 0) { | |
856 | 854 | if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) { |
857 | 855 | ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
858 | 856 | "setpriority(%d) failed", ccf->priority); |
916 | 914 | } |
917 | 915 | } |
918 | 916 | |
919 | if (cpu_affinity) { | |
920 | ngx_setaffinity(cpu_affinity, cycle->log); | |
917 | if (worker >= 0) { | |
918 | cpu_affinity = ngx_get_cpu_affinity(worker); | |
919 | ||
920 | if (cpu_affinity) { | |
921 | ngx_setaffinity(cpu_affinity, cycle->log); | |
922 | } | |
921 | 923 | } |
922 | 924 | |
923 | 925 | #if (NGX_HAVE_PR_SET_DUMPABLE) |
1297 | 1299 | |
1298 | 1300 | ngx_process = NGX_PROCESS_HELPER; |
1299 | 1301 | |
1300 | ngx_worker_process_init(cycle, 0); | |
1302 | ngx_worker_process_init(cycle, -1); | |
1301 | 1303 | |
1302 | 1304 | ngx_close_listening_sockets(cycle); |
1303 | 1305 |