Browse Source

Prevent excessive calling of getenv by debugging code for better performance

Suren A. Chilingaryan 9 years ago
parent
commit
79d9b2924d
5 changed files with 40 additions and 9 deletions
  1. 3 3
      pcilib/CMakeLists.txt
  2. 2 2
      pcilib/debug.c
  3. 6 4
      pcilib/debug.h
  4. 17 0
      pcilib/env.c
  5. 12 0
      pcilib/env.h

+ 3 - 3
pcilib/CMakeLists.txt

@@ -3,8 +3,8 @@ include_directories(
     ${CMAKE_SOURCE_DIR}/pcilib
 )
 
-set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h kmem.h irq.h dma.h event.h plugin.h tools.h error.h debug.h version.h config.h)
-add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c register.c kmem.c irq.c dma.c event.c plugin.c tools.c error.c debug.c)
+set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h kmem.h irq.h dma.h event.h plugin.h tools.h error.h debug.h env.h version.h config.h)
+add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c register.c kmem.c irq.c dma.c event.c plugin.c tools.c error.c debug.c env.c)
 target_link_libraries(pcilib dma protocols ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS})
 add_dependencies(pcilib dma protocols)
 
@@ -16,6 +16,6 @@ install(FILES pcilib.h
     DESTINATION include
 )
 
-install(FILES bar.h kmem.h bank.h register.h dma.h event.h model.h error.h debug.h tools.h export.h version.h
+install(FILES bar.h kmem.h bank.h register.h dma.h event.h model.h error.h debug.h env.h tools.h export.h version.h
     DESTINATION include/pcilib
 )

+ 2 - 2
pcilib/debug.c

@@ -11,10 +11,11 @@
 
 #define PCILIB_MAX_DEBUG_FILENAME_LENGTH 1023
 
+
 void pcilib_debug_message(const char *function, const char *file, int line, pcilib_log_flags_t flags, const char *format, ...) {
     va_list va;
 
-    if (!getenv(function)) return;
+//    if (!getenv(function)) return;
 
     va_start(va, format);
     pcilib_log_vmessage(file, line, PCILIB_LOG_DEBUG, flags, format, va);
@@ -30,7 +31,6 @@ void pcilib_debug_data_buffer(const char *function, size_t size, void *buffer, p
     const char *prefix;
     char fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1];
 
-
     prefix = getenv(function);
     if (!prefix) return;
 

+ 6 - 4
pcilib/debug.h

@@ -2,6 +2,7 @@
 #define _PCILIB_DEBUG_H
 
 #include <stdarg.h>
+#include <pcilib/env.h>
 
 #define PCILIB_DEBUG
 
@@ -12,16 +13,16 @@
 
 
 #ifdef PCILIB_DEBUG_DMA
-# define PCILIB_DEBUG_DMA_MESSAGE(function, ...) pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__) 
-# define PCILIB_DEBUG_DMA_BUFFER(function, ...) pcilib_debug_data_buffer (#function, __VA_ARGS__) 
+# define PCILIB_DEBUG_DMA_MESSAGE(function, ...) if (pcilib_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
+# define PCILIB_DEBUG_DMA_BUFFER(function, ...) if (pcilib_getenv(function##_ENV, #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
 #else /* PCILIB_DEBUG_DMA */
 # define PCILIB_DEBUG_DMA_MESSAGE(function, ...)
 # define PCILIB_DEBUG_DMA_BUFFER(function, ...)
 #endif /* PCILIB_DEBUG_DMA */
 
 #ifdef PCILIB_DEBUG_MISSING_EVENTS
-# define PCILIB_DEBUG_MISSING_EVENTS_MESSAGE(function, ...) pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__) 
-# define PCILIB_DEBUG_MISSING_EVENTS_BUFFER(function, ...) pcilib_debug_data_buffer (#function, __VA_ARGS__) 
+# define PCILIB_DEBUG_MISSING_EVENTS_MESSAGE(function, ...) if (pcilib_getenv(function##_ENV, #function)) { pcilib_debug_message (#function, __FILE__, __LINE__, __VA_ARGS__); }
+# define PCILIB_DEBUG_MISSING_EVENTS_BUFFER(function, ...) if (pcilib_getenv(function##_ENV #function)) { pcilib_debug_data_buffer (#function, __VA_ARGS__); }
 #else /* PCILIB_DEBUG_MISSING_EVENTS */
 # define PCILIB_DEBUG_MISSING_EVENTS_MESSAGE(function, ...)
 # define PCILIB_DEBUG_MISSING_EVENTS_BUFFER(function, ...)
@@ -33,6 +34,7 @@
 #define pcilib_debug_buffer(function, ...) \
     PCILIB_DEBUG_##function##_BUFFER(PCILIB_DEBUG_##function, __VA_ARGS__)
 
+
 typedef enum {
     PCILIB_DEBUG_BUFFER_APPEND = 1,
     PCILIB_DEBUG_BUFFER_MKDIR = 2

+ 17 - 0
pcilib/env.c

@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "env.h"
+
+
+static const char *pcilib_env_cache[PCILIB_MAX_ENV] = {0};
+
+const char *pcilib_getenv(pcilib_env_t env, const char *var) {
+    if (!pcilib_env_cache[env]) {
+	const char *var_env = getenv(var);
+	pcilib_env_cache[env] = var_env?var_env:(void*)-1;
+	return var_env;
+    }
+
+    return (pcilib_env_cache[env] == (void*)-1)?NULL:pcilib_env_cache[env];
+}
+

+ 12 - 0
pcilib/env.h

@@ -0,0 +1,12 @@
+#ifndef _PCILIB_ENV_H
+#define _PCILIB_ENV_H
+
+typedef enum {
+    PCILIB_DEBUG_DMA_ENV,
+    PCILIB_DEBUG_MISSING_EVENTS_ENV,
+    PCILIB_MAX_ENV
+} pcilib_env_t;
+
+const char *pcilib_getenv(pcilib_env_t env, const char *var);
+
+#endif /* _PCILIB_ENV_H */