changeset 4950:c355ca8f2b2c stable-1.2

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.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 10 Dec 2012 16:00:55 +0000
parents 54eb08ebe8cf
children 9ab61d17794f
files src/os/unix/ngx_process_cycle.c
diffstat 1 files changed, 16 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -20,7 +20,7 @@ static void ngx_signal_worker_processes(
 static ngx_uint_t ngx_reap_children(ngx_cycle_t *cycle);
 static void ngx_master_process_exit(ngx_cycle_t *cycle);
 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
-static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority);
+static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker);
 static void ngx_worker_process_exit(ngx_cycle_t *cycle);
 static void ngx_channel_handler(ngx_event_t *ev);
 #if (NGX_THREADS)
@@ -62,7 +62,6 @@ ngx_int_t              ngx_threads_n;
 #endif
 
 
-uint64_t       cpu_affinity;
 static u_char  master_process[] = "master process";
 
 
@@ -360,10 +359,8 @@ ngx_start_worker_processes(ngx_cycle_t *
 
     for (i = 0; i < n; i++) {
 
-        cpu_affinity = ngx_get_cpu_affinity(i);
-
-        ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
-                          "worker process", type);
+        ngx_spawn_process(cycle, ngx_worker_process_cycle,
+                          (void *) (intptr_t) i, "worker process", type);
 
         ch.pid = ngx_processes[ngx_process_slot].pid;
         ch.slot = ngx_process_slot;
@@ -371,8 +368,6 @@ ngx_start_worker_processes(ngx_cycle_t *
 
         ngx_pass_open_channel(cycle, &ch);
     }
-
-    cpu_affinity = 0;
 }
 
 
@@ -726,12 +721,14 @@ ngx_master_process_exit(ngx_cycle_t *cyc
 static void
 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
 {
+    ngx_int_t worker = (intptr_t) data;
+
     ngx_uint_t         i;
     ngx_connection_t  *c;
 
     ngx_process = NGX_PROCESS_WORKER;
 
-    ngx_worker_process_init(cycle, 1);
+    ngx_worker_process_init(cycle, worker);
 
     ngx_setproctitle("worker process");
 
@@ -837,9 +834,10 @@ ngx_worker_process_cycle(ngx_cycle_t *cy
 
 
 static void
-ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
+ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
 {
     sigset_t          set;
+    uint64_t          cpu_affinity;
     ngx_int_t         n;
     ngx_uint_t        i;
     struct rlimit     rlmt;
@@ -853,7 +851,7 @@ ngx_worker_process_init(ngx_cycle_t *cyc
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    if (priority && ccf->priority != 0) {
+    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);
@@ -917,8 +915,12 @@ ngx_worker_process_init(ngx_cycle_t *cyc
         }
     }
 
-    if (cpu_affinity) {
-        ngx_setaffinity(cpu_affinity, cycle->log);
+    if (worker >= 0) {
+        cpu_affinity = ngx_get_cpu_affinity(worker);
+
+        if (cpu_affinity) {
+            ngx_setaffinity(cpu_affinity, cycle->log);
+        }
     }
 
 #if (NGX_HAVE_PR_SET_DUMPABLE)
@@ -1298,7 +1300,7 @@ ngx_cache_manager_process_cycle(ngx_cycl
 
     ngx_process = NGX_PROCESS_HELPER;
 
-    ngx_worker_process_init(cycle, 0);
+    ngx_worker_process_init(cycle, -1);
 
     ngx_close_listening_sockets(cycle);