Klaus Demo nginx / 3a58935
*) share temporary number between workers *) randomize collision offset Igor Sysoev 12 years ago
5 changed file(s) with 35 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
77 #include <ngx_core.h>
88
99
10 static ngx_atomic_uint_t ngx_temp_number;
11 static ngx_atomic_uint_t ngx_random_number;
10 static ngx_atomic_t temp_number = 0;
11 ngx_atomic_t *ngx_temp_number = &temp_number;
12 ngx_atomic_int_t ngx_random_number = 123456;
1213
1314
1415 ssize_t
204205 }
205206
206207
207 void
208 ngx_init_temp_number(void)
209 {
210 ngx_temp_number = 0;
211 ngx_random_number = 123456;
212 }
213
214
215208 ngx_atomic_uint_t
216209 ngx_next_temp_number(ngx_uint_t collision)
217210 {
218 if (collision) {
219 ngx_temp_number += ngx_random_number;
220 }
221
222 return ngx_temp_number++;
211 ngx_atomic_uint_t n, add;
212
213 add = collision ? ngx_random_number : 1;
214
215 n = ngx_atomic_fetch_add(ngx_temp_number, add);
216
217 return n + add;
223218 }
224219
225220
128128 ngx_int_t ngx_copy_file(u_char *from, u_char *to, ngx_copy_file_t *cf);
129129 ngx_int_t ngx_walk_tree(ngx_tree_ctx_t *ctx, ngx_str_t *tree);
130130
131 void ngx_init_temp_number(void);
132131 ngx_atomic_uint_t ngx_next_temp_number(ngx_uint_t collision);
133132
134133 char *ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
137136 char *ngx_conf_set_access_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
138137
139138
139 extern ngx_atomic_t *ngx_temp_number;
140 extern ngx_atomic_int_t ngx_random_number;
141
142
140143 #endif /* _NGX_FILE_H_INCLUDED_ */
4242 ngx_event_actions_t ngx_event_actions;
4343
4444
45 ngx_atomic_t connection_counter = 1;
45 static ngx_atomic_t connection_counter = 1;
4646 ngx_atomic_t *ngx_connection_counter = &connection_counter;
4747
4848
428428 u_char *shared;
429429 size_t size, cl;
430430 ngx_shm_t shm;
431 ngx_time_t *tp;
431432 ngx_core_conf_t *ccf;
432433 ngx_event_conf_t *ecf;
433434
491492 cl = 128;
492493
493494 size = cl /* ngx_accept_mutex */
494 + cl; /* ngx_connection_counter */
495 + cl /* ngx_connection_counter */
496 + cl; /* ngx_temp_number */
495497
496498 #if (NGX_STAT_STUB)
497499
525527
526528 ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
527529
528 #if (NGX_STAT_STUB)
529
530 ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * cl);
531 ngx_stat_handled = (ngx_atomic_t *) (shared + 3 * cl);
532 ngx_stat_requests = (ngx_atomic_t *) (shared + 4 * cl);
533 ngx_stat_active = (ngx_atomic_t *) (shared + 5 * cl);
534 ngx_stat_reading = (ngx_atomic_t *) (shared + 6 * cl);
535 ngx_stat_writing = (ngx_atomic_t *) (shared + 7 * cl);
536
537 #endif
538
539530 (void) ngx_atomic_cmp_set(ngx_connection_counter, 0, 1);
540531
541532 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
542533 "counter: %p, %d",
543534 ngx_connection_counter, *ngx_connection_counter);
535
536 ngx_temp_number = (ngx_atomic_t *) (shared + 2 * cl);
537
538 tp = ngx_timeofday();
539
540 ngx_random_number = (tp->msec << 16) + ngx_pid;
541
542 #if (NGX_STAT_STUB)
543
544 ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
545 ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
546 ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
547 ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
548 ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
549 ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
550
551 #endif
544552
545553 return NGX_OK;
546554 }
276276 {
277277 ngx_uint_t i;
278278
279 ngx_init_temp_number();
280
281279 for (i = 0; ngx_modules[i]; i++) {
282280 if (ngx_modules[i]->init_process) {
283281 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
929927 "sigprocmask() failed");
930928 }
931929
932 ngx_init_temp_number();
933
934930 /*
935931 * disable deleting previous events for the listening sockets because
936932 * in the worker processes there are no events at all at this point
792792
793793 cycle = (ngx_cycle_t *) ngx_cycle;
794794
795 ngx_init_temp_number();
796
797795 for (n = 0; ngx_modules[n]; n++) {
798796 if (ngx_modules[n]->init_process) {
799797 if (ngx_modules[n]->init_process(cycle) == NGX_ERROR) {
10231021 ngx_int_t i;
10241022 ngx_tid_t tid;
10251023
1026 ngx_init_temp_number();
1027
10281024 for (i = 0; ngx_modules[i]; i++) {
10291025 if (ngx_modules[i]->init_process) {
10301026 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {