diff options
| author | Igor Sysoev <igor@sysoev.ru> | 2009-06-15 08:43:10 +0000 |
|---|---|---|
| committer | Igor Sysoev <igor@sysoev.ru> | 2009-06-15 08:43:10 +0000 |
| commit | c4f6d11856d0d5dbf1f02f107dd90f94fe70963d (patch) | |
| tree | 3f132341628fe8fa77a7e00ce26716104bb23a18 | |
| parent | 1bf967880effd1b03ae1be106b790bf55590f0b6 (diff) | |
| download | nginx-c4f6d11856d0d5dbf1f02f107dd90f94fe70963d.tar.gz nginx-c4f6d11856d0d5dbf1f02f107dd90f94fe70963d.tar.bz2 | |
merge r2889, r2918, and r2921:
fix a pool growing and CPU usage for multi-requests keepalive connections:
*) keepalive_requests
*) try to reuse last 4 free large allocation links
*) do not test a pool block space if we can not allocated from
the block 4 times
| -rw-r--r-- | src/core/ngx_connection.h | 2 | ||||
| -rw-r--r-- | src/core/ngx_palloc.c | 18 | ||||
| -rw-r--r-- | src/core/ngx_palloc.h | 1 | ||||
| -rw-r--r-- | src/http/ngx_http_core_module.c | 19 | ||||
| -rw-r--r-- | src/http/ngx_http_core_module.h | 1 | ||||
| -rw-r--r-- | src/http/ngx_http_request.c | 2 |
6 files changed, 40 insertions, 3 deletions
diff --git a/src/core/ngx_connection.h b/src/core/ngx_connection.h index 5fd7a5ead..689b5f0e9 100644 --- a/src/core/ngx_connection.h +++ b/src/core/ngx_connection.h @@ -135,6 +135,8 @@ struct ngx_connection_s { ngx_atomic_uint_t number; + ngx_uint_t requests; + unsigned buffered:8; unsigned log_error:3; /* ngx_connection_log_error_e */ diff --git a/src/core/ngx_palloc.c b/src/core/ngx_palloc.c index 3e1c9f2ad..fd0476803 100644 --- a/src/core/ngx_palloc.c +++ b/src/core/ngx_palloc.c @@ -25,6 +25,7 @@ ngx_create_pool(size_t size, ngx_log_t *log) p->d.last = (u_char *) p + sizeof(ngx_pool_t); p->d.end = (u_char *) p + size; p->d.next = NULL; + p->d.failed = 0; size = size - sizeof(ngx_pool_t); p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; @@ -189,6 +190,7 @@ ngx_palloc_block(ngx_pool_t *pool, size_t size) new->d.end = m + psize; new->d.next = NULL; + new->d.failed = 0; m += sizeof(ngx_pool_data_t); m = ngx_align_ptr(m, NGX_ALIGNMENT); @@ -197,7 +199,7 @@ ngx_palloc_block(ngx_pool_t *pool, size_t size) current = pool->current; for (p = current; p->d.next; p = p->d.next) { - if ((size_t) (p->d.end - p->d.last) < NGX_ALIGNMENT) { + if (p->d.failed++ > 4) { current = p->d.next; } } @@ -214,6 +216,7 @@ static void * ngx_palloc_large(ngx_pool_t *pool, size_t size) { void *p; + ngx_uint_t n; ngx_pool_large_t *large; p = ngx_alloc(size, pool->log); @@ -221,6 +224,19 @@ ngx_palloc_large(ngx_pool_t *pool, size_t size) return NULL; } + n = 0; + + for (large = pool->large; large; large = large->next) { + if (large->alloc == NULL) { + large->alloc = p; + return p; + } + + if (n++ > 3) { + break; + } + } + large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); if (large == NULL) { ngx_free(p); diff --git a/src/core/ngx_palloc.h b/src/core/ngx_palloc.h index f9d5216ed..7706df19c 100644 --- a/src/core/ngx_palloc.h +++ b/src/core/ngx_palloc.h @@ -46,6 +46,7 @@ typedef struct { u_char *last; u_char *end; ngx_pool_t *next; + ngx_uint_t failed; } ngx_pool_data_t; diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c index 966cbc270..538ef7214 100644 --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -440,6 +440,13 @@ static ngx_command_t ngx_http_core_commands[] = { 0, NULL }, + { ngx_string("keepalive_requests"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_core_loc_conf_t, keepalive_requests), + NULL }, + { ngx_string("satisfy"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_enum_slot, @@ -1326,8 +1333,13 @@ ngx_http_update_location_config(ngx_http_request_t *r) r->request_body_in_single_buf = clcf->client_body_in_single_buffer; - if (r->keepalive && clcf->keepalive_timeout == 0) { - r->keepalive = 0; + if (r->keepalive) { + if (clcf->keepalive_timeout == 0) { + r->keepalive = 0; + + } else if (r->connection->requests >= clcf->keepalive_requests) { + r->keepalive = 0; + } } if (!clcf->tcp_nopush) { @@ -2914,6 +2926,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf) lcf->limit_rate = NGX_CONF_UNSET_SIZE; lcf->keepalive_timeout = NGX_CONF_UNSET_MSEC; lcf->keepalive_header = NGX_CONF_UNSET; + lcf->keepalive_requests = NGX_CONF_UNSET_UINT; lcf->lingering_time = NGX_CONF_UNSET_MSEC; lcf->lingering_timeout = NGX_CONF_UNSET_MSEC; lcf->resolver_timeout = NGX_CONF_UNSET_MSEC; @@ -3114,6 +3127,8 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) prev->keepalive_timeout, 75000); ngx_conf_merge_sec_value(conf->keepalive_header, prev->keepalive_header, 0); + ngx_conf_merge_uint_value(conf->keepalive_requests, + prev->keepalive_requests, 100); ngx_conf_merge_msec_value(conf->lingering_time, prev->lingering_time, 30000); ngx_conf_merge_msec_value(conf->lingering_timeout, diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h index dfc07facc..650183f05 100644 --- a/src/http/ngx_http_core_module.h +++ b/src/http/ngx_http_core_module.h @@ -337,6 +337,7 @@ struct ngx_http_core_loc_conf_s { time_t keepalive_header; /* keepalive_timeout */ + ngx_uint_t keepalive_requests; /* keepalive_requests */ ngx_uint_t satisfy; /* satisfy */ ngx_uint_t if_modified_since; /* if_modified_since */ ngx_uint_t client_body_in_file_only; /* client_body_in_file_only */ diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index c9c6db729..4e799e96a 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -259,6 +259,8 @@ ngx_http_init_request(ngx_event_t *rev) return; } + c->requests++; + hc = c->data; if (hc == NULL) { |
