]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
cache: Add cache.fssize() - filesystem size where the cache resides
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 8 Jan 2020 16:39:53 +0000 (17:39 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 29 Jan 2020 10:32:17 +0000 (11:32 +0100)
NEWS
daemon/bindings/cache.rst
daemon/lua/kres-gen.lua
daemon/lua/kres-gen.sh
daemon/lua/sandbox.lua.in
lib/utils.c
lib/utils.h

diff --git a/NEWS b/NEWS
index fadddc2747ea433f1f353904db2a43b3523b22cf..5207920d9e7683b9c5e56541a02f6eb13d692676 100644 (file)
--- 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)
 ================================
 
index 78db78041ed248a120b707f4925d9f02234a4b60..c14cbcfd2ca2205fe0d0cb395685b91c82361742 100644 (file)
@@ -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.
index d464fd5b83b9de514d08cd8051462be9f5325a01..b4b23e41f63d25906a75f5eb20e4dfe5e3828ece 100644 (file)
@@ -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 *);
index d51a037320c8ad347eb6433fbc82234e83963e5b..78688a5dd7ebbd2a48a98659139446c9703908fd 100755 (executable)
@@ -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
index 2a97fa9cf64ae3503de29b21f512764a912fc221..f184e5f2a5ba6e8bf1d01a053bb11fb68c7e350e 100644 (file)
@@ -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
index 0bb36484c2a999b18c72f65b534053ab1cb1b52a..6d327cf91e1d5fbec25a0be875cfb980bfaa0627 100644 (file)
@@ -41,6 +41,7 @@
 #include <string.h>
 #include <sys/time.h>
 #include <sys/stat.h>
+#include <sys/statvfs.h>
 #include <sys/un.h>
 
 /* 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;
+}
+
index c1c7b8be4d9bdc88de0f421435b8e4e5b45a62b1..fc83b30e24715fcf72a88d103d0f3b0cd0a26aa0 100644 (file)
@@ -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);
+