use gcc statement expressions to inline commonly used functions
Costa Tsaousis (ktsaou) committed
Jan 18, 2017 at 01:51 UTC
1757dd9f6e0211bec29c4ce0fb498dde6aa78e7e
7 files changed
+107
-26
CMakeLists.txt
+2
-2
@@ -101,7 +101,7 @@ set(NETDATA_SOURCE_FILES
101
src/registry_person.c
102
src/registry_person.h
103
src/registry_machine.c
104
- 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)
104
+ 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)
105
106
set(APPS_PLUGIN_SOURCE_FILES
107
src/appconfig.c
@@ -126,5 +126,5 @@ add_definitions(-DHAVE_CONFIG_H -DCACHE_DIR="/var/cache/netdata" -DCONFIG_DIR="/
126
add_executable(netdata ${NETDATA_SOURCE_FILES})
127
target_link_libraries (netdata m z uuid ${CMAKE_THREAD_LIBS_INIT})
128
129
-add_executable(apps.plugin ${APPS_PLUGIN_SOURCE_FILES})
129
+add_executable(apps.plugin ${APPS_PLUGIN_SOURCE_FILES} src/inlined.h)
130
target_link_libraries (apps.plugin m ${CMAKE_THREAD_LIBS_INIT})
configure.ac
+1
@@ -129,6 +129,7 @@ AC_C_INLINE
129
AC_FUNC_STRERROR_R
130
AC_C__GENERIC
131
AC_C___ATOMIC
132
+AC_C_STMT_EXPR
133
AC_CHECK_SIZEOF([void *])
134
AC_CANONICAL_HOST
135
AC_HEADER_MAJOR
m4/ax_c_statement_expressions.m4
new
+23
@@ -0,0 +1,23 @@
1
+# AC_C_STMT_EXPR
2
+# -------------
3
+# Define HAVE_STMT_EXPR if compiler has statement expressions.
4
+AN_IDENTIFIER([_Generic], [AC_C_STMT_EXPR])
5
+AC_DEFUN([AC_C_STMT_EXPR],
6
+[AC_CACHE_CHECK([for statement expressions], ac_cv_c_stmt_expr,
7
+[AC_COMPILE_IFELSE(
8
+ [AC_LANG_SOURCE(
9
+ [[int
10
+ main (int argc, char **argv)
11
+ {
12
+ int x = ({ int y = 1; y; });
13
+ return x;
14
+ }
15
+ ]])],
16
+ [ac_cv_c_stmt_expr=yes],
17
+ [ac_cv_c_stmt_expr=no])])
18
+if test $ac_cv_c_stmt_expr = yes; then
19
+ AC_DEFINE([HAVE_STMT_EXPR], 1,
20
+ [Define to 1 if compiler supports statement expressions.])
21
+fi
22
+])# AC_C_STMT_EXPR
23
+
src/Makefile.am
+2
@@ -37,6 +37,7 @@ netdata_SOURCES = \
37
eval.c eval.h \
38
global_statistics.c global_statistics.h \
39
health.c health.h \
40
+ inlined.h \
41
log.c log.h \
42
main.c main.h \
43
plugin_checks.c plugin_checks.h \
@@ -124,6 +125,7 @@ apps_plugin_SOURCES = \
125
avl.c avl.h \
126
clocks.c clocks.h \
127
common.c common.h \
128
+ inlined.h \
129
log.c log.h \
130
procfile.c procfile.h \
131
web_buffer.c web_buffer.h \
src/common.c
+3
-19
@@ -807,7 +807,7 @@ uint32_t simple_hash(const char *name)
807
}
808
*/
809
810
-
810
+/*
811
// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
812
uint32_t simple_hash(const char *name) {
813
unsigned char *s = (unsigned char *) name;
@@ -842,6 +842,7 @@ uint32_t simple_uhash(const char *name) {
842
}
843
return hval;
844
}
845
+*/
846
847
/*
848
// http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
@@ -1165,25 +1166,8 @@ void get_system_HZ(void) {
1166
long ticks;
1167
1168
if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1168
- perror("sysconf");
1169
+ error("Cannot get system clock ticks");
1170
}
1171
1172
hz = (unsigned int) ticks;
1173
}
1173
-
1174
-int read_single_number_file(const char *filename, unsigned long long *result) {
1175
- char buffer[1024 + 1];
1176
-
1177
- int fd = open(filename, O_RDONLY, 0666);
1178
- if(unlikely(fd == -1)) return 1;
1179
-
1180
- ssize_t r = read(fd, buffer, 1024);
1181
- if(unlikely(r == -1)) {
1182
- close(fd);
1183
- return 2;
1184
- }
1185
-
1186
- close(fd);
1187
- *result = strtoull(buffer, NULL, 0);
1188
- return 0;
1189
-}
src/common.h
+1
-5
@@ -207,13 +207,11 @@
207
#include "unit_test.h"
208
#include "ipc.h"
209
#include "backends.h"
210
+#include "inlined.h"
211
212
extern void netdata_fix_chart_id(char *s);
213
extern void netdata_fix_chart_name(char *s);
214
214
-extern uint32_t simple_hash(const char *name);
215
-extern uint32_t simple_uhash(const char *name);
216
-
215
extern void strreverse(char* begin, char* end);
216
extern char *mystrsep(char **ptr, char *s);
217
extern char *trim(char *s);
@@ -277,6 +275,4 @@ extern void get_system_HZ(void);
275
#endif
276
#endif
277
280
-extern int read_single_number_file(const char *filename, unsigned long long *result);
281
-
278
#endif /* NETDATA_COMMON_H */
src/inlined.h
new
+75
@@ -0,0 +1,75 @@
1
+#ifndef NETDATA_INLINED_H
2
+#define NETDATA_INLINED_H
3
+
4
+#include "common.h"
5
+
6
+#ifdef HAVE_STMT_EXPR
7
+// GCC extension to define a function as a preprocessor macro
8
+
9
+#define simple_hash(name) ({ \
10
+ register unsigned char *__hash_source = (unsigned char *)(name); \
11
+ register uint32_t __hash_value = 0x811c9dc5; \
12
+ while (*__hash_source) { \
13
+ __hash_value *= 16777619; \
14
+ __hash_value ^= (uint32_t) *__hash_source++; \
15
+ } \
16
+ __hash_value; \
17
+})
18
+
19
+#define simple_uhash(name) ({ \
20
+ register unsigned char *__hash_source = (unsigned char *)(name); \
21
+ register uint32_t __hash_value = 0x811c9dc5, __hash_char; \
22
+ while ((__hash_char = *__hash_source++)) { \
23
+ if (unlikely(__hash_char >= 'A' && __hash_char <= 'Z')) \
24
+ __hash_char += 'a' - 'A'; \
25
+ __hash_value *= 16777619; \
26
+ __hash_value ^= __hash_char; \
27
+ } \
28
+ __hash_value; \
29
+})
30
+
31
+#else /* ! HAVE_STMT_EXPR */
32
+
33
+// for faster execution, allow the compiler to inline
34
+// these functions that are called to hash strings
35
+static inline uint32_t simple_hash(const char *name) {
36
+ register unsigned char *s = (unsigned char *) name;
37
+ register uint32_t hval = 0x811c9dc5;
38
+ while (*s) {
39
+ hval *= 16777619;
40
+ hval ^= (uint32_t) *s++;
41
+ }
42
+ return hval;
43
+}
44
+
45
+static inline uint32_t simple_uhash(const char *name) {
46
+ register unsigned char *s = (unsigned char *) name;
47
+ register uint32_t hval = 0x811c9dc5, c;
48
+ while ((c = *s++)) {
49
+ if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
50
+ hval *= 16777619;
51
+ hval ^= c;
52
+ }
53
+ return hval;
54
+}
55
+
56
+#endif /* HAVE_STMT_EXPR */
57
+
58
+static inline int read_single_number_file(const char *filename, unsigned long long *result) {
59
+ char buffer[1024 + 1];
60
+
61
+ int fd = open(filename, O_RDONLY, 0666);
62
+ if(unlikely(fd == -1)) return 1;
63
+
64
+ ssize_t r = read(fd, buffer, 1024);
65
+ if(unlikely(r == -1)) {
66
+ close(fd);
67
+ return 2;
68
+ }
69
+
70
+ close(fd);
71
+ *result = strtoull(buffer, NULL, 0);
72
+ return 0;
73
+}
74
+
75
+#endif //NETDATA_INLINED_H