[sheepdog] [PATCH] logger: don't call log_write in sd_debug if possible

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Aug 9 18:49:57 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

The aim of this patch is to optimize codes for normal log level and
not to hesitate to add a debug code even in a critical path.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---

This depends on my patch "add a print function for each log level".


 include/compiler.h |    3 +++
 include/logger.h   |   14 ++++++++++++--
 lib/logger.c       |    6 +++---
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/include/compiler.h b/include/compiler.h
index df38dc8..58ea3ca 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -12,6 +12,9 @@
 #ifndef SD_COMPILER_H
 #define SD_COMPILER_H
 
+#define likely(x)       __builtin_expect(!!(x), 1)
+#define unlikely(x)     __builtin_expect(!!(x), 0)
+
 #define __packed __attribute((packed))
 
 #define __printf(a, b) __attribute__((format(printf, a, b)))
diff --git a/include/logger.h b/include/logger.h
index 1589954..5bb3a65 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -28,6 +28,8 @@ struct logger_user_info {
 	int port;
 };
 
+extern int sd_log_level;
+
 void early_log_init(const char *format_name,
 		struct logger_user_info *user_info);
 int log_init(const char *progname, bool to_stdout, int level, char *outfile);
@@ -68,8 +70,16 @@ void sd_backtrace(void);
 	log_write(SDOG_NOTICE, __func__, __LINE__, fmt, ##args)
 #define sd_info(fmt, args...) \
 	log_write(SDOG_INFO, __func__, __LINE__, fmt, ##args)
-#define sd_debug(fmt, args...) \
-	log_write(SDOG_DEBUG, __func__, __LINE__, fmt, ##args)
+
+/*
+ * 'args' must not contain an operation/function with a side-effect.  It won't
+ * be evaluated when the log level is not SDOG_DEBUG.
+ */
+#define sd_debug(fmt, args...)						\
+({									\
+	if (unlikely(sd_log_level == SDOG_DEBUG))			\
+		log_write(SDOG_DEBUG, __func__, __LINE__, fmt, ##args);	\
+})
 
 #define panic(fmt, args...)			\
 ({						\
diff --git a/lib/logger.c b/lib/logger.c
index 97a5648..f1fefff 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -106,7 +106,7 @@ static __thread int worker_idx;
 static struct logarea *la;
 static const char *log_name;
 static char *log_nowname;
-static int log_level = SDOG_INFO;
+int sd_log_level = SDOG_INFO;
 static pid_t sheep_pid;
 static pid_t logger_pid;
 static key_t semkey;
@@ -445,7 +445,7 @@ void log_write(int prio, const char *func, int line, const char *fmt, ...)
 {
 	va_list ap;
 
-	if (prio > log_level)
+	if (prio > sd_log_level)
 		return;
 
 	va_start(ap, fmt);
@@ -610,7 +610,7 @@ int log_init(const char *program_name, bool to_stdout, int level,
 	char log_dir[PATH_MAX], tmp[PATH_MAX];
 	int size = level == SDOG_DEBUG ? LOG_SPACE_DEBUG_SIZE : LOG_SPACE_SIZE;
 
-	log_level = level;
+	sd_log_level = level;
 
 	log_name = program_name;
 	log_nowname = outfile;
-- 
1.7.9.5



More information about the sheepdog mailing list