Welcome! Log In Create A New Profile

Advanced

[nginx] Cache: introduced min_free cache clearing.

Maxim Dounin
June 22, 2020 12:20PM
details: https://hg.nginx.org/nginx/rev/ccb5ff87ab3e
branches:
changeset: 7670:ccb5ff87ab3e
user: Maxim Dounin <mdounin@mdounin.ru>
date: Mon Jun 22 18:03:00 2020 +0300
description:
Cache: introduced min_free cache clearing.

Clearing cache based on free space left on a file system is
expected to allow better disk utilization in some cases, notably
when disk space might be also used for something other than nginx
cache (including nginx own temporary files) and while loading
cache (when cache size might be inaccurate for a while, effectively
disabling max_size cache clearing).

Based on a patch by Adam Bambuch.

diffstat:

src/http/ngx_http_cache.h | 1 +
src/http/ngx_http_file_cache.c | 43 +++++++++++++++++++++++++++++++++++++++--
src/os/unix/ngx_files.c | 33 ++++++++++++++++++++++++++++++++
src/os/unix/ngx_files.h | 1 +
src/os/win32/ngx_files.c | 13 ++++++++++++
src/os/win32/ngx_files.h | 1 +
6 files changed, 89 insertions(+), 3 deletions(-)

diffs (199 lines):

diff -r 52b34c3f89b4 -r ccb5ff87ab3e src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h Mon Jun 22 18:02:59 2020 +0300
+++ b/src/http/ngx_http_cache.h Mon Jun 22 18:03:00 2020 +0300
@@ -160,6 +160,7 @@ struct ngx_http_file_cache_s {

ngx_path_t *path;

+ off_t min_free;
off_t max_size;
size_t bsize;

diff -r 52b34c3f89b4 -r ccb5ff87ab3e src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c Mon Jun 22 18:02:59 2020 +0300
+++ b/src/http/ngx_http_file_cache.c Mon Jun 22 18:03:00 2020 +0300
@@ -1959,7 +1959,7 @@ ngx_http_file_cache_manager(void *data)
{
ngx_http_file_cache_t *cache = data;

- off_t size;
+ off_t size, free;
time_t wait;
ngx_msec_t elapsed, next;
ngx_uint_t count, watermark;
@@ -1988,7 +1988,19 @@ ngx_http_file_cache_manager(void *data)
size, count, (ngx_int_t) watermark);

if (size < cache->max_size && count < watermark) {
- break;
+
+ if (!cache->min_free) {
+ break;
+ }
+
+ free = ngx_fs_available(cache->path->name.data);
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
+ "http file cache free: %O", free);
+
+ if (free > cache->min_free) {
+ break;
+ }
}

wait = ngx_http_file_cache_forced_expire(cache);
@@ -2304,7 +2316,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t
{
char *confp = conf;

- off_t max_size;
+ off_t max_size, min_free;
u_char *last, *p;
time_t inactive;
ssize_t size;
@@ -2341,6 +2353,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t
name.len = 0;
size = 0;
max_size = NGX_MAX_OFF_T_VALUE;
+ min_free = 0;

value = cf->args->elts;

@@ -2476,6 +2489,29 @@ ngx_http_file_cache_set_slot(ngx_conf_t
continue;
}

+ if (ngx_strncmp(value[i].data, "min_free=", 9) == 0) {
+
+#if (NGX_WIN32 || NGX_HAVE_STATFS || NGX_HAVE_STATVFS)
+
+ s.len = value[i].len - 9;
+ s.data = value[i].data + 9;
+
+ min_free = ngx_parse_offset(&s);
+ if (min_free < 0) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid min_free value \"%V\"", &value[i]);
+ return NGX_CONF_ERROR;
+ }
+
+#else
+ ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
+ "min_free is not supported "
+ "on this platform, ignored");
+#endif
+
+ continue;
+ }
+
if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {

loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
@@ -2607,6 +2643,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t

cache->inactive = inactive;
cache->max_size = max_size;
+ cache->min_free = min_free;

caches = (ngx_array_t *) (confp + cmd->offset);

diff -r 52b34c3f89b4 -r ccb5ff87ab3e src/os/unix/ngx_files.c
--- a/src/os/unix/ngx_files.c Mon Jun 22 18:02:59 2020 +0300
+++ b/src/os/unix/ngx_files.c Mon Jun 22 18:03:00 2020 +0300
@@ -884,6 +884,19 @@ ngx_fs_bsize(u_char *name)
return (size_t) fs.f_bsize;
}

