From: Vladimír Čunát Date: Sat, 22 Aug 2020 09:47:51 +0000 (+0200) Subject: lib/cache kr_cdb_api::space_usage(): also use kr_cdb_pt X-Git-Tag: v5.2.0~37^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9b469602ea8a9cdec38e5fd6d31fab25198dc05a;p=thirdparty%2Fknot-resolver.git lib/cache kr_cdb_api::space_usage(): also use kr_cdb_pt - The malloc-free pair could be avoided without difficulty, but it seemed like premature optimization. - The libknot functions make error handling a bit difficult (zero is theoretically valid and doesn't show error type), but writing this properly without libknot would need 10-20 additional lines of code and the risk of encountering errors in this function seems very low anyway. --- diff --git a/daemon/bindings/cache.c b/daemon/bindings/cache.c index 56b75fc93..7b08374e3 100644 --- a/daemon/bindings/cache.c +++ b/daemon/bindings/cache.c @@ -3,7 +3,6 @@ */ #include "daemon/bindings/impl.h" -#include "lib/cache/cdb_lmdb.h" #include "daemon/zimport.h" @@ -95,11 +94,9 @@ static int cache_stats(lua_State *L) add_stat(read_leq); add_stat(read_leq_miss); /* usage_percent statistics special case - double */ - struct libknot_lmdb_env *libknot_db = kr_cdb_pt2knot_db_t(cache->db); - cache->stats.usage_percent = cache->api->usage_percent(libknot_db); + cache->stats.usage_percent = cache->api->usage_percent(cache->db); lua_pushnumber(L, cache->stats.usage_percent); lua_setfield(L, -2, "usage_percent"); - free(libknot_db); #undef add_stat return 1; diff --git a/lib/cache/cdb_api.h b/lib/cache/cdb_api.h index 33ec2ffa2..d94ea5e95 100644 --- a/lib/cache/cdb_api.h +++ b/lib/cache/cdb_api.h @@ -83,7 +83,8 @@ struct kr_cdb_api { int (*read_leq)(kr_cdb_pt db, struct kr_cdb_stats *stat, knot_db_val_t *key, knot_db_val_t *val); - double (*usage_percent)(knot_db_t *db); + /** Return estimated space usage (0--100). */ + double (*usage_percent)(kr_cdb_pt db); /** Return the current cache size limit in bytes; could be cached by check_health(). */ size_t (*get_maxsize)(kr_cdb_pt db); diff --git a/lib/cache/cdb_lmdb.c b/lib/cache/cdb_lmdb.c index ff152fb7a..68346480a 100644 --- a/lib/cache/cdb_lmdb.c +++ b/lib/cache/cdb_lmdb.c @@ -810,12 +810,13 @@ failure: return lmdb_error(ret); } -static double cdb_usage(knot_db_t *db) +static double cdb_usage_percent(kr_cdb_pt db) { - const size_t db_size = knot_db_lmdb_get_mapsize(db); - const size_t db_usage_abs = knot_db_lmdb_get_usage(db); + knot_db_t *kdb = kr_cdb_pt2knot_db_t(db); + const size_t db_size = knot_db_lmdb_get_mapsize(kdb); + const size_t db_usage_abs = knot_db_lmdb_get_usage(kdb); const double db_usage = (double)db_usage_abs / db_size * 100.0; - + free(kdb); return db_usage; } @@ -847,7 +848,7 @@ const struct kr_cdb_api *kr_cdb_lmdb(void) cdb_readv, cdb_writev, cdb_remove, cdb_match, cdb_read_leq, - cdb_usage, + cdb_usage_percent, cdb_get_maxsize, cdb_check_health, };