From: Vladimír Čunát Date: Wed, 8 Jan 2020 16:39:53 +0000 (+0100) Subject: cache: Add cache.fssize() - filesystem size where the cache resides X-Git-Tag: v5.0.1~3^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38cc9dffcf2e1bde90856a770c598e90d185987f;p=thirdparty%2Fknot-resolver.git cache: Add cache.fssize() - filesystem size where the cache resides --- diff --git a/NEWS b/NEWS index fadddc274..5207920d9 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,11 @@ Bugfixes -------- - systemd: use correct cache location for garbage collector (#543) +Improvements +------------ +- cache: add fssize lua function to configure entire free disk space on dedicated cache partition (#524, !932) + + Knot Resolver 5.0.0 (2020-01-27) ================================ diff --git a/daemon/bindings/cache.rst b/daemon/bindings/cache.rst index 78db78041..c14cbcfd2 100644 --- a/daemon/bindings/cache.rst +++ b/daemon/bindings/cache.rst @@ -37,6 +37,14 @@ Now you can configure cache size to be 90% of the free memory 14 928 MB, i.e. 13 -- 90 % of free memory after machine restart cache.size = 13453 * MB +It is also possible to set the cache size based on the file system size. This is useful +if you use a dedicated partition for cache (e.g. non-persistent tmpfs). It is recommended +to leave some free space for special files, such as locks.: + +.. code-block:: lua + + cache.size = cache.fssize() - 10*MB + .. note:: The :ref:`garbage-collector` can be used to periodically trim the cache. It is enabled and configured by default when running kresd with systemd integration. @@ -68,9 +76,9 @@ and will be lost on power-off or reboot. multiple systemd units, and a shared tmpfs space could be used up by other applications, leading to ``SIGBUS`` errors during runtime. -Mounting the cache directory as tmpfs_ is recommended approach. -Make sure to use appropriate ``size=`` option and don't forget to adjust the -size in the config file as well. +Mounting the cache directory as tmpfs_ is the recommended approach. Make sure +to use appropriate ``size=`` option and don't forget to adjust the size in the +config file as well. .. code-block:: none @@ -79,8 +87,8 @@ size in the config file as well. .. code-block:: lua - # /etc/knot-resolver/config - cache.size = 2 * GB + -- /etc/knot-resolver/kresd.conf + cache.size = cache.fssize() - 10*MB .. _tmpfs: https://en.wikipedia.org/wiki/Tmpfs @@ -167,6 +175,10 @@ Configuration reference .. note:: This may or may not clear the cache, depending on the cache backend. +.. function:: cache.fssize() + + :return: Partition size of cache storage. + .. function:: cache.stats() Return table with low-level statistics for each internal cache operation. diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index d464fd5b8..b4b23e41f 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -362,6 +362,7 @@ void kr_zonecut_set(struct kr_zonecut *, const knot_dname_t *); uint64_t kr_now(); const char *kr_strptime_diff(const char *, const char *, const char *, double *); time_t kr_file_mtime(const char *); +long long kr_fssize(const char *); void lru_free_items_impl(struct lru *); struct lru *lru_create_impl(unsigned int, unsigned int, knot_mm_t *, knot_mm_t *); void *lru_get_impl(struct lru *, const char *, unsigned int, unsigned int, _Bool, _Bool *); diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index d51a03732..78688a5dd 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -225,6 +225,7 @@ ${CDEFS} ${LIBKRES} functions <<-EOF kr_now kr_strptime_diff kr_file_mtime + kr_fssize lru_free_items_impl lru_create_impl lru_get_impl diff --git a/daemon/lua/sandbox.lua.in b/daemon/lua/sandbox.lua.in index 2a97fa9cf..f184e5f2a 100644 --- a/daemon/lua/sandbox.lua.in +++ b/daemon/lua/sandbox.lua.in @@ -278,6 +278,24 @@ modules_ffi_wrap_modcb = function (cb, kr_module_ud) -- this one isn't for layer return cb(kr_module) end +-- Return filesystem size where the cache resides. +cache.fssize = function () + local path = cache.current_storage or '.' + -- As it is now, `path` may or may not include the lmdb:// prefix. + if string.sub(path, 1, 7) == 'lmdb://' then + path = string.sub(path, 8) + end + if #path == 0 then + path = '.' + end + local size = tonumber(ffi.C.kr_fssize(path)) + if size < 0 then + panic('cache.fssize(): %s', ffi.string(ffi.C.knot_strerror(size))) + else + return size + end +end + cache.clear = function (name, exact_name, rr_type, chunk_size, callback, prev_state) if name == nil or (name == '.' and not exact_name) then -- keep same output format as for 'standard' clear diff --git a/lib/utils.c b/lib/utils.c index 0bb36484c..6d327cf91 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -41,6 +41,7 @@ #include #include #include +#include #include /* Always compile-in log symbols, even if disabled. */ @@ -1246,3 +1247,15 @@ time_t kr_file_mtime (const char* fname) { return fstat.st_mtime; } +long long kr_fssize(const char *path) +{ + if (!path) + return kr_error(EINVAL); + + struct statvfs buf; + if (statvfs(path, &buf) != 0) + return kr_error(errno); + + return buf.f_frsize * buf.f_blocks; +} + diff --git a/lib/utils.h b/lib/utils.h index c1c7b8be4..fc83b30e2 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -551,4 +551,8 @@ KR_EXPORT uint16_t kr_pkt_qtype(const knot_pkt_t *pkt); KR_EXPORT uint32_t kr_rrsig_sig_inception(const knot_rdata_t *rdata); KR_EXPORT uint32_t kr_rrsig_sig_expiration(const knot_rdata_t *rdata); KR_EXPORT uint16_t kr_rrsig_type_covered(const knot_rdata_t *rdata); + KR_EXPORT time_t kr_file_mtime (const char* fname); +/** Return filesystem size in bytes. */ +KR_EXPORT long long kr_fssize(const char *path); +