From: Petr Špaček Date: Tue, 25 Aug 2020 15:01:08 +0000 (+0200) Subject: gc: verbose mode is now runtime option X-Git-Tag: v5.1.3~1^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e927fc72d926a492eb2d36ad9abd0f19e73d698;p=thirdparty%2Fknot-resolver.git gc: verbose mode is now runtime option --- diff --git a/utils/cache_gc/db.c b/utils/cache_gc/db.c index 3a853b430..612316589 100644 --- a/utils/cache_gc/db.c +++ b/utils/cache_gc/db.c @@ -1,13 +1,11 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -// #define DEBUG 1 #include "db.h" #include "lib/cache/cdb_lmdb.h" #include "lib/cache/impl.h" -//#include -#include //DEBUG +#include #include #include @@ -124,7 +122,6 @@ static uint8_t entry_labels(knot_db_val_t * key, uint16_t rrtype) return lab; } -#ifdef DEBUG void debug_printbin(const char *str, unsigned int len) { putchar('"'); @@ -137,7 +134,6 @@ void debug_printbin(const char *str, unsigned int len) } putchar('"'); } -#endif /** Return one entry_h reference from a cache DB value. NULL if not consistent/suitable. */ static const struct entry_h *val2entry(const knot_db_val_t val, uint16_t ktype) @@ -161,11 +157,9 @@ static const struct entry_h *val2entry(const knot_db_val_t val, uint16_t ktype) int kr_gc_cache_iter(knot_db_t * knot_db, const kr_cache_gc_cfg_t *cfg, kr_gc_iter_callback callback, void *ctx) { -#ifdef DEBUG unsigned int counter_iter = 0; unsigned int counter_gc_consistent = 0; unsigned int counter_kr_consistent = 0; -#endif knot_db_txn_t txn = { 0 }; knot_db_iter_t *it = NULL; @@ -210,9 +204,7 @@ int kr_gc_cache_iter(knot_db_t * knot_db, const kr_cache_gc_cfg_t *cfg, ret == KNOT_EOK ? kr_gc_key_consistent(key) : NULL; const struct entry_h *entry = NULL; if (entry_type != NULL) { -#ifdef DEBUG counter_gc_consistent++; -#endif entry = val2entry(val, *entry_type); } /* TODO: perhaps improve some details around here: @@ -226,18 +218,18 @@ int kr_gc_cache_iter(knot_db_t * knot_db, const kr_cache_gc_cfg_t *cfg, info.expires_in = entry->time + entry->ttl - now; info.no_labels = entry_labels(&key, *entry_type); } -#ifdef DEBUG counter_iter++; counter_kr_consistent += info.valid; - if (!entry_type || !entry) { // don't log fully consistent entries - printf - ("GC %sconsistent, KR %sconsistent, size %zu, key len %zu: ", - entry_type ? "" : "in", entry ? "" : "IN", - (key.len + val.len), key.len); - debug_printbin(key.data, key.len); - printf("\n"); + if (VERBOSE_STATUS) { + if (!entry_type || !entry) { // don't log fully consistent entries + printf + ("GC %sconsistent, KR %sconsistent, size %zu, key len %zu: ", + entry_type ? "" : "in", entry ? "" : "IN", + (key.len + val.len), key.len); + debug_printbin(key.data, key.len); + printf("\n"); + } } -#endif ret = callback(&key, &info, ctx); if (ret != KNOT_EOK) { @@ -280,9 +272,8 @@ int kr_gc_cache_iter(knot_db_t * knot_db, const kr_cache_gc_cfg_t *cfg, } api->txn_abort(&txn); -#ifdef DEBUG - printf("DEBUG: iterated %u items, gc consistent %u, kr consistent %u\n", - counter_iter, counter_gc_consistent, counter_kr_consistent); -#endif + + kr_log_verbose("DEBUG: iterated %u items, gc consistent %u, kr consistent %u\n", + counter_iter, counter_gc_consistent, counter_kr_consistent); return KNOT_EOK; } diff --git a/utils/cache_gc/db.h b/utils/cache_gc/db.h index 0e20df19d..9d1bef51b 100644 --- a/utils/cache_gc/db.h +++ b/utils/cache_gc/db.h @@ -23,8 +23,6 @@ int kr_gc_cache_iter(knot_db_t * knot_db, const kr_cache_gc_cfg_t *cfg, const uint16_t *kr_gc_key_consistent(knot_db_val_t key); -#ifdef DEBUG /** Printf a *binary* string in a human-readable way. */ void debug_printbin(const char *str, unsigned int len); -#endif diff --git a/utils/cache_gc/kr_cache_gc.c b/utils/cache_gc/kr_cache_gc.c index 5aae95f04..ede6bf60d 100644 --- a/utils/cache_gc/kr_cache_gc.c +++ b/utils/cache_gc/kr_cache_gc.c @@ -14,6 +14,7 @@ #include #include #include "lib/cache/cdb_lmdb.h" +#include "lib/utils.h" #include "kr_cache_gc.h" @@ -218,15 +219,15 @@ int kr_cache_gc(kr_cache_gc_cfg_t *cfg, kr_cache_gc_state_t **state) /* use less precise variant to avoid 32-bit overflow */ ssize_t amount_tofree = cats_sumsize / 100 * cfg->cache_to_be_freed; -#ifdef DEBUG - printf("tofree: %zd / %zd\n", amount_tofree, cats_sumsize); - for (int i = 0; i < CATEGORIES; i++) { - if (cats.categories_sizes[i] > 0) { - printf("category %.2d size %zu\n", i, - cats.categories_sizes[i]); + kr_log_verbose("tofree: %zd / %zd\n", amount_tofree, cats_sumsize); + if (VERBOSE_STATUS) { + for (int i = 0; i < CATEGORIES; i++) { + if (cats.categories_sizes[i] > 0) { + printf("category %.2d size %zu\n", i, + cats.categories_sizes[i]); + } } } -#endif category_t limit_category = CATEGORIES; while (limit_category > 0 && amount_tofree > 0) { @@ -281,13 +282,13 @@ int kr_cache_gc(kr_cache_gc_cfg_t *cfg, kr_cache_gc_state_t **state) break; case KNOT_ENOENT: already_gone++; -#ifdef DEBUG - // kresd normally only inserts (or overwrites), - // so it's generally suspicious when a key goes missing. - printf("Record already gone (key len %zu): ", (*i)->len); - debug_printbin((*i)->data, (*i)->len); - printf("\n"); -#endif + if (VERBOSE_STATUS) { + // kresd normally only inserts (or overwrites), + // so it's generally suspicious when a key goes missing. + printf("Record already gone (key len %zu): ", (*i)->len); + debug_printbin((*i)->data, (*i)->len); + printf("\n"); + } break; case KNOT_ESPACE: printf("Warning: out of space, bailing out to retry later.\n"); diff --git a/utils/cache_gc/main.c b/utils/cache_gc/main.c index 736ade321..520b878a4 100644 --- a/utils/cache_gc/main.c +++ b/utils/cache_gc/main.c @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) }; int o; - while ((o = getopt(argc, argv, "hnc:d:l:L:m:u:f:w:t:")) != -1) { + while ((o = getopt(argc, argv, "hnvc:d:l:L:m:u:f:w:t:")) != -1) { switch (o) { case 'c': cfg.cache_path = optarg; @@ -114,6 +114,9 @@ int main(int argc, char *argv[]) case 'n': cfg.dry_run = true; break; + case 'v': + kr_verbose_set(true); + break; case ':': case '?': case 'h': @@ -129,10 +132,6 @@ int main(int argc, char *argv[]) return 1; } -#ifdef DEBUG - kr_verbose_set(true); // used inside cache operations -#endif - int exit_code = 0; kr_cache_gc_state_t *gc_state = NULL; bool last_espace = false;