]> arthur.barton.de Git - netdata.git/commitdiff
use gcc statement expressions to inline commonly used functions
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Tue, 17 Jan 2017 23:51:13 +0000 (01:51 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Tue, 17 Jan 2017 23:51:13 +0000 (01:51 +0200)
CMakeLists.txt
configure.ac
m4/ax_c_statement_expressions.m4 [new file with mode: 0644]
src/Makefile.am
src/common.c
src/common.h
src/inlined.h [new file with mode: 0644]

index b9ae6a6241d7e9eeeb1bf81b139abb5c6162fb86..4daf76cb5f90515dab482e5a20afa3fb97a406c2 100755 (executable)
@@ -101,7 +101,7 @@ set(NETDATA_SOURCE_FILES
         src/registry_person.c
         src/registry_person.h
         src/registry_machine.c
-        src/registry_machine.h src/registry_internals.c src/registry_init.c src/registry_db.c src/registry_log.c src/proc_uptime.c src/sys_devices_system_edac_mc.c src/plugin_proc_diskspace.c src/plugin_proc_diskspace.h src/simple_pattern.c src/simple_pattern.h)
+        src/registry_machine.h src/registry_internals.c src/registry_init.c src/registry_db.c src/registry_log.c src/proc_uptime.c src/sys_devices_system_edac_mc.c src/plugin_proc_diskspace.c src/plugin_proc_diskspace.h src/simple_pattern.c src/simple_pattern.h src/inlined.h)
 
 set(APPS_PLUGIN_SOURCE_FILES
         src/appconfig.c
@@ -126,5 +126,5 @@ add_definitions(-DHAVE_CONFIG_H -DCACHE_DIR="/var/cache/netdata" -DCONFIG_DIR="/
 add_executable(netdata ${NETDATA_SOURCE_FILES})
 target_link_libraries (netdata m z uuid ${CMAKE_THREAD_LIBS_INIT})
 
-add_executable(apps.plugin ${APPS_PLUGIN_SOURCE_FILES})
+add_executable(apps.plugin ${APPS_PLUGIN_SOURCE_FILES} src/inlined.h)
 target_link_libraries (apps.plugin m ${CMAKE_THREAD_LIBS_INIT})
index 872427944e1acdc8ef587921948211d7cca42b31..002a3aa599acd58d83fcb8c0ee407860d03159ac 100644 (file)
@@ -129,6 +129,7 @@ AC_C_INLINE
 AC_FUNC_STRERROR_R
 AC_C__GENERIC
 AC_C___ATOMIC
+AC_C_STMT_EXPR
 AC_CHECK_SIZEOF([void *])
 AC_CANONICAL_HOST
 AC_HEADER_MAJOR
diff --git a/m4/ax_c_statement_expressions.m4 b/m4/ax_c_statement_expressions.m4
new file mode 100644 (file)
index 0000000..fb259e7
--- /dev/null
@@ -0,0 +1,23 @@
+# AC_C_STMT_EXPR
+# -------------
+# Define HAVE_STMT_EXPR if compiler has statement expressions.
+AN_IDENTIFIER([_Generic], [AC_C_STMT_EXPR])
+AC_DEFUN([AC_C_STMT_EXPR],
+[AC_CACHE_CHECK([for statement expressions], ac_cv_c_stmt_expr,
+[AC_COMPILE_IFELSE(
+   [AC_LANG_SOURCE(
+      [[int
+        main (int argc, char **argv)
+        {
+          int x = ({ int y = 1; y; });
+          return x;
+        }
+      ]])],
+   [ac_cv_c_stmt_expr=yes],
+   [ac_cv_c_stmt_expr=no])])
+if test $ac_cv_c_stmt_expr = yes; then
+  AC_DEFINE([HAVE_STMT_EXPR], 1,
+           [Define to 1 if compiler supports statement expressions.])
+fi
+])# AC_C_STMT_EXPR
+
index 264e58dfb1485a13a8b2ec8a755268919f94d327..d7adc7c2d862cc2f12382cbd119d0c959c614a9e 100644 (file)
@@ -37,6 +37,7 @@ netdata_SOURCES = \
        eval.c eval.h \
        global_statistics.c global_statistics.h \
        health.c health.h \
+       inlined.h \
        log.c log.h \
        main.c main.h \
        plugin_checks.c plugin_checks.h \
@@ -124,6 +125,7 @@ apps_plugin_SOURCES = \
        avl.c avl.h \
        clocks.c clocks.h \
        common.c common.h \
+       inlined.h \
        log.c log.h \
        procfile.c procfile.h \
        web_buffer.c web_buffer.h \
index 36fa0c9be53468ff5cea44affbf86149da8b4422..28ba72594d47cef1535d0611e2755dbad8ad4d68 100644 (file)
@@ -807,7 +807,7 @@ uint32_t simple_hash(const char *name)
 }
 */
 
-
+/*
 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
 uint32_t simple_hash(const char *name) {
     unsigned char *s = (unsigned char *) name;
@@ -842,6 +842,7 @@ uint32_t simple_uhash(const char *name) {
     }
     return hval;
 }
+*/
 
 /*
 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
@@ -1165,25 +1166,8 @@ void get_system_HZ(void) {
     long ticks;
 
     if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
-        perror("sysconf");
+        error("Cannot get system clock ticks");
     }
 
     hz = (unsigned int) ticks;
 }
-
-int read_single_number_file(const char *filename, unsigned long long *result) {
-    char buffer[1024 + 1];
-
-    int fd = open(filename, O_RDONLY, 0666);
-    if(unlikely(fd == -1)) return 1;
-
-    ssize_t r = read(fd, buffer, 1024);
-    if(unlikely(r == -1)) {
-        close(fd);
-        return 2;
-    }
-
-    close(fd);
-    *result = strtoull(buffer, NULL, 0);
-    return 0;
-}
index 9e9cc8fc09c837efe1656a4d2893d7a52265e6d1..26ad467db2f00b6eeb471db070194bd89706e404 100644 (file)
 #include "unit_test.h"
 #include "ipc.h"
 #include "backends.h"
+#include "inlined.h"
 
 extern void netdata_fix_chart_id(char *s);
 extern void netdata_fix_chart_name(char *s);
 
-extern uint32_t simple_hash(const char *name);
-extern uint32_t simple_uhash(const char *name);
-
 extern void strreverse(char* begin, char* end);
 extern char *mystrsep(char **ptr, char *s);
 extern char *trim(char *s);
@@ -277,6 +275,4 @@ extern void get_system_HZ(void);
 #endif
 #endif
 
-extern int read_single_number_file(const char *filename, unsigned long long *result);
-
 #endif /* NETDATA_COMMON_H */
diff --git a/src/inlined.h b/src/inlined.h
new file mode 100644 (file)
index 0000000..87a54dd
--- /dev/null
@@ -0,0 +1,75 @@
+#ifndef NETDATA_INLINED_H
+#define NETDATA_INLINED_H
+
+#include "common.h"
+
+#ifdef HAVE_STMT_EXPR
+// GCC extension to define a function as a preprocessor macro
+
+#define simple_hash(name) ({                                         \
+    register unsigned char *__hash_source = (unsigned char *)(name); \
+    register uint32_t __hash_value = 0x811c9dc5;                     \
+    while (*__hash_source) {                                         \
+        __hash_value *= 16777619;                                    \
+        __hash_value ^= (uint32_t) *__hash_source++;                 \
+    }                                                                \
+    __hash_value;                                                    \
+})
+
+#define simple_uhash(name) ({                                        \
+    register unsigned char *__hash_source = (unsigned char *)(name); \
+    register uint32_t __hash_value = 0x811c9dc5, __hash_char;        \
+    while ((__hash_char = *__hash_source++)) {                       \
+        if (unlikely(__hash_char >= 'A' && __hash_char <= 'Z'))      \
+            __hash_char += 'a' - 'A';                                \
+        __hash_value *= 16777619;                                    \
+        __hash_value ^= __hash_char;                                 \
+    }                                                                \
+    __hash_value;                                                    \
+})
+
+#else /* ! HAVE_STMT_EXPR */
+
+// for faster execution, allow the compiler to inline
+// these functions that are called to hash strings
+static inline uint32_t simple_hash(const char *name) {
+    register unsigned char *s = (unsigned char *) name;
+    register uint32_t hval = 0x811c9dc5;
+    while (*s) {
+        hval *= 16777619;
+        hval ^= (uint32_t) *s++;
+    }
+    return hval;
+}
+
+static inline uint32_t simple_uhash(const char *name) {
+    register unsigned char *s = (unsigned char *) name;
+    register uint32_t hval = 0x811c9dc5, c;
+    while ((c = *s++)) {
+        if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
+        hval *= 16777619;
+        hval ^= c;
+    }
+    return hval;
+}
+
+#endif /* HAVE_STMT_EXPR */
+
+static inline int read_single_number_file(const char *filename, unsigned long long *result) {
+    char buffer[1024 + 1];
+
+    int fd = open(filename, O_RDONLY, 0666);
+    if(unlikely(fd == -1)) return 1;
+
+    ssize_t r = read(fd, buffer, 1024);
+    if(unlikely(r == -1)) {
+        close(fd);
+        return 2;
+    }
+
+    close(fd);
+    *result = strtoull(buffer, NULL, 0);
+    return 0;
+}
+
+#endif //NETDATA_INLINED_H