]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
gc: verbose mode is now runtime option
authorPetr Špaček <petr.spacek@nic.cz>
Tue, 25 Aug 2020 15:01:08 +0000 (17:01 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Mon, 7 Sep 2020 15:47:46 +0000 (17:47 +0200)
utils/cache_gc/db.c
utils/cache_gc/db.h
utils/cache_gc/kr_cache_gc.c
utils/cache_gc/main.c

index 3a853b430b57ebecbd39925da6a26f6431dd1897..612316589271e208f0beae469e76592ef288b12b 100644 (file)
@@ -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 <lib/defines.h>
 
-#include <ctype.h>             //DEBUG
+#include <ctype.h>
 #include <time.h>
 #include <sys/stat.h>
 
@@ -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;
 }
index 0e20df19d43afdfdb9a106284f0f169cf1667fe2..9d1bef51b67cd327b888631b5d7c1cc023234d97 100644 (file)
@@ -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
 
index 5aae95f047af42a8a1d36b7d77102a9f82d7d665..ede6bf60dc130954140cc63c3945da6427f4b842 100644 (file)
@@ -14,6 +14,7 @@
 #include <lib/cache/impl.h>
 #include <lib/defines.h>
 #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");
index 736ade3219e813f7180e21cb8fccc4e9463c8fcb..520b878a4bfb7466bb42d33132ac747304be69ff 100644 (file)
@@ -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;