From: Vladimír Čunát Date: Thu, 20 Jun 2019 12:57:52 +0000 (+0200) Subject: utils/cache_gc: adapt for faster checking of usage X-Git-Tag: v4.1.0^2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59a68322b32cf3436fd28d17ca296c8ab4c24922;p=thirdparty%2Fknot-resolver.git utils/cache_gc: adapt for faster checking of usage - don't print a line in every checking cycle - don't exit when cache isn't found - reduce recommended interval to 10s (compromise) - don't increase cache size (but keep the code for now) --- diff --git a/daemon/README.rst b/daemon/README.rst index 8fbb5dbc5..47bb5146f 100644 --- a/daemon/README.rst +++ b/daemon/README.rst @@ -630,7 +630,7 @@ and configure it to run every minute, use: .. code-block:: bash - $ kres_cache_gc -c /var/cache/knot-resolver -d 60000 + $ kres_cache_gc -c /var/cache/knot-resolver -d 10000 It's also possible to run this under systemd. However, a dedicated systemd unit is not currently part of the upstream package. See `message#167`_ on our diff --git a/utils/cache_gc/db.c b/utils/cache_gc/db.c index 16f287b81..40fba149f 100644 --- a/utils/cache_gc/db.c +++ b/utils/cache_gc/db.c @@ -35,7 +35,7 @@ static knot_db_t *knot_db_t_kres2libknot(const knot_db_t *db) return libknot_db; } -int kr_gc_cache_open(const char *cache_path, struct kr_cache *kres_db, knot_db_t **libknot_db, double *usage) +int kr_gc_cache_open(const char *cache_path, struct kr_cache *kres_db, knot_db_t **libknot_db) { char cache_data[strlen(cache_path) + 10]; snprintf(cache_data, sizeof(cache_data), "%s/data.mdb", cache_path); @@ -64,19 +64,6 @@ open_kr_cache: return -ENOMEM; } - size_t real_size = knot_db_lmdb_get_mapsize(*libknot_db), usageb = knot_db_lmdb_get_usage(*libknot_db); - *usage = (double)usageb / real_size * 100.0; - printf("Cache size: %zu, Usage: %zu (%.2lf%%)\n", real_size, usageb, *usage); - -#if 1 - if (*usage > 90.0) { - free(*libknot_db); - kr_cache_close(kres_db); - cache_size += cache_size / 10; - opts.maxsize = cache_size; - goto open_kr_cache; - } -# endif return 0; } diff --git a/utils/cache_gc/db.h b/utils/cache_gc/db.h index 12e01ec7f..52a7ee36e 100644 --- a/utils/cache_gc/db.h +++ b/utils/cache_gc/db.h @@ -5,7 +5,7 @@ #include "kr_cache_gc.h" -int kr_gc_cache_open(const char *cache_path, struct kr_cache *kres_db, knot_db_t **libknot_db, double *usage); +int kr_gc_cache_open(const char *cache_path, struct kr_cache *kres_db, knot_db_t **libknot_db); void kr_gc_cache_close(struct kr_cache *kres_db, knot_db_t *knot_db); diff --git a/utils/cache_gc/kr_cache_gc.c b/utils/cache_gc/kr_cache_gc.c index 9c6886b34..26a0226cc 100644 --- a/utils/cache_gc/kr_cache_gc.c +++ b/utils/cache_gc/kr_cache_gc.c @@ -143,14 +143,29 @@ int kr_cache_gc(kr_cache_gc_cfg_t *cfg) { struct kr_cache kres_db = { 0 }; knot_db_t *db = NULL; - double db_usage; - int ret = kr_gc_cache_open(cfg->cache_path, &kres_db, &db, &db_usage); + int ret = kr_gc_cache_open(cfg->cache_path, &kres_db, &db); if (ret) { return ret; } - if (cfg->dry_run || db_usage < cfg->cache_max_usage) { + const size_t db_size = knot_db_lmdb_get_mapsize(db); + const size_t db_usage_abs = knot_db_lmdb_get_usage(db); + const double db_usage = (double)db_usage_abs / db_size * 100.0; +#if 0 // Probably not worth it, better reduce the risk by checking more often. + if (db_usage > 90.0) { + free(*libknot_db); + kr_cache_close(kres_db); + cache_size += cache_size / 10; + opts.maxsize = cache_size; + goto open_kr_cache; + } +# endif + const bool large_usage = db_usage >= cfg->cache_max_usage; + if (cfg->dry_run || large_usage) { // don't print this on every size check + printf("Usage: %.2lf%% (%zu / %zu)\n", db_usage, db_usage_abs, db_size); + } + if (cfg->dry_run || !large_usage) { kr_gc_cache_close(&kres_db, db); return KNOT_EOK; } diff --git a/utils/cache_gc/main.c b/utils/cache_gc/main.c index de5da04c3..ad249dba0 100644 --- a/utils/cache_gc/main.c +++ b/utils/cache_gc/main.c @@ -116,7 +116,8 @@ int main(int argc, char *argv[]) do { int ret = kr_cache_gc(&cfg); - if (ret) { + // ENOENT: kresd may not be started yet or cleared the cache now + if (ret && ret != -ENOENT) { printf("Error (%s)\n", kr_strerror(ret)); return 10; }