]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
--verbose: make it more efficient when not --verbose
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 7 Dec 2016 12:05:29 +0000 (13:05 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 7 Dec 2016 12:08:34 +0000 (13:08 +0100)
In particular, don't require to call an externally defined function to
find if in --verbose mode or not.  Now it's just an extern bool.
I'm confident the performance impact of not using -DNLOGDEBUG should be
negligible now.

This comes with a small incompatible API+ABI change, but that shouldn't
matter as we've had a couple of those already since the last release.

daemon/engine.c
daemon/main.c
lib/layer.h
lib/utils.c
lib/utils.h

index 1751855d6bc6b5615863402d6632010cdae33ae7..0e14606d9ab74561a352f5f03626bd483319a330 100644 (file)
@@ -143,7 +143,7 @@ static int l_verbose(lua_State *L)
        if (lua_isboolean(L, 1) || lua_isnumber(L, 1)) {
                kr_debug_set(lua_toboolean(L, 1));
        }
-       lua_pushboolean(L, kr_debug_status());
+       lua_pushboolean(L, kr_debug_do_log);
        return 1;
 }
 
index bf56f35fe8212bd4004fc22647afb4f1c3e43cd6..e0af375476dcfdb6befb905a45ff16545c8f9994 100644 (file)
@@ -343,7 +343,7 @@ static int run_worker(uv_loop_t *loop, struct engine *engine, fd_array_t *ipc_se
        }
        memcpy(&engine->ipc_set, ipc_set, sizeof(*ipc_set));
 
-       tls_setup_logging(kr_debug_status());
+       tls_setup_logging(kr_debug_do_log);
        /* Notify supervisor. */
 #ifdef HAS_SYSTEMD
        sd_notify(0, "READY=1");
index ee33d68eae4f6ba9bf139acf9263671077067726..ab321112722552174f41e064cec221b79bb6686d 100644 (file)
 
 #ifndef NLOGDEBUG
  /** @internal Print a debug message related to resolution. */
- #define QRDEBUG(query, cls, fmt, ...) do { \
+ #define QRDEBUG(query, cls, fmt, ...) WITH_DEBUG { \
     unsigned _ind = 0; \
     for (struct kr_query *q = (query); q; q = q->parent, _ind += 2); \
     kr_log_debug("[%s] %*s" fmt, cls, _ind, "", ##  __VA_ARGS__); \
-    } while (0)
+    }
 #else
  #define QRDEBUG(query, cls, fmt, ...)
 #endif
index d8cc96c8a0d8d3a02b59aee5f0687aa11a8a1346..614187177f846272c8454ed1fb1003ad1071500c 100644 (file)
 #include "lib/module.h"
 #include "lib/resolve.h"
 
+
+/* Always compile-in log symbols, even if disabled. */
+#undef kr_debug_do_log
+#undef kr_debug_set
+#undef kr_log_debug
+
 /* Logging & debugging */
-static bool _env_debug = false;
+bool kr_debug_do_log = false;
 
 /** @internal CSPRNG context */
 static isaac_ctx ISAAC;
@@ -65,24 +71,17 @@ static inline int u16tostr(uint8_t *dst, uint16_t num)
  * Cleanup callbacks.
  */
 
-/* Always compile-in log symbols, even if disabled. */
-#undef kr_debug_set
-#undef kr_debug_status
-#undef kr_log_debug
-
 bool kr_debug_set(bool status)
 {
-       return _env_debug = status;
-}
-
-bool kr_debug_status(void)
-{
-       return _env_debug;
+#ifndef NLOGDEBUG
+       kr_debug_do_log = status;
+#endif
+       return kr_debug_do_log;
 }
 
 void kr_log_debug(const char *fmt, ...)
 {
-       if (_env_debug) {
+       if (kr_debug_do_log) {
                va_list args;
                va_start(args, fmt);
                vprintf(fmt, args);
index 385d881cfe4945abf3c026285e8adc9ad4a9b0b2..0adc906330676cfdcec10e6b313b577a40fc4dcd 100644 (file)
  */
 #define kr_log_info(fmt, ...) do { printf((fmt), ## __VA_ARGS__); fflush(stdout); } while(0)
 #define kr_log_error(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__)
-#ifndef NLOGDEBUG
-/* Toggle debug messages */
+
+/* Always export these, but override direct calls by macros conditionally. */
+/** Whether in --verbose mode.  Only use this for reading. */
+KR_EXPORT extern bool kr_debug_do_log;
+
+/** Set --verbose mode.  Not available if compiled with -DNLOGDEBUG. */
 KR_EXPORT bool kr_debug_set(bool status);
-KR_EXPORT KR_PURE bool kr_debug_status(void);
+
+/** Log a message if in --verbose mode. */
 KR_EXPORT void kr_log_debug(const char *fmt, ...);
-/* Debug block */
-#define WITH_DEBUG if(__builtin_expect(kr_debug_status(), 0))
-#else
-#define kr_debug_status() false
+
+#ifdef NLOGDEBUG
+/* Efficient compile-time disabling of debugging messages. */
+#define kr_debug_do_log false
 #define kr_debug_set(x)
-#define kr_log_debug(fmt, ...)
-#define WITH_DEBUG if(0)
 #endif
 
+/** Block run in --verbose mode; optimized when not run. */
+#define WITH_DEBUG if(__builtin_expect(kr_debug_do_log, false))
+#define kr_log_debug WITH_DEBUG kr_log_debug
+
+
 /* C11 compatibility, but without any implementation so far. */
 #ifndef static_assert
 #define static_assert(cond, msg)