+
+off_t
+ngx_fs_available(u_char *name)
+{
+ struct statfs fs;
+
+ if (statfs((char *) name, &fs) == -1) {
+ return NGX_MAX_OFF_T_VALUE;
+ }
+
+ return (off_t) fs.f_bavail * fs.f_bsize;
+}
+
#elif (NGX_HAVE_STATVFS)

size_t
@@ -908,6 +921,19 @@ ngx_fs_bsize(u_char *name)
return (size_t) fs.f_frsize;
}

+
+off_t
+ngx_fs_available(u_char *name)
+{
+ struct statvfs fs;
+
+ if (statvfs((char *) name, &fs) == -1) {
+ return NGX_MAX_OFF_T_VALUE;
+ }
+
+ return (off_t) fs.f_bavail * fs.f_frsize;
+}
+
#else

size_t
@@ -916,4 +942,11 @@ ngx_fs_bsize(u_char *name)
return 512;
}

+
+off_t
+ngx_fs_available(u_char *name)
+{
+ return NGX_MAX_OFF_T_VALUE;
+}
+
#endif
diff -r 52b34c3f89b4 -r ccb5ff87ab3e src/os/unix/ngx_files.h
--- a/src/os/unix/ngx_files.h Mon Jun 22 18:02:59 2020 +0300
+++ b/src/os/unix/ngx_files.h Mon Jun 22 18:03:00 2020 +0300
@@ -349,6 +349,7 @@ ngx_int_t ngx_directio_off(ngx_fd_t fd);
#endif

size_t ngx_fs_bsize(u_char *name);
+off_t ngx_fs_available(u_char *name);


#if (NGX_HAVE_OPENAT)
diff -r 52b34c3f89b4 -r ccb5ff87ab3e src/os/win32/ngx_files.c
--- a/src/os/win32/ngx_files.c Mon Jun 22 18:02:59 2020 +0300
+++ b/src/os/win32/ngx_files.c Mon Jun 22 18:03:00 2020 +0300
@@ -658,6 +658,19 @@ ngx_fs_bsize(u_char *name)
}


+off_t
+ngx_fs_available(u_char *name)
+{
+ ULARGE_INTEGER navail;
+
+ if (GetDiskFreeSpaceEx((const char *) name, &navail, NULL, NULL) == 0) {
+ return NGX_MAX_OFF_T_VALUE;
+ }
+
+ return (off_t) navail.QuadPart;
+}
+
+
static ngx_int_t
ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
{
diff -r 52b34c3f89b4 -r ccb5ff87ab3e src/os/win32/ngx_files.h
--- a/src/os/win32/ngx_files.h Mon Jun 22 18:02:59 2020 +0300
+++ b/src/os/win32/ngx_files.h Mon Jun 22 18:03:00 2020 +0300
@@ -259,6 +259,7 @@ ngx_int_t ngx_directio_off(ngx_fd_t fd);
#define ngx_directio_off_n "ngx_directio_off_n"

size_t ngx_fs_bsize(u_char *name);
+off_t ngx_fs_available(u_char *name);


#define ngx_stdout GetStdHandle(STD_OUTPUT_HANDLE)
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[nginx] Cache: introduced min_free cache clearing.

Maxim Dounin 329 June 22, 2020 12:20PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 262
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready