summaryrefslogtreecommitdiffstats
path: root/pcilib
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@suren.me>2015-05-08 20:06:36 +0200
committerSuren A. Chilingaryan <csa@suren.me>2015-05-08 20:06:36 +0200
commit79d9b2924d7b67ba08e022792adb92770f64e0c8 (patch)
treedc2ce5058978bfe88de1613291f21aa2ca7d9b0c /pcilib
parenta1889bc2f45b3d944652be6436569980f189922d (diff)
downloadpcitool-79d9b2924d7b67ba08e022792adb92770f64e0c8.tar.gz
pcitool-79d9b2924d7b67ba08e022792adb92770f64e0c8.tar.bz2
pcitool-79d9b2924d7b67ba08e022792adb92770f64e0c8.tar.xz
pcitool-79d9b2924d7b67ba08e022792adb92770f64e0c8.zip
Prevent excessive calling of getenv by debugging code for better performance
Diffstat (limited to 'pcilib')
-rw-r--r--pcilib/CMakeLists.txt6
-rw-r--r--pcilib/debug.c4
-rw-r--r--pcilib/debug.h10
-rw-r--r--pcilib/env.c17
-rw-r--r--pcilib/env.h12
5 files changed, 40 insertions, 9 deletions
diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt
index bb0abfd..c9bf0fb 100644
--- a/pcilib/CMakeLists.txt
+++ b/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
)
diff --git a/pcilib/debug.c b/pcilib/debug.c
index ca31364..5192dc2 100644
--- a/pcilib/debug.c
+++ b/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;
diff --git a/pcilib/debug.h b/pcilib/debug.h
index bff3ab0..ec10467 100644
--- a/pcilib/debug.h
+++ b/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
diff --git a/pcilib/env.c b/pcilib/env.c
new file mode 100644
index 0000000..16444db
--- /dev/null
+++ b/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];
+}
+
diff --git a/pcilib/env.h b/pcilib/env.h
new file mode 100644
index 0000000..4fd40f1
--- /dev/null
+++ b/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 */