comparison src/event/ngx_event.c @ 160:73e8476f9142 NGINX_0_3_27

nginx 0.3.27 *) Change: the "variables_hash_max_size" and "variables_hash_bucket_size" directives. *) Feature: the $body_bytes_sent variable can be used not only in the "log_format" directive. *) Feature: the $ssl_protocol and $ssl_cipher variables. *) Feature: the cache line size detection for widespread CPUs at start time. *) Feature: now the "accept_mutex" directive is supported using fcntl(2) on platforms different from i386, amd64, sparc64, and ppc. *) Feature: the "lock_file" directive and the --with-lock-path=PATH autoconfiguration directive. *) Bugfix: if the HTTPS protocol was used in the "proxy_pass" directive then the requests with the body was not transferred.
author Igor Sysoev <http://sysoev.ru>
date Wed, 08 Feb 2006 00:00:00 +0300
parents bb61aa162c6b
children 3689cd4e3228
comparison
equal deleted inserted replaced
159:25c27e983933 160:73e8476f9142
29 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, 29 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
30 void *conf); 30 void *conf);
31 31
32 static void *ngx_event_create_conf(ngx_cycle_t *cycle); 32 static void *ngx_event_create_conf(ngx_cycle_t *cycle);
33 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf); 33 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
34 static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data);
35 34
36 35
37 static ngx_uint_t ngx_timer_resolution; 36 static ngx_uint_t ngx_timer_resolution;
38 sig_atomic_t ngx_event_timer_alarm; 37 sig_atomic_t ngx_event_timer_alarm;
39 38
46 ngx_atomic_t connection_counter = 1; 45 ngx_atomic_t connection_counter = 1;
47 ngx_atomic_t *ngx_connection_counter = &connection_counter; 46 ngx_atomic_t *ngx_connection_counter = &connection_counter;
48 47
49 48
50 ngx_atomic_t *ngx_accept_mutex_ptr; 49 ngx_atomic_t *ngx_accept_mutex_ptr;
51 ngx_atomic_t *ngx_accept_mutex; 50 ngx_shmtx_t ngx_accept_mutex;
51 ngx_uint_t ngx_use_accept_mutex;
52 ngx_uint_t ngx_accept_mutex_held; 52 ngx_uint_t ngx_accept_mutex_held;
53 ngx_msec_t ngx_accept_mutex_delay; 53 ngx_msec_t ngx_accept_mutex_delay;
54 ngx_int_t ngx_accept_disabled; 54 ngx_int_t ngx_accept_disabled;
55 ngx_file_t ngx_accept_mutex_lock_file; 55 ngx_file_t ngx_accept_mutex_lock_file;
56 56
110 }; 110 };
111 111
112 112
113 static ngx_str_t event_core_name = ngx_string("event_core"); 113 static ngx_str_t event_core_name = ngx_string("event_core");
114 114
115 static ngx_conf_post_t ngx_accept_mutex_post = { ngx_accept_mutex_check } ;
116
117 115
118 static ngx_command_t ngx_event_core_commands[] = { 116 static ngx_command_t ngx_event_core_commands[] = {
119 117
120 { ngx_string("worker_connections"), 118 { ngx_string("worker_connections"),
121 NGX_EVENT_CONF|NGX_CONF_TAKE1, 119 NGX_EVENT_CONF|NGX_CONF_TAKE1,
148 { ngx_string("accept_mutex"), 146 { ngx_string("accept_mutex"),
149 NGX_EVENT_CONF|NGX_CONF_TAKE1, 147 NGX_EVENT_CONF|NGX_CONF_TAKE1,
150 ngx_conf_set_flag_slot, 148 ngx_conf_set_flag_slot,
151 0, 149 0,
152 offsetof(ngx_event_conf_t, accept_mutex), 150 offsetof(ngx_event_conf_t, accept_mutex),
153 &ngx_accept_mutex_post }, 151 NULL },
154 152
155 { ngx_string("accept_mutex_delay"), 153 { ngx_string("accept_mutex_delay"),
156 NGX_EVENT_CONF|NGX_CONF_TAKE1, 154 NGX_EVENT_CONF|NGX_CONF_TAKE1,
157 ngx_conf_set_msec_slot, 155 ngx_conf_set_msec_slot,
158 0, 156 0,
216 } 214 }
217 215
218 #endif 216 #endif
219 } 217 }
220 218
221 if (ngx_accept_mutex) { 219 if (ngx_use_accept_mutex) {
222 if (ngx_accept_disabled > 0) { 220 if (ngx_accept_disabled > 0) {
223 ngx_accept_disabled--; 221 ngx_accept_disabled--;
224 222
225 } else { 223 } else {
226 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) { 224 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
252 if (ngx_posted_accept_events) { 250 if (ngx_posted_accept_events) {
253 ngx_event_process_posted(cycle, &ngx_posted_accept_events); 251 ngx_event_process_posted(cycle, &ngx_posted_accept_events);
254 } 252 }
255 253
256 if (ngx_accept_mutex_held) { 254 if (ngx_accept_mutex_held) {
257 (void) ngx_atomic_cmp_set(ngx_accept_mutex, ngx_pid, 0); 255 ngx_shmtx_unlock(&ngx_accept_mutex);
258 } 256 }
259 257
260 if (delta) { 258 if (delta) {
261 ngx_event_expire_timers(); 259 ngx_event_expire_timers();
262 } 260 }
413 static ngx_int_t 411 static ngx_int_t
414 ngx_event_module_init(ngx_cycle_t *cycle) 412 ngx_event_module_init(ngx_cycle_t *cycle)
415 { 413 {
416 void ***cf; 414 void ***cf;
417 u_char *shared; 415 u_char *shared;
418 size_t size; 416 size_t size, cl;
419 ngx_event_conf_t *ecf; 417 ngx_event_conf_t *ecf;
420 ngx_core_conf_t *ccf; 418 ngx_core_conf_t *ccf;
421 ngx_shm_t shm; 419 ngx_shm_t shm;
422 #if !(NGX_WIN32) 420 #if !(NGX_WIN32)
423 ngx_int_t limit; 421 ngx_int_t limit;
463 } 461 }
464 462
465 #endif /* !(NGX_WIN32) */ 463 #endif /* !(NGX_WIN32) */
466 464
467 465
468 if (ccf->master == 0 || ngx_accept_mutex_ptr) { 466 if (ccf->master == 0) {
469 return NGX_OK; 467 return NGX_OK;
470 } 468 }
471 469
472 470
473 /* TODO: adjust cache line size, 128 is P4 cache line size */ 471 if (ngx_accept_mutex_ptr) {
474 472
475 size = 128 /* ngx_accept_mutex */ 473 /* reinit ngx_accept_mutex */
476 + 128; /* ngx_connection_counter */ 474
475 if (ngx_shmtx_create(&ngx_accept_mutex, (void *) ngx_accept_mutex_ptr,
476 ccf->lock_file.data, cycle->log)
477 != NGX_OK)
478 {
479 return NGX_ERROR;
480 }
481
482 return NGX_OK;
483 }
484
485
486 /* cl should be equal or bigger than cache line size */
487
488 cl = 128;
489
490 size = cl /* ngx_accept_mutex */
491 + cl; /* ngx_connection_counter */
477 492
478 #if (NGX_STAT_STUB) 493 #if (NGX_STAT_STUB)
479 494
480 size += 128 /* ngx_stat_accepted */ 495 size += cl /* ngx_stat_accepted */
481 + 128 /* ngx_stat_handled */ 496 + cl /* ngx_stat_handled */
482 + 128 /* ngx_stat_requests */ 497 + cl /* ngx_stat_requests */
483 + 128 /* ngx_stat_active */ 498 + cl /* ngx_stat_active */
484 + 128 /* ngx_stat_reading */ 499 + cl /* ngx_stat_reading */
485 + 128; /* ngx_stat_writing */ 500 + cl; /* ngx_stat_writing */
486 501
487 #endif 502 #endif
488 503
489 shm.size = size; 504 shm.size = size;
490 shm.log = cycle->log; 505 shm.log = cycle->log;
494 } 509 }
495 510
496 shared = shm.addr; 511 shared = shm.addr;
497 512
498 ngx_accept_mutex_ptr = (ngx_atomic_t *) shared; 513 ngx_accept_mutex_ptr = (ngx_atomic_t *) shared;
499 ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * 128); 514
515 if (ngx_shmtx_create(&ngx_accept_mutex, shared, ccf->lock_file.data,
516 cycle->log)
517 != NGX_OK)
518 {
519 return NGX_ERROR;
520 }
521
522 ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
500 523
501 #if (NGX_STAT_STUB) 524 #if (NGX_STAT_STUB)
502 525
503 ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * 128); 526 ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * cl);
504 ngx_stat_handled = (ngx_atomic_t *) (shared + 3 * 128); 527 ngx_stat_handled = (ngx_atomic_t *) (shared + 3 * cl);
505 ngx_stat_requests = (ngx_atomic_t *) (shared + 4 * 128); 528 ngx_stat_requests = (ngx_atomic_t *) (shared + 4 * cl);
506 ngx_stat_active = (ngx_atomic_t *) (shared + 5 * 128); 529 ngx_stat_active = (ngx_atomic_t *) (shared + 5 * cl);
507 ngx_stat_reading = (ngx_atomic_t *) (shared + 6 * 128); 530 ngx_stat_reading = (ngx_atomic_t *) (shared + 6 * cl);
508 ngx_stat_writing = (ngx_atomic_t *) (shared + 7 * 128); 531 ngx_stat_writing = (ngx_atomic_t *) (shared + 7 * cl);
509 532
510 #endif 533 #endif
511 534
512 *ngx_connection_counter = 1; 535 *ngx_connection_counter = 1;
513 536
555 #endif 578 #endif
556 579
557 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 580 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
558 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); 581 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
559 582
560 if (ngx_accept_mutex_ptr && ccf->worker_processes > 1 && ecf->accept_mutex) 583 if (ccf->worker_processes > 1 && ecf->accept_mutex) {
561 { 584 ngx_use_accept_mutex = 1;
562 ngx_accept_mutex = ngx_accept_mutex_ptr;
563 ngx_accept_mutex_held = 0; 585 ngx_accept_mutex_held = 0;
564 ngx_accept_mutex_delay = ecf->accept_mutex_delay; 586 ngx_accept_mutex_delay = ecf->accept_mutex_delay;
587
588 } else {
589 ngx_use_accept_mutex = 0;
565 } 590 }
566 591
567 #if (NGX_THREADS) 592 #if (NGX_THREADS)
568 ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0); 593 ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0);
569 if (ngx_posted_events_mutex == NULL) { 594 if (ngx_posted_events_mutex == NULL) {
773 798
774 #else 799 #else
775 800
776 rev->handler = ngx_event_accept; 801 rev->handler = ngx_event_accept;
777 802
778 if (ngx_accept_mutex) { 803 if (ngx_use_accept_mutex) {
779 continue; 804 continue;
780 } 805 }
781 806
782 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) { 807 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
783 if (ngx_add_conn(c) == NGX_ERROR) { 808 if (ngx_add_conn(c) == NGX_ERROR) {
1219 1244
1220 return NGX_CONF_OK; 1245 return NGX_CONF_OK;
1221 1246
1222 #endif 1247 #endif
1223 } 1248 }
1224
1225
1226 static char *
1227 ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data)
1228 {
1229 #if !(NGX_HAVE_ATOMIC_OPS)
1230
1231 ngx_flag_t *fp = data;
1232
1233 *fp = 0;
1234
1235 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1236 "\"accept_mutex\" is not supported on this platform, "
1237 "ignored");
1238
1239 #endif
1240
1241 return NGX_CONF_OK;
1242 }