[stgt] [PATCH] Moved check for is_debug from log_debug() function to dprintf() macro.

Alexander Nezhinsky alexandern at Voltaire.COM
Wed Jun 2 19:12:57 CEST 2010


To minimize the overhead of debug printouts during non-debug runs,
the check for "is_debug" is moved from log_debug() function 
to dprintf() macro.

Now log_debug is called only if "is_debug" is set. 
This spares a function call, parameter passing and the costs of 
complex arguments expanding and evaluation.
To minimize the potential cost of log_debug() even more,
the check for is_debug is put under unlikely() statement.

Signed-off-by: Alexander Nezhinsky <alexandern at voltaire.com>
---
 usr/log.c  |    5 +----
 usr/log.h  |    6 +++++-
 usr/tgtd.h |    1 +
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/usr/log.c b/usr/log.c
index 056314a..66f3e14 100644
--- a/usr/log.c
+++ b/usr/log.c
@@ -43,7 +43,7 @@
 
 static struct logarea *la;
 static char *log_name;
-static int is_debug;
+int is_debug = 0;
 static pid_t pid;
 
 static int logarea_init (int size)
@@ -297,9 +297,6 @@ void log_error(const char *fmt, ...)
 
 void log_debug(const char *fmt, ...)
 {
-	if (!is_debug)
-		return;
-
 	va_list ap;
 	va_start(ap, fmt);
 	dolog(LOG_DEBUG, fmt, ap);
diff --git a/usr/log.h b/usr/log.h
index b84f6d6..426237e 100644
--- a/usr/log.h
+++ b/usr/log.h
@@ -28,6 +28,9 @@
 
 #include <sys/sem.h>
 
+#define likely(x)       __builtin_expect(!!(x), 1)
+#define unlikely(x)     __builtin_expect(!!(x), 0)
+
 union semun {
 	int val;
 	struct semid_ds *buf;
@@ -76,7 +79,8 @@ do {									\
 
 #define dprintf(fmt, args...)						\
 do {									\
-	log_debug("%s(%d) " fmt, __FUNCTION__, __LINE__, ##args);	\
+	if (unlikely(is_debug))						\
+		log_debug("%s(%d) " fmt, __FUNCTION__, __LINE__, ##args); \
 } while (0)
 
 #endif	/* LOG_H */
diff --git a/usr/tgtd.h b/usr/tgtd.h
index 3323a9b..cc2c468 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -207,6 +207,7 @@ static inline int kreq_init(void)	\
 #endif
 
 extern int system_active;
+extern int is_debug;
 
 extern int kspace_send_tsk_mgmt_res(struct mgmt_req *mreq);
 extern int kspace_send_cmd_res(uint64_t nid, int result, struct scsi_cmd *);
--
1.6.5
--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list