Merge of r3966, r3967, r3968, r3969, r3970, r3971:

Cache loader improvements:

The cache loader performs two tasks: inserting cache objects in inactivity
list and evaluating total cache size. Reading just directory is enough for
this purpose. Elimination of reading cache files saves at least one disk I/O
operation per file.
This commit is contained in:
Igor Sysoev 2011-08-29 09:51:56 +00:00
parent 283936abca
commit 34f579fb6d
5 changed files with 23 additions and 67 deletions

View File

@ -926,6 +926,7 @@ ngx_walk_tree(ngx_tree_ctx_t *ctx, ngx_str_t *tree)
"tree file \"%s\"", file.data); "tree file \"%s\"", file.data);
ctx->size = ngx_de_size(&dir); ctx->size = ngx_de_size(&dir);
ctx->fs_size = ngx_de_fs_size(&dir);
ctx->access = ngx_de_access(&dir); ctx->access = ngx_de_access(&dir);
ctx->mtime = ngx_de_mtime(&dir); ctx->mtime = ngx_de_mtime(&dir);

View File

@ -104,6 +104,7 @@ typedef ngx_int_t (*ngx_tree_handler_pt) (ngx_tree_ctx_t *ctx, ngx_str_t *name);
struct ngx_tree_ctx_s { struct ngx_tree_ctx_s {
off_t size; off_t size;
off_t fs_size;
ngx_uint_t access; ngx_uint_t access;
time_t mtime; time_t mtime;

View File

@ -531,26 +531,19 @@ ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
goto done; goto done;
} }
if (fcn->exists) { if (fcn->exists || fcn->uses >= c->min_uses) {
c->exists = fcn->exists; c->exists = fcn->exists;
if (fcn->body_start) {
c->body_start = fcn->body_start; c->body_start = fcn->body_start;
}
rc = NGX_OK; rc = NGX_OK;
goto done; goto done;
} }
if (fcn->uses >= c->min_uses) {
c->exists = fcn->exists;
c->body_start = fcn->body_start;
rc = NGX_OK;
} else {
rc = NGX_AGAIN; rc = NGX_AGAIN;
}
goto done; goto done;
} }
@ -1329,67 +1322,26 @@ static ngx_int_t
ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name) ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
{ {
u_char *p; u_char *p;
ngx_fd_t fd;
ngx_int_t n; ngx_int_t n;
ngx_uint_t i; ngx_uint_t i;
ngx_file_info_t fi;
ngx_http_cache_t c; ngx_http_cache_t c;
ngx_http_file_cache_t *cache; ngx_http_file_cache_t *cache;
ngx_http_file_cache_header_t h;
if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) { if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
return NGX_ERROR; return NGX_ERROR;
} }
ngx_memzero(&c, sizeof(ngx_http_cache_t)); if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
fd = ngx_open_file(name->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
if (fd == NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_open_file_n " \"%s\" failed", name->data);
return NGX_ERROR;
}
c.file.fd = fd;
c.file.name = *name;
c.file.log = ctx->log;
n = ngx_read_file(&c.file, (u_char *) &h,
sizeof(ngx_http_file_cache_header_t), 0);
if (n == NGX_ERROR) {
return NGX_ERROR;
}
if ((size_t) n < sizeof(ngx_http_file_cache_header_t)) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, 0, ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
"cache file \"%s\" is too small", name->data); "cache file \"%s\" is too small", name->data);
return NGX_ERROR; return NGX_ERROR;
} }
ngx_memzero(&c, sizeof(ngx_http_cache_t));
cache = ctx->data; cache = ctx->data;
if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { c.length = ctx->size;
ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno, c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
ngx_fd_info_n " \"%s\" failed", name->data);
} else {
c.uniq = ngx_file_uniq(&fi);
c.valid_sec = h.valid_sec;
c.valid_msec = h.valid_msec;
c.body_start = h.body_start;
c.length = ngx_file_size(&fi);
c.fs_size = (ngx_file_fs_size(&fi) + cache->bsize - 1) / cache->bsize;
}
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
ngx_close_file_n " \"%s\" failed", name->data);
}
if (c.body_start == 0) {
return NGX_ERROR;
}
p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN]; p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
@ -1436,14 +1388,14 @@ ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
fcn->uses = 1; fcn->uses = 1;
fcn->count = 0; fcn->count = 0;
fcn->valid_msec = c->valid_msec; fcn->valid_msec = 0;
fcn->error = 0; fcn->error = 0;
fcn->exists = 1; fcn->exists = 1;
fcn->updating = 0; fcn->updating = 0;
fcn->deleting = 0; fcn->deleting = 0;
fcn->uniq = c->uniq; fcn->uniq = 0;
fcn->valid_sec = c->valid_sec; fcn->valid_sec = 0;
fcn->body_start = c->body_start; fcn->body_start = 0;
fcn->fs_size = c->fs_size; fcn->fs_size = c->fs_size;
cache->sh->size += c->fs_size; cache->sh->size += c->fs_size;

View File

@ -253,6 +253,7 @@ ngx_de_info(u_char *name, ngx_dir_t *dir)
#define ngx_de_access(dir) (((dir)->info.st_mode) & 0777) #define ngx_de_access(dir) (((dir)->info.st_mode) & 0777)
#define ngx_de_size(dir) (dir)->info.st_size #define ngx_de_size(dir) (dir)->info.st_size
#define ngx_de_fs_size(dir) ((dir)->info.st_blocks * 512)
#define ngx_de_mtime(dir) (dir)->info.st_mtime #define ngx_de_mtime(dir) (dir)->info.st_mtime

View File

@ -227,6 +227,7 @@ ngx_int_t ngx_de_link_info(u_char *name, ngx_dir_t *dir);
#define ngx_de_access(dir) 0 #define ngx_de_access(dir) 0
#define ngx_de_size(dir) \ #define ngx_de_size(dir) \
(((off_t) (dir)->finddata.nFileSizeHigh << 32) | (dir)->finddata.nFileSizeLow) (((off_t) (dir)->finddata.nFileSizeHigh << 32) | (dir)->finddata.nFileSizeLow)
#define ngx_de_fs_size(dir) ngx_de_size(dir)
/* 116444736000000000 is commented in src/os/win32/ngx_time.c */ /* 116444736000000000 is commented in src/os/win32/ngx_time.c */