*) share temporary number between workers

*) randomize collision offset
This commit is contained in:
Igor Sysoev 2009-08-21 09:06:35 +00:00
parent f95847d0ba
commit 3a58935936
5 changed files with 34 additions and 36 deletions

View File

@ -8,8 +8,9 @@
#include <ngx_core.h>
static ngx_atomic_uint_t ngx_temp_number;
static ngx_atomic_uint_t ngx_random_number;
static ngx_atomic_t temp_number = 0;
ngx_atomic_t *ngx_temp_number = &temp_number;
ngx_atomic_int_t ngx_random_number = 123456;
ssize_t
@ -205,22 +206,16 @@ ngx_create_full_path(u_char *dir, ngx_uint_t access)
}
void
ngx_init_temp_number(void)
{
ngx_temp_number = 0;
ngx_random_number = 123456;
}
ngx_atomic_uint_t
ngx_next_temp_number(ngx_uint_t collision)
{
if (collision) {
ngx_temp_number += ngx_random_number;
}
ngx_atomic_uint_t n, add;
return ngx_temp_number++;
add = collision ? ngx_random_number : 1;
n = ngx_atomic_fetch_add(ngx_temp_number, add);
return n + add;
}

View File

@ -129,7 +129,6 @@ ngx_int_t ngx_ext_rename_file(ngx_str_t *src, ngx_str_t *to,
ngx_int_t ngx_copy_file(u_char *from, u_char *to, ngx_copy_file_t *cf);
ngx_int_t ngx_walk_tree(ngx_tree_ctx_t *ctx, ngx_str_t *tree);
void ngx_init_temp_number(void);
ngx_atomic_uint_t ngx_next_temp_number(ngx_uint_t collision);
char *ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@ -138,4 +137,8 @@ char *ngx_conf_merge_path_value(ngx_conf_t *cf, ngx_path_t **path,
char *ngx_conf_set_access_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
extern ngx_atomic_t *ngx_temp_number;
extern ngx_atomic_int_t ngx_random_number;
#endif /* _NGX_FILE_H_INCLUDED_ */

View File

@ -43,7 +43,7 @@ ngx_uint_t ngx_event_flags;
ngx_event_actions_t ngx_event_actions;
ngx_atomic_t connection_counter = 1;
static ngx_atomic_t connection_counter = 1;
ngx_atomic_t *ngx_connection_counter = &connection_counter;
@ -429,6 +429,7 @@ ngx_event_module_init(ngx_cycle_t *cycle)
u_char *shared;
size_t size, cl;
ngx_shm_t shm;
ngx_time_t *tp;
ngx_core_conf_t *ccf;
ngx_event_conf_t *ecf;
@ -492,7 +493,8 @@ ngx_event_module_init(ngx_cycle_t *cycle)
cl = 128;
size = cl /* ngx_accept_mutex */
+ cl; /* ngx_connection_counter */
+ cl /* ngx_connection_counter */
+ cl; /* ngx_temp_number */
#if (NGX_STAT_STUB)
@ -526,23 +528,29 @@ ngx_event_module_init(ngx_cycle_t *cycle)
ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
#if (NGX_STAT_STUB)
ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * cl);
ngx_stat_handled = (ngx_atomic_t *) (shared + 3 * cl);
ngx_stat_requests = (ngx_atomic_t *) (shared + 4 * cl);
ngx_stat_active = (ngx_atomic_t *) (shared + 5 * cl);
ngx_stat_reading = (ngx_atomic_t *) (shared + 6 * cl);
ngx_stat_writing = (ngx_atomic_t *) (shared + 7 * cl);
#endif
(void) ngx_atomic_cmp_set(ngx_connection_counter, 0, 1);
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"counter: %p, %d",
ngx_connection_counter, *ngx_connection_counter);
ngx_temp_number = (ngx_atomic_t *) (shared + 2 * cl);
tp = ngx_timeofday();
ngx_random_number = (tp->msec << 16) + ngx_pid;
#if (NGX_STAT_STUB)
ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
#endif
return NGX_OK;
}

View File

@ -277,8 +277,6 @@ ngx_single_process_cycle(ngx_cycle_t *cycle)
{
ngx_uint_t i;
ngx_init_temp_number();
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_process) {
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
@ -930,8 +928,6 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
"sigprocmask() failed");
}
ngx_init_temp_number();
/*
* disable deleting previous events for the listening sockets because
* in the worker processes there are no events at all at this point

View File

@ -793,8 +793,6 @@ ngx_worker_thread(void *data)
cycle = (ngx_cycle_t *) ngx_cycle;
ngx_init_temp_number();
for (n = 0; ngx_modules[n]; n++) {
if (ngx_modules[n]->init_process) {
if (ngx_modules[n]->init_process(cycle) == NGX_ERROR) {
@ -1024,8 +1022,6 @@ ngx_single_process_cycle(ngx_cycle_t *cycle)
ngx_int_t i;
ngx_tid_t tid;
ngx_init_temp_number();
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_process) {
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {